|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Avalonia; |
| 4 | +using Avalonia.Animation.Easings; |
| 5 | +using Avalonia.Automation; |
| 6 | +using Avalonia.Controls; |
| 7 | +using Avalonia.Input; |
| 8 | +using Avalonia.Layout; |
| 9 | +using Avalonia.Media; |
| 10 | +using Avalonia.Metadata; |
| 11 | + |
| 12 | +namespace AvaloniaDemo.Components.UI; |
| 13 | + |
| 14 | +// Dialog panel — backdrop + centered box: max-w-lg rounded-lg border bg-background p-6 |
| 15 | +// shadow-lg, close button top-right; fades and zooms in from 95%. XAML children go to Items. |
| 16 | +public class DialogContent : Border, IShellOverlayContent |
| 17 | +{ |
| 18 | + public static readonly StyledProperty<bool> ShowCloseButtonProperty = |
| 19 | + AvaloniaProperty.Register<DialogContent, bool>(nameof(ShowCloseButton), true); |
| 20 | + |
| 21 | + private readonly Border _backdrop; |
| 22 | + private readonly Border _box; |
| 23 | + private readonly Border _close; |
| 24 | + private readonly StackPanel _body = new() { Spacing = 16 }; |
| 25 | + |
| 26 | + static DialogContent() |
| 27 | + { |
| 28 | + ShowCloseButtonProperty.Changed.AddClassHandler<DialogContent>((c, e) => c._close.IsVisible = (bool)e.NewValue!); |
| 29 | + } |
| 30 | + |
| 31 | + public bool ShowCloseButton |
| 32 | + { |
| 33 | + get => GetValue(ShowCloseButtonProperty); |
| 34 | + set => SetValue(ShowCloseButtonProperty, value); |
| 35 | + } |
| 36 | + |
| 37 | + [Content] |
| 38 | + public Controls Items => _body.Children; |
| 39 | + |
| 40 | + public DialogContent() |
| 41 | + { |
| 42 | + _backdrop = new Border(); |
| 43 | + _backdrop.Token(BackgroundProperty, ShellToken.Overlay); |
| 44 | + _backdrop.PointerPressed += (_, e) => |
| 45 | + { |
| 46 | + this.FindParentOfType<Dialog>()?.SetOpen(false); |
| 47 | + e.Handled = true; |
| 48 | + }; |
| 49 | + |
| 50 | + _close = new Border |
| 51 | + { |
| 52 | + Child = new Icon { Kind = IconName.X, Size = 16, Token = ShellToken.MutedForeground }, |
| 53 | + Width = 24, |
| 54 | + Height = 24, |
| 55 | + CornerRadius = new CornerRadius(ShellTheme.RadiusSm), |
| 56 | + Background = Brushes.Transparent, |
| 57 | + HorizontalAlignment = HorizontalAlignment.Right, |
| 58 | + VerticalAlignment = VerticalAlignment.Top, |
| 59 | + Margin = new Thickness(0, -8, -8, 0), |
| 60 | + Cursor = new Cursor(StandardCursorType.Hand) |
| 61 | + }; |
| 62 | + AutomationProperties.SetName(_close, "Close"); |
| 63 | + _close.PointerEntered += (_, _) => _close.Token(BackgroundProperty, ShellToken.Accent); |
| 64 | + _close.PointerExited += (_, _) => { _close.ClearToken(BackgroundProperty); _close.Background = Brushes.Transparent; }; |
| 65 | + _close.PointerReleased += (_, e) => |
| 66 | + { |
| 67 | + this.FindParentOfType<Dialog>()?.SetOpen(false); |
| 68 | + e.Handled = true; |
| 69 | + }; |
| 70 | + |
| 71 | + _box = new Border |
| 72 | + { |
| 73 | + Child = new Panel { Children = { _body, _close } }, |
| 74 | + Padding = new Thickness(24), |
| 75 | + BorderThickness = new Thickness(1), |
| 76 | + CornerRadius = new CornerRadius(ShellTheme.RadiusLg), |
| 77 | + HorizontalAlignment = HorizontalAlignment.Center, |
| 78 | + VerticalAlignment = VerticalAlignment.Center, |
| 79 | + BoxShadow = new BoxShadows(new BoxShadow { OffsetY = 10, Blur = 24, Color = ShellTheme.Shadow(0.2) }) |
| 80 | + }; |
| 81 | + _box.Token(BackgroundProperty, ShellToken.Background).Token(BorderBrushProperty, ShellToken.Border); |
| 82 | + // Clicks on the box must not reach the backdrop behind it. |
| 83 | + _box.PointerPressed += (_, e) => e.Handled = true; |
| 84 | + |
| 85 | + Child = new Panel { Children = { _backdrop, _box } }; |
| 86 | + SizeChanged += (_, e) => _box.Width = Math.Max(0, Math.Min(512, e.NewSize.Width - 32)); |
| 87 | + } |
| 88 | + |
| 89 | + public async Task AnimateAsync(bool open) |
| 90 | + { |
| 91 | + if (open) |
| 92 | + { |
| 93 | + ShellMotion.Set(_backdrop, 0, "scale(1)"); |
| 94 | + ShellMotion.Set(_box, 0, "scale(0.95)"); |
| 95 | + await Task.WhenAll( |
| 96 | + ShellMotion.To(_backdrop, 1, "scale(1)", 150, new CubicEaseOut()), |
| 97 | + ShellMotion.To(_box, 1, "scale(1)", 150, new CubicEaseOut())); |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + await Task.WhenAll( |
| 102 | + ShellMotion.To(_backdrop, 0, "scale(1)", 120, new CubicEaseIn()), |
| 103 | + ShellMotion.To(_box, 0, "scale(0.95)", 120, new CubicEaseIn())); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments