SystemTrayMenu/Business/WaitToLoadMenu.cs

115 lines
3.1 KiB
C#
Raw Normal View History

// <copyright file="WaitToLoadMenu.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.Business
{
using System;
using System.Windows.Threading;
using SystemTrayMenu.DataClasses;
internal class WaitToLoadMenu : IDisposable
{
private readonly DispatcherTimer timerStartLoad = new();
2023-05-19 09:28:52 +12:00
private RowData? 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 09:28:52 +12:00
internal event Action<RowData>? MouseSelect;
internal bool MouseActive { get; set; }
public void Dispose() => timerStartLoad.Stop();
2023-05-19 09:28:52 +12:00
internal void MouseEnter(RowData 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 09:28:52 +12:00
internal void RowSelectionChanged(RowData? 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 09:28:52 +12:00
internal void OpenSubMenuByMouse(RowData itemData)
{
2023-04-25 08:38:36 +12:00
timerStartLoad.Stop();
2023-05-25 09:47:41 +12:00
StopLoadMenu?.Invoke();
2023-05-19 06:42:46 +12:00
SetData(itemData);
2023-04-25 08:38:36 +12:00
MouseActive = true;
2023-05-19 06:42:46 +12:00
OpenSubMenu();
}
2023-05-19 09:28:52 +12:00
internal void OpenSubMenuByKey(RowData 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-25 09:47:41 +12:00
StopLoadMenu?.Invoke();
2023-05-19 06:42:46 +12:00
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 09:28:52 +12:00
private void SetData(RowData itemData)
{
2023-05-19 06:42:46 +12:00
if (currentItemData != itemData)
{
alreadyOpened = false;
2023-05-15 09:25:34 +12:00
currentItemData = itemData;
}
}
}
}