diff --git a/src/Ursa/Controls/NavMenu/NavMenu.cs b/src/Ursa/Controls/NavMenu/NavMenu.cs index 1a06dc3..4861df5 100644 --- a/src/Ursa/Controls/NavMenu/NavMenu.cs +++ b/src/Ursa/Controls/NavMenu/NavMenu.cs @@ -1,5 +1,6 @@ using Avalonia.Controls; using Avalonia.Controls.Primitives; +using Avalonia.LogicalTree; namespace Ursa.Controls; @@ -14,4 +15,18 @@ public class NavMenu: SelectingItemsControl { return new NavMenuItem(); } + + internal void SelectItem(NavMenuItem item) + { + if (item.IsSelected) return; + var children = this.LogicalChildren.OfType(); + foreach (var child in children) + { + if (child != item) + { + child.IsSelected = false; + } + } + item.IsSelected = true; + } } \ No newline at end of file diff --git a/src/Ursa/Controls/NavMenu/NavMenuItem.cs b/src/Ursa/Controls/NavMenu/NavMenuItem.cs index 860e6f8..8ea4137 100644 --- a/src/Ursa/Controls/NavMenu/NavMenuItem.cs +++ b/src/Ursa/Controls/NavMenu/NavMenuItem.cs @@ -1,11 +1,18 @@ using Avalonia; using Avalonia.Controls; +using Avalonia.Controls.Mixins; using Avalonia.Controls.Primitives; +using Avalonia.Controls.Templates; +using Avalonia.Input; +using Avalonia.LogicalTree; +using Avalonia.VisualTree; namespace Ursa.Controls; public class NavMenuItem: HeaderedSelectingItemsControl { + private NavMenu? _rootMenu; + public static readonly StyledProperty IconProperty = AvaloniaProperty.Register( nameof(Icon)); @@ -14,6 +21,30 @@ public class NavMenuItem: HeaderedSelectingItemsControl get => GetValue(IconProperty); set => SetValue(IconProperty, value); } + + public static readonly StyledProperty IconTemplateProperty = AvaloniaProperty.Register( + nameof(IconTemplate)); + + public IDataTemplate? IconTemplate + { + get => GetValue(IconTemplateProperty); + set => SetValue(IconTemplateProperty, value); + } + + public new static readonly StyledProperty IsSelectedProperty = + SelectingItemsControl.IsSelectedProperty.AddOwner(); + + public bool IsSelected + { + get => GetValue(IsSelectedProperty); + set => SetValue(IsSelectedProperty, value); + } + + static NavMenuItem() + { + SelectableMixin.Attach(IsSelectedProperty); + PressedMixin.Attach(); + } protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey) { @@ -24,4 +55,26 @@ public class NavMenuItem: HeaderedSelectingItemsControl { return new NavMenuItem(); } + + protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnAttachedToVisualTree(e); + _rootMenu = GetRootMenu(); + } + + protected override void OnPointerPressed(PointerPressedEventArgs e) + { + base.OnPointerPressed(e); + _rootMenu?.SelectItem(this); + } + + private NavMenu? GetRootMenu() + { + var root = this.FindAncestorOfType(); + if (root is null) + { + root = this.FindLogicalAncestorOfType(); + } + return root; + } } \ No newline at end of file