misc: re-organize code.

This commit is contained in:
rabbitism
2024-09-06 23:31:34 +08:00
parent 747bfc4695
commit c678d82d6b
3 changed files with 207 additions and 243 deletions

View File

@@ -13,22 +13,77 @@ using Irihi.Avalonia.Shared.Helpers;
namespace Ursa.Controls;
[PseudoClasses(PC_HorizontalCollapsed)]
public class NavMenu: ItemsControl
public class NavMenu : ItemsControl
{
public const string PC_HorizontalCollapsed = ":horizontal-collapsed";
public static readonly StyledProperty<object?> SelectedItemProperty = AvaloniaProperty.Register<NavMenu, object?>(
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
{
get => GetValue(SelectedItemProperty);
set => SetValue(SelectedItemProperty, value);
}
public static readonly StyledProperty<IBinding?> IconBindingProperty = AvaloniaProperty.Register<NavMenu, IBinding?>(
nameof(IconBinding));
[AssignBinding]
[InheritDataTypeFromItems(nameof(ItemsSource))]
public IBinding? IconBinding
@@ -37,9 +92,6 @@ public class NavMenu: ItemsControl
set => SetValue(IconBindingProperty, value);
}
public static readonly StyledProperty<IBinding?> HeaderBindingProperty = AvaloniaProperty.Register<NavMenu, IBinding?>(
nameof(HeaderBinding));
[AssignBinding]
[InheritDataTypeFromItems(nameof(ItemsSource))]
public IBinding? HeaderBinding
@@ -48,9 +100,6 @@ public class NavMenu: ItemsControl
set => SetValue(HeaderBindingProperty, value);
}
public static readonly StyledProperty<IBinding?> SubMenuBindingProperty = AvaloniaProperty.Register<NavMenu, IBinding?>(
nameof(SubMenuBinding));
[AssignBinding]
[InheritDataTypeFromItems(nameof(ItemsSource))]
public IBinding? SubMenuBinding
@@ -59,9 +108,6 @@ public class NavMenu: ItemsControl
set => SetValue(SubMenuBindingProperty, value);
}
public static readonly StyledProperty<IBinding?> CommandBindingProperty = AvaloniaProperty.Register<NavMenu, IBinding?>(
nameof(CommandBinding));
[AssignBinding]
[InheritDataTypeFromItems(nameof(ItemsSource))]
public IBinding? CommandBinding
@@ -70,11 +116,8 @@ public class NavMenu: ItemsControl
set => SetValue(CommandBindingProperty, value);
}
public static readonly StyledProperty<IDataTemplate?> HeaderTemplateProperty = AvaloniaProperty.Register<NavMenu, IDataTemplate?>(
nameof(HeaderTemplate));
/// <summary>
/// Header Template is used for MenuItem headers, not menu header.
/// Header Template is used for MenuItem headers, not menu header.
/// </summary>
public IDataTemplate? HeaderTemplate
{
@@ -82,126 +125,98 @@ public class NavMenu: ItemsControl
set => SetValue(HeaderTemplateProperty, value);
}
public static readonly StyledProperty<IDataTemplate?> IconTemplateProperty = AvaloniaProperty.Register<NavMenu, IDataTemplate?>(
nameof(IconTemplate));
public IDataTemplate? IconTemplate
{
get => GetValue(IconTemplateProperty);
set => SetValue(IconTemplateProperty, value);
}
public static readonly StyledProperty<double> SubMenuIndentProperty = AvaloniaProperty.Register<NavMenu, double>(
nameof(SubMenuIndent));
public double SubMenuIndent
{
get => GetValue(SubMenuIndentProperty);
set => SetValue(SubMenuIndentProperty, value);
}
public static readonly StyledProperty<bool> IsHorizontalCollapsedProperty = AvaloniaProperty.Register<NavMenu, bool>(
nameof(IsHorizontalCollapsed));
public bool IsHorizontalCollapsed
{
get => GetValue(IsHorizontalCollapsedProperty);
set => SetValue(IsHorizontalCollapsedProperty, value);
}
public static readonly StyledProperty<object?> HeaderProperty =
HeaderedContentControl.HeaderProperty.AddOwner<NavMenu>();
public object? Header
{
get => GetValue(HeaderProperty);
set => SetValue(HeaderProperty, value);
}
public static readonly StyledProperty<object?> FooterProperty = AvaloniaProperty.Register<NavMenu, object?>(
nameof(Footer));
public object? Footer
{
get => GetValue(FooterProperty);
set => SetValue(FooterProperty, value);
}
public static readonly StyledProperty<double> ExpandWidthProperty = AvaloniaProperty.Register<NavMenu, double>(
nameof(ExpandWidth), double.NaN);
public double ExpandWidth
{
get => GetValue(ExpandWidthProperty);
set => SetValue(ExpandWidthProperty, value);
}
public static readonly StyledProperty<double> CollapseWidthProperty = AvaloniaProperty.Register<NavMenu, double>(
nameof(CollapseWidth), double.NaN);
public double CollapseWidth
{
get => GetValue(CollapseWidthProperty);
set => SetValue(CollapseWidthProperty, value);
}
public static readonly AttachedProperty<bool> CanToggleProperty =
AvaloniaProperty.RegisterAttached<NavMenu, InputElement, bool>("CanToggle");
public static void SetCanToggle(InputElement obj, bool value)
{
obj.SetValue(CanToggleProperty, value);
}
public static bool GetCanToggle(InputElement obj)
{
return obj.GetValue(CanToggleProperty);
}
public static void SetCanToggle(InputElement obj, bool value) => obj.SetValue(CanToggleProperty, value);
public static bool GetCanToggle(InputElement obj) => obj.GetValue(CanToggleProperty);
public static readonly RoutedEvent<SelectionChangedEventArgs> SelectionChangedEvent = RoutedEvent.Register<NavMenu, SelectionChangedEventArgs>(nameof(SelectionChanged), RoutingStrategies.Bubble);
public event EventHandler<SelectionChangedEventArgs>? SelectionChanged
{
add => AddHandler(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)
{
if (e.NewValue.Value)
{
input.AddHandler(PointerPressedEvent, OnElementToggle);
}
else
{
input.RemoveHandler(PointerPressedEvent, OnElementToggle);
}
}
private static void OnElementToggle(object? sender, RoutedEventArgs args)
{
if (sender is not InputElement input) return;
var nav = input.FindLogicalAncestorOfType<NavMenu>();
if(nav is null) return;
bool collapsed = nav.IsHorizontalCollapsed;
if (nav is null) return;
var collapsed = nav.IsHorizontalCollapsed;
nav.IsHorizontalCollapsed = !collapsed;
}
/// <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>
/// <param name="args"></param>
private void OnSelectedItemChange(AvaloniaPropertyChangedEventArgs<object?> args)
{
SelectionChangedEventArgs a = new SelectionChangedEventArgs(
var a = new SelectionChangedEventArgs(
SelectionChangedEvent,
new [] { args.OldValue.Value },
new [] { args.NewValue.Value });
new[] { args.OldValue.Value },
new[] { args.NewValue.Value });
if (_updateFromUI)
{
RaiseEvent(a);
return;
}
var newValue = args.NewValue.Value;
if (newValue is null)
{
@@ -209,20 +224,17 @@ public class NavMenu: ItemsControl
RaiseEvent(a);
return;
}
var leaves = GetLeafMenus();
bool found = false;
var found = false;
foreach (var leaf in leaves)
{
if (leaf == newValue || leaf.DataContext == newValue)
{
leaf.SelectItem(leaf);
found = true;
}
}
if (!found)
{
ClearAll();
}
if (!found) ClearAll();
RaiseEvent(a);
}
@@ -236,57 +248,37 @@ public class NavMenu: ItemsControl
return new NavMenuItem();
}
private bool _updateFromUI;
internal void SelectItem(NavMenuItem item, NavMenuItem parent)
{
_updateFromUI = true;
foreach (var child in LogicalChildren)
{
if (Equals(child, parent))
{
continue;
}
if (child is NavMenuItem navMenuItem)
{
navMenuItem.ClearSelection();
}
if (Equals(child, parent)) 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;
}
else
{
SelectedItem = item;
}
item.BringIntoView();
_updateFromUI = false;
}
private IEnumerable<NavMenuItem> GetLeafMenus()
{
foreach (var child in LogicalChildren)
{
foreach (var child in LogicalChildren)
if (child is NavMenuItem item)
{
var leafs = item.GetLeafMenus();
foreach (var leaf in leafs)
{
yield return leaf;
}
foreach (var leaf in leafs) yield return leaf;
}
}
}
private void ClearAll()
{
foreach (var child in LogicalChildren)
{
foreach (var child in LogicalChildren)
if (child is NavMenuItem item)
{
item.ClearSelection();
}
}
}
}