feat: wow, first prototype of navigation menu.

This commit is contained in:
rabbitism
2023-06-22 22:00:28 +08:00
parent f4bb4d379e
commit bacf1a6330
9 changed files with 322 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
using System.Collections;
using System.Collections.Specialized;
using Avalonia;
using Avalonia.Collections;
using Avalonia.Controls;
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;
public class NavigationMenu: HeaderedSelectingItemsControl
{
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?> SelectedMenuItemProperty = AvaloniaProperty.Register<NavigationMenu, object?>(
nameof(SelectedMenuItem));
public object? SelectedMenuItem
{
get => GetValue(SelectedMenuItemProperty);
set => SetValue(SelectedMenuItemProperty, value);
}
internal void UpdateSelection(NavigationMenuItem source)
{
var children = this.ItemsPanelRoot?.Children;
if (children is not null)
{
foreach (var child in children)
{
NavigationMenuItem? item = null;
if (child is NavigationMenuItem i)
{
item = i;
}
else if (child is ContentPresenter { Child: NavigationMenuItem i2 })
{
item = i2;
}
if (item != null)
{
if(Equals(item, source)) continue;
item.SetSelection(null, false, false);
}
}
}
}
}

View File

@@ -0,0 +1,99 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
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_Empty)]
public class NavigationMenuItem: HeaderedSelectingItemsControl
{
public const string PC_Closed = ":closed";
public const string PC_Selected = ":selected";
public const string PC_Empty = ":empty";
private NavigationMenu? _rootMenu;
private IDisposable? _ownerSubscription;
private IDisposable? _itemsBinding;
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
_rootMenu = this.FindAncestorOfType<NavigationMenu>();
if (ItemTemplate == null && _rootMenu?.ItemTemplate != null)
{
SetCurrentValue(ItemTemplateProperty, _rootMenu.ItemTemplate);
}
if (ItemContainerTheme == null && _rootMenu?.ItemContainerTheme != null)
{
SetCurrentValue(ItemContainerThemeProperty, _rootMenu.ItemContainerTheme);
}
}
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
// Leaf menu node, can be selected.
if (this.ItemCount == 0)
{
var parents = this.GetSelfAndLogicalAncestors();
if (_rootMenu is not null && parents.Contains(_rootMenu))
{
object? o = this.DataContext ?? this;
_rootMenu.SelectedMenuItem = o;
}
}
e.Handled = true;
SetSelection(this, true, true);
}
internal void SetSelection(NavigationMenuItem? source, bool selected, bool propagateToParent = false)
{
this.PseudoClasses.Set(PC_Selected, selected);
var children = this.ItemsPanelRoot?.Children;
if (children is not null)
{
foreach (var child in children)
{
NavigationMenuItem? item = null;
if (child is NavigationMenuItem i)
{
item = i;
}
else if (child is ContentPresenter { Child: NavigationMenuItem i2 })
{
item = i2;
}
if (item != null)
{
if(Equals(item, source)) continue;
item.SetSelection(this, false, false);
}
}
}
if (propagateToParent)
{
var parent = this.FindAncestorOfType<NavigationMenuItem>();
if (parent != null)
{
parent.SetSelection(this, selected, true);
}
else
{
if (selected)
{
_rootMenu?.UpdateSelection(this);
}
}
}
}
}

View File

@@ -0,0 +1,6 @@
namespace Ursa.Controls;
public class NavigationMenuSeparator: NavigationMenuItem
{
}