feat: update dependency, simplify code, remove navigation control.

This commit is contained in:
rabbitism
2024-02-25 18:00:08 +08:00
parent a948481f62
commit 6ef16d2ec7
19 changed files with 19 additions and 997 deletions

View File

@@ -6,6 +6,7 @@ using Avalonia.Controls.Primitives;
using Avalonia.Data;
using Avalonia.Media;
using Avalonia.Metadata;
using Irihi.Avalonia.Shared.Helpers;
namespace Ursa.Controls;
@@ -61,22 +62,10 @@ public class ButtonGroup: ItemsControl
base.PrepareContainerForItemOverride(container, item, index);
if(container is Button button)
{
if ( CommandBinding is not null)
{
button[!Button.CommandProperty] = CommandBinding;
}
if ( CommandParameterBinding is not null)
{
button[!Button.CommandParameterProperty] = CommandParameterBinding;
}
if ( ContentBinding is not null)
{
button[!Button.ContentProperty] = ContentBinding;
}
if (ItemTemplate is not null)
{
button.ContentTemplate = ItemTemplate;
}
button.TryBind(Button.CommandProperty, CommandBinding);
button.TryBind(Button.CommandParameterProperty, CommandParameterBinding);
button.TryBind(ContentControl.ContentProperty, ContentBinding);
button.TryBind(ContentControl.ContentTemplateProperty, this[!ItemTemplateProperty]);
}
}
}

View File

@@ -37,7 +37,7 @@ public class EnumSelector: TemplatedControl
if (o is not EnumSelector selector) return null;
if (value is null) return null;
if (value.GetType() != selector.EnumType) return null;
var first = selector.Values.FirstOrDefault(a => Equals(a.Value, value));
var first = selector.Values?.FirstOrDefault(a => Equals(a.Value, value));
if (first is null) return null;
return value;
}

View File

@@ -57,7 +57,7 @@ public class FormItem: ContentControl
static FormItem()
{
PropertyToPseudoClassMixin.Attach<FormItem>(NoLabelProperty, PC_NoLabel);
NoLabelProperty.AffectsPseudoClass<FormItem>(PC_NoLabel);
}
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)

View File

@@ -76,7 +76,7 @@ public class TwoTonePathIcon: TemplatedControl
ForegroundProperty,
ActiveForegroundProperty,
ActiveStrokeBrushProperty);
PropertyToPseudoClassMixin.Attach<TwoTonePathIcon>(IsActiveProperty, PC_Active);
IsActiveProperty.AffectsPseudoClass<TwoTonePathIcon>(PC_Active);
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)

View File

@@ -146,7 +146,7 @@ public class NavMenu: ItemsControl
static NavMenu()
{
SelectedItemProperty.Changed.AddClassHandler<NavMenu, object?>((o, e) => o.OnSelectedItemChange(e));
PropertyToPseudoClassMixin.Attach<NavMenu>(IsHorizontalCollapsedProperty, PC_HorizontalCollapsed);
IsHorizontalCollapsedProperty.AffectsPseudoClass<NavMenu>(PC_HorizontalCollapsed);
CanToggleProperty.Changed.AddClassHandler<InputElement, bool>(OnInputRegisteredAsToggle);
}

View File

