feat: wow, first prototype of navigation menu.
This commit is contained in:
42
src/Ursa.Themes.Semi/Controls/Navigation.axaml
Normal file
42
src/Ursa.Themes.Semi/Controls/Navigation.axaml
Normal file
@@ -0,0 +1,42 @@
|
||||
<ResourceDictionary
|
||||
xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:u="https://irihi.tech/ursa">
|
||||
<!-- Add Resources Here -->
|
||||
<ControlTheme x:Key="{x:Type u:NavigationMenu}" TargetType="u:NavigationMenu">
|
||||
<Setter Property="Template">
|
||||
<ControlTemplate TargetType="u:NavigationMenu">
|
||||
<Border Background="{TemplateBinding Background}">
|
||||
<StackPanel>
|
||||
<ContentPresenter Name="PART_HeaderPresenter" Content="Hello" />
|
||||
<ItemsPresenter ItemsPanel="{TemplateBinding ItemsPanel}" />
|
||||
</StackPanel>
|
||||
</Border>
|
||||
</ControlTemplate>
|
||||
</Setter>
|
||||
</ControlTheme>
|
||||
|
||||
<ControlTheme x:Key="{x:Type u:NavigationMenuItem}" TargetType="u:NavigationMenuItem">
|
||||
<Setter Property="Background" Value="Transparent" />
|
||||
<Setter Property="Template">
|
||||
<ControlTemplate TargetType="u:NavigationMenuItem">
|
||||
<Border
|
||||
Padding="4"
|
||||
BorderBrush="Black"
|
||||
BorderThickness="1">
|
||||
<StackPanel>
|
||||
<ContentPresenter
|
||||
Name="PART_HeaderPresenter"
|
||||
Background="{TemplateBinding Background}"
|
||||
Content="{TemplateBinding Header}"
|
||||
ContentTemplate="{TemplateBinding HeaderTemplate}" />
|
||||
<ItemsPresenter ItemsPanel="{TemplateBinding ItemsPanel}" />
|
||||
</StackPanel>
|
||||
</Border>
|
||||
</ControlTemplate>
|
||||
</Setter>
|
||||
<Style Selector="^:selected">
|
||||
<Setter Property="u:NavigationMenuItem.Background" Value="Red" />
|
||||
</Style>
|
||||
</ControlTheme>
|
||||
</ResourceDictionary>
|
||||
@@ -5,6 +5,7 @@
|
||||
<ResourceInclude Source="Banner.axaml" />
|
||||
<ResourceInclude Source="Divider.axaml" />
|
||||
<ResourceInclude Source="IPv4Box.axaml" />
|
||||
<ResourceInclude Source="Navigation.axaml" />
|
||||
<ResourceInclude Source="Pagination.axaml" />
|
||||
<ResourceInclude Source="Timeline.axaml" />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
|
||||
68
src/Ursa/Controls/Navigation/NavigationMenu.cs
Normal file
68
src/Ursa/Controls/Navigation/NavigationMenu.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
99
src/Ursa/Controls/Navigation/NavigationMenuItem.cs
Normal file
99
src/Ursa/Controls/Navigation/NavigationMenuItem.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
6
src/Ursa/Controls/Navigation/NavigationMenuSeparator.cs
Normal file
6
src/Ursa/Controls/Navigation/NavigationMenuSeparator.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public class NavigationMenuSeparator: NavigationMenuItem
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user