123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- using Avalonia;
- using Avalonia.Controls;
- using Avalonia.Controls.Presenters;
- using Avalonia.Controls.Primitives;
- using Avalonia.Input;
- namespace SukiUI.Controls;
- public class SukiSideMenuItem : ListBoxItem
- {
- public static readonly StyledProperty<object?> IconProperty =
- AvaloniaProperty.Register<SukiSideMenuItem, object?>(nameof(Icon));
- private Border? _border;
- public object? Icon
- {
- get => GetValue(IconProperty);
- set => SetValue(IconProperty, value);
- }
- public static readonly StyledProperty<string?> HeaderProperty =
- AvaloniaProperty.Register<SukiSideMenuItem, string?>(nameof(Header));
- public string? Header
- {
- get => GetValue(HeaderProperty);
- set => SetValue(HeaderProperty, value);
- }
- public static readonly StyledProperty<object> PageContentProperty =
- AvaloniaProperty.Register<SukiSideMenuItem, object>(nameof(PageContent));
- public object PageContent
- {
- get => GetValue(PageContentProperty);
- set => SetValue(PageContentProperty, value);
- }
- public void Show()
- {
- if (_border == null)
- {
- return;
- }
- _border.MaxHeight = 200.0;
- }
- public void Hide()
- {
- if (_border == null)
- {
- return;
- }
- _border.MaxHeight = 0.0;
- }
- protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
- {
- base.OnApplyTemplate(e);
- _border = e.NameScope.Get<Border>("PART_Border");
- if (e.NameScope.Get<ContentPresenter>("PART_AltDisplay") is { } contentControl)
- {
- if (Header is not null || Icon is not null)
- {
- contentControl.IsVisible = false;
- }
- }
- }
- protected override void OnPointerPressed(PointerPressedEventArgs e)
- {
- base.OnPointerPressed(e);
- if (e.Handled)
- return;
- if (!e.Handled && ItemsControl.ItemsControlFromItemContainer(this) is SukiSideMenu owner)
- {
- var p = e.GetCurrentPoint(this);
- if (p.Properties.PointerUpdateKind is PointerUpdateKind.LeftButtonPressed or
- PointerUpdateKind.RightButtonPressed)
- {
- if (p.Pointer.Type == PointerType.Mouse)
- {
- // If the pressed point comes from a mouse, perform the selection immediately.
- e.Handled = owner.UpdateSelectionFromPointerEvent(this);
- }
- }
- }
- }
-
- public static readonly StyledProperty<bool> IsContentMovableProperty =
- AvaloniaProperty.Register<SukiSideMenuItem, bool>(nameof(IsContentMovable), defaultValue: true);
- public bool IsContentMovable
- {
- get => GetValue(IsContentMovableProperty);
- set => SetValue(IsContentMovableProperty, value);
- }
-
- public static readonly StyledProperty<bool> IsTopMenuExpandedProperty =
- AvaloniaProperty.Register<SukiSideMenuItem, bool>(nameof(IsTopMenuExpanded), defaultValue: true);
- public bool IsTopMenuExpanded
- {
- get => GetValue(IsTopMenuExpandedProperty);
- set => SetValue(IsTopMenuExpandedProperty, value);
- }
- }
|