Simplified use of MainMenu and updating menus' item lists

This commit is contained in:
Peter Kirmeier 2023-04-27 23:24:25 +02:00
parent b735ad4b46
commit 175c93e511
2 changed files with 45 additions and 55 deletions

View file

@ -46,7 +46,6 @@ namespace SystemTrayMenu.Business
private int lastMouseDownRowIndex = -1; private int lastMouseDownRowIndex = -1;
#endif #endif
private bool showMenuAfterMainPreload; private bool showMenuAfterMainPreload;
private bool hideSubmenuDuringRefreshSearch;
public Menus() public Menus()
{ {
@ -119,7 +118,7 @@ namespace SystemTrayMenu.Business
#endif #endif
joystickHelper = new(); joystickHelper = new();
joystickHelper.KeyPressed += (key, modifiers) => menus[0]?.Dispatcher.Invoke(keyboardInput.CmdKeyProcessed, new object[] { null!, key, modifiers }); joystickHelper.KeyPressed += (key, modifiers) => MainMenu?.Dispatcher.Invoke(keyboardInput.CmdKeyProcessed, new object[] { null!, key, modifiers });
timerStillActiveCheck.Interval = TimeSpan.FromMilliseconds(Settings.Default.TimeUntilClosesAfterEnterPressed + 20); timerStillActiveCheck.Interval = TimeSpan.FromMilliseconds(Settings.Default.TimeUntilClosesAfterEnterPressed + 20);
timerStillActiveCheck.Tick += (sender, e) => StillActiveTick(); timerStillActiveCheck.Tick += (sender, e) => StillActiveTick();
@ -187,7 +186,9 @@ namespace SystemTrayMenu.Business
Closing, Closing,
} }
private bool IsMainUsable => menus[0]?.IsUsable ?? false; private Menu? MainMenu => menus[0];
private bool IsMainUsable => MainMenu?.IsUsable ?? false;
private IEnumerable<Menu> AsEnumerable => menus.Where(m => m != null && !m.IsClosed)!; private IEnumerable<Menu> AsEnumerable => menus.Where(m => m != null && !m.IsClosed)!;
@ -209,7 +210,7 @@ namespace SystemTrayMenu.Business
timerStillActiveCheck.Stop(); timerStillActiveCheck.Stop();
waitLeave.Stop(); waitLeave.Stop();
IconReader.Dispose(); IconReader.Dispose();
menus[0]?.Close(); MainMenu?.Close();
foreach (FileSystemWatcher watcher in watchers) foreach (FileSystemWatcher watcher in watchers)
{ {
@ -267,9 +268,8 @@ namespace SystemTrayMenu.Business
} }
else else
{ {
Menu? menu = menus[0];
if (openCloseState == OpenCloseState.Opening || if (openCloseState == OpenCloseState.Opening ||
(menu != null && menu.Visibility == Visibility.Visible && openCloseState == OpenCloseState.Default)) ((MainMenu?.Visibility ?? Visibility.Collapsed) == Visibility.Visible && openCloseState == OpenCloseState.Default))
{ {
openCloseState = OpenCloseState.Closing; openCloseState = OpenCloseState.Closing;
MenusFadeOut(); MenusFadeOut();
@ -353,7 +353,7 @@ namespace SystemTrayMenu.Business
if (e.Result == null) if (e.Result == null)
{ {
Menu? menu = menus[0]; Menu? menu = MainMenu;
if (menu != null) if (menu != null)
{ {
// The main menu gets loaded again // The main menu gets loaded again
@ -392,7 +392,7 @@ namespace SystemTrayMenu.Business
IconReader.IsPreloading = false; IconReader.IsPreloading = false;
if (showMenuAfterMainPreload) if (showMenuAfterMainPreload)
{ {
menus[0]?.ShowWithFade(); MainMenu?.ShowWithFade();
} }
} }
else else
@ -443,10 +443,8 @@ namespace SystemTrayMenu.Business
if (menuData.DirectoryState != MenuDataDirectoryState.Undefined) if (menuData.DirectoryState != MenuDataDirectoryState.Undefined)
{ {
// Sub Menu (completed) // Sub Menu (completed)
menu.AddItemsToMenu(menuData.RowDatas); menu.AddItemsToMenu(menuData.RowDatas, menuData.DirectoryState, true);
menu.SetSubMenuState(menuData.DirectoryState);
AdjustMenusSizeAndLocation(menu.Level); AdjustMenusSizeAndLocation(menu.Level);
menu.TimerUpdateIconsStart();
} }
else else
{ {
@ -516,17 +514,17 @@ namespace SystemTrayMenu.Business
} }
menu.SearchTextChanged += Menu_SearchTextChanged; menu.SearchTextChanged += Menu_SearchTextChanged;
void Menu_SearchTextChanged(Menu menu, bool isSearchStringEmpty) void Menu_SearchTextChanged(Menu menu, bool isSearchStringEmpty, bool causedByWatcherUpdate)
{ {
keyboardInput.SearchTextChanged(menu, isSearchStringEmpty); keyboardInput.SearchTextChanged(menu, isSearchStringEmpty);
AdjustMenusSizeAndLocation(menu.Level + 1); AdjustMenusSizeAndLocation(menu.Level + 1);
searchTextChanging = false; searchTextChanging = false;
// if any open menu close // if any open menu close
if (menu.Level + 1 < menus.Length) if (!causedByWatcherUpdate && menu.Level + 1 < menus.Length)
{ {
Menu? menuToClose = menus[menu.Level + 1]; Menu? menuToClose = menus[menu.Level + 1];
if (menuToClose != null && hideSubmenuDuringRefreshSearch) if (menuToClose != null)
{ {
HideOldMenu(menuToClose); HideOldMenu(menuToClose);
} }
@ -831,7 +829,7 @@ namespace SystemTrayMenu.Business
{ {
if (IsMainUsable) if (IsMainUsable)
{ {
Menu? menu = menus[0]; Menu? menu = MainMenu;
if (menu != null) if (menu != null)
{ {
menu.RelocateOnNextShow = true; menu.RelocateOnNextShow = true;
@ -1031,7 +1029,7 @@ namespace SystemTrayMenu.Business
private void WatcherProcessItem(object sender, EventArgs e) private void WatcherProcessItem(object sender, EventArgs e)
{ {
Menu? menu = menus[0]; Menu? menu = MainMenu;
bool useHistory = false; bool useHistory = false;
if (menu == null) if (menu == null)
{ {
@ -1050,17 +1048,17 @@ namespace SystemTrayMenu.Business
if (e is RenamedEventArgs renamedEventArgs) if (e is RenamedEventArgs renamedEventArgs)
{ {
menus[0]?.Dispatcher.Invoke(() => RenameItem(renamedEventArgs)); MainMenu?.Dispatcher.Invoke(() => RenameItem(renamedEventArgs));
} }
else if (e is FileSystemEventArgs fileSystemEventArgs) else if (e is FileSystemEventArgs fileSystemEventArgs)
{ {
if (fileSystemEventArgs.ChangeType == WatcherChangeTypes.Deleted) if (fileSystemEventArgs.ChangeType == WatcherChangeTypes.Deleted)
{ {
menus[0]?.Dispatcher.Invoke(() => DeleteItem(fileSystemEventArgs)); MainMenu?.Dispatcher.Invoke(() => DeleteItem(fileSystemEventArgs));
} }
else if (fileSystemEventArgs.ChangeType == WatcherChangeTypes.Created) else if (fileSystemEventArgs.ChangeType == WatcherChangeTypes.Created)
{ {
menus[0]?.Dispatcher.Invoke(() => CreateItem(fileSystemEventArgs)); MainMenu?.Dispatcher.Invoke(() => CreateItem(fileSystemEventArgs));
} }
} }
} }
@ -1070,7 +1068,7 @@ namespace SystemTrayMenu.Business
try try
{ {
List<RowData> rowDatas = new(); List<RowData> rowDatas = new();
ListView? dgv = menus[0]?.GetDataGridView(); ListView? dgv = MainMenu?.GetDataGridView();
if (dgv != null) if (dgv != null)
{ {
foreach (ListViewItemData item in dgv.Items) foreach (ListViewItemData item in dgv.Items)
@ -1111,13 +1109,8 @@ namespace SystemTrayMenu.Business
rowDatas = DirectoryHelpers.SortItems(rowDatas); rowDatas = DirectoryHelpers.SortItems(rowDatas);
keyboardInput.ClearIsSelectedByKey(); keyboardInput.ClearIsSelectedByKey();
menus[0]?.AddItemsToMenu(rowDatas); MainMenu?.AddItemsToMenu(rowDatas, null, true);
MainMenu?.OnWatcherUpdate();
hideSubmenuDuringRefreshSearch = false;
menus[0]?.RefreshSearchText();
hideSubmenuDuringRefreshSearch = true;
menus[0]?.TimerUpdateIconsStart();
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -1129,7 +1122,7 @@ namespace SystemTrayMenu.Business
{ {
try try
{ {
ListView? dgv = menus[0]?.GetDataGridView(); ListView? dgv = MainMenu?.GetDataGridView();
if (dgv != null) if (dgv != null)
{ {
List<ListViewItemData> rowsToRemove = new(); List<ListViewItemData> rowsToRemove = new();
@ -1152,10 +1145,7 @@ namespace SystemTrayMenu.Business
} }
keyboardInput.ClearIsSelectedByKey(); keyboardInput.ClearIsSelectedByKey();
MainMenu?.OnWatcherUpdate();
hideSubmenuDuringRefreshSearch = false;
menus[0]?.RefreshSearchText();
hideSubmenuDuringRefreshSearch = true;
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -1185,7 +1175,7 @@ namespace SystemTrayMenu.Business
rowData, rowData,
}; };
ListView? dgv = menus[0]?.GetDataGridView(); ListView? dgv = MainMenu?.GetDataGridView();
if (dgv != null) if (dgv != null)
{ {
foreach (ListViewItemData item in dgv.Items) foreach (ListViewItemData item in dgv.Items)
@ -1196,13 +1186,8 @@ namespace SystemTrayMenu.Business
rowDatas = DirectoryHelpers.SortItems(rowDatas); rowDatas = DirectoryHelpers.SortItems(rowDatas);
keyboardInput.ClearIsSelectedByKey(); keyboardInput.ClearIsSelectedByKey();
menus[0]?.AddItemsToMenu(rowDatas); MainMenu?.AddItemsToMenu(rowDatas, null, true);
MainMenu?.OnWatcherUpdate();
hideSubmenuDuringRefreshSearch = false;
menus[0]?.RefreshSearchText();
hideSubmenuDuringRefreshSearch = true;
menus[0]?.TimerUpdateIconsStart();
} }
catch (Exception ex) catch (Exception ex)
{ {

View file

@ -154,7 +154,7 @@ namespace SystemTrayMenu.UserInterface
MouseUp += Menu_MouseUp; MouseUp += Menu_MouseUp;
MouseMove += Menu_MouseMove; MouseMove += Menu_MouseMove;
textBoxSearch.TextChanged += (_, _) => TextBoxSearch_TextChanged(); textBoxSearch.TextChanged += (_, _) => TextBoxSearch_TextChanged(false);
textBoxSearch.ContextMenu = new() textBoxSearch.ContextMenu = new()
{ {
Background = SystemColors.ControlBrush, Background = SystemColors.ControlBrush,
@ -266,14 +266,14 @@ namespace SystemTrayMenu.UserInterface
} }
#endif #endif
Loaded += (sender, e) => Loaded += (_, _) =>
{ {
NativeMethods.HideFromAltTab(this); NativeMethods.HideFromAltTab(this);
RaiseEvent(new(routedEvent: FadeInEvent)); RaiseEvent(new(routedEvent: FadeInEvent));
}; };
Closed += (sender, e) => Closed += (_, _) =>
{ {
foreach (ListViewItemData item in dgv.Items) foreach (ListViewItemData item in dgv.Items)
{ {
@ -281,7 +281,7 @@ namespace SystemTrayMenu.UserInterface
} }
}; };
AddItemsToMenu(menuData.RowDatas); AddItemsToMenu(menuData.RowDatas, null, false);
} }
internal event Action? MenuScrolled; internal event Action? MenuScrolled;
@ -296,7 +296,7 @@ namespace SystemTrayMenu.UserInterface
internal event Action? SearchTextChanging; internal event Action? SearchTextChanging;
internal event Action<Menu, bool>? SearchTextChanged; internal event Action<Menu, bool, bool>? SearchTextChanged;
internal event Action? UserDragsMenu; internal event Action? UserDragsMenu;
@ -360,9 +360,9 @@ namespace SystemTrayMenu.UserInterface
} }
} }
internal void RefreshSearchText() internal void OnWatcherUpdate()
{ {
TextBoxSearch_TextChanged(); TextBoxSearch_TextChanged(true);
if (dgv.Items.Count > 0) if (dgv.Items.Count > 0)
{ {
dgv.ScrollIntoView(dgv.Items[0]); dgv.ScrollIntoView(dgv.Items[0]);
@ -439,7 +439,7 @@ namespace SystemTrayMenu.UserInterface
((CollectionView)CollectionViewSource.GetDefaultView(dgv.ItemsSource)).Refresh(); ((CollectionView)CollectionViewSource.GetDefaultView(dgv.ItemsSource)).Refresh();
} }
internal void AddItemsToMenu(List<RowData> data) internal void AddItemsToMenu(List<RowData> data, MenuDataDirectoryState? state, bool startIconLoading)
{ {
int foldersCount = 0; int foldersCount = 0;
int filesCount = 0; int filesCount = 0;
@ -471,6 +471,16 @@ namespace SystemTrayMenu.UserInterface
dgv.ItemsSource = items; dgv.ItemsSource = items;
SetCounts(foldersCount, filesCount); SetCounts(foldersCount, filesCount);
if (state != null)
{
SetSubMenuState(state.Value);
}
if (startIconLoading)
{
timerUpdateIcons.Start();
}
} }
internal void ActivateWithFade() internal void ActivateWithFade()
@ -531,11 +541,6 @@ namespace SystemTrayMenu.UserInterface
} }
} }
internal void TimerUpdateIconsStart()
{
timerUpdateIcons.Start();
}
/// <summary> /// <summary>
/// Update the position and size of the menu. /// Update the position and size of the menu.
/// </summary> /// </summary>
@ -987,7 +992,7 @@ namespace SystemTrayMenu.UserInterface
} }
} }
private void TextBoxSearch_TextChanged() private void TextBoxSearch_TextChanged(bool causedByWatcherUpdate)
{ {
SearchTextChanging?.Invoke(); SearchTextChanging?.Invoke();
@ -1088,7 +1093,7 @@ namespace SystemTrayMenu.UserInterface
SetCounts(foldersCount, filesCount); SetCounts(foldersCount, filesCount);
#endif #endif
SearchTextChanged?.Invoke(this, string.IsNullOrEmpty(userPattern)); SearchTextChanged?.Invoke(this, string.IsNullOrEmpty(userPattern), causedByWatcherUpdate);
#if TODO // SEARCH #if TODO // SEARCH
if (anyIconNotUpdated) if (anyIconNotUpdated)
{ {