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

View file

@ -154,7 +154,7 @@ namespace SystemTrayMenu.UserInterface
MouseUp += Menu_MouseUp;
MouseMove += Menu_MouseMove;
textBoxSearch.TextChanged += (_, _) => TextBoxSearch_TextChanged();
textBoxSearch.TextChanged += (_, _) => TextBoxSearch_TextChanged(false);
textBoxSearch.ContextMenu = new()
{
Background = SystemColors.ControlBrush,
@ -266,14 +266,14 @@ namespace SystemTrayMenu.UserInterface
}
#endif
Loaded += (sender, e) =>
Loaded += (_, _) =>
{
NativeMethods.HideFromAltTab(this);
RaiseEvent(new(routedEvent: FadeInEvent));
};
Closed += (sender, e) =>
Closed += (_, _) =>
{
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;
@ -296,7 +296,7 @@ namespace SystemTrayMenu.UserInterface
internal event Action? SearchTextChanging;
internal event Action<Menu, bool>? SearchTextChanged;
internal event Action<Menu, bool, bool>? SearchTextChanged;
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)
{
dgv.ScrollIntoView(dgv.Items[0]);
@ -439,7 +439,7 @@ namespace SystemTrayMenu.UserInterface
((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 filesCount = 0;
@ -471,6 +471,16 @@ namespace SystemTrayMenu.UserInterface
dgv.ItemsSource = items;
SetCounts(foldersCount, filesCount);
if (state != null)
{
SetSubMenuState(state.Value);
}
if (startIconLoading)
{
timerUpdateIcons.Start();
}
}
internal void ActivateWithFade()
@ -531,11 +541,6 @@ namespace SystemTrayMenu.UserInterface
}
}
internal void TimerUpdateIconsStart()
{
timerUpdateIcons.Start();
}
/// <summary>
/// Update the position and size of the menu.
/// </summary>
@ -987,7 +992,7 @@ namespace SystemTrayMenu.UserInterface
}
}
private void TextBoxSearch_TextChanged()
private void TextBoxSearch_TextChanged(bool causedByWatcherUpdate)
{
SearchTextChanging?.Invoke();
@ -1088,7 +1093,7 @@ namespace SystemTrayMenu.UserInterface
SetCounts(foldersCount, filesCount);
#endif
SearchTextChanged?.Invoke(this, string.IsNullOrEmpty(userPattern));
SearchTextChanged?.Invoke(this, string.IsNullOrEmpty(userPattern), causedByWatcherUpdate);
#if TODO // SEARCH
if (anyIconNotUpdated)
{