Skip to content

Commit effabb0

Browse files
committed
style: add a background for image diff view
1 parent 91bc1ee commit effabb0

File tree

2 files changed

+51
-18
lines changed

2 files changed

+51
-18
lines changed

src/SourceGit/Views/DiffView.axaml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@
111111

112112
<!-- Image Diff -->
113113
<DataTemplate DataType="m:ImageDiff">
114-
<Grid Margin="8,8,8,0" RowDefinitions="Auto,*,Auto" HorizontalAlignment="Center" VerticalAlignment="Center">
115-
<Grid Grid.Row="0" Margin="0,0,0,8" ColumnDefinitions="Auto,Auto,*,Auto,Auto">
114+
<Grid RowDefinitions="Auto,*,Auto" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="8,8,8,0">
115+
<Grid Grid.Row="0" ColumnDefinitions="Auto,Auto,*,Auto,Auto">
116116
<Border Grid.Column="0" Height="16" Background="{DynamicResource Brush.Badge}" CornerRadius="8" VerticalAlignment="Center">
117117
<TextBlock Classes="monospace" Text="{DynamicResource Text.Diff.Binary.Old}" Margin="8,0" FontSize="10"/>
118118
</Border>
@@ -126,19 +126,21 @@
126126
<TextBlock Grid.Column="4" Classes="monospace" Text="{Binding NewSize}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>
127127
</Grid>
128128

129-
<Border Grid.Row="1" BorderThickness="1" BorderBrush="{DynamicResource Brush.Border1}">
130-
<v:ImageDiffView Alpha="{Binding #ImageDiffSlider.Value}"
131-
OldImage="{Binding Old}"
132-
NewImage="{Binding New}"/>
129+
<Border Grid.Row="1" Background="{DynamicResource Brush.Window}" Effect="drop-shadow(0 0 8 #A0000000)" Margin="0,8,0,0">
130+
<Border BorderThickness="1" BorderBrush="{DynamicResource Brush.Border1}" Margin="8">
131+
<v:ImageDiffView Alpha="{Binding #ImageDiffSlider.Value}"
132+
OldImage="{Binding Old}"
133+
NewImage="{Binding New}"/>
134+
</Border>
133135
</Border>
134136

135137
<Slider Grid.Row="2"
136138
x:Name="ImageDiffSlider"
137139
Minimum="0" Maximum="1"
138140
VerticalAlignment="Top"
139-
TickPlacement="BottomRight"
140-
TickFrequency="0.1"
141+
TickPlacement="None"
141142
Margin="0,4,0,0"
143+
MinHeight="0"
142144
Foreground="{DynamicResource Brush.Border1}"
143145
Value="0.5">
144146
<Slider.Resources>

src/SourceGit/Views/DiffView.axaml.cs

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Avalonia.Controls;
55
using Avalonia.Media;
66
using Avalonia.Media.Imaging;
7+
using Avalonia.Styling;
78

89
namespace SourceGit.Views
910
{
@@ -45,27 +46,54 @@ static ImageDiffView()
4546
public override void Render(DrawingContext context)
4647
{
4748
var alpha = Alpha;
48-
var x = Bounds.Width * Alpha;
49+
var bgMaskBrush = new SolidColorBrush(ActualThemeVariant == ThemeVariant.Dark ? 0xFF404040 : 0xFFBBBBBB);
50+
51+
var bg = new DrawingGroup()
52+
{
53+
Children =
54+
{
55+
new GeometryDrawing() { Brush = bgMaskBrush, Geometry = new RectangleGeometry(new Rect(0, 0, 12, 12)) },
56+
new GeometryDrawing() { Brush = bgMaskBrush, Geometry = new RectangleGeometry(new Rect(12, 12, 12, 12)) },
57+
}
58+
};
59+
60+
var brushBG = new DrawingBrush(bg)
61+
{
62+
AlignmentX = AlignmentX.Left,
63+
AlignmentY = AlignmentY.Top,
64+
DestinationRect = new RelativeRect(new Size(24, 24), RelativeUnit.Absolute),
65+
Stretch = Stretch.None,
66+
TileMode = TileMode.Tile,
67+
};
68+
69+
context.FillRectangle(brushBG, new Rect(Bounds.Size));
4970

5071
var left = OldImage;
5172
if (left != null && alpha > 0)
5273
{
5374
var src = new Rect(0, 0, left.Size.Width * Alpha, left.Size.Height);
54-
var dst = new Rect(0, 0, x, Bounds.Height);
75+
var dst = new Rect(8, 8, (Bounds.Width - 16) * Alpha, Bounds.Height - 16);
5576
context.DrawImage(left, src, dst);
5677
}
5778

5879
var right = NewImage;
5980
if (right != null)
6081
{
6182
var src = new Rect(right.Size.Width * Alpha, 0, right.Size.Width - right.Size.Width * Alpha, right.Size.Height);
62-
var dst = new Rect(x, 0, Bounds.Width - x, Bounds.Height);
83+
var dst = new Rect((Bounds.Width - 16) * Alpha + 8, 8, (Bounds.Width - 16) * (1 - Alpha), Bounds.Height - 16);
6384
context.DrawImage(right, src, dst);
6485
}
65-
86+
87+
var x = (Bounds.Width - 16) * Alpha + 8;
6688
context.DrawLine(new Pen(Brushes.DarkGreen, 2), new Point(x, 0), new Point(x, Bounds.Height));
6789
}
6890

91+
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
92+
{
93+
base.OnPropertyChanged(change);
94+
if (change.Property.Name == "ActualThemeVariant") InvalidateVisual();
95+
}
96+
6997
protected override Size MeasureOverride(Size availableSize)
7098
{
7199
var left = OldImage;
@@ -87,21 +115,24 @@ protected override Size MeasureOverride(Size availableSize)
87115

88116
private Size GetDesiredSize(Size img, Size available)
89117
{
90-
if (img.Width <= available.Width)
118+
var w = available.Width - 16;
119+
var h = available.Height - 16;
120+
121+
if (img.Width <= w)
91122
{
92-
if (img.Height <= available.Height)
123+
if (img.Height <= h)
93124
{
94-
return img;
125+
return new Size(img.Width + 16, img.Height + 16);
95126
}
96127
else
97128
{
98-
return new Size(available.Height * img.Width / img.Height, available.Height);
129+
return new Size(h * img.Width / img.Height + 16, available.Height);
99130
}
100131
}
101132
else
102133
{
103-
var s = Math.Max(img.Width / available.Width, img.Height / available.Height);
104-
return new Size(img.Width / s, img.Height / s);
134+
var s = Math.Max(img.Width / w, img.Height / h);
135+
return new Size(img.Width / s + 16, img.Height / s + 16);
105136
}
106137
}
107138
}

0 commit comments

Comments
 (0)