SystemTrayMenu/Business/WaitToLoadMenu.cs

115 lines
3.2 KiB
C#
Raw Normal View History

// <copyright file="WaitToLoadMenu.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.Handler
{
using System;
using System.Windows.Threading;
2022-11-20 07:33:23 +13:00
using static SystemTrayMenu.UserInterface.Menu;
internal class WaitToLoadMenu : IDisposable
{
private readonly DispatcherTimer timerStartLoad = new();
private ListViewItemData? currentItemData;
private bool alreadyOpened;
internal WaitToLoadMenu()
{
timerStartLoad.Interval = TimeSpan.FromMilliseconds(Properties.Settings.Default.TimeUntilOpens);
2023-05-19 06:42:46 +12:00
timerStartLoad.Tick += OpenSubMenuByTimer;
}
2023-04-17 09:27:27 +12:00
internal event Action? StopLoadMenu;
2023-05-19 06:42:46 +12:00
internal event Action<ListViewItemData>? MouseSelect;
internal bool MouseActive { get; set; }
public void Dispose() => timerStartLoad.Stop();
2023-05-19 06:42:46 +12:00
internal void MouseEnter(ListViewItemData itemData)
{
if (MouseActive)
{
2023-05-19 06:42:46 +12:00
MouseSelect?.Invoke(itemData);
timerStartLoad.Stop();
StopLoadMenu?.Invoke();
2023-05-19 06:42:46 +12:00
SetData(itemData);
timerStartLoad.Start();
}
}
internal void MouseLeave()
{
if (MouseActive)
{
timerStartLoad.Stop();
StopLoadMenu?.Invoke();
2023-05-19 06:42:46 +12:00
currentItemData = null;
}
}
2023-05-19 06:42:46 +12:00
internal void RowSelectionChanged(ListViewItemData? itemData)
{
// Deselect
timerStartLoad.Stop();
StopLoadMenu?.Invoke();
2023-05-19 06:42:46 +12:00
currentItemData = null;
MouseActive = false;
// Select (if any)
2023-05-19 06:42:46 +12:00
if (itemData != null)
{
2023-05-19 06:42:46 +12:00
SetData(itemData);
timerStartLoad.Start();
}
}
2023-05-19 06:42:46 +12:00
internal void OpenSubMenuByMouse(ListViewItemData itemData)
{
2023-04-25 08:38:36 +12:00
timerStartLoad.Stop();
2023-05-19 06:42:46 +12:00
StopLoadMenu?.Invoke(); // TODO: Missing in v1 ?
SetData(itemData);
2023-04-25 08:38:36 +12:00
MouseActive = true;
2023-05-19 06:42:46 +12:00
OpenSubMenu();
}
2023-05-19 06:42:46 +12:00
internal void OpenSubMenuByKey(ListViewItemData itemData)
{
timerStartLoad.Stop();
StopLoadMenu?.Invoke();
2023-05-19 06:42:46 +12:00
SetData(itemData);
MouseActive = false;
2023-05-19 06:42:46 +12:00
OpenSubMenu();
}
2023-05-19 06:42:46 +12:00
private void OpenSubMenuByTimer(object? sender, EventArgs e)
{
timerStartLoad.Stop();
2023-05-19 06:42:46 +12:00
StopLoadMenu?.Invoke(); // TODO: Missing in v1 ?
OpenSubMenu();
}
2023-05-19 06:42:46 +12:00
private void OpenSubMenu()
{
2023-05-19 06:42:46 +12:00
if (!alreadyOpened && currentItemData != null)
{
alreadyOpened = true; // TODO: Check if this bool is still needed?
2023-05-19 06:42:46 +12:00
currentItemData.OpenSubMenu();
}
}
2023-05-19 06:42:46 +12:00
private void SetData(ListViewItemData itemData)
{
2023-05-19 06:42:46 +12:00
if (currentItemData != itemData)
{
alreadyOpened = false;
2023-05-15 09:25:34 +12:00
currentItemData = itemData;
}
}
}
}