misc: re-organize code.
This commit is contained in:
@@ -13,22 +13,77 @@ using Irihi.Avalonia.Shared.Helpers;
|
|||||||
namespace Ursa.Controls;
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
[PseudoClasses(PC_HorizontalCollapsed)]
|
[PseudoClasses(PC_HorizontalCollapsed)]
|
||||||
public class NavMenu: ItemsControl
|
public class NavMenu : ItemsControl
|
||||||
{
|
{
|
||||||
public const string PC_HorizontalCollapsed = ":horizontal-collapsed";
|
public const string PC_HorizontalCollapsed = ":horizontal-collapsed";
|
||||||
|
|
||||||
public static readonly StyledProperty<object?> SelectedItemProperty = AvaloniaProperty.Register<NavMenu, object?>(
|
public static readonly StyledProperty<object?> SelectedItemProperty = AvaloniaProperty.Register<NavMenu, object?>(
|
||||||
nameof(SelectedItem), defaultBindingMode: BindingMode.TwoWay);
|
nameof(SelectedItem), defaultBindingMode: BindingMode.TwoWay);
|
||||||
|
|
||||||
|
public static readonly StyledProperty<IBinding?> IconBindingProperty =
|
||||||
|
AvaloniaProperty.Register<NavMenu, IBinding?>(
|
||||||
|
nameof(IconBinding));
|
||||||
|
|
||||||
|
public static readonly StyledProperty<IBinding?> HeaderBindingProperty =
|
||||||
|
AvaloniaProperty.Register<NavMenu, IBinding?>(
|
||||||
|
nameof(HeaderBinding));
|
||||||
|
|
||||||
|
public static readonly StyledProperty<IBinding?> SubMenuBindingProperty =
|
||||||
|
AvaloniaProperty.Register<NavMenu, IBinding?>(
|
||||||
|
nameof(SubMenuBinding));
|
||||||
|
|
||||||
|
public static readonly StyledProperty<IBinding?> CommandBindingProperty =
|
||||||
|
AvaloniaProperty.Register<NavMenu, IBinding?>(
|
||||||
|
nameof(CommandBinding));
|
||||||
|
|
||||||
|
public static readonly StyledProperty<IDataTemplate?> HeaderTemplateProperty =
|
||||||
|
AvaloniaProperty.Register<NavMenu, IDataTemplate?>(
|
||||||
|
nameof(HeaderTemplate));
|
||||||
|
|
||||||
|
public static readonly StyledProperty<IDataTemplate?> IconTemplateProperty =
|
||||||
|
AvaloniaProperty.Register<NavMenu, IDataTemplate?>(
|
||||||
|
nameof(IconTemplate));
|
||||||
|
|
||||||
|
public static readonly StyledProperty<double> SubMenuIndentProperty = AvaloniaProperty.Register<NavMenu, double>(
|
||||||
|
nameof(SubMenuIndent));
|
||||||
|
|
||||||
|
public static readonly StyledProperty<bool> IsHorizontalCollapsedProperty =
|
||||||
|
AvaloniaProperty.Register<NavMenu, bool>(
|
||||||
|
nameof(IsHorizontalCollapsed));
|
||||||
|
|
||||||
|
public static readonly StyledProperty<object?> HeaderProperty =
|
||||||
|
HeaderedContentControl.HeaderProperty.AddOwner<NavMenu>();
|
||||||
|
|
||||||
|
public static readonly StyledProperty<object?> FooterProperty = AvaloniaProperty.Register<NavMenu, object?>(
|
||||||
|
nameof(Footer));
|
||||||
|
|
||||||
|
public static readonly StyledProperty<double> ExpandWidthProperty = AvaloniaProperty.Register<NavMenu, double>(
|
||||||
|
nameof(ExpandWidth), double.NaN);
|
||||||
|
|
||||||
|
public static readonly StyledProperty<double> CollapseWidthProperty = AvaloniaProperty.Register<NavMenu, double>(
|
||||||
|
nameof(CollapseWidth), double.NaN);
|
||||||
|
|
||||||
|
public static readonly AttachedProperty<bool> CanToggleProperty =
|
||||||
|
AvaloniaProperty.RegisterAttached<NavMenu, InputElement, bool>("CanToggle");
|
||||||
|
|
||||||
|
public static readonly RoutedEvent<SelectionChangedEventArgs> SelectionChangedEvent =
|
||||||
|
RoutedEvent.Register<NavMenu, SelectionChangedEventArgs>(nameof(SelectionChanged), RoutingStrategies.Bubble);
|
||||||
|
|
||||||
|
private bool _updateFromUI;
|
||||||
|
|
||||||
|
static NavMenu()
|
||||||
|
{
|
||||||
|
SelectedItemProperty.Changed.AddClassHandler<NavMenu, object?>((o, e) => o.OnSelectedItemChange(e));
|
||||||
|
IsHorizontalCollapsedProperty.AffectsPseudoClass<NavMenu>(PC_HorizontalCollapsed);
|
||||||
|
CanToggleProperty.Changed.AddClassHandler<InputElement, bool>(OnInputRegisteredAsToggle);
|
||||||
|
}
|
||||||
|
|
||||||
public object? SelectedItem
|
public object? SelectedItem
|
||||||
{
|
{
|
||||||
get => GetValue(SelectedItemProperty);
|
get => GetValue(SelectedItemProperty);
|
||||||
set => SetValue(SelectedItemProperty, value);
|
set => SetValue(SelectedItemProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<IBinding?> IconBindingProperty = AvaloniaProperty.Register<NavMenu, IBinding?>(
|
|
||||||
nameof(IconBinding));
|
|
||||||
|
|
||||||
[AssignBinding]
|
[AssignBinding]
|
||||||
[InheritDataTypeFromItems(nameof(ItemsSource))]
|
[InheritDataTypeFromItems(nameof(ItemsSource))]
|
||||||
public IBinding? IconBinding
|
public IBinding? IconBinding
|
||||||
@@ -37,9 +92,6 @@ public class NavMenu: ItemsControl
|
|||||||
set => SetValue(IconBindingProperty, value);
|
set => SetValue(IconBindingProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<IBinding?> HeaderBindingProperty = AvaloniaProperty.Register<NavMenu, IBinding?>(
|
|
||||||
nameof(HeaderBinding));
|
|
||||||
|
|
||||||
[AssignBinding]
|
[AssignBinding]
|
||||||
[InheritDataTypeFromItems(nameof(ItemsSource))]
|
[InheritDataTypeFromItems(nameof(ItemsSource))]
|
||||||
public IBinding? HeaderBinding
|
public IBinding? HeaderBinding
|
||||||
@@ -48,9 +100,6 @@ public class NavMenu: ItemsControl
|
|||||||
set => SetValue(HeaderBindingProperty, value);
|
set => SetValue(HeaderBindingProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<IBinding?> SubMenuBindingProperty = AvaloniaProperty.Register<NavMenu, IBinding?>(
|
|
||||||
nameof(SubMenuBinding));
|
|
||||||
|
|
||||||
[AssignBinding]
|
[AssignBinding]
|
||||||
[InheritDataTypeFromItems(nameof(ItemsSource))]
|
[InheritDataTypeFromItems(nameof(ItemsSource))]
|
||||||
public IBinding? SubMenuBinding
|
public IBinding? SubMenuBinding
|
||||||
@@ -59,9 +108,6 @@ public class NavMenu: ItemsControl
|
|||||||
set => SetValue(SubMenuBindingProperty, value);
|
set => SetValue(SubMenuBindingProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<IBinding?> CommandBindingProperty = AvaloniaProperty.Register<NavMenu, IBinding?>(
|
|
||||||
nameof(CommandBinding));
|
|
||||||
|
|
||||||
[AssignBinding]
|
[AssignBinding]
|
||||||
[InheritDataTypeFromItems(nameof(ItemsSource))]
|
[InheritDataTypeFromItems(nameof(ItemsSource))]
|
||||||
public IBinding? CommandBinding
|
public IBinding? CommandBinding
|
||||||
@@ -70,11 +116,8 @@ public class NavMenu: ItemsControl
|
|||||||
set => SetValue(CommandBindingProperty, value);
|
set => SetValue(CommandBindingProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<IDataTemplate?> HeaderTemplateProperty = AvaloniaProperty.Register<NavMenu, IDataTemplate?>(
|
|
||||||
nameof(HeaderTemplate));
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Header Template is used for MenuItem headers, not menu header.
|
/// Header Template is used for MenuItem headers, not menu header.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IDataTemplate? HeaderTemplate
|
public IDataTemplate? HeaderTemplate
|
||||||
{
|
{
|
||||||
@@ -82,76 +125,57 @@ public class NavMenu: ItemsControl
|
|||||||
set => SetValue(HeaderTemplateProperty, value);
|
set => SetValue(HeaderTemplateProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<IDataTemplate?> IconTemplateProperty = AvaloniaProperty.Register<NavMenu, IDataTemplate?>(
|
|
||||||
nameof(IconTemplate));
|
|
||||||
|
|
||||||
public IDataTemplate? IconTemplate
|
public IDataTemplate? IconTemplate
|
||||||
{
|
{
|
||||||
get => GetValue(IconTemplateProperty);
|
get => GetValue(IconTemplateProperty);
|
||||||
set => SetValue(IconTemplateProperty, value);
|
set => SetValue(IconTemplateProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<double> SubMenuIndentProperty = AvaloniaProperty.Register<NavMenu, double>(
|
|
||||||
nameof(SubMenuIndent));
|
|
||||||
|
|
||||||
public double SubMenuIndent
|
public double SubMenuIndent
|
||||||
{
|
{
|
||||||
get => GetValue(SubMenuIndentProperty);
|
get => GetValue(SubMenuIndentProperty);
|
||||||
set => SetValue(SubMenuIndentProperty, value);
|
set => SetValue(SubMenuIndentProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<bool> IsHorizontalCollapsedProperty = AvaloniaProperty.Register<NavMenu, bool>(
|
|
||||||
nameof(IsHorizontalCollapsed));
|
|
||||||
|
|
||||||
public bool IsHorizontalCollapsed
|
public bool IsHorizontalCollapsed
|
||||||
{
|
{
|
||||||
get => GetValue(IsHorizontalCollapsedProperty);
|
get => GetValue(IsHorizontalCollapsedProperty);
|
||||||
set => SetValue(IsHorizontalCollapsedProperty, value);
|
set => SetValue(IsHorizontalCollapsedProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<object?> HeaderProperty =
|
|
||||||
HeaderedContentControl.HeaderProperty.AddOwner<NavMenu>();
|
|
||||||
|
|
||||||
public object? Header
|
public object? Header
|
||||||
{
|
{
|
||||||
get => GetValue(HeaderProperty);
|
get => GetValue(HeaderProperty);
|
||||||
set => SetValue(HeaderProperty, value);
|
set => SetValue(HeaderProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<object?> FooterProperty = AvaloniaProperty.Register<NavMenu, object?>(
|
|
||||||
nameof(Footer));
|
|
||||||
|
|
||||||
public object? Footer
|
public object? Footer
|
||||||
{
|
{
|
||||||
get => GetValue(FooterProperty);
|
get => GetValue(FooterProperty);
|
||||||
set => SetValue(FooterProperty, value);
|
set => SetValue(FooterProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<double> ExpandWidthProperty = AvaloniaProperty.Register<NavMenu, double>(
|
|
||||||
nameof(ExpandWidth), double.NaN);
|
|
||||||
|
|
||||||
public double ExpandWidth
|
public double ExpandWidth
|
||||||
{
|
{
|
||||||
get => GetValue(ExpandWidthProperty);
|
get => GetValue(ExpandWidthProperty);
|
||||||
set => SetValue(ExpandWidthProperty, value);
|
set => SetValue(ExpandWidthProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<double> CollapseWidthProperty = AvaloniaProperty.Register<NavMenu, double>(
|
|
||||||
nameof(CollapseWidth), double.NaN);
|
|
||||||
|
|
||||||
public double CollapseWidth
|
public double CollapseWidth
|
||||||
{
|
{
|
||||||
get => GetValue(CollapseWidthProperty);
|
get => GetValue(CollapseWidthProperty);
|
||||||
set => SetValue(CollapseWidthProperty, value);
|
set => SetValue(CollapseWidthProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly AttachedProperty<bool> CanToggleProperty =
|
public static void SetCanToggle(InputElement obj, bool value)
|
||||||
AvaloniaProperty.RegisterAttached<NavMenu, InputElement, bool>("CanToggle");
|
{
|
||||||
|
obj.SetValue(CanToggleProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
public static void SetCanToggle(InputElement obj, bool value) => obj.SetValue(CanToggleProperty, value);
|
public static bool GetCanToggle(InputElement obj)
|
||||||
public static bool GetCanToggle(InputElement obj) => obj.GetValue(CanToggleProperty);
|
{
|
||||||
|
return obj.GetValue(CanToggleProperty);
|
||||||
public static readonly RoutedEvent<SelectionChangedEventArgs> SelectionChangedEvent = RoutedEvent.Register<NavMenu, SelectionChangedEventArgs>(nameof(SelectionChanged), RoutingStrategies.Bubble);
|
}
|
||||||
|
|
||||||
public event EventHandler<SelectionChangedEventArgs>? SelectionChanged
|
public event EventHandler<SelectionChangedEventArgs>? SelectionChanged
|
||||||
{
|
{
|
||||||
@@ -159,49 +183,40 @@ public class NavMenu: ItemsControl
|
|||||||
remove => RemoveHandler(SelectionChangedEvent, value);
|
remove => RemoveHandler(SelectionChangedEvent, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static NavMenu()
|
|
||||||
{
|
|
||||||
SelectedItemProperty.Changed.AddClassHandler<NavMenu, object?>((o, e) => o.OnSelectedItemChange(e));
|
|
||||||
IsHorizontalCollapsedProperty.AffectsPseudoClass<NavMenu>(PC_HorizontalCollapsed);
|
|
||||||
CanToggleProperty.Changed.AddClassHandler<InputElement, bool>(OnInputRegisteredAsToggle);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void OnInputRegisteredAsToggle(InputElement input, AvaloniaPropertyChangedEventArgs<bool> e)
|
private static void OnInputRegisteredAsToggle(InputElement input, AvaloniaPropertyChangedEventArgs<bool> e)
|
||||||
{
|
{
|
||||||
if (e.NewValue.Value)
|
if (e.NewValue.Value)
|
||||||
{
|
|
||||||
input.AddHandler(PointerPressedEvent, OnElementToggle);
|
input.AddHandler(PointerPressedEvent, OnElementToggle);
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
input.RemoveHandler(PointerPressedEvent, OnElementToggle);
|
input.RemoveHandler(PointerPressedEvent, OnElementToggle);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void OnElementToggle(object? sender, RoutedEventArgs args)
|
private static void OnElementToggle(object? sender, RoutedEventArgs args)
|
||||||
{
|
{
|
||||||
if (sender is not InputElement input) return;
|
if (sender is not InputElement input) return;
|
||||||
var nav = input.FindLogicalAncestorOfType<NavMenu>();
|
var nav = input.FindLogicalAncestorOfType<NavMenu>();
|
||||||
if(nav is null) return;
|
if (nav is null) return;
|
||||||
bool collapsed = nav.IsHorizontalCollapsed;
|
var collapsed = nav.IsHorizontalCollapsed;
|
||||||
nav.IsHorizontalCollapsed = !collapsed;
|
nav.IsHorizontalCollapsed = !collapsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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.
|
/// 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.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="args"></param>
|
/// <param name="args"></param>
|
||||||
private void OnSelectedItemChange(AvaloniaPropertyChangedEventArgs<object?> args)
|
private void OnSelectedItemChange(AvaloniaPropertyChangedEventArgs<object?> args)
|
||||||
{
|
{
|
||||||
SelectionChangedEventArgs a = new SelectionChangedEventArgs(
|
var a = new SelectionChangedEventArgs(
|
||||||
SelectionChangedEvent,
|
SelectionChangedEvent,
|
||||||
new [] { args.OldValue.Value },
|
new[] { args.OldValue.Value },
|
||||||
new [] { args.NewValue.Value });
|
new[] { args.NewValue.Value });
|
||||||
if (_updateFromUI)
|
if (_updateFromUI)
|
||||||
{
|
{
|
||||||
RaiseEvent(a);
|
RaiseEvent(a);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var newValue = args.NewValue.Value;
|
var newValue = args.NewValue.Value;
|
||||||
if (newValue is null)
|
if (newValue is null)
|
||||||
{
|
{
|
||||||
@@ -209,20 +224,17 @@ public class NavMenu: ItemsControl
|
|||||||
RaiseEvent(a);
|
RaiseEvent(a);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var leaves = GetLeafMenus();
|
var leaves = GetLeafMenus();
|
||||||
bool found = false;
|
var found = false;
|
||||||
foreach (var leaf in leaves)
|
foreach (var leaf in leaves)
|
||||||
{
|
|
||||||
if (leaf == newValue || leaf.DataContext == newValue)
|
if (leaf == newValue || leaf.DataContext == newValue)
|
||||||
{
|
{
|
||||||
leaf.SelectItem(leaf);
|
leaf.SelectItem(leaf);
|
||||||
found = true;
|
found = true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (!found)
|
if (!found) ClearAll();
|
||||||
{
|
|
||||||
ClearAll();
|
|
||||||
}
|
|
||||||
RaiseEvent(a);
|
RaiseEvent(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,30 +248,19 @@ public class NavMenu: ItemsControl
|
|||||||
return new NavMenuItem();
|
return new NavMenuItem();
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool _updateFromUI;
|
|
||||||
|
|
||||||
internal void SelectItem(NavMenuItem item, NavMenuItem parent)
|
internal void SelectItem(NavMenuItem item, NavMenuItem parent)
|
||||||
{
|
{
|
||||||
_updateFromUI = true;
|
_updateFromUI = true;
|
||||||
foreach (var child in LogicalChildren)
|
foreach (var child in LogicalChildren)
|
||||||
{
|
{
|
||||||
if (Equals(child, parent))
|
if (Equals(child, parent)) continue;
|
||||||
{
|
if (child is NavMenuItem navMenuItem) navMenuItem.ClearSelection();
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (child is NavMenuItem navMenuItem)
|
|
||||||
{
|
|
||||||
navMenuItem.ClearSelection();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (item.DataContext is not null && item.DataContext != this.DataContext)
|
|
||||||
{
|
if (item.DataContext is not null && item.DataContext != DataContext)
|
||||||
SelectedItem = item.DataContext;
|
SelectedItem = item.DataContext;
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
SelectedItem = item;
|
SelectedItem = item;
|
||||||
}
|
|
||||||
item.BringIntoView();
|
item.BringIntoView();
|
||||||
_updateFromUI = false;
|
_updateFromUI = false;
|
||||||
}
|
}
|
||||||
@@ -267,26 +268,17 @@ public class NavMenu: ItemsControl
|
|||||||
private IEnumerable<NavMenuItem> GetLeafMenus()
|
private IEnumerable<NavMenuItem> GetLeafMenus()
|
||||||
{
|
{
|
||||||
foreach (var child in LogicalChildren)
|
foreach (var child in LogicalChildren)
|
||||||
{
|
|
||||||
if (child is NavMenuItem item)
|
if (child is NavMenuItem item)
|
||||||
{
|
{
|
||||||
var leafs = item.GetLeafMenus();
|
var leafs = item.GetLeafMenus();
|
||||||
foreach (var leaf in leafs)
|
foreach (var leaf in leafs) yield return leaf;
|
||||||
{
|
|
||||||
yield return leaf;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ClearAll()
|
private void ClearAll()
|
||||||
{
|
{
|
||||||
foreach (var child in LogicalChildren)
|
foreach (var child in LogicalChildren)
|
||||||
{
|
|
||||||
if (child is NavMenuItem item)
|
if (child is NavMenuItem item)
|
||||||
{
|
|
||||||
item.ClearSelection();
|
item.ClearSelection();
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -16,10 +16,10 @@ using Irihi.Avalonia.Shared.Helpers;
|
|||||||
namespace Ursa.Controls;
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Navigation Menu Item
|
/// Navigation Menu Item
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[PseudoClasses(PC_Highlighted, PC_HorizontalCollapsed, PC_VerticalCollapsed, PC_FirstLevel, PC_Selector)]
|
[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_Highlighted = ":highlighted";
|
||||||
public const string PC_FirstLevel = ":first-level";
|
public const string PC_FirstLevel = ":first-level";
|
||||||
@@ -27,116 +27,55 @@ public class NavMenuItem: HeaderedItemsControl
|
|||||||
public const string PC_VerticalCollapsed = ":vertical-collapsed";
|
public const string PC_VerticalCollapsed = ":vertical-collapsed";
|
||||||
public const string PC_Selector = ":selector";
|
public const string PC_Selector = ":selector";
|
||||||
|
|
||||||
private NavMenu? _rootMenu;
|
private static readonly Point InvalidPoint = new(double.NaN, double.NaN);
|
||||||
private Popup? _popup;
|
|
||||||
private Panel? _overflowPanel;
|
|
||||||
|
|
||||||
private static readonly Point InvalidPoint = new (double.NaN, double.NaN);
|
|
||||||
private Point _pointerDownPoint = InvalidPoint;
|
|
||||||
|
|
||||||
public static readonly StyledProperty<object?> IconProperty = AvaloniaProperty.Register<NavMenuItem, object?>(
|
public static readonly StyledProperty<object?> IconProperty = AvaloniaProperty.Register<NavMenuItem, object?>(
|
||||||
nameof(Icon));
|
nameof(Icon));
|
||||||
|
|
||||||
public object? Icon
|
public static readonly StyledProperty<IDataTemplate?> IconTemplateProperty =
|
||||||
{
|
AvaloniaProperty.Register<NavMenuItem, IDataTemplate?>(
|
||||||
get => GetValue(IconProperty);
|
nameof(IconTemplate));
|
||||||
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<ICommand?> CommandProperty = Button.CommandProperty.AddOwner<NavMenuItem>();
|
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 =
|
public static readonly StyledProperty<object?> CommandParameterProperty =
|
||||||
Button.CommandParameterProperty.AddOwner<NavMenuItem>();
|
Button.CommandParameterProperty.AddOwner<NavMenuItem>();
|
||||||
|
|
||||||
public object? CommandParameter
|
|
||||||
{
|
|
||||||
get => GetValue(CommandParameterProperty);
|
|
||||||
set => SetValue(CommandParameterProperty, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<bool> IsSelectedProperty =
|
public static readonly StyledProperty<bool> IsSelectedProperty =
|
||||||
SelectingItemsControl.IsSelectedProperty.AddOwner<NavMenuItem>();
|
SelectingItemsControl.IsSelectedProperty.AddOwner<NavMenuItem>();
|
||||||
|
|
||||||
public bool IsSelected
|
public static readonly RoutedEvent<RoutedEventArgs> IsSelectedChangedEvent =
|
||||||
{
|
RoutedEvent.Register<SelectingItemsControl, RoutedEventArgs>("IsSelectedChanged", RoutingStrategies.Bubble);
|
||||||
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 DirectProperty<NavMenuItem, bool> IsHighlightedProperty =
|
public static readonly DirectProperty<NavMenuItem, bool> IsHighlightedProperty =
|
||||||
AvaloniaProperty.RegisterDirect<NavMenuItem, bool>(
|
AvaloniaProperty.RegisterDirect<NavMenuItem, bool>(
|
||||||
nameof(IsHighlighted), o => o.IsHighlighted, (o, v) => o.IsHighlighted = v,
|
nameof(IsHighlighted), o => o.IsHighlighted, (o, v) => o.IsHighlighted = v,
|
||||||
defaultBindingMode: BindingMode.TwoWay);
|
defaultBindingMode: BindingMode.TwoWay);
|
||||||
|
|
||||||
public bool IsHighlighted
|
|
||||||
{
|
|
||||||
get => _isHighlighted;
|
|
||||||
private set => SetAndRaise(IsHighlightedProperty, ref _isHighlighted, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<bool> IsHorizontalCollapsedProperty =
|
public static readonly StyledProperty<bool> IsHorizontalCollapsedProperty =
|
||||||
NavMenu.IsHorizontalCollapsedProperty.AddOwner<NavMenuItem>();
|
NavMenu.IsHorizontalCollapsedProperty.AddOwner<NavMenuItem>();
|
||||||
|
|
||||||
public bool IsHorizontalCollapsed
|
public static readonly StyledProperty<bool> IsVerticalCollapsedProperty =
|
||||||
{
|
AvaloniaProperty.Register<NavMenuItem, bool>(
|
||||||
get => GetValue(IsHorizontalCollapsedProperty);
|
nameof(IsVerticalCollapsed));
|
||||||
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<double> SubMenuIndentProperty =
|
public static readonly StyledProperty<double> SubMenuIndentProperty =
|
||||||
NavMenu.SubMenuIndentProperty.AddOwner<NavMenuItem>();
|
NavMenu.SubMenuIndentProperty.AddOwner<NavMenuItem>();
|
||||||
|
|
||||||
public double SubMenuIndent
|
internal static readonly DirectProperty<NavMenuItem, int> LevelProperty =
|
||||||
{
|
AvaloniaProperty.RegisterDirect<NavMenuItem, int>(
|
||||||
get => GetValue(SubMenuIndentProperty);
|
nameof(Level), o => o.Level, (o, v) => o.Level = v);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<bool> IsSeparatorProperty = AvaloniaProperty.Register<NavMenuItem, bool>(
|
public static readonly StyledProperty<bool> IsSeparatorProperty = AvaloniaProperty.Register<NavMenuItem, bool>(
|
||||||
nameof(IsSeparator));
|
nameof(IsSeparator));
|
||||||
|
|
||||||
public bool IsSeparator
|
private bool _isHighlighted;
|
||||||
{
|
private int _level;
|
||||||
get => GetValue(IsSeparatorProperty);
|
private Panel? _overflowPanel;
|
||||||
set => SetValue(IsSeparatorProperty, value);
|
private Point _pointerDownPoint = InvalidPoint;
|
||||||
}
|
private Popup? _popup;
|
||||||
|
|
||||||
|
private NavMenu? _rootMenu;
|
||||||
|
|
||||||
static NavMenuItem()
|
static NavMenuItem()
|
||||||
{
|
{
|
||||||
@@ -152,21 +91,81 @@ public class NavMenuItem: HeaderedItemsControl
|
|||||||
item.OnIsHorizontalCollapsedChanged(args));
|
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)
|
private void OnIsHorizontalCollapsedChanged(AvaloniaPropertyChangedEventArgs<bool> args)
|
||||||
{
|
{
|
||||||
if (args.NewValue.Value)
|
if (args.NewValue.Value)
|
||||||
{
|
{
|
||||||
if (this.ItemsPanelRoot is OverflowStackPanel s)
|
if (ItemsPanelRoot is OverflowStackPanel s) s.MoveChildrenToOverflowPanel();
|
||||||
{
|
|
||||||
s.MoveChildrenToOverflowPanel();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (this.ItemsPanelRoot is OverflowStackPanel s)
|
if (ItemsPanelRoot is OverflowStackPanel s) s.MoveChildrenToMainPanel();
|
||||||
{
|
|
||||||
s.MoveChildrenToMainPanel();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,7 +193,7 @@ public class NavMenuItem: HeaderedItemsControl
|
|||||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnApplyTemplate(e);
|
base.OnApplyTemplate(e);
|
||||||
SetCurrentValue(LevelProperty,CalculateDistanceFromLogicalParent<NavMenu>(this));
|
SetCurrentValue(LevelProperty, CalculateDistanceFromLogicalParent<NavMenu>(this));
|
||||||
_popup = e.NameScope.Find<Popup>("PART_Popup");
|
_popup = e.NameScope.Find<Popup>("PART_Popup");
|
||||||
_overflowPanel = e.NameScope.Find<Panel>("PART_OverflowPanel");
|
_overflowPanel = e.NameScope.Find<Panel>("PART_OverflowPanel");
|
||||||
if (_rootMenu is not null)
|
if (_rootMenu is not null)
|
||||||
@@ -213,11 +212,8 @@ public class NavMenuItem: HeaderedItemsControl
|
|||||||
protected override void OnLoaded(RoutedEventArgs e)
|
protected override void OnLoaded(RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnLoaded(e);
|
base.OnLoaded(e);
|
||||||
var root = this.ItemsPanelRoot;
|
var root = ItemsPanelRoot;
|
||||||
if (root is OverflowStackPanel stack)
|
if (root is OverflowStackPanel stack) stack.OverflowPanel = _overflowPanel;
|
||||||
{
|
|
||||||
stack.OverflowPanel = _overflowPanel;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnPointerPressed(PointerPressedEventArgs e)
|
protected override void OnPointerPressed(PointerPressedEventArgs e)
|
||||||
@@ -227,6 +223,7 @@ public class NavMenuItem: HeaderedItemsControl
|
|||||||
e.Handled = true;
|
e.Handled = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
base.OnPointerPressed(e);
|
base.OnPointerPressed(e);
|
||||||
if (e.Handled) return;
|
if (e.Handled) return;
|
||||||
|
|
||||||
@@ -235,7 +232,7 @@ public class NavMenuItem: HeaderedItemsControl
|
|||||||
or PointerUpdateKind.RightButtonPressed)) return;
|
or PointerUpdateKind.RightButtonPressed)) return;
|
||||||
if (p.Pointer.Type == PointerType.Mouse)
|
if (p.Pointer.Type == PointerType.Mouse)
|
||||||
{
|
{
|
||||||
if (this.ItemCount == 0)
|
if (ItemCount == 0)
|
||||||
{
|
{
|
||||||
SelectItem(this);
|
SelectItem(this);
|
||||||
Command?.Execute(CommandParameter);
|
Command?.Execute(CommandParameter);
|
||||||
@@ -252,13 +249,9 @@ public class NavMenuItem: HeaderedItemsControl
|
|||||||
{
|
{
|
||||||
if (_popup is null || e.Source is not Visual v || _popup.IsInsidePopup(v)) return;
|
if (_popup is null || e.Source is not Visual v || _popup.IsInsidePopup(v)) return;
|
||||||
if (_popup.IsOpen)
|
if (_popup.IsOpen)
|
||||||
{
|
|
||||||
_popup.Close();
|
_popup.Close();
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
_popup.Open();
|
_popup.Open();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -276,7 +269,7 @@ public class NavMenuItem: HeaderedItemsControl
|
|||||||
{
|
{
|
||||||
var point = e.GetCurrentPoint(this);
|
var point = e.GetCurrentPoint(this);
|
||||||
if (!new Rect(Bounds.Size).ContainsExclusive(point.Position) || e.Pointer.Type != PointerType.Touch) return;
|
if (!new Rect(Bounds.Size).ContainsExclusive(point.Position) || e.Pointer.Type != PointerType.Touch) return;
|
||||||
if (this.ItemCount == 0)
|
if (ItemCount == 0)
|
||||||
{
|
{
|
||||||
SelectItem(this);
|
SelectItem(this);
|
||||||
Command?.Execute(CommandParameter);
|
Command?.Execute(CommandParameter);
|
||||||
@@ -293,13 +286,9 @@ public class NavMenuItem: HeaderedItemsControl
|
|||||||
{
|
{
|
||||||
if (_popup is null || e.Source is not Visual v || _popup.IsInsidePopup(v)) return;
|
if (_popup is null || e.Source is not Visual v || _popup.IsInsidePopup(v)) return;
|
||||||
if (_popup.IsOpen)
|
if (_popup.IsOpen)
|
||||||
{
|
|
||||||
_popup.Close();
|
_popup.Close();
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
_popup.Open();
|
_popup.Open();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -317,26 +306,21 @@ public class NavMenuItem: HeaderedItemsControl
|
|||||||
SetCurrentValue(IsSelectedProperty, false);
|
SetCurrentValue(IsSelectedProperty, false);
|
||||||
SetCurrentValue(IsHighlightedProperty, true);
|
SetCurrentValue(IsHighlightedProperty, true);
|
||||||
}
|
}
|
||||||
if (this.Parent is NavMenuItem menuItem)
|
|
||||||
|
if (Parent is NavMenuItem menuItem)
|
||||||
{
|
{
|
||||||
menuItem.SelectItem(item);
|
menuItem.SelectItem(item);
|
||||||
var items = menuItem.LogicalChildren.OfType<NavMenuItem>();
|
var items = menuItem.LogicalChildren.OfType<NavMenuItem>();
|
||||||
foreach (var child in items)
|
foreach (var child in items)
|
||||||
{
|
|
||||||
if (child != this)
|
if (child != this)
|
||||||
{
|
|
||||||
child.ClearSelection();
|
child.ClearSelection();
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (this.Parent is NavMenu menu)
|
else if (Parent is NavMenu menu)
|
||||||
{
|
{
|
||||||
menu.SelectItem(item, this);
|
menu.SelectItem(item, this);
|
||||||
}
|
}
|
||||||
if(_popup is not null)
|
|
||||||
{
|
if (_popup is not null) _popup.Close();
|
||||||
_popup.Close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void ClearSelection()
|
internal void ClearSelection()
|
||||||
@@ -344,12 +328,8 @@ public class NavMenuItem: HeaderedItemsControl
|
|||||||
SetCurrentValue(IsHighlightedProperty, false);
|
SetCurrentValue(IsHighlightedProperty, false);
|
||||||
SetCurrentValue(IsSelectedProperty, false);
|
SetCurrentValue(IsSelectedProperty, false);
|
||||||
foreach (var child in LogicalChildren)
|
foreach (var child in LogicalChildren)
|
||||||
{
|
|
||||||
if (child is NavMenuItem item)
|
if (child is NavMenuItem item)
|
||||||
{
|
|
||||||
item.ClearSelection();
|
item.ClearSelection();
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private NavMenu? GetRootMenu()
|
private NavMenu? GetRootMenu()
|
||||||
@@ -364,10 +344,7 @@ public class NavMenuItem: HeaderedItemsControl
|
|||||||
|
|
||||||
while (logical != null && !(logical is T))
|
while (logical != null && !(logical is T))
|
||||||
{
|
{
|
||||||
if (logical is NavMenuItem)
|
if (logical is NavMenuItem) result++;
|
||||||
{
|
|
||||||
result++;
|
|
||||||
}
|
|
||||||
logical = logical.LogicalParent;
|
logical = logical.LogicalParent;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -376,21 +353,17 @@ public class NavMenuItem: HeaderedItemsControl
|
|||||||
|
|
||||||
internal IEnumerable<NavMenuItem> GetLeafMenus()
|
internal IEnumerable<NavMenuItem> GetLeafMenus()
|
||||||
{
|
{
|
||||||
if (this.ItemCount == 0)
|
if (ItemCount == 0)
|
||||||
{
|
{
|
||||||
yield return this;
|
yield return this;
|
||||||
yield break;
|
yield break;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var child in LogicalChildren)
|
foreach (var child in LogicalChildren)
|
||||||
{
|
|
||||||
if (child is NavMenuItem item)
|
if (child is NavMenuItem item)
|
||||||
{
|
{
|
||||||
var items = item.GetLeafMenus();
|
var items = item.GetLeafMenus();
|
||||||
foreach (var i in items)
|
foreach (var i in items) yield return i;
|
||||||
{
|
|
||||||
yield return i;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,12 +2,13 @@ using Avalonia.Controls;
|
|||||||
|
|
||||||
namespace Ursa.Controls;
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
public class OverflowStackPanel: StackPanel
|
public class OverflowStackPanel : StackPanel
|
||||||
{
|
{
|
||||||
public Panel? OverflowPanel { get; set; }
|
public Panel? OverflowPanel { get; set; }
|
||||||
|
|
||||||
public void MoveChildrenToOverflowPanel()
|
public void MoveChildrenToOverflowPanel()
|
||||||
{
|
{
|
||||||
var children = this.Children.ToList();
|
var children = Children.ToList();
|
||||||
foreach (var child in children)
|
foreach (var child in children)
|
||||||
{
|
{
|
||||||
Children.Remove(child);
|
Children.Remove(child);
|
||||||
@@ -17,14 +18,12 @@ public class OverflowStackPanel: StackPanel
|
|||||||
|
|
||||||
public void MoveChildrenToMainPanel()
|
public void MoveChildrenToMainPanel()
|
||||||
{
|
{
|
||||||
var children = this.OverflowPanel?.Children.ToList();
|
var children = OverflowPanel?.Children.ToList();
|
||||||
if (children != null && children.Count > 0)
|
if (children is not null && children.Count > 0)
|
||||||
{
|
|
||||||
foreach (var child in children)
|
foreach (var child in children)
|
||||||
{
|
{
|
||||||
OverflowPanel?.Children.Remove(child);
|
OverflowPanel?.Children.Remove(child);
|
||||||
Children.Add(child);
|
Children.Add(child);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user