forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffView.axaml.cs
More file actions
169 lines (147 loc) · 5.26 KB
/
Copy pathDiffView.axaml.cs
File metadata and controls
169 lines (147 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Styling;
namespace SourceGit.Views
{
public class ImageDiffView : Control
{
public static readonly StyledProperty<double> AlphaProperty =
AvaloniaProperty.Register<ImageDiffView, double>(nameof(Alpha), 0.5);
public double Alpha
{
get => GetValue(AlphaProperty);
set => SetValue(AlphaProperty, value);
}
public static readonly StyledProperty<Bitmap> OldImageProperty =
AvaloniaProperty.Register<ImageDiffView, Bitmap>(nameof(OldImage), null);
public Bitmap OldImage
{
get => GetValue(OldImageProperty);
set => SetValue(OldImageProperty, value);
}
public static readonly StyledProperty<Bitmap> NewImageProperty =
AvaloniaProperty.Register<ImageDiffView, Bitmap>(nameof(NewImage), null);
public Bitmap NewImage
{
get => GetValue(NewImageProperty);
set => SetValue(NewImageProperty, value);
}
static ImageDiffView()
{
AffectsMeasure<ImageDiffView>(OldImageProperty, NewImageProperty);
AffectsRender<ImageDiffView>(AlphaProperty);
}
public override void Render(DrawingContext context)
{
if (_bgBrush == null)
{
var maskBrush = new SolidColorBrush(ActualThemeVariant == ThemeVariant.Dark ? 0xFF404040 : 0xFFBBBBBB);
var bg = new DrawingGroup()
{
Children =
{
new GeometryDrawing() { Brush = maskBrush, Geometry = new RectangleGeometry(new Rect(0, 0, 12, 12)) },
new GeometryDrawing() { Brush = maskBrush, Geometry = new RectangleGeometry(new Rect(12, 12, 12, 12)) },
}
};
_bgBrush = new DrawingBrush(bg)
{
AlignmentX = AlignmentX.Left,
AlignmentY = AlignmentY.Top,
DestinationRect = new RelativeRect(new Size(24, 24), RelativeUnit.Absolute),
Stretch = Stretch.None,
TileMode = TileMode.Tile,
};
}
context.FillRectangle(_bgBrush, new Rect(Bounds.Size));
var alpha = Alpha;
var w = Bounds.Width - 16;
var h = Bounds.Height - 16;
var x = w * alpha;
var left = OldImage;
if (left != null && alpha > 0)
{
var src = new Rect(0, 0, left.Size.Width * alpha, left.Size.Height);
var dst = new Rect(8, 8, x, h);
context.DrawImage(left, src, dst);
}
var right = NewImage;
if (right != null && alpha < 1)
{
var src = new Rect(right.Size.Width * alpha, 0, right.Size.Width * (1 - alpha), right.Size.Height);
var dst = new Rect(x + 8, 8, w - x, h);
context.DrawImage(right, src, dst);
}
context.DrawLine(new Pen(Brushes.DarkGreen, 2), new Point(x + 8, 0), new Point(x + 8, Bounds.Height));
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property.Name == "ActualThemeVariant")
{
_bgBrush = null;
InvalidateVisual();
}
}
protected override Size MeasureOverride(Size availableSize)
{
var left = OldImage;
var right = NewImage;
if (left != null)
{
var lSize = GetDesiredSize(left.Size, availableSize);
if (right != null)
{
var rSize = GetDesiredSize(right.Size, availableSize);
if (rSize.Width > lSize.Width)
return rSize;
return lSize;
}
else
{
return lSize;
}
}
else if (right != null)
{
return GetDesiredSize(right.Size, availableSize);
}
else
{
return availableSize;
}
}
private Size GetDesiredSize(Size img, Size available)
{
var w = available.Width - 16;
var h = available.Height - 16;
if (img.Width <= w)
{
if (img.Height <= h)
{
return new Size(img.Width + 16, img.Height + 16);
}
else
{
return new Size(h * img.Width / img.Height + 16, available.Height);
}
}
else
{
var s = Math.Max(img.Width / w, img.Height / h);
return new Size(img.Width / s + 16, img.Height / s + 16);
}
}
private DrawingBrush _bgBrush = null;
}
public partial class DiffView : UserControl
{
public DiffView()
{
InitializeComponent();
}
}
}