misc: re-organize code.
This commit is contained in:
@@ -16,127 +16,66 @@ using Irihi.Avalonia.Shared.Helpers;
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Navigation Menu Item
|
||||
/// Navigation Menu Item
|
||||
/// </summary>
|
||||
[PseudoClasses(PC_Highlighted, PC_HorizontalCollapsed, PC_VerticalCollapsed, PC_FirstLevel, PC_Selector)]
|
||||
public class NavMenuItem: HeaderedItemsControl
|
||||
public class NavMenuItem : HeaderedItemsControl
|
||||
{
|
||||
public const string PC_Highlighted = ":highlighted";
|
||||
public const string PC_FirstLevel = ":first-level";
|
||||
public const string PC_HorizontalCollapsed = ":horizontal-collapsed";
|
||||
public const string PC_VerticalCollapsed = ":vertical-collapsed";
|
||||
public const string PC_Selector = ":selector";
|
||||
|
||||
private NavMenu? _rootMenu;
|
||||
private Popup? _popup;
|
||||
private Panel? _overflowPanel;
|
||||
|
||||
private static readonly Point InvalidPoint = new (double.NaN, double.NaN);
|
||||
private Point _pointerDownPoint = InvalidPoint;
|
||||
|
||||
|
||||
private static readonly Point InvalidPoint = new(double.NaN, double.NaN);
|
||||
|
||||
public static readonly StyledProperty<object?> IconProperty = AvaloniaProperty.Register<NavMenuItem, object?>(
|
||||
nameof(Icon));
|
||||
|
||||
public object? Icon
|
||||
{
|
||||
get => GetValue(IconProperty);
|
||||
set => SetValue(IconProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<IDataTemplate?> IconTemplateProperty = AvaloniaProperty.Register<NavMenuItem, IDataTemplate?>(
|
||||
nameof(IconTemplate));
|
||||
|
||||
public IDataTemplate? IconTemplate
|
||||
{
|
||||
get => GetValue(IconTemplateProperty);
|
||||
set => SetValue(IconTemplateProperty, value);
|
||||
}
|
||||
public static readonly StyledProperty<IDataTemplate?> IconTemplateProperty =
|
||||
AvaloniaProperty.Register<NavMenuItem, IDataTemplate?>(
|
||||
nameof(IconTemplate));
|
||||
|
||||
public static readonly StyledProperty<ICommand?> CommandProperty = Button.CommandProperty.AddOwner<NavMenuItem>();
|
||||
|
||||
public ICommand? Command
|
||||
{
|
||||
get => GetValue(CommandProperty);
|
||||
set => SetValue(CommandProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<object?> CommandParameterProperty =
|
||||
Button.CommandParameterProperty.AddOwner<NavMenuItem>();
|
||||
|
||||
public object? CommandParameter
|
||||
{
|
||||
get => GetValue(CommandParameterProperty);
|
||||
set => SetValue(CommandParameterProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<bool> IsSelectedProperty =
|
||||
SelectingItemsControl.IsSelectedProperty.AddOwner<NavMenuItem>();
|
||||
|
||||
public bool IsSelected
|
||||
{
|
||||
get => GetValue(IsSelectedProperty);
|
||||
set => SetValue(IsSelectedProperty, value);
|
||||
}
|
||||
|
||||
public static readonly RoutedEvent<RoutedEventArgs> IsSelectedChangedEvent = RoutedEvent.Register<SelectingItemsControl, RoutedEventArgs>("IsSelectedChanged", RoutingStrategies.Bubble);
|
||||
|
||||
private bool _isHighlighted;
|
||||
public static readonly RoutedEvent<RoutedEventArgs> IsSelectedChangedEvent =
|
||||
RoutedEvent.Register<SelectingItemsControl, RoutedEventArgs>("IsSelectedChanged", RoutingStrategies.Bubble);
|
||||
|
||||
public static readonly DirectProperty<NavMenuItem, bool> IsHighlightedProperty =
|
||||
AvaloniaProperty.RegisterDirect<NavMenuItem, bool>(
|
||||
nameof(IsHighlighted), o => o.IsHighlighted, (o, v) => o.IsHighlighted = v,
|
||||
defaultBindingMode: BindingMode.TwoWay);
|
||||
|
||||
public bool IsHighlighted
|
||||
{
|
||||
get => _isHighlighted;
|
||||
private set => SetAndRaise(IsHighlightedProperty, ref _isHighlighted, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<bool> IsHorizontalCollapsedProperty =
|
||||
NavMenu.IsHorizontalCollapsedProperty.AddOwner<NavMenuItem>();
|
||||
|
||||
public bool IsHorizontalCollapsed
|
||||
{
|
||||
get => GetValue(IsHorizontalCollapsedProperty);
|
||||
set => SetValue(IsHorizontalCollapsedProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<bool> IsVerticalCollapsedProperty = AvaloniaProperty.Register<NavMenuItem, bool>(
|
||||
nameof(IsVerticalCollapsed));
|
||||
|
||||
public bool IsVerticalCollapsed
|
||||
{
|
||||
get => GetValue(IsVerticalCollapsedProperty);
|
||||
set => SetValue(IsVerticalCollapsedProperty, value);
|
||||
}
|
||||
public static readonly StyledProperty<bool> IsVerticalCollapsedProperty =
|
||||
AvaloniaProperty.Register<NavMenuItem, bool>(
|
||||
nameof(IsVerticalCollapsed));
|
||||
|
||||
public static readonly StyledProperty<double> SubMenuIndentProperty =
|
||||
NavMenu.SubMenuIndentProperty.AddOwner<NavMenuItem>();
|
||||
|
||||
public double SubMenuIndent
|
||||
{
|
||||
get => GetValue(SubMenuIndentProperty);
|
||||
set => SetValue(SubMenuIndentProperty, value);
|
||||
}
|
||||
|
||||
internal static readonly DirectProperty<NavMenuItem, int> LevelProperty = AvaloniaProperty.RegisterDirect<NavMenuItem, int>(
|
||||
nameof(Level), o => o.Level, (o, v) => o.Level = v);
|
||||
private int _level;
|
||||
public int Level
|
||||
{
|
||||
get => _level;
|
||||
set => SetAndRaise(LevelProperty, ref _level, value);
|
||||
}
|
||||
internal static readonly DirectProperty<NavMenuItem, int> LevelProperty =
|
||||
AvaloniaProperty.RegisterDirect<NavMenuItem, int>(
|
||||
nameof(Level), o => o.Level, (o, v) => o.Level = v);
|
||||
|
||||
public static readonly StyledProperty<bool> IsSeparatorProperty = AvaloniaProperty.Register<NavMenuItem, bool>(
|
||||
nameof(IsSeparator));
|
||||
|
||||
public bool IsSeparator
|
||||
{
|
||||
get => GetValue(IsSeparatorProperty);
|
||||
set => SetValue(IsSeparatorProperty, value);
|
||||
}
|
||||
private bool _isHighlighted;
|
||||
private int _level;
|
||||
private Panel? _overflowPanel;
|
||||
private Point _pointerDownPoint = InvalidPoint;
|
||||
private Popup? _popup;
|
||||
|
||||
private NavMenu? _rootMenu;
|
||||
|
||||
static NavMenuItem()
|
||||
{
|
||||
@@ -152,21 +91,81 @@ public class NavMenuItem: HeaderedItemsControl
|
||||
item.OnIsHorizontalCollapsedChanged(args));
|
||||
}
|
||||
|
||||
public object? Icon
|
||||
{
|
||||
get => GetValue(IconProperty);
|
||||
set => SetValue(IconProperty, value);
|
||||
}
|
||||
|
||||
public IDataTemplate? IconTemplate
|
||||
{
|
||||
get => GetValue(IconTemplateProperty);
|
||||
set => SetValue(IconTemplateProperty, value);
|
||||
}
|
||||
|
||||
public ICommand? Command
|
||||
{
|
||||
get => GetValue(CommandProperty);
|
||||
set => SetValue(CommandProperty, value);
|
||||
}
|
||||
|
||||
public object? CommandParameter
|
||||
{
|
||||
get => GetValue(CommandParameterProperty);
|
||||
set => SetValue(CommandParameterProperty, value);
|
||||
}
|
||||
|
||||
public bool IsSelected
|
||||
{
|
||||
get => GetValue(IsSelectedProperty);
|
||||
set => SetValue(IsSelectedProperty, value);
|
||||
}
|
||||
|
||||
public bool IsHighlighted
|
||||
{
|
||||
get => _isHighlighted;
|
||||
private set => SetAndRaise(IsHighlightedProperty, ref _isHighlighted, value);
|
||||
}
|
||||
|
||||
public bool IsHorizontalCollapsed
|
||||
{
|
||||
get => GetValue(IsHorizontalCollapsedProperty);
|
||||
set => SetValue(IsHorizontalCollapsedProperty, value);
|
||||
}
|
||||
|
||||
public bool IsVerticalCollapsed
|
||||
{
|
||||
get => GetValue(IsVerticalCollapsedProperty);
|
||||
set => SetValue(IsVerticalCollapsedProperty, value);
|
||||
}
|
||||
|
||||
public double SubMenuIndent
|
||||
{
|
||||
get => GetValue(SubMenuIndentProperty);
|
||||
set => SetValue(SubMenuIndentProperty, value);
|
||||
}
|
||||
|
||||
public int Level
|
||||
{
|
||||
get => _level;
|
||||
set => SetAndRaise(LevelProperty, ref _level, value);
|
||||
}
|
||||
|
||||
public bool IsSeparator
|
||||
{
|
||||
get => GetValue(IsSeparatorProperty);
|
||||
set => SetValue(IsSeparatorProperty, value);
|
||||
}
|
||||
|
||||
private void OnIsHorizontalCollapsedChanged(AvaloniaPropertyChangedEventArgs<bool> args)
|
||||
{
|
||||
if (args.NewValue.Value)
|
||||
{
|
||||
if (this.ItemsPanelRoot is OverflowStackPanel s)
|
||||
{
|
||||
s.MoveChildrenToOverflowPanel();
|
||||
}
|
||||
if (ItemsPanelRoot is OverflowStackPanel s) s.MoveChildrenToOverflowPanel();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (this.ItemsPanelRoot is OverflowStackPanel s)
|
||||
{
|
||||
s.MoveChildrenToMainPanel();
|
||||
}
|
||||
if (ItemsPanelRoot is OverflowStackPanel s) s.MoveChildrenToMainPanel();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,7 +183,7 @@ public class NavMenuItem: HeaderedItemsControl
|
||||
{
|
||||
return new NavMenuItem();
|
||||
}
|
||||
|
||||
|
||||
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
base.OnAttachedToVisualTree(e);
|
||||
@@ -194,7 +193,7 @@ public class NavMenuItem: HeaderedItemsControl
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
SetCurrentValue(LevelProperty,CalculateDistanceFromLogicalParent<NavMenu>(this));
|
||||
SetCurrentValue(LevelProperty, CalculateDistanceFromLogicalParent<NavMenu>(this));
|
||||
_popup = e.NameScope.Find<Popup>("PART_Popup");
|
||||
_overflowPanel = e.NameScope.Find<Panel>("PART_OverflowPanel");
|
||||
if (_rootMenu is not null)
|
||||
@@ -213,11 +212,8 @@ public class NavMenuItem: HeaderedItemsControl
|
||||
protected override void OnLoaded(RoutedEventArgs e)
|
||||
{
|
||||
base.OnLoaded(e);
|
||||
var root = this.ItemsPanelRoot;
|
||||
if (root is OverflowStackPanel stack)
|
||||
{
|
||||
stack.OverflowPanel = _overflowPanel;
|
||||
}
|
||||
var root = ItemsPanelRoot;
|
||||
if (root is OverflowStackPanel stack) stack.OverflowPanel = _overflowPanel;
|
||||
}
|
||||
|
||||
protected override void OnPointerPressed(PointerPressedEventArgs e)
|
||||
@@ -227,6 +223,7 @@ public class NavMenuItem: HeaderedItemsControl
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
base.OnPointerPressed(e);
|
||||
if (e.Handled) return;
|
||||
|
||||
@@ -235,7 +232,7 @@ public class NavMenuItem: HeaderedItemsControl
|
||||
or PointerUpdateKind.RightButtonPressed)) return;
|
||||
if (p.Pointer.Type == PointerType.Mouse)
|
||||
{
|
||||
if (this.ItemCount == 0)
|
||||
if (ItemCount == 0)
|
||||
{
|
||||
SelectItem(this);
|
||||
Command?.Execute(CommandParameter);
|
||||
@@ -243,7 +240,7 @@ public class NavMenuItem: HeaderedItemsControl
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!IsHorizontalCollapsed)
|
||||
if (!IsHorizontalCollapsed)
|
||||
{
|
||||
SetCurrentValue(IsVerticalCollapsedProperty, !IsVerticalCollapsed);
|
||||
e.Handled = true;
|
||||
@@ -252,13 +249,9 @@ public class NavMenuItem: HeaderedItemsControl
|
||||
{
|
||||
if (_popup is null || e.Source is not Visual v || _popup.IsInsidePopup(v)) return;
|
||||
if (_popup.IsOpen)
|
||||
{
|
||||
_popup.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
_popup.Open();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -276,7 +269,7 @@ public class NavMenuItem: HeaderedItemsControl
|
||||
{
|
||||
var point = e.GetCurrentPoint(this);
|
||||
if (!new Rect(Bounds.Size).ContainsExclusive(point.Position) || e.Pointer.Type != PointerType.Touch) return;
|
||||
if (this.ItemCount == 0)
|
||||
if (ItemCount == 0)
|
||||
{
|
||||
SelectItem(this);
|
||||
Command?.Execute(CommandParameter);
|
||||
@@ -284,7 +277,7 @@ public class NavMenuItem: HeaderedItemsControl
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!IsHorizontalCollapsed)
|
||||
if (!IsHorizontalCollapsed)
|
||||
{
|
||||
SetCurrentValue(IsVerticalCollapsedProperty, !IsVerticalCollapsed);
|
||||
e.Handled = true;
|
||||
@@ -293,13 +286,9 @@ public class NavMenuItem: HeaderedItemsControl
|
||||
{
|
||||
if (_popup is null || e.Source is not Visual v || _popup.IsInsidePopup(v)) return;
|
||||
if (_popup.IsOpen)
|
||||
{
|
||||
_popup.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
_popup.Open();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -317,26 +306,21 @@ public class NavMenuItem: HeaderedItemsControl
|
||||
SetCurrentValue(IsSelectedProperty, false);
|
||||
SetCurrentValue(IsHighlightedProperty, true);
|
||||
}
|
||||
if (this.Parent is NavMenuItem menuItem)
|
||||
|
||||
if (Parent is NavMenuItem menuItem)
|
||||
{
|
||||
menuItem.SelectItem(item);
|
||||
var items = menuItem.LogicalChildren.OfType<NavMenuItem>();
|
||||
foreach (var child in items)
|
||||
{
|
||||
if (child != this)
|
||||
{
|
||||
child.ClearSelection();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (this.Parent is NavMenu menu)
|
||||
else if (Parent is NavMenu menu)
|
||||
{
|
||||
menu.SelectItem(item, this);
|
||||
}
|
||||
if(_popup is not null)
|
||||
{
|
||||
_popup.Close();
|
||||
}
|
||||
|
||||
if (_popup is not null) _popup.Close();
|
||||
}
|
||||
|
||||
internal void ClearSelection()
|
||||
@@ -344,12 +328,8 @@ public class NavMenuItem: HeaderedItemsControl
|
||||
SetCurrentValue(IsHighlightedProperty, false);
|
||||
SetCurrentValue(IsSelectedProperty, false);
|
||||
foreach (var child in LogicalChildren)
|
||||
{
|
||||
if (child is NavMenuItem item)
|
||||
{
|
||||
item.ClearSelection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private NavMenu? GetRootMenu()
|
||||
@@ -357,40 +337,33 @@ public class NavMenuItem: HeaderedItemsControl
|
||||
var root = this.FindAncestorOfType<NavMenu>() ?? this.FindLogicalAncestorOfType<NavMenu>();
|
||||
return root;
|
||||
}
|
||||
|
||||
|
||||
private static int CalculateDistanceFromLogicalParent<T>(ILogical? logical, int @default = -1) where T : class
|
||||
{
|
||||
var result = 0;
|
||||
|
||||
while (logical != null && !(logical is T))
|
||||
{
|
||||
if (logical is NavMenuItem)
|
||||
{
|
||||
result++;
|
||||
}
|
||||
if (logical is NavMenuItem) result++;
|
||||
logical = logical.LogicalParent;
|
||||
}
|
||||
|
||||
return logical != null ? result : @default;
|
||||
}
|
||||
|
||||
|
||||
internal IEnumerable<NavMenuItem> GetLeafMenus()
|
||||
{
|
||||
if (this.ItemCount == 0)
|
||||
if (ItemCount == 0)
|
||||
{
|
||||
yield return this;
|
||||
yield break;
|
||||
}
|
||||
foreach (var child in LogicalChildren)
|
||||
{
|
||||
|
||||
foreach (var child in LogicalChildren)
|
||||
if (child is NavMenuItem item)
|
||||
{
|
||||
var items = item.GetLeafMenus();
|
||||
foreach (var i in items)
|
||||
{
|
||||
yield return i;
|
||||
}
|
||||
foreach (var i in items) yield return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user