@@ -140,10 +140,10 @@ public class NavMenuItem: HeaderedItemsControl
// SelectableMixin.Attach<NavMenuItem>(IsSelectedProperty);
PressedMixin.Attach<NavMenuItem>();
LevelProperty.Changed.AddClassHandler<NavMenuItem, int>((item, args) => item.OnLevelChange(args));
PropertyToPseudoClassMixin.Attach<NavMenuItem>(IsHighlightedProperty, PC_Highlighted);
PropertyToPseudoClassMixin.Attach<NavMenuItem>(IsHorizontalCollapsedProperty, PC_HorizontalCollapsed);
PropertyToPseudoClassMixin.Attach<NavMenuItem>(IsVerticalCollapsedProperty, PC_VerticalCollapsed);
PropertyToPseudoClassMixin.Attach<NavMenuItem>(IsSelectedProperty, ":selected", IsSelectedChangedEvent);
IsHighlightedProperty.AffectsPseudoClass<NavMenuItem>(PC_Highlighted);
IsHorizontalCollapsedProperty.AffectsPseudoClass<NavMenuItem>(PC_HorizontalCollapsed);
IsVerticalCollapsedProperty.AffectsPseudoClass<NavMenuItem>(PC_VerticalCollapsed);
IsSelectedProperty.AffectsPseudoClass<NavMenuItem>(":selected", IsSelectedChangedEvent);
IsHorizontalCollapsedProperty.Changed.AddClassHandler<NavMenuItem, bool>((item, args) =>
item.OnIsHorizontalCollapsedChanged(args));
}
@@ -195,22 +195,10 @@ public class NavMenuItem: HeaderedItemsControl
_overflowPanel = e.NameScope.Find<Panel>("PART_OverflowPanel");
if (_rootMenu is not null)
{
if (_rootMenu.IconBinding is not null)
{
this[!IconProperty] = _rootMenu.IconBinding;
}
if (_rootMenu.HeaderBinding is not null)
{
this[!HeaderProperty] = _rootMenu.HeaderBinding;
}
if (_rootMenu.SubMenuBinding is not null)
{
this[!ItemsSourceProperty] = _rootMenu.SubMenuBinding;
}
if (_rootMenu.CommandBinding is not null)
{
this[!CommandProperty] = _rootMenu.CommandBinding;
}
this.TryBind(IconProperty, _rootMenu.IconBinding);
this.TryBind(HeaderProperty, _rootMenu.HeaderBinding);
this.TryBind(ItemsSourceProperty, _rootMenu.SubMenuBinding);
this.TryBind(CommandProperty, _rootMenu.CommandBinding);
this[!IconTemplateProperty] = _rootMenu[!NavMenu.IconTemplateProperty];
this[!HeaderTemplateProperty] = _rootMenu[!NavMenu.HeaderTemplateProperty];
this[!SubMenuIndentProperty] = _rootMenu[!NavMenu.SubMenuIndentProperty];

View File

@@ -1,150 +0,0 @@
using System.Collections;
using System.Collections.Specialized;
using Avalonia;
using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Input;
using Avalonia.Markup.Xaml.Templates;
using Avalonia.Metadata;
namespace Ursa.Controls;
[PseudoClasses(PC_Closed)]
[TemplatePart(Name = PART_CloseButton, Type = typeof(ToggleButton))]
public class NavigationMenu: HeaderedItemsControl
{
public const string PC_Closed = ":closed";
public const string PART_CloseButton = "PART_CloseButton";
public static readonly StyledProperty<object?> FooterProperty = AvaloniaProperty.Register<NavigationMenu, object?>(
nameof(Footer));
public object? Footer
{
get => GetValue(FooterProperty);
set => SetValue(FooterProperty, value);
}
public static readonly StyledProperty<IDataTemplate> FooterTemplateProperty = AvaloniaProperty.Register<NavigationMenu, IDataTemplate>(
nameof(FooterTemplate));
public IDataTemplate FooterTemplate
{
get => GetValue(FooterTemplateProperty);
set => SetValue(FooterTemplateProperty, value);
}
public static readonly StyledProperty<object?> IconProperty = AvaloniaProperty.Register<NavigationMenu, object?>(
nameof(Icon));
public object? Icon
{
get => GetValue(IconProperty);
set => SetValue(IconProperty, value);
}
public static readonly StyledProperty<object?> SelectedItemProperty = AvaloniaProperty.Register<NavigationMenu, object?>(
nameof(SelectedItem));
public object? SelectedItem
{
get => GetValue(SelectedItemProperty);
set => SetValue(SelectedItemProperty, value);
}
public static readonly StyledProperty<bool> ShowCollapseButtonProperty = AvaloniaProperty.Register<NavigationMenu, bool>(
nameof(ShowCollapseButton));
public bool ShowCollapseButton
{
get => GetValue(ShowCollapseButtonProperty);
set => SetValue(ShowCollapseButtonProperty, value);
}
public static readonly StyledProperty<bool> IsClosedProperty = AvaloniaProperty.Register<NavigationMenu, bool>(
nameof(IsClosed));
public bool IsClosed
{
get => GetValue(IsClosedProperty);
set => SetValue(IsClosedProperty, value);
}
public static readonly StyledProperty<double> OpenedWidthProperty = AvaloniaProperty.Register<NavigationMenu, double>(
nameof(OpenedWidth));
public double OpenedWidth
{
get => GetValue(OpenedWidthProperty);
set => SetValue(OpenedWidthProperty, value);
}
public static readonly StyledProperty<double> ClosedWidthProperty = AvaloniaProperty.Register<NavigationMenu, double>(
nameof(ClosedWidth));
public double ClosedWidth
{
get => GetValue(ClosedWidthProperty);
set => SetValue(ClosedWidthProperty, value);
}
static NavigationMenu()
{
SelectedItemProperty.Changed.AddClassHandler<NavigationMenu>((o, e) => o.OnSelectionItemChanged(e));
IsClosedProperty.Changed.AddClassHandler<NavigationMenu>((o,e)=>o.OnIsClosedChanged(e));
}
private void OnSelectionItemChanged(AvaloniaPropertyChangedEventArgs args)
{
var newItem = args.GetNewValue<object?>();
if (newItem is not null)
{
UpdateSelectionFromSelectedItem(newItem);
}
}
private void OnIsClosedChanged(AvaloniaPropertyChangedEventArgs args)
{
bool newValue = args.GetNewValue<bool>();
PseudoClasses.Set(PC_Closed, newValue);
}
internal void UpdateSelection(NavigationMenuItem source)
{
var children = this.ItemsPanelRoot?.Children;
if (children is not null)
{
foreach (var child in children)
{
NavigationMenuItem? item = NavigationMenuItem.GetMenuItemFromControl(child);
if (item != null)
{
if(Equals(item, source)) continue;
item.SetSelection(null, false, false);
}
}
}
}
internal void UpdateSelectionFromSelectedItem(object? o)
{
var children = this.ItemsPanelRoot?.Children;
if (children is not null)
{
foreach (var child in children)
{
NavigationMenuItem? item = NavigationMenuItem.GetMenuItemFromControl(child);
if(item is null) continue;
item.UpdateSelectionFromSelectedItem(o);
}
}
}
}

View File

@@ -1,282 +0,0 @@
using System.Windows.Input;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Mixins;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.LogicalTree;
using Avalonia.Markup.Xaml.Templates;
using Avalonia.Reactive;
using Avalonia.VisualTree;
namespace Ursa.Controls;
[PseudoClasses(PC_Closed, PC_Selected, PC_Highlighted, PC_Collapsed, PC_TopLevel)]
[TemplatePart(PART_Popup, typeof(Popup))]
public class NavigationMenuItem: HeaderedSelectingItemsControl
{
public const string PC_Closed = ":closed";
public const string PC_Selected = ":selected";
public const string PC_Highlighted= ":highlighted";
public const string PC_Collapsed = ":collapsed";
public const string PC_TopLevel = ":top-level";
public const string PART_Popup = "PART_Popup";
private NavigationMenu? _rootMenu;
private IDisposable? _ownerSubscription;
private IDisposable? _itemsBinding;
private bool _isCollapsed;
private Popup? _popup;
public static readonly StyledProperty<bool> IsClosedProperty = AvaloniaProperty.Register<NavigationMenuItem, bool>(
nameof(IsClosed));
public bool IsClosed
{
get => GetValue(IsClosedProperty);
set => SetValue(IsClosedProperty, value);
}
public static readonly StyledProperty<object?> IconProperty = AvaloniaProperty.Register<NavigationMenuItem, object?>(
nameof(Icon));
public object? Icon
{
get => GetValue(IconProperty);
set => SetValue(IconProperty, value);
}
public static readonly StyledProperty<IDataTemplate> IconTemplateProperty = AvaloniaProperty.Register<NavigationMenuItem, IDataTemplate>(
nameof(IconTemplate));
public IDataTemplate IconTemplate
{
get => GetValue(IconTemplateProperty);
set => SetValue(IconTemplateProperty, value);
}
public static readonly DirectProperty<NavigationMenuItem, int> LevelProperty = AvaloniaProperty.RegisterDirect<NavigationMenuItem, int>(
nameof(Level), o => o.Level);
private int _level;
public int Level
{
get => _level;
private set => SetAndRaise(LevelProperty, ref _level, value);
}
public static readonly StyledProperty<ICommand> CommandProperty = AvaloniaProperty.Register<NavigationMenuItem, ICommand>(
nameof(Command));
public ICommand Command
{
get => GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
public static readonly StyledProperty<object?> CommandParameterProperty = AvaloniaProperty.Register<NavigationMenuItem, object?>(
nameof(CommandParameter));
public object? CommandParameter
{
get => GetValue(CommandParameterProperty);
set => SetValue(CommandParameterProperty, value);
}
public static readonly DirectProperty<NavigationMenuItem, bool> IsTopLevelMenuItemProperty = AvaloniaProperty.RegisterDirect<NavigationMenuItem, bool>(
nameof(IsTopLevelMenuItem), o => o.IsTopLevelMenuItem, (o, v) => o.IsTopLevelMenuItem = v);
private bool _isTopLevelMenuItem;
public bool IsTopLevelMenuItem
{
get => _isTopLevelMenuItem;
set => SetAndRaise(IsTopLevelMenuItemProperty, ref _isTopLevelMenuItem, value);
}
public static readonly StyledProperty<bool> IsPopupOpenProperty = AvaloniaProperty.Register<NavigationMenuItem, bool>(
nameof(IsPopupOpen));
public bool IsPopupOpen
{
get => GetValue(IsPopupOpenProperty);
set => SetValue(IsPopupOpenProperty, value);
}
static NavigationMenuItem()
{
IsClosedProperty.Changed.AddClassHandler<NavigationMenuItem>((o, e) => o.OnIsClosedChanged(e));
PressedMixin.Attach<NavigationMenuItem>();
}
private void OnIsClosedChanged(AvaloniaPropertyChangedEventArgs args)
{
bool newValue = args.GetNewValue<bool>();
PseudoClasses.Set(PC_Closed, newValue);
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
GetRootMenu();
if (ItemTemplate == null && _rootMenu?.ItemTemplate != null)
{
SetCurrentValue(ItemTemplateProperty, _rootMenu.ItemTemplate);
}
if (ItemContainerTheme == null && _rootMenu?.ItemContainerTheme != null)
{
SetCurrentValue(ItemContainerThemeProperty, _rootMenu.ItemContainerTheme);
}
if (_rootMenu is not null)
{
// IsClosed = _rootMenu.IsClosed;
}
_rootMenu?.GetObservable(NavigationMenu.IsClosedProperty)
.Subscribe(new AnonymousObserver<bool>(a => this.IsClosed = a));
_rootMenu?.UpdateSelectionFromSelectedItem(_rootMenu.SelectedItem);
_popup = e.NameScope.Find<Popup>(PART_Popup);
Level = CalculateDistanceFromLogicalParent<NavigationMenu>(this) - 1;
bool isTopLevel = Level == 0;
IsTopLevelMenuItem = isTopLevel;
PseudoClasses.Set(PC_TopLevel, isTopLevel);
}
private void GetRootMenu()
{
_rootMenu = this.FindAncestorOfType<NavigationMenu>();
if (_rootMenu is null)
{
var parents = this.FindLogicalAncestorOfType<NavigationMenu>();
if (parents is not null)
{
_rootMenu = parents;
}
}
}
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
// Leaf menu node, can be selected.
if (this.ItemCount == 0)
{
if (_rootMenu is not null )
{
object? o = this.DataContext == _rootMenu.DataContext ? this : this.DataContext ?? this;
_rootMenu.SelectedItem = o;
}
SetSelection(this, true, true);
}
// Non-leaf node, act as a toggle button.
else
{
_isCollapsed = !_isCollapsed;
this.PseudoClasses.Set(PC_Collapsed, _isCollapsed);
if (_popup is not null)
{
_popup.IsOpen = !_popup.IsOpen;
}
}
e.Handled = true;
Command?.Execute(CommandParameter);
}
internal void SetSelection(NavigationMenuItem? source, bool selected, bool propagateToParent = false)
{
if (Equals(this, source) && this.ItemCount == 0)
{
this.PseudoClasses.Set(PC_Highlighted, selected);
this.PseudoClasses.Set(PC_Selected, selected);
}
else
{
this.PseudoClasses.Set(PC_Selected, false);
this.PseudoClasses.Set(PC_Highlighted, selected);
}
var children = this.ItemsPanelRoot?.Children;
if (children is not null)
{
foreach (var child in children)
{
NavigationMenuItem? item = GetMenuItemFromControl(child);
if (item != null)
{
if(Equals(item, source)) continue;
item.SetSelection(this, false, false);
}
}
}
if (propagateToParent)
{
var parent = this.FindLogicalAncestorOfType<NavigationMenuItem>();
if (parent != null)
{
parent.SetSelection(this, selected, true);
}
else
{
if (selected && source!=null)
{
_rootMenu?.UpdateSelection(this);
}
}
}
}
internal void UpdateSelectionFromSelectedItem(object? o)
{
if (o is null)
{
this.SetSelection(this, false, false);
return;
}
if (Equals(this, o) || Equals(this.DataContext, o))
{
this.SetSelection(this, true, true);
}
else
{
var children = this.ItemsPanelRoot?.Children;
if (children is not null)
{
foreach (var child in children)
{
NavigationMenuItem? item = GetMenuItemFromControl(child);
if (item != null)
{
item.UpdateSelectionFromSelectedItem(o);
}
}
}
}
}
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 NavigationMenuItem)
{
result++;
}
logical = logical.LogicalParent;
}
return logical != null ? result : @default;
}
public static NavigationMenuItem? GetMenuItemFromControl(Control? control)
{
if (control is null) return null;
if (control is NavigationMenuItem item) return item;
if (control is ContentPresenter { Child: NavigationMenuItem item2 }) return item2;
return null;
}
}

View File

@@ -1,11 +0,0 @@
using Avalonia.Input;
namespace Ursa.Controls;
public class NavigationMenuSeparator: NavigationMenuItem
{
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
e.Handled = true;
}
}