feat: add keyboard navigation to NavMenu
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Metadata;
|
||||
using Avalonia.Controls.Presenters;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Controls.Templates;
|
||||
using Avalonia.Data;
|
||||
@@ -8,13 +9,17 @@ using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.LogicalTree;
|
||||
using Avalonia.Metadata;
|
||||
using Avalonia.VisualTree;
|
||||
using Irihi.Avalonia.Shared.Helpers;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
[TemplatePart(PART_ItemsPresenter, typeof(ItemsPresenter))]
|
||||
[PseudoClasses(PC_HorizontalCollapsed)]
|
||||
public class NavMenu : ItemsControl
|
||||
public class NavMenu : ItemsControl, ICustomKeyboardNavigation
|
||||
{
|
||||
public const string PART_ItemsPresenter = "PART_ItemsPresenter";
|
||||
|
||||
public const string PC_HorizontalCollapsed = ":horizontal-collapsed";
|
||||
|
||||
public static readonly StyledProperty<object?> SelectedItemProperty = AvaloniaProperty.Register<NavMenu, object?>(
|
||||
@@ -69,7 +74,9 @@ public class NavMenu : ItemsControl
|
||||
public static readonly RoutedEvent<SelectionChangedEventArgs> SelectionChangedEvent =
|
||||
RoutedEvent.Register<NavMenu, SelectionChangedEventArgs>(nameof(SelectionChanged), RoutingStrategies.Bubble);
|
||||
|
||||
private ItemsPresenter? _itemsPresenter = null;
|
||||
private bool _isSelectionFromUI = false;
|
||||
private bool _isNavigatingMenu = false;
|
||||
|
||||
static NavMenu()
|
||||
{
|
||||
@@ -193,12 +200,34 @@ public class NavMenu : ItemsControl
|
||||
return new NavMenuItem();
|
||||
}
|
||||
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
|
||||
_itemsPresenter = e.NameScope.Find<ItemsPresenter>(PART_ItemsPresenter);
|
||||
if (_itemsPresenter is not null)
|
||||
KeyboardNavigation.SetTabNavigation(_itemsPresenter, KeyboardNavigationMode.Once);
|
||||
}
|
||||
|
||||
protected override void OnLoaded(RoutedEventArgs e)
|
||||
{
|
||||
base.OnLoaded(e);
|
||||
TryToSelectItem(SelectedItem);
|
||||
}
|
||||
|
||||
protected override void OnKeyDown(KeyEventArgs e)
|
||||
{
|
||||
if (e.Handled) return;
|
||||
|
||||
var source = GetContainerFromEventSource(e.Source);
|
||||
if (GetNextItem(source, e.Key) is { } target)
|
||||
{
|
||||
if (e.Key is Key.Up or Key.Down && source is { Level: 1 } firstLevelSource)
|
||||
firstLevelSource.CloseAllOpenPopups();
|
||||
e.Handled = target.Focus(NavigationMethod.Directional);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// this implementation only works in the case that only leaf menu item is allowed to select. It will be changed if we
|
||||
/// introduce parent level selection in the future.
|
||||
@@ -269,6 +298,96 @@ public class NavMenu : ItemsControl
|
||||
item.ClearSelection();
|
||||
}
|
||||
|
||||
(bool handled, IInputElement? next) ICustomKeyboardNavigation.GetNext(IInputElement element, NavigationDirection direction)
|
||||
{
|
||||
if (direction is not NavigationDirection.Next and not NavigationDirection.Previous ||
|
||||
_isNavigatingMenu ||
|
||||
_itemsPresenter is null) return (false, null);
|
||||
|
||||
var visual = element as Visual;
|
||||
|
||||
if (!_itemsPresenter.IsVisualAncestorOf(visual))
|
||||
{
|
||||
_isNavigatingMenu = true;
|
||||
var next = KeyboardNavigationHandler.GetNext(element, direction);
|
||||
_isNavigatingMenu = false;
|
||||
|
||||
if (_itemsPresenter.IsVisualAncestorOf(next as Visual))
|
||||
{
|
||||
var target = IsHorizontalCollapsed
|
||||
? GetRootMenuItem(GetContainerForItem(SelectedItem))
|
||||
: GetUncollapsedOrTopmostMenuItem(GetContainerForItem(SelectedItem));
|
||||
target ??= ContainerFromIndex(this, 0);
|
||||
|
||||
return (target is not null, target);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_isNavigatingMenu = true;
|
||||
var next = KeyboardNavigationHandler.GetNext(_itemsPresenter, direction);
|
||||
_isNavigatingMenu = false;
|
||||
|
||||
if (element is NavMenuItem { Level: 1 } firstLevelItem) firstLevelItem.CloseAllOpenPopups();
|
||||
|
||||
return (true, next);
|
||||
}
|
||||
|
||||
return (false, null);
|
||||
}
|
||||
|
||||
private NavMenuItem? GetNextItem(NavMenuItem? current, Key key, bool skipChildren = false)
|
||||
{
|
||||
if (current?.Parent is not ItemsControl parent) return null;
|
||||
|
||||
var index = IndexFromContainer(parent, current);
|
||||
|
||||
return key switch
|
||||
{
|
||||
Key.Up => NavigateUp(),
|
||||
Key.Down => NavigateDown(),
|
||||
Key.Right when !IsHorizontalCollapsed => NavigateDown(),
|
||||
Key.Left or Key.Escape => parent as NavMenuItem,
|
||||
Key.Right or Key.Enter => FirstChild(current),
|
||||
_ => null
|
||||
};
|
||||
|
||||
NavMenuItem? NavigateUp()
|
||||
{
|
||||
if (FindSibling(parent, index - 1, -1) is { } previous)
|
||||
return !IsHorizontalCollapsed && !previous.IsVerticalCollapsed && previous.ItemCount > 0
|
||||
? LastChild(previous)
|
||||
: previous;
|
||||
|
||||
return IsHorizontalCollapsed ? LastChild(parent) : parent as NavMenuItem;
|
||||
}
|
||||
|
||||
NavMenuItem? NavigateDown()
|
||||
{
|
||||
if (!skipChildren &&
|
||||
!IsHorizontalCollapsed &&
|
||||
!current.IsVerticalCollapsed &&
|
||||
current.ItemCount > 0 &&
|
||||
FirstChild(current) is { } firstChild) return firstChild;
|
||||
|
||||
if (FindSibling(parent, index + 1, 1) is { } next) return next;
|
||||
|
||||
if (IsHorizontalCollapsed) return FirstChild(parent);
|
||||
|
||||
return GetNextItem(parent as NavMenuItem, key, true);
|
||||
}
|
||||
|
||||
static NavMenuItem? FirstChild(ItemsControl control) => FindSibling(control, 0, 1);
|
||||
static NavMenuItem? LastChild(ItemsControl control) => FindSibling(control, control.ItemCount - 1, -1);
|
||||
|
||||
static NavMenuItem? FindSibling(ItemsControl control, int start, int step)
|
||||
{
|
||||
for (var i = start; i >= 0 && i < control.ItemCount; i += step)
|
||||
if (ContainerFromIndex(control, i) is { IsEnabled: true, IsSeparator: false } item) return item;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private bool TryToSelectItem(object? item)
|
||||
{
|
||||
if (item is null) return false;
|
||||
@@ -293,4 +412,78 @@ public class NavMenu : ItemsControl
|
||||
foreach (var leaf in leafs) yield return leaf;
|
||||
}
|
||||
}
|
||||
|
||||
public NavMenuItem? GetContainerForItem(object? item)
|
||||
{
|
||||
if (item == null) return null;
|
||||
return GetContainerForItem(this, item);
|
||||
|
||||
static NavMenuItem? GetContainerForItem(ItemsControl control, object item)
|
||||
{
|
||||
if (ContainerFromItem(control, item) is NavMenuItem container) return container;
|
||||
|
||||
var children = GetRealizedContainers(control);
|
||||
foreach (var child in children)
|
||||
if (GetContainerForItem(child, item) is { } childContainer) return childContainer;
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private NavMenuItem? GetContainerFromEventSource(object? eventSource)
|
||||
{
|
||||
if (eventSource is not Visual visual) return null;
|
||||
|
||||
return visual.GetSelfAndVisualAncestors()
|
||||
.OfType<NavMenuItem>()
|
||||
.FirstOrDefault(i => i.RootMenu == this);
|
||||
}
|
||||
|
||||
private static NavMenuItem? ContainerFromIndex(ItemsControl itemsControl, int index) =>
|
||||
itemsControl is NavMenuItem navMenuItem
|
||||
? navMenuItem.CollapsedAwareContainerFromIndex(index)
|
||||
: (NavMenuItem?)itemsControl.ContainerFromIndex(index);
|
||||
|
||||
private static NavMenuItem? ContainerFromItem(ItemsControl itemsControl, object item) =>
|
||||
itemsControl is NavMenuItem navMenuItem
|
||||
? navMenuItem.CollapsedAwareContainerFromItem(item)
|
||||
: (NavMenuItem?)itemsControl.ContainerFromItem(item);
|
||||
|
||||
private static int IndexFromContainer(ItemsControl itemsControl, NavMenuItem container) =>
|
||||
itemsControl is NavMenuItem navMenuItem
|
||||
? navMenuItem.CollapsedAwareIndexFromContainer(container)
|
||||
: itemsControl.IndexFromContainer(container);
|
||||
|
||||
private static IEnumerable<NavMenuItem> GetRealizedContainers(ItemsControl itemsControl)
|
||||
{
|
||||
var realizedContainers = itemsControl is NavMenuItem navMenuItem
|
||||
? navMenuItem.CollapsedAwareRealizedContainers()
|
||||
: itemsControl.GetRealizedContainers();
|
||||
|
||||
return realizedContainers.OfType<NavMenuItem>();
|
||||
}
|
||||
|
||||
private static NavMenuItem? GetRootMenuItem(NavMenuItem? item)
|
||||
{
|
||||
if (item is null) return null;
|
||||
|
||||
return item.GetSelfAndLogicalAncestors()
|
||||
.OfType<NavMenuItem>()
|
||||
.FirstOrDefault(i => i.Level == 1);
|
||||
}
|
||||
|
||||
private static NavMenuItem? GetUncollapsedOrTopmostMenuItem(NavMenuItem? item)
|
||||
{
|
||||
NavMenuItem? result = null;
|
||||
|
||||
while (item is not null)
|
||||
{
|
||||
var currentParent = item.Parent as NavMenuItem;
|
||||
result ??= currentParent?.IsVerticalCollapsed is false ? item : null;
|
||||
if (item.Level == 1) return result ?? item;
|
||||
item = currentParent;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user