Simplified Menu construction

This commit is contained in:
Peter Kirmeier 2023-04-15 21:23:28 +02:00
parent 5a5bdb2410
commit 562a799a46
3 changed files with 42 additions and 33 deletions

View file

@ -85,7 +85,7 @@ namespace SystemTrayMenu.Business
if (Settings.Default.AppearAtMouseLocation) if (Settings.Default.AppearAtMouseLocation)
{ {
menus[0].Tag = null; menus[0].RowDataParent = null;
} }
AsEnumerable.ToList().ForEach(m => { m.ShowWithFade(); }); AsEnumerable.ToList().ForEach(m => { m.ShowWithFade(); });
@ -100,7 +100,7 @@ namespace SystemTrayMenu.Business
if (IconReader.MainPreload) if (IconReader.MainPreload)
{ {
workerMainMenu.DoWork -= LoadMenu; workerMainMenu.DoWork -= LoadMenu;
menus[0] = Create(menuData, new DirectoryInfo(Config.Path).Name); menus[0] = Create(menuData, Config.Path);
menus[0].Loaded += (s, e) => ExecuteWatcherHistory(); menus[0].Loaded += (s, e) => ExecuteWatcherHistory();
IconReader.MainPreload = false; IconReader.MainPreload = false;
@ -155,21 +155,17 @@ namespace SystemTrayMenu.Business
{ {
if (menus[0].IsUsable && if (menus[0].IsUsable &&
(menus[rowData.Level + 1] == null || (menus[rowData.Level + 1] == null ||
menus[rowData.Level + 1].Tag as RowData != rowData)) menus[rowData.Level + 1].RowDataParent != rowData))
{ {
CreateAndShowLoadingMenu(rowData); CreateAndShowLoadingMenu(rowData);
void CreateAndShowLoadingMenu(RowData rowData) void CreateAndShowLoadingMenu(RowData rowDataParent)
{ {
MenuData menuDataLoading = new(rowData.Level + 1) MenuData menuDataLoading = new(rowDataParent.Level + 1)
{ {
DirectoryState = MenuDataDirectoryState.Valid, RowDataParent = rowDataParent,
}; };
Menu menuLoading = Create(menuDataLoading, rowDataParent.Path);
Menu menuLoading = Create(menuDataLoading, new DirectoryInfo(rowData.Path).Name); menus[menuDataLoading.Level] = menuLoading;
menus[rowData.Level + 1] = menuLoading;
menuLoading.Tag = menuDataLoading.RowDataParent = rowData;
menuDataLoading.RowDataParent.SubMenu = menuLoading;
menuLoading.SetBehavior(MenuDataDirectoryState.Undefined);
ShowSubMenu(menuLoading); ShowSubMenu(menuLoading);
} }
@ -207,10 +203,7 @@ namespace SystemTrayMenu.Business
if (menuData.DirectoryState != MenuDataDirectoryState.Undefined && if (menuData.DirectoryState != MenuDataDirectoryState.Undefined &&
menus[0].IsUsable) menus[0].IsUsable)
{ {
Menu menu = Create(menuData, new DirectoryInfo(menuData.RowDataParent.ResolvedPath).Name, menuData.RowDataParent.ResolvedPath); Menu menu = Create(menuData, menuData.RowDataParent.ResolvedPath);
menu.SetBehavior(menuData.DirectoryState);
menu.Tag = menuData.RowDataParent;
menuData.RowDataParent.SubMenu = menu; menuData.RowDataParent.SubMenu = menu;
if (menus[0].IsUsable) if (menus[0].IsUsable)
{ {
@ -597,12 +590,9 @@ namespace SystemTrayMenu.Business
return (App.TaskbarLogo != null && App.TaskbarLogo.IsActive) || IsShellContextMenuOpen(); return (App.TaskbarLogo != null && App.TaskbarLogo.IsActive) || IsShellContextMenuOpen();
} }
private Menu Create(MenuData menuData, string title, string? path = null) private Menu Create(MenuData menuData, string path)
{ {
Menu menu = new(title, menuData.Level, menuData.DirectoryState) Menu menu = new(menuData, path);
{
FolderPath = path,
};
menu.MenuScrolled += AdjustMenusSizeAndLocation; // TODO: Only update vertical location while scrolling? menu.MenuScrolled += AdjustMenusSizeAndLocation; // TODO: Only update vertical location while scrolling?
menu.MouseLeave += (_, _) => menu.MouseLeave += (_, _) =>
@ -1001,7 +991,7 @@ namespace SystemTrayMenu.Business
{ {
if (menus[0].IsUsable) if (menus[0].IsUsable)
{ {
menus[0].Tag = null; menus[0].RowDataParent = null;
} }
}); });
} }
@ -1026,7 +1016,7 @@ namespace SystemTrayMenu.Business
foreach (ListViewItemData item in dgvPrevious.Items) foreach (ListViewItemData item in dgvPrevious.Items)
{ {
RowData rowDataToClear = item.data; RowData rowDataToClear = item.data;
if (rowDataToClear == (RowData)menuToShow.Tag) if (rowDataToClear == menuToShow.RowDataParent)
{ {
rowDataToClear.IsMenuOpen = keepOrSetIsMenuOpen; rowDataToClear.IsMenuOpen = keepOrSetIsMenuOpen;
} }

View file

@ -38,7 +38,7 @@ namespace SystemTrayMenu.Helper
string path; string path;
if (menu != null) if (menu != null)
{ {
RowData rowData = (RowData)menu.Tag; RowData rowData = menu.RowDataParent;
if (rowData != null) if (rowData != null)
{ {
path = rowData.ResolvedPath; path = rowData.ResolvedPath;

View file

@ -7,6 +7,7 @@ namespace SystemTrayMenu.UserInterface
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data; using System.Windows.Data;
@ -33,6 +34,7 @@ namespace SystemTrayMenu.UserInterface
private static readonly RoutedEvent FadeOutEvent = EventManager.RegisterRoutedEvent( private static readonly RoutedEvent FadeOutEvent = EventManager.RegisterRoutedEvent(
nameof(FadeOut), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Menu)); nameof(FadeOut), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Menu));
private readonly string folderPath;
#if TODO // SEARCH #if TODO // SEARCH
public const string RowFilterShowAll = "[SortIndex] LIKE '%0%'"; public const string RowFilterShowAll = "[SortIndex] LIKE '%0%'";
#endif #endif
@ -47,7 +49,7 @@ namespace SystemTrayMenu.UserInterface
private bool isClosed = false; // TODO WPF Replace Forms wrapper private bool isClosed = false; // TODO WPF Replace Forms wrapper
private DispatcherTimer timerUpdateIcons = new DispatcherTimer(DispatcherPriority.Render, Dispatcher.CurrentDispatcher); private DispatcherTimer timerUpdateIcons = new DispatcherTimer(DispatcherPriority.Render, Dispatcher.CurrentDispatcher);
internal Menu(string title, int level, MenuDataDirectoryState directoryState) internal Menu(MenuData menuData, string path)
{ {
timerUpdateIcons.Tick += TimerUpdateIcons_Tick; timerUpdateIcons.Tick += TimerUpdateIcons_Tick;
Closed += (_, _) => Closed += (_, _) =>
@ -58,13 +60,24 @@ namespace SystemTrayMenu.UserInterface
InitializeComponent(); InitializeComponent();
Level = level; folderPath = path;
RowDataParent = menuData.RowDataParent;
Level = menuData.Level;
if (Level == 0) if (Level == 0)
{ {
// Use Main Menu DPI for all further calculations // Use Main Menu DPI for all further calculations
Scaling.CalculateFactorByDpi(this); Scaling.CalculateFactorByDpi(this);
} }
else
{
// This will be a submenu
if (RowDataParent != null)
{
RowDataParent.SubMenu = this;
}
}
string title = new DirectoryInfo(path).Name;
if (title.Length > MenuDefines.LengthMax) if (title.Length > MenuDefines.LengthMax)
{ {
title = $"{title[..MenuDefines.LengthMax]}..."; title = $"{title[..MenuDefines.LengthMax]}...";
@ -226,6 +239,12 @@ namespace SystemTrayMenu.UserInterface
item.data.SubMenu?.Close(); item.data.SubMenu?.Close();
} }
}; };
// This will be a submenu..
if (Level > 0)
{
SetBehavior(menuData.DirectoryState);
}
} }
internal event Action? MenuScrolled; internal event Action? MenuScrolled;
@ -290,7 +309,7 @@ namespace SystemTrayMenu.UserInterface
internal int Level { get; set; } internal int Level { get; set; }
internal string? FolderPath { get; set; } internal RowData? RowDataParent { get; set; }
internal bool IsUsable => Visibility == Visibility.Visible && !isFading && !IsDisposed && !Disposing; internal bool IsUsable => Visibility == Visibility.Visible && !isFading && !IsDisposed && !Disposing;
@ -548,14 +567,14 @@ namespace SystemTrayMenu.UserInterface
else if (useCustomLocation && !isCustomLocationOutsideOfScreen) else if (useCustomLocation && !isCustomLocationOutsideOfScreen)
{ {
// Do not adjust location again because Cursor.Postion changed // Do not adjust location again because Cursor.Postion changed
if (Tag != null) if (RowDataParent != null)
{ {
return; return;
} }
// Use this menu as predecessor and overwrite location with CustomLocation // Use this menu as predecessor and overwrite location with CustomLocation
menuPredecessor = this; menuPredecessor = this;
Tag = new RowData(); RowDataParent = new RowData();
Left = Settings.Default.CustomLocationX; Left = Settings.Default.CustomLocationX;
Top = Settings.Default.CustomLocationY; Top = Settings.Default.CustomLocationY;
directionToRight = true; directionToRight = true;
@ -565,14 +584,14 @@ namespace SystemTrayMenu.UserInterface
else if (Settings.Default.AppearAtMouseLocation) else if (Settings.Default.AppearAtMouseLocation)
{ {
// Do not adjust location again because Cursor.Postion changed // Do not adjust location again because Cursor.Postion changed
if (Tag != null) if (RowDataParent != null)
{ {
return; return;
} }
// Use this menu as predecessor and overwrite location with Cursor.Postion // Use this menu as predecessor and overwrite location with Cursor.Postion
menuPredecessor = this; menuPredecessor = this;
Tag = new RowData(); RowDataParent = new RowData();
var position = Mouse.GetPosition(this); var position = Mouse.GetPosition(this);
Left = position.X; Left = position.X;
Top = position.Y - labelTitle.Height; Top = position.Y - labelTitle.Height;
@ -691,7 +710,7 @@ namespace SystemTrayMenu.UserInterface
{ {
case StartLocation.Predecessor: case StartLocation.Predecessor:
RowData trigger = (RowData)Tag; RowData trigger = RowDataParent;
ListView dgv = menuPredecessor!.GetDataGridView() !; ListView dgv = menuPredecessor!.GetDataGridView() !;
// Set position on same height as the selected row from predecessor // Set position on same height as the selected row from predecessor
@ -1110,7 +1129,7 @@ namespace SystemTrayMenu.UserInterface
private void PictureBoxOpenFolder_Click(object sender, RoutedEventArgs e) private void PictureBoxOpenFolder_Click(object sender, RoutedEventArgs e)
{ {
Menus.OpenFolder(FolderPath); Menus.OpenFolder(folderPath);
} }
#if TODO // BorderColors #if TODO // BorderColors