|
| 1 | +using System; |
| 2 | + |
| 3 | +using Avalonia; |
| 4 | +using Avalonia.Controls; |
| 5 | +using Avalonia.Controls.Shapes; |
| 6 | +using Avalonia.Interactivity; |
| 7 | +using Avalonia.Media; |
| 8 | +using Avalonia.Threading; |
| 9 | + |
| 10 | +namespace SourceGit.Views |
| 11 | +{ |
| 12 | + public class CopyButton : Button |
| 13 | + { |
| 14 | + protected override Type StyleKeyOverride => typeof(Button); |
| 15 | + |
| 16 | + public static readonly DirectProperty<CopyButton, string> CopyTextProperty = |
| 17 | + AvaloniaProperty.RegisterDirect<CopyButton, string>( |
| 18 | + nameof(CopyText), |
| 19 | + static o => o.CopyText, |
| 20 | + static (o, v) => o.CopyText = v); |
| 21 | + |
| 22 | + public string CopyText |
| 23 | + { |
| 24 | + get => _copyText; |
| 25 | + set => SetAndRaise(CopyTextProperty, ref _copyText, value); |
| 26 | + } |
| 27 | + |
| 28 | + public static readonly DirectProperty<CopyButton, bool> IsCopiedProperty = |
| 29 | + AvaloniaProperty.RegisterDirect<CopyButton, bool>( |
| 30 | + nameof(IsCopied), |
| 31 | + static o => o.IsCopied); |
| 32 | + |
| 33 | + public bool IsCopied |
| 34 | + { |
| 35 | + get => _isCopied; |
| 36 | + private set => SetAndRaise(IsCopiedProperty, ref _isCopied, value); |
| 37 | + } |
| 38 | + |
| 39 | + public CopyButton() |
| 40 | + { |
| 41 | + Classes.Add("icon_button"); |
| 42 | + |
| 43 | + _copyIcon = new Path() { Width = 12, Height = 12 }; |
| 44 | + _checkIcon = new Path() |
| 45 | + { |
| 46 | + Width = 14, |
| 47 | + Height = 14, |
| 48 | + Margin = new Thickness(0, 2, 0, 0), |
| 49 | + Fill = Brushes.Green, |
| 50 | + IsVisible = false, |
| 51 | + }; |
| 52 | + |
| 53 | + var grid = new Grid(); |
| 54 | + grid.Children.Add(_copyIcon); |
| 55 | + grid.Children.Add(_checkIcon); |
| 56 | + Content = grid; |
| 57 | + } |
| 58 | + |
| 59 | + protected override void OnLoaded(RoutedEventArgs e) |
| 60 | + { |
| 61 | + base.OnLoaded(e); |
| 62 | + |
| 63 | + if (this.FindResource("Icons.Copy") is Geometry copyGeo) |
| 64 | + _copyIcon.Data = copyGeo; |
| 65 | + if (this.FindResource("Icons.Check") is Geometry checkGeo) |
| 66 | + _checkIcon.Data = checkGeo; |
| 67 | + |
| 68 | + _resetTimer = new DispatcherTimer(); |
| 69 | + _resetTimer.Interval = TimeSpan.FromSeconds(1); |
| 70 | + _resetTimer.Tag = this; |
| 71 | + _resetTimer.Tick += static (o, _) => |
| 72 | + { |
| 73 | + if (o is DispatcherTimer { Tag: CopyButton btn } timer) |
| 74 | + { |
| 75 | + btn.IsCopied = false; |
| 76 | + timer.IsEnabled = false; |
| 77 | + } |
| 78 | + }; |
| 79 | + _resetTimer.IsEnabled = false; |
| 80 | + } |
| 81 | + |
| 82 | + protected override void OnUnloaded(RoutedEventArgs e) |
| 83 | + { |
| 84 | + _resetTimer.Tag = null; |
| 85 | + _resetTimer.IsEnabled = false; |
| 86 | + base.OnUnloaded(e); |
| 87 | + } |
| 88 | + |
| 89 | + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) |
| 90 | + { |
| 91 | + base.OnPropertyChanged(change); |
| 92 | + |
| 93 | + if (change.Property == IsCopiedProperty) |
| 94 | + { |
| 95 | + _copyIcon.IsVisible = !_isCopied; |
| 96 | + _checkIcon.IsVisible = _isCopied; |
| 97 | + } |
| 98 | + else if (change.Property == CopyTextProperty) |
| 99 | + { |
| 100 | + // Reset the copied state when CopyText changes (e.g. switching to a different commit) |
| 101 | + IsCopied = false; |
| 102 | + _resetTimer?.Stop(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + protected override async void OnClick() |
| 107 | + { |
| 108 | + base.OnClick(); |
| 109 | + |
| 110 | + var text = CopyText; |
| 111 | + if (string.IsNullOrEmpty(text)) |
| 112 | + return; |
| 113 | + |
| 114 | + await this.CopyTextAsync(text); |
| 115 | + IsCopied = true; |
| 116 | + _resetTimer?.Start(); |
| 117 | + } |
| 118 | + |
| 119 | + private readonly Path _copyIcon; |
| 120 | + private readonly Path _checkIcon; |
| 121 | + private string _copyText = string.Empty; |
| 122 | + private bool _isCopied = false; |
| 123 | + private DispatcherTimer _resetTimer = null; |
| 124 | + } |
| 125 | +} |
0 commit comments