|
| 1 | +using Avalonia; |
| 2 | +using Avalonia.Controls; |
| 3 | +using Avalonia.Controls.Metadata; |
| 4 | +using Avalonia.Controls.Primitives; |
| 5 | +using Avalonia.Input; |
| 6 | +using Avalonia.Interactivity; |
| 7 | + |
| 8 | +namespace HandyControl.Controls; |
| 9 | + |
| 10 | +[TemplatePart(PartCloseButton, typeof(Button))] |
| 11 | +public class Tag : HeaderedContentControl |
| 12 | +{ |
| 13 | + public const string PartCloseButton = "PART_CloseButton"; |
| 14 | + |
| 15 | + private Button? _closeButton; |
| 16 | + |
| 17 | + public static readonly StyledProperty<bool> ShowCloseButtonProperty = |
| 18 | + AvaloniaProperty.Register<Tag, bool>(nameof(ShowCloseButton), true); |
| 19 | + |
| 20 | + public bool ShowCloseButton |
| 21 | + { |
| 22 | + get => GetValue(ShowCloseButtonProperty); |
| 23 | + set => SetValue(ShowCloseButtonProperty, value); |
| 24 | + } |
| 25 | + |
| 26 | + public static readonly StyledProperty<bool> SelectableProperty = |
| 27 | + AvaloniaProperty.Register<Tag, bool>(nameof(Selectable)); |
| 28 | + |
| 29 | + public bool Selectable |
| 30 | + { |
| 31 | + get => GetValue(SelectableProperty); |
| 32 | + set => SetValue(SelectableProperty, value); |
| 33 | + } |
| 34 | + |
| 35 | + public static readonly StyledProperty<bool> IsSelectedProperty = |
| 36 | + AvaloniaProperty.Register<Tag, bool>(nameof(IsSelected)); |
| 37 | + |
| 38 | + public bool IsSelected |
| 39 | + { |
| 40 | + get => GetValue(IsSelectedProperty); |
| 41 | + set => SetValue(IsSelectedProperty, value); |
| 42 | + } |
| 43 | + |
| 44 | + public static readonly RoutedEvent<RoutedEventArgs> SelectedEvent = |
| 45 | + RoutedEvent.Register<Tag, RoutedEventArgs>(nameof(Selected), RoutingStrategies.Bubble); |
| 46 | + |
| 47 | + public event System.EventHandler<RoutedEventArgs> Selected |
| 48 | + { |
| 49 | + add => AddHandler(SelectedEvent, value); |
| 50 | + remove => RemoveHandler(SelectedEvent, value); |
| 51 | + } |
| 52 | + |
| 53 | + public static readonly RoutedEvent<CancelRoutedEventArgs> ClosingEvent = |
| 54 | + RoutedEvent.Register<Tag, CancelRoutedEventArgs>(nameof(Closing), RoutingStrategies.Bubble); |
| 55 | + |
| 56 | + public event System.EventHandler<CancelRoutedEventArgs> Closing |
| 57 | + { |
| 58 | + add => AddHandler(ClosingEvent, value); |
| 59 | + remove => RemoveHandler(ClosingEvent, value); |
| 60 | + } |
| 61 | + |
| 62 | + public static readonly RoutedEvent<RoutedEventArgs> ClosedEvent = |
| 63 | + RoutedEvent.Register<Tag, RoutedEventArgs>(nameof(Closed), RoutingStrategies.Bubble); |
| 64 | + |
| 65 | + public event System.EventHandler<RoutedEventArgs> Closed |
| 66 | + { |
| 67 | + add => AddHandler(ClosedEvent, value); |
| 68 | + remove => RemoveHandler(ClosedEvent, value); |
| 69 | + } |
| 70 | + |
| 71 | + static Tag() |
| 72 | + { |
| 73 | + IsSelectedProperty.Changed.AddClassHandler<Tag>((tag, e) => tag.OnIsSelectedChanged(e)); |
| 74 | + } |
| 75 | + |
| 76 | + private void OnIsSelectedChanged(AvaloniaPropertyChangedEventArgs e) |
| 77 | + { |
| 78 | + var newValue = e.GetNewValue<bool>(); |
| 79 | + PseudoClasses.Set(":selected", newValue); |
| 80 | + RaiseEvent(new RoutedEventArgs(SelectedEvent, this)); |
| 81 | + } |
| 82 | + |
| 83 | + protected override void OnApplyTemplate(TemplateAppliedEventArgs e) |
| 84 | + { |
| 85 | + base.OnApplyTemplate(e); |
| 86 | + |
| 87 | + if (_closeButton != null) |
| 88 | + { |
| 89 | + _closeButton.Click -= CloseButton_Click; |
| 90 | + } |
| 91 | + |
| 92 | + _closeButton = e.NameScope.Find<Button>(PartCloseButton); |
| 93 | + |
| 94 | + if (_closeButton != null) |
| 95 | + { |
| 96 | + _closeButton.Click += CloseButton_Click; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private void CloseButton_Click(object? sender, RoutedEventArgs e) |
| 101 | + { |
| 102 | + RequestClose(); |
| 103 | + } |
| 104 | + |
| 105 | + internal void RequestClose() |
| 106 | + { |
| 107 | + var argsClosing = new CancelRoutedEventArgs(ClosingEvent, this); |
| 108 | + RaiseEvent(argsClosing); |
| 109 | + if (argsClosing.Cancel) |
| 110 | + { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + RaiseEvent(new RoutedEventArgs(ClosedEvent, this)); |
| 115 | + } |
| 116 | + |
| 117 | + protected override void OnPointerPressed(PointerPressedEventArgs e) |
| 118 | + { |
| 119 | + base.OnPointerPressed(e); |
| 120 | + |
| 121 | + if (Selectable && e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) |
| 122 | + { |
| 123 | + IsSelected = true; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public void Hide() |
| 128 | + { |
| 129 | + IsVisible = false; |
| 130 | + } |
| 131 | +} |
0 commit comments