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;
|
2023-04-17 09:27:27 +12:00
|
|
|
|
using System.Data.Common;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
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;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
using ListView = System.Windows.Controls.ListView;
|
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
|
|
|
|
|
2020-07-07 09:37:55 +12:00
|
|
|
|
private int iRowKey = -1;
|
2021-04-17 12:39:48 +12:00
|
|
|
|
private int iMenuKey;
|
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
|
|
|
|
|
2022-12-04 10:41:03 +13:00
|
|
|
|
internal event Action<ListView, int>? RowSelected;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
|
2023-04-17 09:27:27 +12:00
|
|
|
|
internal event Action<int, ListView?>? RowDeselected;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
|
2022-12-04 10:41:03 +13:00
|
|
|
|
internal event Action<ListView, int>? EnterPressed;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
|
2022-12-04 10:41:03 +13:00
|
|
|
|
internal event Action? Cleared;
|
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()
|
|
|
|
|
{
|
|
|
|
|
iRowKey = -1;
|
|
|
|
|
iMenuKey = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 09:46:53 +13:00
|
|
|
|
internal void CmdKeyProcessed(Menu? sender, Key key, ModifierKeys modifiers)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2022-09-27 06:02:18 +13:00
|
|
|
|
sender ??= menus[iMenuKey];
|
|
|
|
|
|
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);
|
|
|
|
|
menus[iMenuKey]?.FocusTextBox();
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
menus[iMenuKey]?.FocusTextBox();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
{
|
2022-12-04 10:41:03 +13:00
|
|
|
|
ListView? dgv = menus[iMenuKey]?.GetDataGridView();
|
|
|
|
|
if (dgv != null)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2022-12-04 10:41:03 +13:00
|
|
|
|
if (iRowKey > -1 && dgv.Items.Count > iRowKey)
|
|
|
|
|
{
|
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
|
|
|
|
{
|
2022-12-04 10:41:03 +13:00
|
|
|
|
ListView? dgv = menu.GetDataGridView();
|
|
|
|
|
if (dgv != null)
|
|
|
|
|
{
|
|
|
|
|
if (dgv.Items.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
Select(dgv, 0, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 07:15:45 +12:00
|
|
|
|
internal void ClearIsSelectedByKey()
|
|
|
|
|
{
|
|
|
|
|
ClearIsSelectedByKey(iMenuKey, iRowKey);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-23 11:02:31 +13:00
|
|
|
|
internal void Select(ListView dgv, int index, bool refreshview)
|
2020-07-08 03:05:19 +12:00
|
|
|
|
{
|
2022-10-23 11:02:31 +13:00
|
|
|
|
int newiMenuKey = ((Menu)dgv.GetParentWindow()).Level;
|
|
|
|
|
if (index != iRowKey || newiMenuKey != iMenuKey)
|
2020-07-08 03:05:19 +12:00
|
|
|
|
{
|
|
|
|
|
ClearIsSelectedByKey();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-23 11:02:31 +13:00
|
|
|
|
iRowKey = index;
|
2020-07-08 03:05:19 +12:00
|
|
|
|
iMenuKey = newiMenuKey;
|
2020-08-04 20:58:20 +12:00
|
|
|
|
|
2022-10-23 11:02:31 +13:00
|
|
|
|
if (dgv.Items.Count > index)
|
2020-07-08 03:05:19 +12:00
|
|
|
|
{
|
2022-11-26 11:31:20 +13:00
|
|
|
|
Menu.ListViewItemData itemData = (Menu.ListViewItemData)dgv.Items[index];
|
|
|
|
|
RowData rowData = itemData.data;
|
2021-11-11 11:39:52 +13:00
|
|
|
|
if (rowData != null)
|
|
|
|
|
{
|
|
|
|
|
rowData.IsSelected = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-04 20:58:20 +12:00
|
|
|
|
if (refreshview)
|
|
|
|
|
{
|
2022-11-26 11:31:20 +13:00
|
|
|
|
if (dgv.SelectedItems.Contains(itemData))
|
|
|
|
|
{
|
|
|
|
|
dgv.SelectedItems.Remove(itemData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dgv.SelectedItems.Add(itemData);
|
2020-08-04 20:58:20 +12:00
|
|
|
|
}
|
2020-07-08 03:05:19 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
private bool IsAnyMenuSelectedByKey(
|
2022-12-04 10:41:03 +13:00
|
|
|
|
ref ListView? dgv,
|
2022-12-06 09:46:53 +13:00
|
|
|
|
ref Menu? menuFromSelected,
|
2020-06-21 03:38:21 +12:00
|
|
|
|
ref string textselected)
|
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
Menu? menu = menus[iMenuKey];
|
2020-06-21 03:38:21 +12:00
|
|
|
|
bool isStillSelected = false;
|
|
|
|
|
if (menu != null &&
|
|
|
|
|
iRowKey > -1)
|
|
|
|
|
{
|
|
|
|
|
dgv = menu.GetDataGridView();
|
2022-12-04 10:41:03 +13:00
|
|
|
|
if (dgv != null)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2022-12-04 10:41:03 +13:00
|
|
|
|
if (dgv.Items.Count > iRowKey)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2022-12-04 10:41:03 +13:00
|
|
|
|
Menu.ListViewItemData itemData = (Menu.ListViewItemData)dgv.Items[iRowKey];
|
|
|
|
|
RowData rowData = itemData.data;
|
|
|
|
|
if (rowData.IsSelected)
|
|
|
|
|
{
|
|
|
|
|
isStillSelected = true;
|
|
|
|
|
menuFromSelected = rowData.SubMenu;
|
|
|
|
|
textselected = itemData.ColumnText;
|
|
|
|
|
}
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isStillSelected;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 08:27:52 +13:00
|
|
|
|
private void SelectByKey(Key key, ModifierKeys modifiers, string keyInput = "", bool keepSelection = false)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
|
|
|
|
int iRowBefore = iRowKey;
|
|
|
|
|
int iMenuBefore = iMenuKey;
|
|
|
|
|
|
2023-04-17 09:27:27 +12:00
|
|
|
|
Menu? menu = menus[iMenuKey];
|
2022-12-06 09:46:53 +13:00
|
|
|
|
ListView? dgv = null;
|
|
|
|
|
ListView? dgvBefore = null;
|
|
|
|
|
Menu? menuFromSelected = null;
|
2020-06-21 03:38:21 +12:00
|
|
|
|
string textselected = string.Empty;
|
|
|
|
|
bool isStillSelected = IsAnyMenuSelectedByKey(ref dgv, ref menuFromSelected, ref textselected);
|
|
|
|
|
if (isStillSelected)
|
|
|
|
|
{
|
2020-07-07 09:37:55 +12:00
|
|
|
|
if (keepSelection)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
|
|
|
|
// If current selection is still valid for this search then skip selecting different item
|
|
|
|
|
if (textselected.StartsWith(keyInput, true, CultureInfo.InvariantCulture))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dgvBefore = dgv;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ResetSelectedByKey();
|
|
|
|
|
menu = menus[iMenuKey];
|
2023-04-17 09:27:27 +12:00
|
|
|
|
dgv = menu?.GetDataGridView();
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool toClear = false;
|
2022-11-29 08:27:52 +13:00
|
|
|
|
bool handled = false;
|
|
|
|
|
switch (key)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2022-10-23 11:02:31 +13:00
|
|
|
|
case Key.Enter:
|
2023-04-17 09:27:27 +12:00
|
|
|
|
if ((modifiers == ModifierKeys.None) && (iRowKey > -1 && dgv != null && dgv.Items.Count > iRowKey))
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2022-10-23 11:02:31 +13:00
|
|
|
|
RowData trigger = ((Menu.ListViewItemData)dgv.Items[iRowKey]).data;
|
2020-06-21 03:38:21 +12:00
|
|
|
|
if (trigger.IsMenuOpen || !trigger.ContainsMenu)
|
|
|
|
|
{
|
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-04-17 09:27:27 +12:00
|
|
|
|
RowDeselected?.Invoke(iRowBefore, dgvBefore);
|
2020-06-21 03:38:21 +12:00
|
|
|
|
SelectRow(dgv, iRowKey);
|
2023-04-17 09:27:27 +12:00
|
|
|
|
EnterPressed?.Invoke(dgv, iRowKey);
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
2022-11-29 08:27:52 +13:00
|
|
|
|
|
|
|
|
|
handled = true;
|
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-04-17 09:27:27 +12:00
|
|
|
|
dgv != null &&
|
2022-11-29 08:27:52 +13:00
|
|
|
|
(SelectMatchedReverse(dgv, iRowKey) ||
|
|
|
|
|
SelectMatchedReverse(dgv, dgv.Items.Count - 1)))
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
RowDeselected?.Invoke(iRowBefore, dgvBefore);
|
2020-06-21 03:38:21 +12:00
|
|
|
|
SelectRow(dgv, iRowKey);
|
|
|
|
|
toClear = true;
|
2022-11-29 08:27:52 +13:00
|
|
|
|
handled = true;
|
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.Down:
|
2022-11-29 08:27:52 +13:00
|
|
|
|
if ((modifiers == ModifierKeys.None) &&
|
|
|
|
|
(SelectMatched(dgv, iRowKey) ||
|
|
|
|
|
SelectMatched(dgv, 0)))
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
RowDeselected?.Invoke(iRowBefore, dgvBefore);
|
2020-06-21 03:38:21 +12:00
|
|
|
|
SelectRow(dgv, iRowKey);
|
|
|
|
|
toClear = true;
|
2022-11-29 08:27:52 +13:00
|
|
|
|
handled = true;
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
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:
|
2022-11-29 08:27:52 +13:00
|
|
|
|
if ((modifiers == ModifierKeys.None) && SelectMatched(dgv, 0))
|
2022-02-05 02:37:37 +13:00
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
RowDeselected?.Invoke(iRowBefore, dgvBefore);
|
2022-02-05 02:37:37 +13:00
|
|
|
|
SelectRow(dgv, iRowKey);
|
|
|
|
|
toClear = true;
|
2022-11-29 08:27:52 +13:00
|
|
|
|
handled = true;
|
2022-02-05 02:37:37 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
case Key.End:
|
2023-04-17 09:27:27 +12:00
|
|
|
|
if ((modifiers == ModifierKeys.None) &&
|
|
|
|
|
dgv != null &&
|
|
|
|
|
SelectMatchedReverse(dgv, dgv.Items.Count - 1))
|
2022-02-05 02:37:37 +13:00
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
RowDeselected?.Invoke(iRowBefore, dgvBefore);
|
2022-02-05 02:37:37 +13:00
|
|
|
|
SelectRow(dgv, iRowKey);
|
|
|
|
|
toClear = true;
|
2022-11-29 08:27:52 +13:00
|
|
|
|
handled = true;
|
2022-02-05 02:37:37 +13:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
break;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
case Key.Left:
|
2023-04-17 09:27:27 +12:00
|
|
|
|
if (modifiers == ModifierKeys.None &&
|
|
|
|
|
dgv != null &&
|
|
|
|
|
dgvBefore != null)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
Menu? nextMenu = menus[iMenuKey + 1];
|
|
|
|
|
bool nextMenuLocationIsLeft = nextMenu != null && menu != null && nextMenu.Location.X < menu.Location.X;
|
|
|
|
|
Menu? previousMenu = menus[iMenuKey - 1];
|
|
|
|
|
bool previousMenuLocationIsRight = iMenuKey > 0 && previousMenu != null && menu != null && menu.Location.X < previousMenu.Location.X;
|
2022-11-29 08:27:52 +13:00
|
|
|
|
if (nextMenuLocationIsLeft || previousMenuLocationIsRight)
|
|
|
|
|
{
|
|
|
|
|
SelectNextMenu(iRowBefore, ref dgv, dgvBefore, menuFromSelected, isStillSelected, ref toClear);
|
|
|
|
|
}
|
|
|
|
|
else if (iMenuKey > 0)
|
|
|
|
|
{
|
|
|
|
|
SelectPreviousMenu(iRowBefore, ref menu, ref dgv, dgvBefore, ref toClear);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handled = true;
|
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.Right:
|
2023-04-17 09:27:27 +12:00
|
|
|
|
if (modifiers == ModifierKeys.None &&
|
|
|
|
|
dgv != null &&
|
|
|
|
|
dgvBefore != null)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2022-11-29 08:27:52 +13:00
|
|
|
|
bool nextMenuLocationIsRight = menus[iMenuKey + 1]?.Location.X > menus[iMenuKey]?.Location.X;
|
|
|
|
|
bool previousMenuLocationIsLeft = iMenuKey > 0 && menus[iMenuKey]?.Location.X > menus[iMenuKey - 1]?.Location.X;
|
|
|
|
|
if (nextMenuLocationIsRight || previousMenuLocationIsLeft)
|
|
|
|
|
{
|
|
|
|
|
SelectNextMenu(iRowBefore, ref dgv, dgvBefore, menuFromSelected, isStillSelected, ref toClear);
|
|
|
|
|
}
|
|
|
|
|
else if (iMenuKey > 0)
|
|
|
|
|
{
|
|
|
|
|
SelectPreviousMenu(iRowBefore, ref menu, ref dgv, dgvBefore, ref toClear);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handled = true;
|
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-04-17 09:27:27 +12:00
|
|
|
|
RowDeselected?.Invoke(iRowBefore, dgvBefore);
|
2022-11-29 08:27:52 +13:00
|
|
|
|
iMenuKey = 0;
|
|
|
|
|
iRowKey = -1;
|
|
|
|
|
toClear = true;
|
|
|
|
|
ClosePressed?.Invoke();
|
|
|
|
|
|
|
|
|
|
handled = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2022-11-29 08:27:52 +13:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!handled)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(keyInput))
|
|
|
|
|
{
|
|
|
|
|
if (SelectMatched(dgv, iRowKey, keyInput) ||
|
|
|
|
|
SelectMatched(dgv, 0, keyInput))
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
RowDeselected?.Invoke(iRowBefore, null);
|
2022-11-29 08:27:52 +13:00
|
|
|
|
SelectRow(dgv, iRowKey);
|
|
|
|
|
toClear = true;
|
|
|
|
|
}
|
|
|
|
|
else if (isStillSelected)
|
|
|
|
|
{
|
|
|
|
|
iRowKey = iRowBefore - 1;
|
2020-06-21 03:38:21 +12:00
|
|
|
|
if (SelectMatched(dgv, iRowKey, keyInput) ||
|
|
|
|
|
SelectMatched(dgv, 0, keyInput))
|
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
RowDeselected?.Invoke(iRowBefore, null);
|
2020-06-21 03:38:21 +12:00
|
|
|
|
SelectRow(dgv, iRowKey);
|
|
|
|
|
}
|
2022-11-29 08:27:52 +13:00
|
|
|
|
else
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2022-11-29 08:27:52 +13:00
|
|
|
|
iRowKey = iRowBefore;
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
if (isStillSelected && toClear)
|
|
|
|
|
{
|
|
|
|
|
ClearIsSelectedByKey(iMenuBefore, iRowBefore);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 09:27:27 +12:00
|
|
|
|
private void SelectPreviousMenu(int iRowBefore, ref Menu? menu, ref ListView? dgv, ListView? dgvBefore, ref bool toClear)
|
2021-12-10 08:00:33 +13:00
|
|
|
|
{
|
|
|
|
|
if (iMenuKey > 0)
|
|
|
|
|
{
|
|
|
|
|
if (menus[iMenuKey - 1] != null)
|
|
|
|
|
{
|
|
|
|
|
iMenuKey -= 1;
|
|
|
|
|
iRowKey = -1;
|
|
|
|
|
menu = menus[iMenuKey];
|
2023-04-17 09:27:27 +12:00
|
|
|
|
dgv = menu?.GetDataGridView();
|
|
|
|
|
if (dgv != null)
|
2021-12-10 08:00:33 +13:00
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
if (SelectMatched(dgv, dgv.Items.IndexOf(dgv.SelectedItems.Count > 0 ? dgv.SelectedItems[0] : null)) ||
|
|
|
|
|
SelectMatched(dgv, 0))
|
|
|
|
|
{
|
|
|
|
|
RowDeselected?.Invoke(iRowBefore, dgvBefore);
|
|
|
|
|
SelectRow(dgv, iRowKey);
|
|
|
|
|
toClear = true;
|
|
|
|
|
}
|
2021-12-10 08:00:33 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
RowDeselected?.Invoke(iRowBefore, dgvBefore);
|
2021-12-10 08:00:33 +13:00
|
|
|
|
iMenuKey = 0;
|
|
|
|
|
iRowKey = -1;
|
|
|
|
|
toClear = true;
|
|
|
|
|
Cleared?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 09:27:27 +12:00
|
|
|
|
private void SelectNextMenu(int iRowBefore, ref ListView? dgv, ListView dgvBefore, Menu? menuFromSelected, bool isStillSelected, ref bool toClear)
|
2021-12-10 08:00:33 +13:00
|
|
|
|
{
|
|
|
|
|
int iMenuKeyNext = iMenuKey + 1;
|
|
|
|
|
if (isStillSelected)
|
|
|
|
|
{
|
|
|
|
|
if (menuFromSelected != null &&
|
|
|
|
|
menuFromSelected == menus[iMenuKeyNext])
|
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
dgv = menuFromSelected?.GetDataGridView();
|
|
|
|
|
if (dgv != null && dgv.Items.Count > 0)
|
2021-12-10 08:00:33 +13:00
|
|
|
|
{
|
|
|
|
|
iMenuKey += 1;
|
|
|
|
|
iRowKey = -1;
|
|
|
|
|
if (SelectMatched(dgv, iRowKey) ||
|
|
|
|
|
SelectMatched(dgv, 0))
|
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
RowDeselected?.Invoke(iRowBefore, dgvBefore);
|
2021-12-10 08:00:33 +13:00
|
|
|
|
SelectRow(dgv, iRowKey);
|
|
|
|
|
toClear = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
iRowKey = -1;
|
|
|
|
|
iMenuKey = menus.Where(m => m != null).Count() - 1;
|
2023-04-17 09:27:27 +12:00
|
|
|
|
Menu? lastMenu = menus[iMenuKey];
|
|
|
|
|
if (lastMenu != null)
|
2021-12-10 08:00:33 +13:00
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
dgv = lastMenu?.GetDataGridView();
|
2021-12-10 08:00:33 +13:00
|
|
|
|
if (SelectMatched(dgv, iRowKey) ||
|
|
|
|
|
SelectMatched(dgv, 0))
|
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
RowDeselected?.Invoke(iRowBefore, dgvBefore);
|
2021-12-10 08:00:33 +13:00
|
|
|
|
SelectRow(dgv, iRowKey);
|
|
|
|
|
toClear = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 09:27:27 +12:00
|
|
|
|
private void SelectRow(ListView? dgv, int iRowKey)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
if (dgv != null)
|
|
|
|
|
{
|
|
|
|
|
InUse = true;
|
|
|
|
|
RowSelected?.Invoke(dgv, iRowKey);
|
|
|
|
|
}
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 09:27:27 +12:00
|
|
|
|
private bool SelectMatched(ListView? dgv, int indexStart, string keyInput = "")
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
|
|
|
|
bool found = false;
|
2023-04-17 09:27:27 +12:00
|
|
|
|
if (dgv != null)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
for (int i = indexStart; i < dgv.Items.Count; i++)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
if (Select(dgv, i, keyInput))
|
|
|
|
|
{
|
|
|
|
|
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-04-17 09:27:27 +12:00
|
|
|
|
private bool SelectMatchedReverse(ListView? dgv, int indexStart, string keyInput = "")
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
|
|
|
|
bool found = false;
|
|
|
|
|
for (int i = indexStart; i > -1; i--)
|
|
|
|
|
{
|
|
|
|
|
if (Select(dgv, i, keyInput))
|
|
|
|
|
{
|
|
|
|
|
found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
return found;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 09:27:27 +12:00
|
|
|
|
private bool Select(ListView? dgv, int i, string keyInput = "")
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
|
|
|
|
bool found = false;
|
|
|
|
|
if (i > -1 &&
|
|
|
|
|
i != iRowKey &&
|
2023-04-17 09:27:27 +12:00
|
|
|
|
dgv != null &&
|
2022-10-23 11:02:31 +13:00
|
|
|
|
dgv.Items.Count > i)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2022-11-26 11:31:20 +13:00
|
|
|
|
Menu.ListViewItemData itemData = (Menu.ListViewItemData)dgv.Items[i];
|
|
|
|
|
RowData rowData = itemData.data;
|
|
|
|
|
if (itemData.ColumnText.StartsWith(keyInput, true, CultureInfo.InvariantCulture))
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
|
|
|
|
iRowKey = rowData.RowIndex;
|
|
|
|
|
rowData.IsSelected = true;
|
2022-11-26 11:31:20 +13:00
|
|
|
|
if (dgv.SelectedItems.Contains(itemData))
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2022-11-26 11:31:20 +13:00
|
|
|
|
dgv.SelectedItems.Remove(itemData);
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-26 11:31:20 +13:00
|
|
|
|
dgv.SelectedItems.Add(itemData);
|
|
|
|
|
dgv.ScrollIntoView(itemData);
|
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
found = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 07:15:45 +12:00
|
|
|
|
return found;
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ClearIsSelectedByKey(int menuIndex, int rowIndex)
|
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
Menu? menu = menus[menuIndex];
|
2020-06-21 03:38:21 +12:00
|
|
|
|
if (menu != null && rowIndex > -1)
|
|
|
|
|
{
|
2023-04-17 09:27:27 +12:00
|
|
|
|
ListView? dgv = menu?.GetDataGridView();
|
|
|
|
|
if (dgv != null && dgv.Items.Count > rowIndex)
|
2020-06-21 03:38:21 +12:00
|
|
|
|
{
|
2022-11-26 11:31:20 +13:00
|
|
|
|
Menu.ListViewItemData itemData = (Menu.ListViewItemData)dgv.Items[rowIndex];
|
|
|
|
|
RowData rowData = itemData.data;
|
|
|
|
|
if (dgv.SelectedItems.Contains(itemData))
|
|
|
|
|
{
|
|
|
|
|
dgv.SelectedItems.Remove(itemData);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-22 05:25:31 +13:00
|
|
|
|
if (rowData != null)
|
|
|
|
|
{
|
|
|
|
|
rowData.IsSelected = false;
|
|
|
|
|
rowData.IsClicking = false;
|
|
|
|
|
}
|
2020-06-21 03:38:21 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-09 01:21:15 +13:00
|
|
|
|
}
|