|
| 1 | +using Avalonia; |
| 2 | +using Avalonia.Controls; |
| 3 | +using Avalonia.Controls.Shapes; |
| 4 | +using Avalonia.Media; |
| 5 | + |
| 6 | +namespace SourceGit.Views |
| 7 | +{ |
| 8 | + public class HistoryFilterIcon : ContentControl |
| 9 | + { |
| 10 | + public static readonly DirectProperty<HistoryFilterIcon, Models.HistoryFilter> FilterProperty = |
| 11 | + AvaloniaProperty.RegisterDirect<HistoryFilterIcon, Models.HistoryFilter>( |
| 12 | + nameof(Filter), |
| 13 | + static o => o.Filter, |
| 14 | + static (o, v) => o.Filter = v); |
| 15 | + |
| 16 | + public Models.HistoryFilter Filter |
| 17 | + { |
| 18 | + get => _filter; |
| 19 | + set => SetAndRaise(FilterProperty, ref _filter, value); |
| 20 | + } |
| 21 | + |
| 22 | + public static readonly DirectProperty<HistoryFilterIcon, bool> IsFilterValidProperty = |
| 23 | + AvaloniaProperty.RegisterDirect<HistoryFilterIcon, bool>( |
| 24 | + nameof(IsFilterValid), |
| 25 | + static o => o.IsFilterValid, |
| 26 | + static (o, v) => o.IsFilterValid = v); |
| 27 | + |
| 28 | + public bool IsFilterValid |
| 29 | + { |
| 30 | + get => _isFilterValid; |
| 31 | + set => SetAndRaise(IsFilterValidProperty, ref _isFilterValid, value); |
| 32 | + } |
| 33 | + |
| 34 | + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) |
| 35 | + { |
| 36 | + base.OnPropertyChanged(change); |
| 37 | + |
| 38 | + if (change.Property == FilterProperty || change.Property == IsFilterValidProperty) |
| 39 | + UpdateContent(); |
| 40 | + } |
| 41 | + |
| 42 | + private void UpdateContent() |
| 43 | + { |
| 44 | + if (_filter == null) |
| 45 | + { |
| 46 | + Content = null; |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + if (_filter.Type is Models.FilterType.LocalBranch or Models.FilterType.RemoteBranch) |
| 51 | + { |
| 52 | + Padding = new Thickness(0); |
| 53 | + |
| 54 | + if (_isFilterValid) |
| 55 | + CreateContent("Icons.Branch"); |
| 56 | + else |
| 57 | + CreateContent("Icons.Error", Brushes.DarkOrange); |
| 58 | + } |
| 59 | + else if (_filter.Type is Models.FilterType.Tag) |
| 60 | + { |
| 61 | + Padding = new Thickness(0); |
| 62 | + |
| 63 | + if (_isFilterValid) |
| 64 | + CreateContent("Icons.Tag"); |
| 65 | + else |
| 66 | + CreateContent("Icons.Error", Brushes.DarkOrange); |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + Padding = new Thickness(0, 1, 0, 0); |
| 71 | + CreateContent("Icons.Folder"); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private void CreateContent(string iconKey, IBrush fill = null) |
| 76 | + { |
| 77 | + if (this.FindResource(iconKey) is not StreamGeometry geo) |
| 78 | + return; |
| 79 | + |
| 80 | + var path = new Path() |
| 81 | + { |
| 82 | + Width = 10, |
| 83 | + Height = 10, |
| 84 | + Data = geo, |
| 85 | + }; |
| 86 | + |
| 87 | + if (fill != null) |
| 88 | + path.Fill = fill; |
| 89 | + |
| 90 | + Content = path; |
| 91 | + } |
| 92 | + |
| 93 | + private Models.HistoryFilter _filter = null; |
| 94 | + private bool _isFilterValid = true; |
| 95 | + } |
| 96 | +} |
0 commit comments