SukiSideMenuItem.axaml.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.Presenters;
  4. using Avalonia.Controls.Primitives;
  5. using Avalonia.Input;
  6. namespace SukiUI.Controls;
  7. public class SukiSideMenuItem : ListBoxItem
  8. {
  9. public static readonly StyledProperty<object?> IconProperty =
  10. AvaloniaProperty.Register<SukiSideMenuItem, object?>(nameof(Icon));
  11. private Border? _border;
  12. public object? Icon
  13. {
  14. get => GetValue(IconProperty);
  15. set => SetValue(IconProperty, value);
  16. }
  17. public static readonly StyledProperty<string?> HeaderProperty =
  18. AvaloniaProperty.Register<SukiSideMenuItem, string?>(nameof(Header));
  19. public string? Header
  20. {
  21. get => GetValue(HeaderProperty);
  22. set => SetValue(HeaderProperty, value);
  23. }
  24. public static readonly StyledProperty<object> PageContentProperty =
  25. AvaloniaProperty.Register<SukiSideMenuItem, object>(nameof(PageContent));
  26. public object PageContent
  27. {
  28. get => GetValue(PageContentProperty);
  29. set => SetValue(PageContentProperty, value);
  30. }
  31. public void Show()
  32. {
  33. if (_border == null)
  34. {
  35. return;
  36. }
  37. _border.MaxHeight = 200.0;
  38. }
  39. public void Hide()
  40. {
  41. if (_border == null)
  42. {
  43. return;
  44. }
  45. _border.MaxHeight = 0.0;
  46. }
  47. protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
  48. {
  49. base.OnApplyTemplate(e);
  50. _border = e.NameScope.Get<Border>("PART_Border");
  51. if (e.NameScope.Get<ContentPresenter>("PART_AltDisplay") is { } contentControl)
  52. {
  53. if (Header is not null || Icon is not null)
  54. {
  55. contentControl.IsVisible = false;
  56. }
  57. }
  58. }
  59. protected override void OnPointerPressed(PointerPressedEventArgs e)
  60. {
  61. base.OnPointerPressed(e);
  62. if (e.Handled)
  63. return;
  64. if (!e.Handled && ItemsControl.ItemsControlFromItemContainer(this) is SukiSideMenu owner)
  65. {
  66. var p = e.GetCurrentPoint(this);
  67. if (p.Properties.PointerUpdateKind is PointerUpdateKind.LeftButtonPressed or
  68. PointerUpdateKind.RightButtonPressed)
  69. {
  70. if (p.Pointer.Type == PointerType.Mouse)
  71. {
  72. // If the pressed point comes from a mouse, perform the selection immediately.
  73. e.Handled = owner.UpdateSelectionFromPointerEvent(this);
  74. }
  75. }
  76. }
  77. }
  78. public static readonly StyledProperty<bool> IsContentMovableProperty =
  79. AvaloniaProperty.Register<SukiSideMenuItem, bool>(nameof(IsContentMovable), defaultValue: true);
  80. public bool IsContentMovable
  81. {
  82. get => GetValue(IsContentMovableProperty);
  83. set => SetValue(IsContentMovableProperty, value);
  84. }
  85. public static readonly StyledProperty<bool> IsTopMenuExpandedProperty =
  86. AvaloniaProperty.Register<SukiSideMenuItem, bool>(nameof(IsTopMenuExpanded), defaultValue: true);
  87. public bool IsTopMenuExpanded
  88. {
  89. get => GetValue(IsTopMenuExpandedProperty);
  90. set => SetValue(IsTopMenuExpandedProperty, value);
  91. }
  92. }