Reduce event dependencies during startup (preloading)

Remove obsolete code
This commit is contained in:
Peter Kirmeier 2023-04-20 00:50:29 +02:00
parent 74d5be3890
commit 3464e3e4b9
8 changed files with 36 additions and 216 deletions

View file

@ -28,14 +28,17 @@ namespace SystemTrayMenu
menus.LoadStarted += menuNotifyIcon.LoadingStart;
menus.LoadStopped += menuNotifyIcon.LoadingStop;
menuNotifyIcon.Click += () => menus.SwitchOpenClose(true);
menus.MainPreload();
if (Settings.Default.ShowInTaskbar)
{
TaskbarLogo = new TaskbarLogo();
TaskbarLogo = new ();
TaskbarLogo.Activated += (_, _) => menus.SwitchOpenCloseByTaskbarItem();
TaskbarLogo.Show();
}
else
{
menus.FirstStartInBackground();
}
if (Settings.Default.CheckForUpdates)
{

View file

@ -331,13 +331,6 @@ namespace SystemTrayMenu.Handler
{
ClosePressed?.Invoke();
}
#if TODO // WPF: Can be removed as we do not paint ourselfs?
if (iRowKey > -1 && dgv.Rows.Count > iRowKey)
{
// Raise Dgv_RowPostPaint to show ProcessStarted
dgv.InvalidateRow(iRowKey);
}
#endif
}
else
{

View file

@ -45,11 +45,6 @@ namespace SystemTrayMenu.Business
private bool searchTextChanging;
private int lastMouseDownRowIndex = -1;
private bool showMenuAfterMainPreload;
#if TODO // TOUCH
private int dragSwipeScrollingStartRowIndex = -1;
private bool isDraggingSwipeScrolling;
#endif
private bool isDragSwipeScrolled;
private bool hideSubmenuDuringRefreshSearch;
public Menus()
@ -107,12 +102,13 @@ namespace SystemTrayMenu.Business
switch (menuData.DirectoryState)
{
case MenuDataDirectoryState.Valid:
if (IconReader.MainPreload)
if (IconReader.IsPreloading)
{
workerMainMenu.DoWork -= LoadMenu;
workerMainMenu.CancelAsync();
Create(menuData, Config.Path); // Level 0 Main Menu
IconReader.MainPreload = false;
IconReader.IsPreloading = false;
if (showMenuAfterMainPreload)
{
AsEnumerable.ToList().ForEach(m => { m.ShowWithFade(); });
@ -258,7 +254,6 @@ namespace SystemTrayMenu.Business
joystickHelper = new();
joystickHelper.KeyPressed += (key, modifiers) => menus[0]?.Dispatcher.Invoke(keyboardInput.CmdKeyProcessed, new object[] { null!, key, modifiers });
timerShowProcessStartedAsLoadingIcon.Interval = TimeSpan.FromMilliseconds(Settings.Default.TimeUntilClosesAfterEnterPressed);
timerStillActiveCheck.Interval = TimeSpan.FromMilliseconds(Settings.Default.TimeUntilClosesAfterEnterPressed + 20);
timerStillActiveCheck.Tick += (sender, e) => StillActiveTick();
void StillActiveTick()
@ -327,7 +322,7 @@ namespace SystemTrayMenu.Business
private bool IsMainUsable => menus[0]?.IsUsable ?? false;
private IEnumerable<Menu> AsEnumerable => menus.Where(m => m != null && !m.IsDisposed)!;
private IEnumerable<Menu> AsEnumerable => menus.Where(m => m != null && !m.IsClosed)!;
private List<Menu> AsList => AsEnumerable.ToList();
@ -372,20 +367,23 @@ namespace SystemTrayMenu.Business
internal void SwitchOpenCloseByTaskbarItem()
{
SwitchOpenClose(true);
// User started with taskbar or clicked on taskbar: remember to open menu after preload has finished
showMenuAfterMainPreload = true;
SwitchOpenClose(true, true);
timerStillActiveCheck.Start();
}
internal bool IsOpenCloseStateOpening()
internal void FirstStartInBackground()
{
return openCloseState == OpenCloseState.Opening;
dispatchter.Invoke(() => SwitchOpenClose(false, true));
}
internal void SwitchOpenClose(bool byClick, bool isMainPreload = false)
internal void SwitchOpenClose(bool byClick, bool allowPreloading = false)
{
// Ignore open close events during main preload #248
if (IconReader.MainPreload && !isMainPreload)
if (IconReader.IsPreloading && !allowPreloading)
{
// User pressed hotkey or clicked on notifyicon: remember to open menu after preload has finished
showMenuAfterMainPreload = true;
return;
}
@ -426,21 +424,6 @@ namespace SystemTrayMenu.Business
deactivatedTime = DateTime.MinValue;
}
internal void MainPreload()
{
IconReader.MainPreload = true;
timerShowProcessStartedAsLoadingIcon.Tick += Tick;
timerShowProcessStartedAsLoadingIcon.Interval = TimeSpan.FromMilliseconds(5);
timerShowProcessStartedAsLoadingIcon.Start();
void Tick(object? sender, EventArgs e)
{
timerShowProcessStartedAsLoadingIcon.Tick -= Tick;
timerShowProcessStartedAsLoadingIcon.Interval = TimeSpan.FromMilliseconds(Settings.Default.TimeUntilClosesAfterEnterPressed);
SwitchOpenClose(false, true);
}
}
internal void StartWorker()
{
if (Settings.Default.GenerateShortcutsToDrives)
@ -451,8 +434,7 @@ namespace SystemTrayMenu.Business
if (!workerMainMenu.IsBusy)
{
LoadStarted?.Invoke();
workerMainMenu.RunWorkerAsync(
new object[] { Config.Path, 0 });
workerMainMenu.RunWorkerAsync(null);
}
}
@ -489,16 +471,6 @@ namespace SystemTrayMenu.Business
eDoWork.Result = menuData;
}
private static int GetRowUnderCursor(ListView dgv, Point location)
{
#if TODO // TOUCH
ListView.HitTestInfo myHitTest = dgv.HitTest(location.X, location.Y);
return myHitTest.RowIndex;
#else
return 0;
#endif
}
private bool IsActive()
{
bool IsShellContextMenuOpen()
@ -543,9 +515,7 @@ namespace SystemTrayMenu.Business
};
menu.MouseEnter += (_, _) => waitLeave.Stop();
menu.CmdKeyProcessed += keyboardInput.CmdKeyProcessed;
#if TODO // Misc MouseEvents and TOUCH
menu.KeyPressCheck += Menu_KeyPressCheck;
#endif
menu.SearchTextChanging += Menu_SearchTextChanging;
void Menu_SearchTextChanging()
{
@ -585,7 +555,7 @@ namespace SystemTrayMenu.Business
menu.Deactivated += Deactivate;
void Deactivate(object? sender, EventArgs e)
{
if (IsOpenCloseStateOpening())
if (openCloseState == OpenCloseState.Opening)
{
Log.Info("Ignored Deactivate, because openCloseState == OpenCloseState.Opening");
}
@ -625,11 +595,7 @@ namespace SystemTrayMenu.Business
{
#if TODO // Misc MouseEvents
dgv.MouseLeave += dgvMouseRow.MouseLeave;
dgv.MouseLeave += Dgv_MouseLeave;
dgv.MouseMove += waitToOpenMenu.MouseMove;
#endif
#if TODO // TOUCH
dgv.MouseMove += Dgv_MouseMove;
#endif
dgv.SelectionChanged += Dgv_SelectionChanged;
#if TODO // BorderColors and PaintEvent
@ -685,53 +651,6 @@ namespace SystemTrayMenu.Business
}
}
#if TODO // TOUCH
private void Dgv_MouseMove(object sender, MouseEventArgs e)
{
if (isDraggingSwipeScrolling)
{
ListView dgv = (ListView)sender;
int newRow = GetRowUnderCursor(dgv, e.Location);
if (newRow > -1)
{
int delta = dragSwipeScrollingStartRowIndex - newRow;
if (DoScroll(dgv, ref delta))
{
dragSwipeScrollingStartRowIndex += delta;
}
}
}
}
private bool DoScroll(ListView dgv, ref int delta)
{
bool scrolled = false;
if (delta != 0)
{
if (delta < 0 && dgv.FirstDisplayedScrollingRowIndex == 0)
{
delta = 0;
}
int newFirstDisplayedScrollingRowIndex = dgv.FirstDisplayedScrollingRowIndex + (delta * 2);
if (newFirstDisplayedScrollingRowIndex < 0 || newFirstDisplayedScrollingRowIndex >= dgv.RowCount)
{
newFirstDisplayedScrollingRowIndex = dgv.FirstDisplayedScrollingRowIndex + delta;
}
if (newFirstDisplayedScrollingRowIndex > -1 && newFirstDisplayedScrollingRowIndex < dgv.RowCount)
{
isDragSwipeScrolled = true;
dgv.FirstDisplayedScrollingRowIndex = newFirstDisplayedScrollingRowIndex;
scrolled = dgv.FirstDisplayedScrollingRowIndex == newFirstDisplayedScrollingRowIndex;
}
}
return scrolled;
}
#endif
private void Dgv_MouseDown(object sender, int index, MouseButtonEventArgs e)
{
ListView dgv = (ListView)sender;
@ -745,37 +664,11 @@ namespace SystemTrayMenu.Business
{
lastMouseDownRowIndex = index;
}
#if TODO // TOUCH
Menu menu = (Menu)((ListView)sender).GetParentWindow();
if (menu != null && menu.ScrollbarVisible)
{
bool isTouchEnabled = DllImports.NativeMethods.IsTouchEnabled();
if ((isTouchEnabled && Settings.Default.SwipeScrollingEnabledTouch) ||
(!isTouchEnabled && Settings.Default.SwipeScrollingEnabled))
{
isDraggingSwipeScrolling = true;
}
dragSwipeScrollingStartRowIndex = GetRowUnderCursor(dgv, e.Location);
}
#endif
}
private void Dgv_MouseUp(object sender, int index, MouseButtonEventArgs e)
{
lastMouseDownRowIndex = -1;
#if TODO // TOUCH
isDraggingSwipeScrolling = false;
#endif
isDragSwipeScrolled = false;
}
private void Dgv_MouseLeave(object sender, EventArgs e)
{
#if TODO // TOUCH
isDraggingSwipeScrolling = false;
#endif
isDragSwipeScrolled = false;
}
private void MouseEnterOk(ListView dgv, int rowIndex)
@ -801,8 +694,7 @@ namespace SystemTrayMenu.Business
{
ListView dgv = (ListView)sender;
if (!isDragSwipeScrolled &&
e.RowIndex == lastMouseDownRowIndex &&
if (e.RowIndex == lastMouseDownRowIndex &&
e.RowIndex > -1 &&
e.RowIndex < dgv.Items.Count)
{
@ -825,14 +717,11 @@ namespace SystemTrayMenu.Business
if (e.ClickCount == 1)
{
if (!isDragSwipeScrolled)
{
lastMouseDownRowIndex = -1;
lastMouseDownRowIndex = -1;
((Menu.ListViewItemData)sender.Items[index]).data.MouseClick(e, out doClose);
((Menu.ListViewItemData)sender.Items[index]).data.MouseClick(e, out doClose);
waitToOpenMenu.ClickOpensInstantly(sender, index);
}
waitToOpenMenu.ClickOpensInstantly(sender, index);
}
else if (e.ClickCount == 2)
{
@ -945,6 +834,8 @@ namespace SystemTrayMenu.Business
{
rowData.ProcessStarted = false;
row.Cells[0].Value = Resources.StaticResources.LoadingIcon;
timerShowProcessStartedAsLoadingIcon.Stop();
timerShowProcessStartedAsLoadingIcon.Interval = TimeSpan.FromMilliseconds(Settings.Default.TimeUntilClosesAfterEnterPressed);
timerShowProcessStartedAsLoadingIcon.Tick += Tick;
void Tick(object sender, EventArgs e)
{
@ -952,8 +843,6 @@ namespace SystemTrayMenu.Business
timerShowProcessStartedAsLoadingIcon.Stop();
row.Cells[0].Value = rowData.ReadIcon(false);
}
timerShowProcessStartedAsLoadingIcon.Stop();
timerShowProcessStartedAsLoadingIcon.Start();
timerStillActiveCheck.Stop();
timerStillActiveCheck.Start();
@ -1157,16 +1046,6 @@ namespace SystemTrayMenu.Business
}
}
#if TODO // Misc MouseEvents and TOUCH
private void Menu_KeyPressCheck(object sender, KeyPressEventArgs e)
{
if (isDraggingSwipeScrolling)
{
e.Handled = true;
}
}
#endif
private void ExecuteWatcherHistory()
{
foreach (var fileSystemEventArgs in watcherHistory)

View file

@ -45,10 +45,6 @@ namespace SystemTrayMenu.Handler
public void Dispose()
{
timerStartLoad.Stop();
#if TODO // WPF: Can be optimized away?
dgv?.Dispose();
dgvTmp?.Dispose();
#endif
}
internal void MouseEnter(object sender, int rowIndex)
@ -136,11 +132,7 @@ namespace SystemTrayMenu.Handler
if (mouseMoveEvents > 6)
{
MouseActive = true;
#if TODO // WPF: Can be optimized away?
if (dgvTmp != null && !dgvTmp.IsDisposed)
#else
if (dgvTmp != null)
#endif
{
MouseEnter(dgvTmp, rowIndexTmp);
}

View file

@ -75,9 +75,6 @@ namespace SystemTrayMenu.Helpers
if (disposing)
{
timerRaiseRowMouseLeave.Stop();
#if TODO // WPF: Can be optimized away?
senderObject?.Dispose();
#endif
}
}

View file

@ -43,18 +43,17 @@ namespace SystemTrayMenu.UserInterface
private bool directionToRight;
private bool mouseDown;
private Point lastLocation;
#if TODO // SEARCH
private bool isSetSearchText;
#endif
private bool isClosed = false; // TODO WPF Replace Forms wrapper
internal Menu(MenuData menuData, string path)
{
timerUpdateIcons.Tick += TimerUpdateIcons_Tick;
Closed += (_, _) =>
{
timerUpdateIcons.Stop();
isClosed = true; // TODO WPF Replace Forms wrapper
IsClosed = true; // TODO WPF Replace Forms wrapper
};
InitializeComponent();
@ -228,7 +227,7 @@ namespace SystemTrayMenu.UserInterface
pictureBoxRestart.MouseEnter += ControlsMouseEnter;
pictureBoxSearch.MouseEnter += ControlsMouseEnter;
tableLayoutPanelMenu.MouseEnter += ControlsMouseEnter;
tableLayoutPanelDgvAndScrollbar.MouseEnter += ControlsMouseEnter;
dgv.MouseEnter += ControlsMouseEnter;
tableLayoutPanelBottom.MouseEnter += ControlsMouseEnter;
labelStatus.MouseEnter += ControlsMouseEnter;
void ControlsMouseEnter(object sender, EventArgs e)
@ -245,7 +244,7 @@ namespace SystemTrayMenu.UserInterface
pictureBoxRestart.MouseLeave += ControlsMouseLeave;
pictureBoxSearch.MouseLeave += ControlsMouseLeave;
tableLayoutPanelMenu.MouseLeave += ControlsMouseLeave;
tableLayoutPanelDgvAndScrollbar.MouseLeave += ControlsMouseLeave;
dgv.MouseLeave += ControlsMouseLeave;
tableLayoutPanelBottom.MouseLeave += ControlsMouseLeave;
labelStatus.MouseLeave += ControlsMouseLeave;
void ControlsMouseLeave(object sender, EventArgs e)
@ -253,7 +252,7 @@ namespace SystemTrayMenu.UserInterface
MouseLeave?.Invoke();
}
#endif
#if TODO // TOUCH
#if TODO // Misc MouseEvents
bool isTouchEnabled = NativeMethods.IsTouchEnabled();
if ((isTouchEnabled && Settings.Default.DragDropItemsEnabledTouch) ||
(!isTouchEnabled && Settings.Default.DragDropItemsEnabled))
@ -290,10 +289,6 @@ namespace SystemTrayMenu.UserInterface
internal event Action<Menu, Key, ModifierKeys>? CmdKeyProcessed;
#if TODO // Misc MouseEvents and TOUCH
internal event EventHandler<KeyPressEventArgs> KeyPressCheck;
#endif
internal event Action? SearchTextChanging;
internal event Action<Menu, bool>? SearchTextChanged;
@ -330,23 +325,15 @@ namespace SystemTrayMenu.UserInterface
TopRight,
}
public bool IsDisposed => isClosed; // TODO WPF Replace Forms wrapper
public bool Disposing => isClosed; // TODO WPF Replace Forms wrapper
public System.Drawing.Point Location => new ((int)Left, (int)Top); // TODO WPF Replace Forms wrapper)
public System.Drawing.Point Location => new ((int)Left, (int)Top); // TODO WPF Replace Forms wrapper
internal int Level { get; set; }
internal RowData? RowDataParent { get; set; }
internal bool IsUsable => Visibility == Visibility.Visible && !isFading && !IsDisposed && !Disposing;
internal bool IsClosed { get; private set; } = false;
#if TODO // TOUCH
internal bool ScrollbarVisible { get; private set; }
private ListView tableLayoutPanelDgvAndScrollbar => dgv; // TODO WPF Remove and replace with dgv
#endif
internal bool IsUsable => Visibility == Visibility.Visible && !isFading && !IsClosed;
internal void ResetSearchText()
{
@ -609,7 +596,6 @@ namespace SystemTrayMenu.UserInterface
else
{
// Layout cannot be calculated during loading, postpone this event
// TODO: Make sure lampa capture is registered only once
Loaded += (_, _) => AdjustWindowPositionInternal();
}
@ -964,12 +950,6 @@ namespace SystemTrayMenu.UserInterface
}
}
#if TODO // Misc MouseEvents and TOUCH
private void TextBoxSearch_KeyPress(object sender, KeyPressEventArgs e)
{
KeyPressCheck?.Invoke(sender, e);
}
#endif
private void TextBoxSearch_TextChanged()
{
SearchTextChanging?.Invoke();
@ -1077,11 +1057,6 @@ namespace SystemTrayMenu.UserInterface
{
timerUpdateIcons.Start();
}
if (dgv.Rows.Count > 0)
{
dgv.FirstDisplayedScrollingRowIndex = 0;
}
#endif
}

View file

@ -46,14 +46,6 @@ namespace SystemTrayMenu.UserInterface
Title = myname;
Closed += (_, _) => Application.Current.Shutdown();
Deactivated += SetStateNormal;
Activated += (object? sender, EventArgs e) =>
{
SetStateNormal(sender, e);
Activate();
UpdateLayout();
Focus();
};
ContentRendered += MoveOutOfScreen;
}
@ -63,16 +55,5 @@ namespace SystemTrayMenu.UserInterface
ContentRendered -= MoveOutOfScreen;
Top += SystemParameters.VirtualScreenHeight;
}
/// <summary>
/// This ensures that next click on taskbaritem works as activate event/click event.
/// </summary>
private void SetStateNormal(object? sender, EventArgs e)
{
if (IsActive)
{
WindowState = WindowState.Normal;
}
}
}
}

View file

@ -36,7 +36,7 @@ namespace SystemTrayMenu.Utilities
}
// see https://github.com/Hofknecht/SystemTrayMenu/issues/209.
public static bool MainPreload { get; set; }
public static bool IsPreloading { get; set; } = true;
public static void Dispose(bool includingMainMenu = true)
{
@ -158,7 +158,7 @@ namespace SystemTrayMenu.Utilities
if (updateIconInBackground)
{
if (MainPreload)
if (IsPreloading)
{
DictIconCache(isMainMenu).GetOrAdd(key, GetFolder);
}