|
5 | 5 | using Avalonia.Controls.Primitives; |
6 | 6 | using Avalonia.Interactivity; |
7 | 7 | using Avalonia.Media; |
| 8 | +using Avalonia.Media.Imaging; |
| 9 | +using Avalonia.Styling; |
8 | 10 |
|
9 | 11 | using AvaloniaEdit; |
10 | 12 | using AvaloniaEdit.Document; |
|
13 | 15 |
|
14 | 16 | namespace SourceGit.Views |
15 | 17 | { |
| 18 | + public class RevisionImageFileView : Control |
| 19 | + { |
| 20 | + public static readonly StyledProperty<Bitmap> SourceProperty = |
| 21 | + AvaloniaProperty.Register<ImageDiffView, Bitmap>(nameof(Source), null); |
| 22 | + |
| 23 | + public Bitmap Source |
| 24 | + { |
| 25 | + get => GetValue(SourceProperty); |
| 26 | + set => SetValue(SourceProperty, value); |
| 27 | + } |
| 28 | + |
| 29 | + static RevisionImageFileView() |
| 30 | + { |
| 31 | + AffectsMeasure<RevisionImageFileView>(SourceProperty); |
| 32 | + } |
| 33 | + |
| 34 | + public override void Render(DrawingContext context) |
| 35 | + { |
| 36 | + base.Render(context); |
| 37 | + |
| 38 | + var bgMaskBrush = new SolidColorBrush(ActualThemeVariant == ThemeVariant.Dark ? 0xFF404040 : 0xFFBBBBBB); |
| 39 | + |
| 40 | + var bg = new DrawingGroup() |
| 41 | + { |
| 42 | + Children = |
| 43 | + { |
| 44 | + new GeometryDrawing() { Brush = bgMaskBrush, Geometry = new RectangleGeometry(new Rect(0, 0, 12, 12)) }, |
| 45 | + new GeometryDrawing() { Brush = bgMaskBrush, Geometry = new RectangleGeometry(new Rect(12, 12, 12, 12)) }, |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + var brushBG = new DrawingBrush(bg) |
| 50 | + { |
| 51 | + AlignmentX = AlignmentX.Left, |
| 52 | + AlignmentY = AlignmentY.Top, |
| 53 | + DestinationRect = new RelativeRect(new Size(24, 24), RelativeUnit.Absolute), |
| 54 | + Stretch = Stretch.None, |
| 55 | + TileMode = TileMode.Tile, |
| 56 | + }; |
| 57 | + |
| 58 | + context.FillRectangle(brushBG, new Rect(Bounds.Size)); |
| 59 | + |
| 60 | + var source = Source; |
| 61 | + if (source != null) |
| 62 | + { |
| 63 | + context.DrawImage(source, new Rect(source.Size), new Rect(8, 8, Bounds.Width - 16, Bounds.Height - 16)); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) |
| 68 | + { |
| 69 | + base.OnPropertyChanged(change); |
| 70 | + if (change.Property.Name == "ActualThemeVariant") InvalidateVisual(); |
| 71 | + } |
| 72 | + |
| 73 | + protected override Size MeasureOverride(Size availableSize) |
| 74 | + { |
| 75 | + var source = Source; |
| 76 | + if (source == null) |
| 77 | + { |
| 78 | + return availableSize; |
| 79 | + } |
| 80 | + |
| 81 | + var w = availableSize.Width - 16; |
| 82 | + var h = availableSize.Height - 16; |
| 83 | + var size = source.Size; |
| 84 | + if (size.Width <= w) |
| 85 | + { |
| 86 | + if (size.Height <= h) |
| 87 | + { |
| 88 | + return new Size(size.Width + 16, size.Height + 16); |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + return new Size(h * size.Width / size.Height + 16, availableSize.Height); |
| 93 | + } |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + var scale = Math.Max(size.Width / w, size.Height / h); |
| 98 | + return new Size(size.Width / scale + 16, size.Height / scale + 16); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
16 | 103 | public class RevisionTextFileView : TextEditor |
17 | 104 | { |
18 | 105 | protected override Type StyleKeyOverride => typeof(TextEditor); |
|
0 commit comments