From 8d5690928feba14759a8efb384fd82ee83eecec1 Mon Sep 17 00:00:00 2001 From: Markus Hofknecht Date: Mon, 4 Oct 2021 17:47:39 +0200 Subject: [PATCH] [Feature] if folder empty or not exists let user choose new root folder (#192), version 1.0.19.5 --- Business/Menus.cs | 33 ++++++++++++++------------ Business/Program.cs | 30 +++++++++++------------- Config/Config.cs | 57 +++++++-------------------------------------- 3 files changed, 40 insertions(+), 80 deletions(-) diff --git a/Business/Menus.cs b/Business/Menus.cs index bba78a8..c24312a 100644 --- a/Business/Menus.cs +++ b/Business/Menus.cs @@ -79,6 +79,8 @@ namespace SystemTrayMenu.Business MessageBox.Show(Translator.GetText( "MessageRootFolderEmpty")); OpenFolder(); + Config.SetFolderByUser(); + openCloseState = OpenCloseState.Default; showingMessageBox = false; } @@ -88,8 +90,10 @@ namespace SystemTrayMenu.Business { showingMessageBox = true; MessageBox.Show(Translator.GetText( - "MessageRootFolderNoAccess")); + "MessageRootFolderNoAccess")); OpenFolder(); + Config.SetFolderByUser(); + openCloseState = OpenCloseState.Default; showingMessageBox = false; } @@ -124,6 +128,12 @@ namespace SystemTrayMenu.Business { loadingRowData = rowData; + Menu menuLoading = menus[rowData.MenuLevel + 1]; + if (menuLoading != null && menuLoading.IsLoadingMenu) + { + CloseMenu(rowData.MenuLevel + 1); + } + CreateAndShowLoadingMenu(rowData); void CreateAndShowLoadingMenu(RowData rowData) { @@ -163,22 +173,15 @@ namespace SystemTrayMenu.Business void LoadSubMenuCompleted(object senderCompleted, RunWorkerCompletedEventArgs e) { MenuData menuData = (MenuData)e.Result; - if (menuData.Validity == MenuDataValidity.AbortedOrUnknown) + + Menu menuLoading = menus[menuData.Level]; + if (menuLoading != null && menuLoading.IsLoadingMenu) { - CloseLoadingMenu(); - void CloseLoadingMenu() - { - if (loadingRowData != null) - { - Menu menuLoading = menus[loadingRowData.MenuLevel + 1]; - if (menuLoading != null && menuLoading.IsLoadingMenu) - { - CloseMenu(loadingRowData.MenuLevel + 1); - } - } - } + CloseMenu(menuData.Level); } - else if (menus[0].IsUsable) + + if (menuData.Validity != MenuDataValidity.AbortedOrUnknown && + menus[0].IsUsable) { Menu menu = Create(menuData); switch (menuData.Validity) diff --git a/Business/Program.cs b/Business/Program.cs index 6707b95..9883fc8 100644 --- a/Business/Program.cs +++ b/Business/Program.cs @@ -24,25 +24,23 @@ namespace SystemTrayMenu Translator.Initialize(); Config.Initialize(); Config.SetFolderByWindowsContextMenu(args); - if (Config.LoadOrSetByUser()) + Config.LoadOrSetByUser(); + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.ThreadException += ThreadException; + static void ThreadException(object s, ThreadExceptionEventArgs t) { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - Application.ThreadException += ThreadException; - static void ThreadException(object s, ThreadExceptionEventArgs t) - { - AskUserSendError(t.Exception); - } + AskUserSendError(t.Exception); + } - Scaling.Initialize(); - FolderOptions.Initialize(); + Scaling.Initialize(); + FolderOptions.Initialize(); - using (new App()) - { - isStartup = false; - Log.WriteApplicationRuns(); - Application.Run(); - } + using (new App()) + { + isStartup = false; + Log.WriteApplicationRuns(); + Application.Run(); } Config.Dispose(); diff --git a/Config/Config.cs b/Config/Config.cs index 5b18519..158a612 100644 --- a/Config/Config.cs +++ b/Config/Config.cs @@ -57,11 +57,9 @@ namespace SystemTrayMenu } } - public static bool LoadOrSetByUser() + public static void LoadOrSetByUser() { - bool pathOK = IsPathOK(Path); - - if (!pathOK) + if (string.IsNullOrEmpty(Path)) { string textFirstStart = Translator.GetText("TextFirstStart"); MessageBox.Show( @@ -69,64 +67,25 @@ namespace SystemTrayMenu Translator.GetText("SystemTrayMenu"), MessageBoxButtons.OK); ShowHelpFAQ(); - pathOK = SetFolderByUser(); + SetFolderByUser(); } - - return pathOK; } - public static bool SetFolderByUser(bool save = true) + public static void SetFolderByUser(bool save = true) { - bool pathOK = false; - bool userAborted = false; using (FolderDialog dialog = new FolderDialog()) { dialog.InitialFolder = Path; - do + if (dialog.ShowDialog() == DialogResult.OK) { - if (dialog.ShowDialog() == DialogResult.OK) + Settings.Default.PathDirectory = dialog.Folder; + if (save) { - if (IsPathOK(dialog.Folder)) - { - pathOK = true; - Settings.Default.PathDirectory = - dialog.Folder; - if (save) - { - Settings.Default.Save(); - } - } - } - else - { - userAborted = true; + Settings.Default.Save(); } } - while (!pathOK && !userAborted); } - - return pathOK; - } - - private static bool IsPathOK(string path) - { - bool isPathOK = false; - - bool folderContainsFiles = false; - try - { - folderContainsFiles = Directory.GetFiles(path).Length > 0; - } - catch (Exception ex) - { - Log.Warn($"path:'{path}'", ex); - } - - isPathOK = FileLnk.IsNetworkPath(path) || - (Directory.Exists(path) && folderContainsFiles); - - return isPathOK; } internal static void ShowHelpFAQ()