Remove state openCloseState and ClosePressed events by refactoring

This commit is contained in:
Peter Kirmeier 2023-05-16 19:32:48 +02:00
parent 0fb1a086bb
commit d75d183918
3 changed files with 35 additions and 77 deletions

View file

@ -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<Menu?>? RowSelectionChanged;
internal event Action<Menu, ListViewItemData>? 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

View file

@ -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,54 +235,29 @@ namespace SystemTrayMenu.Business
waitToOpenMenu.MouseActive = byClick;
if (openCloseState == OpenCloseState.Opening ||
(openCloseState == OpenCloseState.Default && (mainMenu?.Visibility ?? Visibility.Collapsed) == Visibility.Visible))
{
openCloseState = OpenCloseState.Closing;
MenusFadeOut();
if (workerMainMenu.IsBusy)
{
// Stop current loading process of main menu
workerMainMenu.CancelAsync();
LoadStopped?.Invoke();
}
if (IsVisibleAnyMenu(mainMenu) == null)
else if (mainMenu != null && mainMenu.Visibility == Visibility.Visible)
{
openCloseState = OpenCloseState.Default;
}
// 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);
}
}
}
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)
{
@ -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,18 +565,11 @@ namespace SystemTrayMenu.Business
}
else
{
MenusFadeOut();
}
}
}
}
private void MenusFadeOut()
{
openCloseState = OpenCloseState.Closing;
mainMenu?.HideWithFade(true);
}
}
}
}
private void GetScreenBounds(out Rect screenBounds, out bool useCustomLocation, out StartLocation startLocation)
{

View file

@ -243,8 +243,6 @@ namespace SystemTrayMenu.UserInterface
internal event Action<Menu, ListViewItemData>? 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();
}
}