From d75d18391872a220f344ff730e06ebca8a89cdcb Mon Sep 17 00:00:00 2001 From: Peter Kirmeier Date: Tue, 16 May 2023 19:32:48 +0200 Subject: [PATCH] Remove state openCloseState and ClosePressed events by refactoring --- Business/KeyboardInput.cs | 7 +-- Business/Menus.cs | 89 +++++++++----------------------------- UserInterface/Menu.xaml.cs | 16 +++++-- 3 files changed, 35 insertions(+), 77 deletions(-) diff --git a/Business/KeyboardInput.cs b/Business/KeyboardInput.cs index 8f254dc..02fc7c0 100644 --- a/Business/KeyboardInput.cs +++ b/Business/KeyboardInput.cs @@ -5,7 +5,6 @@ namespace SystemTrayMenu.Handler { using System; - using System.Windows.Controls; using System.Windows.Input; using SystemTrayMenu.DataClasses; using SystemTrayMenu.Helpers; @@ -21,8 +20,6 @@ namespace SystemTrayMenu.Handler internal event Action? HotKeyPressed; - internal event Action? ClosePressed; - internal event Action? RowSelectionChanged; internal event Action? EnterPressed; @@ -125,7 +122,7 @@ namespace SystemTrayMenu.Handler { focussedMenu = null; RowSelectionChanged?.Invoke(null); // TODO: Refactory to just a trigger for WaitToLoadMenu ? - ClosePressed?.Invoke(); + sender.HideAllMenus(); } break; @@ -163,7 +160,7 @@ namespace SystemTrayMenu.Handler rowBefore.OpenItem(out bool doCloseAfterOpen); if (doCloseAfterOpen) { - ClosePressed?.Invoke(); + menuBefore.HideAllMenus(); } } else diff --git a/Business/Menus.cs b/Business/Menus.cs index a7b5648..895b342 100644 --- a/Business/Menus.cs +++ b/Business/Menus.cs @@ -36,7 +36,6 @@ namespace SystemTrayMenu.Business private readonly DispatcherTimer timerShowProcessStartedAsLoadingIcon = new(); private readonly DispatcherTimer timerStillActiveCheck = new(); private readonly DispatcherTimer waitLeave = new(); - private OpenCloseState openCloseState = OpenCloseState.Default; private TaskbarPosition taskbarPosition = TaskbarPosition.Unknown; private bool showMenuAfterMainPreload; private Menu? mainMenu; @@ -51,7 +50,6 @@ namespace SystemTrayMenu.Business } keyboardInput.HotKeyPressed += () => SwitchOpenClose(false, false); - keyboardInput.ClosePressed += MenusFadeOut; keyboardInput.RowSelectionChanged += waitToOpenMenu.RowSelectionChanged; keyboardInput.EnterPressed += waitToOpenMenu.EnterOpensInstantly; @@ -185,13 +183,6 @@ namespace SystemTrayMenu.Business internal event Action? LoadStopped; - private enum OpenCloseState - { - Default, - Opening, - Closing, - } - [MemberNotNullWhen(true, nameof(mainMenu))] private bool IsMainUsable => mainMenu != null && mainMenu.Visibility == Visibility.Visible; @@ -244,55 +235,30 @@ namespace SystemTrayMenu.Business waitToOpenMenu.MouseActive = byClick; - if (openCloseState == OpenCloseState.Opening || - (openCloseState == OpenCloseState.Default && (mainMenu?.Visibility ?? Visibility.Collapsed) == Visibility.Visible)) + if (workerMainMenu.IsBusy) { - openCloseState = OpenCloseState.Closing; - - MenusFadeOut(); - - if (workerMainMenu.IsBusy) - { - workerMainMenu.CancelAsync(); - } - - if (IsVisibleAnyMenu(mainMenu) == null) - { - openCloseState = OpenCloseState.Default; - } + // Stop current loading process of main menu + workerMainMenu.CancelAsync(); + LoadStopped?.Invoke(); + } + else if (mainMenu != null && mainMenu.Visibility == Visibility.Visible) + { + // Main menu is visible, hide all menus + mainMenu.HideWithFade(true); } else { - openCloseState = OpenCloseState.Opening; - + // Main menu is hidden or even not created at all, (create and) show it if (Settings.Default.GenerateShortcutsToDrives) { - GenerateDriveShortcuts.Start(); + GenerateDriveShortcuts.Start(); // TODO: Once or actually on every startup? } - if (!workerMainMenu.IsBusy) - { - LoadStarted?.Invoke(); - workerMainMenu.RunWorkerAsync(null); - } + LoadStarted?.Invoke(); + workerMainMenu.RunWorkerAsync(null); } } - private static Menu? IsVisibleAnyMenu(Menu? menu) - { - while (menu != null) - { - if (menu.Visibility == Visibility.Visible) - { - break; - } - - menu = menu.SubMenu; - } - - return menu; - } - private static Menu? IsMouseOverAnyMenu(Menu? menu) { while (menu != null) @@ -411,8 +377,6 @@ namespace SystemTrayMenu.Business break; } } - - openCloseState = OpenCloseState.Default; } private void LoadSubMenuCompleted(object? senderCompleted, RunWorkerCompletedEventArgs e) @@ -515,11 +479,8 @@ namespace SystemTrayMenu.Business menu.Deactivated += Deactivate; void Deactivate(object? sender, EventArgs e) { - if (openCloseState == OpenCloseState.Opening) - { - Log.Info("Ignored Deactivate, because openCloseState == OpenCloseState.Opening"); - } - else if (!Settings.Default.StaysOpenWhenFocusLostAfterEnterPressed) + // TODO: Does this check make any sense here? + if (!Settings.Default.StaysOpenWhenFocusLostAfterEnterPressed) { FadeHalfOrOutIfNeeded(); } @@ -541,7 +502,6 @@ namespace SystemTrayMenu.Business menu.CellMouseLeave += waitToOpenMenu.MouseLeave; menu.CellMouseDown += keyboardInput.SelectByMouse; menu.CellOpenOnClick += waitToOpenMenu.ClickOpensInstantly; - menu.ClosePressed += MenusFadeOut; if (menu.Level == 0) { @@ -571,17 +531,15 @@ namespace SystemTrayMenu.Business menu.Activate(); } } - - if (menu.Visibility != Visibility.Visible && menu.Level != 0) + else if (menu.Level != 0) { + // Close down non-visible sub menus menu.Close(); } - - if (IsVisibleAnyMenu(mainMenu) == null) + else { + // Non-visible main menu, do some housekeeping IconReader.ClearCacheWhenLimitReached(); - - openCloseState = OpenCloseState.Default; } } @@ -607,19 +565,12 @@ namespace SystemTrayMenu.Business } else { - MenusFadeOut(); + mainMenu?.HideWithFade(true); } } } } - private void MenusFadeOut() - { - openCloseState = OpenCloseState.Closing; - - mainMenu?.HideWithFade(true); - } - private void GetScreenBounds(out Rect screenBounds, out bool useCustomLocation, out StartLocation startLocation) { if (Settings.Default.AppearAtMouseLocation) diff --git a/UserInterface/Menu.xaml.cs b/UserInterface/Menu.xaml.cs index 254eb6b..fc89e2b 100644 --- a/UserInterface/Menu.xaml.cs +++ b/UserInterface/Menu.xaml.cs @@ -243,8 +243,6 @@ namespace SystemTrayMenu.UserInterface internal event Action? CellOpenOnClick; - internal event Action? ClosePressed; - internal event RoutedEventHandler FadeToTransparent { add { AddHandler(FadeToTransparentEvent, value); } @@ -519,6 +517,18 @@ namespace SystemTrayMenu.UserInterface } } + internal void HideAllMenus() + { + // Find main menu and close/hide all + Menu menu = this; + while (menu.ParentMenu != null) + { + menu = menu.ParentMenu; + } + + menu.HideWithFade(true); + } + internal void HideWithFade(bool recursive) { if (recursive) @@ -1247,7 +1257,7 @@ namespace SystemTrayMenu.UserInterface if (doClose) { - ClosePressed?.Invoke(); + HideAllMenus(); } }