From 2af11dea7ebf3c248cc5fc44c75449d0552e3d2d Mon Sep 17 00:00:00 2001 From: rabbitism Date: Thu, 29 Feb 2024 23:45:44 +0800 Subject: [PATCH] feat: initialize breadcrumb. --- src/Ursa/Controls/Breadcrumb/Breadcrumb.cs | 85 +++++++++++++++++++ .../Controls/Breadcrumb/BreadcrumbItem.cs | 41 +++++++++ 2 files changed, 126 insertions(+) create mode 100644 src/Ursa/Controls/Breadcrumb/Breadcrumb.cs create mode 100644 src/Ursa/Controls/Breadcrumb/BreadcrumbItem.cs diff --git a/src/Ursa/Controls/Breadcrumb/Breadcrumb.cs b/src/Ursa/Controls/Breadcrumb/Breadcrumb.cs new file mode 100644 index 0000000..d2ab31c --- /dev/null +++ b/src/Ursa/Controls/Breadcrumb/Breadcrumb.cs @@ -0,0 +1,85 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Controls.Templates; +using Avalonia.Data; +using Avalonia.Layout; +using Avalonia.Metadata; + +namespace Ursa.Controls; + +public class Breadcrumb: ItemsControl +{ + private static readonly ITemplate _defaultPanel = + new FuncTemplate(() => new StackPanel() { Orientation = Orientation.Horizontal }); + + + public static readonly StyledProperty IconBindingProperty = AvaloniaProperty.Register( + nameof(IconBinding)); + + [AssignBinding] + [InheritDataTypeFromItems(nameof(ItemsSource))] + public IBinding? IconBinding + { + get => GetValue(IconBindingProperty); + set => SetValue(IconBindingProperty, value); + } + + public static readonly StyledProperty CommandBindingProperty = AvaloniaProperty.Register( + nameof(CommandBinding)); + + [AssignBinding] + [InheritDataTypeFromItems(nameof(ItemsSource))] + public IBinding? CommandBinding + { + get => GetValue(CommandBindingProperty); + set => SetValue(CommandBindingProperty, value); + } + + public static readonly StyledProperty SeparatorProperty = AvaloniaProperty.Register( + nameof(Separator)); + + /// + /// Separator between items. + /// Usage: Separator can only be raw string or ITemplate<Control>. + /// + public object? Separator + { + get => GetValue(SeparatorProperty); + set => SetValue(SeparatorProperty, value); + } + + static Breadcrumb() + { + ItemsPanelProperty.OverrideDefaultValue(_defaultPanel); + } + + protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey) + { + return NeedsContainer(item, out recycleKey); + } + + protected override Control CreateContainerForItemOverride(object? item, int index, object? recycleKey) + { + return new BreadcrumbItem(); + } + + protected override void PrepareContainerForItemOverride(Control container, object? item, int index) + { + base.PrepareContainerForItemOverride(container, item, index); + if (container is BreadcrumbItem breadcrumbItem) + { + if (!breadcrumbItem.IsSet(BreadcrumbItem.SeparatorProperty)) + { + SeparatorProperty.Changed.AddClassHandler((o, e) => + { + breadcrumbItem.Separator = e.NewValue.Value switch + { + string s => s, + ITemplate t => t.Build(), + _ => e.NewValue.Value?.ToString() + }; + }); + } + } + } +} \ No newline at end of file diff --git a/src/Ursa/Controls/Breadcrumb/BreadcrumbItem.cs b/src/Ursa/Controls/Breadcrumb/BreadcrumbItem.cs new file mode 100644 index 0000000..1de6be1 --- /dev/null +++ b/src/Ursa/Controls/Breadcrumb/BreadcrumbItem.cs @@ -0,0 +1,41 @@ +using System.Windows.Input; +using Avalonia; +using Avalonia.Controls; + +namespace Ursa.Controls; + +public class BreadcrumbItem: ContentControl +{ + public static readonly StyledProperty SeparatorProperty = + AvaloniaProperty.Register( + nameof(Separator), "/"); + + public object? Separator + { + get => GetValue(SeparatorProperty); + set => SetValue(SeparatorProperty, value); + } + + public static readonly StyledProperty IconProperty = AvaloniaProperty.Register( + nameof(Icon)); + + public object? Icon + { + get => GetValue(IconProperty); + set => SetValue(IconProperty, value); + } + + public static readonly StyledProperty CommandProperty = AvaloniaProperty.Register( + nameof(Command)); + + public ICommand? Command + { + get => GetValue(CommandProperty); + set => SetValue(CommandProperty, value); + } + + static BreadcrumbItem() + { + + } +} \ No newline at end of file