|
| 1 | +using System; |
| 2 | + |
| 3 | +using Avalonia; |
| 4 | +using Avalonia.Controls; |
| 5 | +using Avalonia.Media; |
| 6 | + |
| 7 | +namespace SourceGit.Views |
| 8 | +{ |
| 9 | + public class BisectStateIndicator : Control |
| 10 | + { |
| 11 | + public static readonly StyledProperty<IBrush> BackgroundProperty = |
| 12 | + AvaloniaProperty.Register<BisectStateIndicator, IBrush>(nameof(Background), Brushes.Transparent); |
| 13 | + |
| 14 | + public IBrush Background |
| 15 | + { |
| 16 | + get => GetValue(BackgroundProperty); |
| 17 | + set => SetValue(BackgroundProperty, value); |
| 18 | + } |
| 19 | + |
| 20 | + public static readonly StyledProperty<IBrush> ForegroundProperty = |
| 21 | + AvaloniaProperty.Register<BisectStateIndicator, IBrush>(nameof(Foreground), Brushes.White); |
| 22 | + |
| 23 | + public IBrush Foreground |
| 24 | + { |
| 25 | + get => GetValue(ForegroundProperty); |
| 26 | + set => SetValue(ForegroundProperty, value); |
| 27 | + } |
| 28 | + |
| 29 | + public static readonly StyledProperty<Models.Bisect> BisectProperty = |
| 30 | + AvaloniaProperty.Register<BisectStateIndicator, Models.Bisect>(nameof(Bisect)); |
| 31 | + |
| 32 | + public Models.Bisect Bisect |
| 33 | + { |
| 34 | + get => GetValue(BisectProperty); |
| 35 | + set => SetValue(BisectProperty, value); |
| 36 | + } |
| 37 | + |
| 38 | + static BisectStateIndicator() |
| 39 | + { |
| 40 | + AffectsMeasure<BisectStateIndicator>(BisectProperty); |
| 41 | + AffectsRender<BisectStateIndicator>(BackgroundProperty, ForegroundProperty); |
| 42 | + } |
| 43 | + |
| 44 | + public override void Render(DrawingContext context) |
| 45 | + { |
| 46 | + if (_flags == Models.BisectCommitFlag.None) |
| 47 | + return; |
| 48 | + |
| 49 | + if (_prefix == null) |
| 50 | + { |
| 51 | + _prefix = LoadIcon("Icons.Bisect"); |
| 52 | + _good = LoadIcon("Icons.Check"); |
| 53 | + _bad = LoadIcon("Icons.Bad"); |
| 54 | + } |
| 55 | + |
| 56 | + var x = 0.0; |
| 57 | + |
| 58 | + if (_flags.HasFlag(Models.BisectCommitFlag.Good)) |
| 59 | + { |
| 60 | + RenderImpl(context, Brushes.Green, _good, x); |
| 61 | + x += 36; |
| 62 | + } |
| 63 | + |
| 64 | + if (_flags.HasFlag(Models.BisectCommitFlag.Bad)) |
| 65 | + RenderImpl(context, Brushes.Red, _bad, x); |
| 66 | + } |
| 67 | + |
| 68 | + protected override Size MeasureOverride(Size availableSize) |
| 69 | + { |
| 70 | + var desiredFlags = Models.BisectCommitFlag.None; |
| 71 | + var desiredWidth = 0.0; |
| 72 | + if (Bisect is { } bisect && DataContext is Models.Commit commit) |
| 73 | + { |
| 74 | + var sha = commit.SHA; |
| 75 | + if (bisect.Goods.Contains(sha)) |
| 76 | + { |
| 77 | + desiredFlags |= Models.BisectCommitFlag.Good; |
| 78 | + desiredWidth = 36; |
| 79 | + } |
| 80 | + |
| 81 | + if (bisect.Bads.Contains(sha)) |
| 82 | + { |
| 83 | + desiredFlags |= Models.BisectCommitFlag.Bad; |
| 84 | + desiredWidth += 36; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (desiredFlags != _flags) |
| 89 | + { |
| 90 | + _flags = desiredFlags; |
| 91 | + InvalidateVisual(); |
| 92 | + } |
| 93 | + |
| 94 | + return new Size(desiredWidth, desiredWidth > 0 ? 16 : 0); |
| 95 | + } |
| 96 | + |
| 97 | + private Geometry LoadIcon(string key) |
| 98 | + { |
| 99 | + var geo = this.FindResource(key) as StreamGeometry; |
| 100 | + var drawGeo = geo!.Clone(); |
| 101 | + var iconBounds = drawGeo.Bounds; |
| 102 | + var translation = Matrix.CreateTranslation(-(Vector)iconBounds.Position); |
| 103 | + var scale = Math.Min(10.0 / iconBounds.Width, 10.0 / iconBounds.Height); |
| 104 | + var transform = translation * Matrix.CreateScale(scale, scale); |
| 105 | + if (drawGeo.Transform == null || drawGeo.Transform.Value == Matrix.Identity) |
| 106 | + drawGeo.Transform = new MatrixTransform(transform); |
| 107 | + else |
| 108 | + drawGeo.Transform = new MatrixTransform(drawGeo.Transform.Value * transform); |
| 109 | + |
| 110 | + return drawGeo; |
| 111 | + } |
| 112 | + |
| 113 | + private void RenderImpl(DrawingContext context, IBrush brush, Geometry icon, double x) |
| 114 | + { |
| 115 | + var entireRect = new RoundedRect(new Rect(x, 0, 32, 16), new CornerRadius(2)); |
| 116 | + var stateRect = new RoundedRect(new Rect(x + 16, 0, 16, 16), new CornerRadius(0, 2, 2, 0)); |
| 117 | + context.DrawRectangle(Background, new Pen(brush), entireRect); |
| 118 | + using (context.PushOpacity(.2)) |
| 119 | + context.DrawRectangle(brush, null, stateRect); |
| 120 | + context.DrawLine(new Pen(brush), new Point(x + 16, 0), new Point(x + 16, 16)); |
| 121 | + |
| 122 | + using (context.PushTransform(Matrix.CreateTranslation(x + 3, 3))) |
| 123 | + context.DrawGeometry(Foreground, null, _prefix); |
| 124 | + |
| 125 | + using (context.PushTransform(Matrix.CreateTranslation(x + 19, 3))) |
| 126 | + context.DrawGeometry(Foreground, null, icon); |
| 127 | + } |
| 128 | + |
| 129 | + private Geometry _prefix = null; |
| 130 | + private Geometry _good = null; |
| 131 | + private Geometry _bad = null; |
| 132 | + private Models.BisectCommitFlag _flags = Models.BisectCommitFlag.None; |
| 133 | + } |
| 134 | +} |
0 commit comments