2020-07-07 07:15:45 +12:00
|
|
|
|
// <copyright file="KeyboardInput.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
2020-06-21 03:38:21 +12:00
|
|
|
|
|
|
|
|
|
namespace SystemTrayMenu.Handler
|
|
|
|
|
{
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2023-05-03 10:34:30 +12:00
|
|
|
|
using System.Windows.Controls;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
using System.Windows.Input;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using SystemTrayMenu.DataClasses;
|
2023-04-17 00:17:33 +12:00
|
|
|
|
using SystemTrayMenu.Helpers;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using SystemTrayMenu.Utilities;
|
2023-04-25 10:24:48 +12:00
|
|
|
|
using static SystemTrayMenu.UserInterface.Menu;
|
2021-06-27 21:35:07 +12:00
|
|
|
|
using Menu = SystemTrayMenu.UserInterface.Menu;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
internal class KeyboardInput : IDisposable
|
|
|
|
|
{
|
2023-04-17 06:17:24 +12:00
|
|
|
|
private readonly Menu?[] menus;
|
2021-11-17 12:13:46 +13:00
|
|
|
|
private readonly KeyboardHook hook = new();
|
2020-06-21 03:38:21 +12:00
|
|
|
|
|
2023-05-04 09:18:03 +12:00
|
|
|
|
private Menu? focussedMenu;
|
2023-05-04 10:34:23 +12:00
|
|
|
|
private ListViewItemData? focussedRow;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
|
2023-04-17 06:17:24 +12:00
|
|
|
|
public KeyboardInput(Menu?[] menus)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
|
|
|
|
this.menus = menus;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 10:41:03 +13:00
|
|
|
|
internal event Action? HotKeyPressed;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
|
2022-12-04 10:41:03 +13:00
|
|
|
|
internal event Action? ClosePressed;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
|
2023-05-05 10:08:39 +12:00
|
|
|
|
internal event Action<Menu, ListViewItemData>? RowSelected;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
|
2023-05-05 10:08:39 +12:00
|
|
|
|
internal event Action<Menu?, ListViewItemData?>? RowDeselected;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
|
2023-05-05 10:08:39 +12:00
|
|
|
|
internal event Action<Menu, ListViewItemData>? EnterPressed;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
|
2021-04-17 12:39:48 +12:00
|
|
|
|
internal bool InUse { get; set; }
|
2020-07-08 03:05:19 +12:00
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
hook.Dispose();
|
2020-05-31 05:59:33 +12:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
internal void RegisterHotKey()
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(Properties.Settings.Default.HotKey))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-07-04 08:02:16 +12:00
|
|
|
|
hook.RegisterHotKey();
|
2021-10-27 09:09:40 +13:00
|
|
|
|
hook.KeyPressed += (sender, e) => HotKeyPressed?.Invoke();
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
catch (InvalidOperationException ex)
|
|
|
|
|
{
|
2020-06-26 06:54:27 +12:00
|
|
|
|
Log.Warn($"key:'{Properties.Settings.Default.HotKey}'", ex);
|
2020-06-21 03:38:21 +12:00
|
|
|
|
Properties.Settings.Default.HotKey = string.Empty;
|
|
|
|
|
Properties.Settings.Default.Save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void ResetSelectedByKey()
|
|
|
|
|
{
|
2023-05-04 09:18:03 +12:00
|
|
|
|
focussedMenu = null;
|
2023-05-04 10:34:23 +12:00
|
|
|
|
focussedRow = null;
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-03 08:04:32 +12:00
|
|
|
|
internal void CmdKeyProcessed(Menu sender, Key key, ModifierKeys modifiers)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2022-11-29 08:27:52 +13:00
|
|
|
|
switch (key)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2022-10-23 11:02:31 +13:00
|
|
|
|
case Key.Enter:
|
2022-11-29 08:27:52 +13:00
|
|
|
|
if (modifiers == ModifierKeys.None)
|
|
|
|
|
{
|
|
|
|
|
SelectByKey(key, modifiers);
|
2023-05-04 09:18:03 +12:00
|
|
|
|
focussedMenu?.FocusTextBox();
|
2022-11-29 08:27:52 +13:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-11 11:39:52 +13:00
|
|
|
|
break;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
case Key.Left:
|
|
|
|
|
case Key.Right:
|
|
|
|
|
case Key.Home:
|
|
|
|
|
case Key.End:
|
|
|
|
|
case Key.Up:
|
|
|
|
|
case Key.Down:
|
|
|
|
|
case Key.Escape:
|
2022-11-29 08:27:52 +13:00
|
|
|
|
if (modifiers == ModifierKeys.None)
|
|
|
|
|
{
|
|
|
|
|
SelectByKey(key, modifiers);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
break;
|
2022-11-29 08:27:52 +13:00
|
|
|
|
case Key.F4:
|
|
|
|
|
if (modifiers == ModifierKeys.Alt)
|
|
|
|
|
{
|
|
|
|
|
SelectByKey(key, modifiers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case Key.F:
|
|
|
|
|
if (modifiers == ModifierKeys.Control)
|
|
|
|
|
{
|
2023-05-04 09:18:03 +12:00
|
|
|
|
focussedMenu?.FocusTextBox();
|
2022-11-29 08:27:52 +13:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-31 05:59:33 +12:00
|
|
|
|
break;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
case Key.Tab:
|
2022-11-29 08:27:52 +13:00
|
|
|
|
if (modifiers == ModifierKeys.None)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2022-11-29 08:27:52 +13:00
|
|
|
|
int indexOfTheCurrentMenu = GetMenuIndex(sender);
|
2020-06-21 03:38:21 +12:00
|
|
|
|
int indexMax = menus.Where(m => m != null).Count() - 1;
|
|
|
|
|
int indexNew = 0;
|
|
|
|
|
if (indexOfTheCurrentMenu > 0)
|
|
|
|
|
{
|
|
|
|
|
indexNew = indexOfTheCurrentMenu - 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
indexNew = indexMax;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 12:52:24 +13:00
|
|
|
|
menus[indexNew]?.FocusTextBox();
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
2022-11-29 08:27:52 +13:00
|
|
|
|
else if (modifiers == ModifierKeys.Shift)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2022-11-29 08:27:52 +13:00
|
|
|
|
int indexOfTheCurrentMenu = GetMenuIndex(sender);
|
2020-06-21 03:38:21 +12:00
|
|
|
|
int indexMax = menus.Where(m => m != null).Count() - 1;
|
|
|
|
|
int indexNew = 0;
|
|
|
|
|
if (indexOfTheCurrentMenu < indexMax)
|
|
|
|
|
{
|
|
|
|
|
indexNew = indexOfTheCurrentMenu + 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
indexNew = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 12:52:24 +13:00
|
|
|
|
menus[indexNew]?.FocusTextBox();
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
break;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
case Key.Apps:
|
2022-11-29 08:27:52 +13:00
|
|
|
|
if (modifiers == ModifierKeys.None)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-05-04 09:18:03 +12:00
|
|
|
|
ListView? dgv = focussedMenu?.GetDataGridView();
|
2022-12-04 10:41:03 +13:00
|
|
|
|
if (dgv != null)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-05-04 10:34:23 +12:00
|
|
|
|
if (focussedRow != null)
|
2022-12-04 10:41:03 +13:00
|
|
|
|
{
|
2022-11-30 10:48:45 +13:00
|
|
|
|
#if TODO // WPF: Better way to open context menu (as it looks like this is the code's intention)
|
2022-12-04 10:41:03 +13:00
|
|
|
|
Point point = dgv.GetCellDisplayRectangle(2, iRowKey, false).Location;
|
|
|
|
|
RowData trigger = (RowData)dgv.Rows[iRowKey].Cells[2].Value;
|
|
|
|
|
MouseEventArgs mouseEventArgs = new(MouseButtons.Right, 1, point.X, point.Y, 0);
|
|
|
|
|
trigger.MouseDown(dgv, mouseEventArgs);
|
2022-10-23 11:02:31 +13:00
|
|
|
|
#endif
|
2022-12-04 10:41:03 +13:00
|
|
|
|
}
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2023-04-17 09:27:27 +12:00
|
|
|
|
int GetMenuIndex(in Menu? currentMenu)
|
2020-07-07 07:15:45 +12:00
|
|
|
|
{
|
|
|
|
|
int index = 0;
|
2023-04-17 09:27:27 +12:00
|
|
|
|
foreach (Menu? menuFindIndex in menus.Where(m => m != null))
|
2020-07-07 07:15:45 +12:00
|
|
|
|
{
|
|
|
|
|
if (currentMenu == menuFindIndex)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return index;
|
|
|
|
|
}
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void SearchTextChanging()
|
|
|
|
|
{
|
|
|
|
|
ClearIsSelectedByKey();
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-11 23:04:21 +12:00
|
|
|
|
internal void SearchTextChanged(Menu menu, bool isSearchStringEmpty)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2022-06-11 23:04:21 +12:00
|
|
|
|
if (isSearchStringEmpty)
|
|
|
|
|
{
|
|
|
|
|
ClearIsSelectedByKey();
|
|
|
|
|
}
|
2022-12-04 10:41:03 +13:00
|
|
|
|
else
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
ListView dgv = menu.GetDataGridView();
|
|
|
|
|
if (dgv.Items.Count > 0)
|
2022-12-04 10:41:03 +13:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
Select(dgv, (ListViewItemData)dgv.Items[0], true);
|
2022-12-04 10:41:03 +13:00
|
|
|
|
}
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 07:15:45 +12:00
|
|
|
|
internal void ClearIsSelectedByKey()
|
|
|
|
|
{
|
2023-05-04 10:34:23 +12:00
|
|
|
|
ClearIsSelectedByKey(focussedMenu, focussedRow);
|
2020-07-07 07:15:45 +12:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 10:24:48 +12:00
|
|
|
|
internal void Select(ListView dgv, ListViewItemData itemData, bool refreshview)
|
2020-07-08 03:05:19 +12:00
|
|
|
|
{
|
2023-05-04 09:18:03 +12:00
|
|
|
|
Menu menu = (Menu)dgv.GetParentWindow();
|
2023-05-04 10:34:23 +12:00
|
|
|
|
if (itemData != focussedRow || menu != focussedMenu)
|
2020-07-08 03:05:19 +12:00
|
|
|
|
{
|
|
|
|
|
ClearIsSelectedByKey();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 09:18:03 +12:00
|
|
|
|
focussedMenu = menu;
|
2023-05-04 10:34:23 +12:00
|
|
|
|
focussedRow = itemData;
|
2020-08-04 20:58:20 +12:00
|
|
|
|
|
2023-05-04 10:34:23 +12:00
|
|
|
|
itemData.data.IsSelected = true;
|
2021-11-11 11:39:52 +13:00
|
|
|
|
|
2023-04-25 10:24:48 +12:00
|
|
|
|
if (refreshview)
|
|
|
|
|
{
|
|
|
|
|
if (dgv.SelectedItems.Contains(itemData))
|
2020-08-04 20:58:20 +12:00
|
|
|
|
{
|
2023-04-25 10:24:48 +12:00
|
|
|
|
dgv.SelectedItems.Remove(itemData);
|
2020-08-04 20:58:20 +12:00
|
|
|
|
}
|
2023-04-25 10:24:48 +12:00
|
|
|
|
|
|
|
|
|
dgv.SelectedItems.Add(itemData);
|
2020-07-08 03:05:19 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 10:34:23 +12:00
|
|
|
|
private static void ClearIsSelectedByKey(Menu? menu, ListViewItemData? itemData)
|
|
|
|
|
{
|
|
|
|
|
if (menu != null && itemData != null)
|
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
ListView dgv = menu.GetDataGridView();
|
|
|
|
|
if (dgv.SelectedItems.Contains(itemData))
|
2023-05-04 10:34:23 +12:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
dgv.SelectedItems.Remove(itemData);
|
2023-05-04 10:34:23 +12:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-05 10:08:39 +12:00
|
|
|
|
itemData.data.IsSelected = false;
|
|
|
|
|
itemData.data.IsClicking = false;
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-05 10:08:39 +12:00
|
|
|
|
private void SelectByKey(Key key, ModifierKeys modifiers)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
Menu? menuFromSelected;
|
|
|
|
|
Menu? menuBefore;
|
|
|
|
|
ListView? dgvBefore;
|
2023-05-04 10:34:23 +12:00
|
|
|
|
ListViewItemData? rowBefore = focussedRow;
|
2020-06-21 03:38:21 +12:00
|
|
|
|
|
2023-05-05 10:08:39 +12:00
|
|
|
|
menuFromSelected = focussedRow?.data.IsSelected ?? false ? focussedRow.data.SubMenu : null;
|
|
|
|
|
if (menuFromSelected != null)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
menuBefore = focussedMenu;
|
|
|
|
|
dgvBefore = menuBefore?.GetDataGridView();
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ResetSelectedByKey();
|
2023-05-05 10:08:39 +12:00
|
|
|
|
menuBefore = null;
|
|
|
|
|
dgvBefore = null;
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool toClear = false;
|
2022-11-29 08:27:52 +13:00
|
|
|
|
switch (key)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2022-10-23 11:02:31 +13:00
|
|
|
|
case Key.Enter:
|
2023-05-05 10:08:39 +12:00
|
|
|
|
if ((modifiers == ModifierKeys.None) && rowBefore != null && menuBefore != null)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
RowData trigger = rowBefore.data;
|
2023-04-30 04:57:39 +12:00
|
|
|
|
if (trigger.IsMenuOpen || !trigger.IsPointingToFolder)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-04-25 08:38:36 +12:00
|
|
|
|
trigger.OpenItem(out bool doCloseAfterOpen);
|
|
|
|
|
if (doCloseAfterOpen)
|
2021-05-02 23:12:18 +12:00
|
|
|
|
{
|
|
|
|
|
ClosePressed?.Invoke();
|
|
|
|
|
}
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
RaiseRowSelectionChanged(menuBefore, rowBefore);
|
|
|
|
|
EnterPressed?.Invoke(menuBefore, rowBefore);
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
break;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
case Key.Up:
|
2022-11-29 08:27:52 +13:00
|
|
|
|
if ((modifiers == ModifierKeys.None) &&
|
2023-05-05 10:08:39 +12:00
|
|
|
|
dgvBefore != null &&
|
|
|
|
|
(TrySelectPrevious(dgvBefore, dgvBefore.Items.IndexOf(rowBefore)) ||
|
|
|
|
|
TrySelectPrevious(dgvBefore, dgvBefore.Items.Count - 1)))
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
RaiseRowSelectionChanged(menuBefore, rowBefore);
|
2020-06-21 03:38:21 +12:00
|
|
|
|
toClear = true;
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
break;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
case Key.Down:
|
2022-11-29 08:27:52 +13:00
|
|
|
|
if ((modifiers == ModifierKeys.None) &&
|
2023-05-05 10:08:39 +12:00
|
|
|
|
dgvBefore != null &&
|
|
|
|
|
(TrySelectNext(dgvBefore, dgvBefore.Items.IndexOf(rowBefore)) ||
|
|
|
|
|
TrySelectNext(dgvBefore, 0)))
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
RaiseRowSelectionChanged(menuBefore, rowBefore);
|
2020-06-21 03:38:21 +12:00
|
|
|
|
toClear = true;
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2022-02-05 02:37:37 +13:00
|
|
|
|
break;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
case Key.Home:
|
2023-05-05 10:08:39 +12:00
|
|
|
|
if ((modifiers == ModifierKeys.None) &&
|
|
|
|
|
dgvBefore != null &&
|
|
|
|
|
TrySelectNext(dgvBefore, 0))
|
2022-02-05 02:37:37 +13:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
RaiseRowSelectionChanged(menuBefore, rowBefore);
|
2022-02-05 02:37:37 +13:00
|
|
|
|
toClear = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
case Key.End:
|
2023-04-17 09:27:27 +12:00
|
|
|
|
if ((modifiers == ModifierKeys.None) &&
|
2023-05-05 10:08:39 +12:00
|
|
|
|
dgvBefore != null &&
|
|
|
|
|
TrySelectPrevious(dgvBefore, dgvBefore.Items.Count - 1))
|
2022-02-05 02:37:37 +13:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
RaiseRowSelectionChanged(menuBefore, rowBefore);
|
2022-02-05 02:37:37 +13:00
|
|
|
|
toClear = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
break;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
case Key.Left:
|
2023-05-05 10:08:39 +12:00
|
|
|
|
case Key.Right:
|
2023-04-17 09:27:27 +12:00
|
|
|
|
if (modifiers == ModifierKeys.None &&
|
2023-05-05 10:08:39 +12:00
|
|
|
|
dgvBefore != null &&
|
|
|
|
|
focussedMenu != null)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
// True, when next is left and key is left = true OR next is right (=not left) and key is right (not left)
|
|
|
|
|
bool nextMenuInKeyDirection = (focussedMenu?.SubMenu?.Location.X < focussedMenu?.Location.X) == (key == Key.Left);
|
2022-11-29 08:27:52 +13:00
|
|
|
|
|
2023-05-05 10:08:39 +12:00
|
|
|
|
// TODO: Check what this actually does as it is only true for wrap arounds on screen corners
|
|
|
|
|
// but why not simply just select prev menu instead?
|
|
|
|
|
// True, when prev is right (=not left) but key is left = true OR prev is left but key is right (not left)
|
|
|
|
|
bool prevMenuAgainstKeyDirection = (focussedMenu?.Location.X < focussedMenu?.ParentMenu?.Location.X) == (key == Key.Left);
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2023-05-05 10:08:39 +12:00
|
|
|
|
if (nextMenuInKeyDirection || prevMenuAgainstKeyDirection)
|
2022-11-29 08:27:52 +13:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
// Next is in key direction or prev is opposite of key direction ==> TrySelect sub/next menu
|
|
|
|
|
if (menuFromSelected != null)
|
|
|
|
|
{
|
|
|
|
|
if (menuFromSelected == focussedMenu?.SubMenu)
|
|
|
|
|
{
|
|
|
|
|
ListView dgv = menuFromSelected.GetDataGridView();
|
|
|
|
|
if (dgv != null && dgv.Items.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
focussedMenu = menuFromSelected;
|
|
|
|
|
focussedRow = null;
|
|
|
|
|
if (TrySelectNext(dgv, 0))
|
|
|
|
|
{
|
|
|
|
|
RaiseRowSelectionChanged(menuBefore, rowBefore);
|
|
|
|
|
toClear = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
focussedMenu = menus[0];
|
|
|
|
|
while (focussedMenu?.SubMenu != null)
|
|
|
|
|
{
|
|
|
|
|
focussedMenu = focussedMenu.SubMenu;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
focussedRow = null;
|
|
|
|
|
Menu? lastMenu = focussedMenu;
|
|
|
|
|
if (lastMenu != null)
|
|
|
|
|
{
|
|
|
|
|
ListView dgv = lastMenu.GetDataGridView();
|
|
|
|
|
if (TrySelectNext(dgv, 0))
|
|
|
|
|
{
|
|
|
|
|
RaiseRowSelectionChanged(menuBefore, rowBefore);
|
|
|
|
|
toClear = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-29 08:27:52 +13:00
|
|
|
|
}
|
2023-05-05 10:08:39 +12:00
|
|
|
|
else if (focussedMenu?.ParentMenu != null)
|
2022-11-29 08:27:52 +13:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
// Next is in opposite key direction and prev is in key direction ==> TrySelect parent/prev menu
|
|
|
|
|
focussedMenu = focussedMenu.ParentMenu;
|
|
|
|
|
focussedRow = null;
|
|
|
|
|
ListView dgv = focussedMenu.GetDataGridView();
|
|
|
|
|
if (TrySelectNext(dgv, dgv.Items.IndexOf(dgv.SelectedItems.Count > 0 ? dgv.SelectedItems[0] : null)) ||
|
|
|
|
|
TrySelectNext(dgv, 0))
|
|
|
|
|
{
|
|
|
|
|
RaiseRowSelectionChanged(menuBefore, rowBefore);
|
|
|
|
|
toClear = true;
|
|
|
|
|
}
|
2022-11-29 08:27:52 +13:00
|
|
|
|
}
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
break;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
case Key.Escape:
|
2022-11-29 08:27:52 +13:00
|
|
|
|
case Key.F4:
|
|
|
|
|
if ((key == Key.Escape && modifiers == ModifierKeys.None) ||
|
|
|
|
|
(key == Key.F4 && modifiers == ModifierKeys.Alt))
|
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
RowDeselected?.Invoke(menuBefore, rowBefore);
|
2023-05-03 10:34:30 +12:00
|
|
|
|
ResetSelectedByKey();
|
2022-11-29 08:27:52 +13:00
|
|
|
|
toClear = true;
|
|
|
|
|
ClosePressed?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2022-11-29 08:27:52 +13:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-05 10:08:39 +12:00
|
|
|
|
if (menuFromSelected != null && toClear)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-05-04 10:34:23 +12:00
|
|
|
|
ClearIsSelectedByKey(menuBefore, rowBefore);
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-05 10:08:39 +12:00
|
|
|
|
private void RaiseRowSelectionChanged(Menu? menuBefore, ListViewItemData? rowBefore)
|
2021-12-10 08:00:33 +13:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
RowDeselected?.Invoke(menuBefore, rowBefore);
|
2021-12-10 08:00:33 +13:00
|
|
|
|
|
2023-05-05 10:08:39 +12:00
|
|
|
|
if (focussedMenu != null && focussedRow != null)
|
2023-04-17 09:27:27 +12:00
|
|
|
|
{
|
|
|
|
|
InUse = true;
|
2023-05-05 10:08:39 +12:00
|
|
|
|
RowSelected?.Invoke(focussedMenu, focussedRow);
|
2023-04-17 09:27:27 +12:00
|
|
|
|
}
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-05 10:08:39 +12:00
|
|
|
|
private bool TrySelectNext(ListView dgv, int indexStart)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
|
|
|
|
bool found = false;
|
2023-05-05 10:08:39 +12:00
|
|
|
|
if (indexStart >= 0)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-05-04 10:34:23 +12:00
|
|
|
|
for (uint i = (uint)indexStart; i < dgv.Items.Count; i++)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
if (TrySelect(dgv, (ListViewItemData)dgv.Items[(int)i]))
|
2023-04-17 09:27:27 +12:00
|
|
|
|
{
|
|
|
|
|
found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
return found;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-05 10:08:39 +12:00
|
|
|
|
private bool TrySelectPrevious(ListView dgv, int indexStart)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
|
|
|
|
bool found = false;
|
2023-05-04 10:34:23 +12:00
|
|
|
|
if (indexStart > 0)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
if (dgv.Items.Count <= indexStart)
|
|
|
|
|
{
|
|
|
|
|
indexStart = dgv.Items.Count - 1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 10:34:23 +12:00
|
|
|
|
for (int i = indexStart; i > -1; i--)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
if (TrySelect(dgv, (ListViewItemData)dgv.Items[i]))
|
2023-05-04 10:34:23 +12:00
|
|
|
|
{
|
|
|
|
|
found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
return found;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-05 10:08:39 +12:00
|
|
|
|
private bool TrySelect(ListView dgv, ListViewItemData itemData)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
|
|
|
|
bool found = false;
|
2023-05-05 10:08:39 +12:00
|
|
|
|
if (itemData != focussedRow)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
focussedRow = itemData;
|
|
|
|
|
itemData.data.IsSelected = true;
|
|
|
|
|
if (dgv.SelectedItems.Contains(itemData))
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-05-05 10:08:39 +12:00
|
|
|
|
dgv.SelectedItems.Remove(itemData);
|
|
|
|
|
}
|
2020-06-21 03:38:21 +12:00
|
|
|
|
|
2023-05-05 10:08:39 +12:00
|
|
|
|
dgv.SelectedItems.Add(itemData);
|
|
|
|
|
dgv.ScrollIntoView(itemData);
|
2022-11-26 11:31:20 +13:00
|
|
|
|
|
2023-05-05 10:08:39 +12:00
|
|
|
|
found = true;
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 07:15:45 +12:00
|
|
|
|
return found;
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-03 10:34:30 +12:00
|
|
|
|
}
|