GlassCard.axaml.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows.Input;
  4. using Avalonia;
  5. using Avalonia.Controls;
  6. using Avalonia.Controls.Presenters;
  7. using Avalonia.Controls.Primitives;
  8. using Avalonia.Input;
  9. using Avalonia.Interactivity;
  10. using Avalonia.LogicalTree;
  11. using Avalonia.Rendering.Composition;
  12. using Avalonia.Rendering.Composition.Animations;
  13. using SukiUI.Helpers;
  14. namespace SukiUI.Controls;
  15. public class GlassCard : ContentControl
  16. {
  17. public new static readonly StyledProperty<CornerRadius> CornerRadiusProperty =
  18. AvaloniaProperty.Register<GlassCard, CornerRadius>(nameof(CornerRadius), new CornerRadius(20));
  19. public new CornerRadius CornerRadius
  20. {
  21. get => GetValue(CornerRadiusProperty);
  22. set => SetValue(CornerRadiusProperty, value);
  23. }
  24. public new static readonly StyledProperty<Thickness> BorderThicknessProperty =
  25. AvaloniaProperty.Register<GlassCard, Thickness>(nameof(BorderThickness), new Thickness(1));
  26. public new Thickness BorderThickness
  27. {
  28. get => GetValue(BorderThicknessProperty);
  29. set => SetValue(BorderThicknessProperty, value);
  30. }
  31. public static readonly StyledProperty<bool> IsAnimatedProperty =
  32. AvaloniaProperty.Register<GlassCard, bool>(nameof(IsAnimated), true);
  33. public bool IsAnimated
  34. {
  35. get => GetValue(IsAnimatedProperty);
  36. set => SetValue(IsAnimatedProperty, value);
  37. }
  38. public static readonly StyledProperty<bool> IsOpaqueProperty =
  39. AvaloniaProperty.Register<GlassCard, bool>(nameof(IsOpaque), false);
  40. public bool IsOpaque
  41. {
  42. get => GetValue(IsOpaqueProperty);
  43. set => SetValue(IsOpaqueProperty, value);
  44. }
  45. public static readonly StyledProperty<bool> IsInteractiveProperty = AvaloniaProperty.Register<GlassCard, bool>(nameof(IsInteractive));
  46. public bool IsInteractive
  47. {
  48. get => GetValue(IsInteractiveProperty);
  49. set => SetValue(IsInteractiveProperty, value);
  50. }
  51. public static readonly StyledProperty<ICommand?> CommandProperty = AvaloniaProperty.Register<GlassCard, ICommand?>(nameof(Command));
  52. public ICommand? Command
  53. {
  54. get => GetValue(CommandProperty);
  55. set => SetValue(CommandProperty, value);
  56. }
  57. public static readonly StyledProperty<object?> CommandParameterProperty = AvaloniaProperty.Register<GlassCard, object?>(nameof(CommandParameter));
  58. public object? CommandParameter
  59. {
  60. get => GetValue(CommandParameterProperty);
  61. set => SetValue(CommandParameterProperty, value);
  62. }
  63. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  64. {
  65. base.OnAttachedToVisualTree(e);
  66. if (ContextMenu is null) return;
  67. ContextMenu.Opening += ContextMenuOnOpening;
  68. }
  69. protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
  70. {
  71. base.OnApplyTemplate(e);
  72. if (IsAnimated)
  73. {
  74. var b = e.NameScope.Get<Panel>("RootPanel");
  75. b.Loaded += (sender, args) =>
  76. {
  77. var v = ElementComposition.GetElementVisual(b);
  78. CompositionAnimationHelper.MakeOpacityAnimated(v);
  79. };
  80. var b2 = e.NameScope.Get<Border>("PART_BorderCard");
  81. b2.Loaded += (sender, args) =>
  82. {
  83. var v = ElementComposition.GetElementVisual(b2);
  84. CompositionAnimationHelper.MakeSizeAnimated(v);
  85. };
  86. var b3 = e.NameScope.Get<Border>("PART_ClipBorder");
  87. b3.Loaded += (sender, args) =>
  88. {
  89. var v = ElementComposition.GetElementVisual(b3);
  90. CompositionAnimationHelper.MakeSizeAnimated(v);
  91. };
  92. }
  93. }
  94. private void ContextMenuOnOpening(object sender, CancelEventArgs e)
  95. {
  96. PseudoClasses.Set(":pointerdown", false);
  97. }
  98. protected override void OnPointerPressed(PointerPressedEventArgs e)
  99. {
  100. base.OnPointerPressed(e);
  101. PseudoClasses.Set(":pointerdown", true);
  102. if(IsInteractive && Command is not null && Command.CanExecute(CommandParameter))
  103. Command.Execute(CommandParameter);
  104. }
  105. protected override void OnPointerReleased(PointerReleasedEventArgs e)
  106. {
  107. base.OnPointerReleased(e);
  108. PseudoClasses.Set(":pointerdown", false);
  109. }
  110. }