Skip to content

Commit 760d64c

Browse files
committed
style: use custom RevisionImageFileView to preview images.
1 parent 4651f30 commit 760d64c

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

src/SourceGit/Views/RevisionFiles.axaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@
9292
</DataTemplate>
9393

9494
<DataTemplate DataType="m:RevisionImageFile">
95-
<Image Source="{Binding Image}" Margin="8"/>
95+
<Border Background="{DynamicResource Brush.Window}" Effect="drop-shadow(0 0 8 #A0000000)" Margin="0,8" VerticalAlignment="Center" HorizontalAlignment="Center">
96+
<Border BorderThickness="1" BorderBrush="{DynamicResource Brush.Border1}" Margin="8">
97+
<v:RevisionImageFileView Source="{Binding Image}"/>
98+
</Border>
99+
</Border>
96100
</DataTemplate>
97101

98102
<DataTemplate DataType="m:RevisionLFSObject">

src/SourceGit/Views/RevisionFiles.axaml.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using Avalonia.Controls.Primitives;
66
using Avalonia.Interactivity;
77
using Avalonia.Media;
8+
using Avalonia.Media.Imaging;
9+
using Avalonia.Styling;
810

911
using AvaloniaEdit;
1012
using AvaloniaEdit.Document;
@@ -13,6 +15,91 @@
1315

1416
namespace SourceGit.Views
1517
{
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+
16103
public class RevisionTextFileView : TextEditor
17104
{
18105
protected override Type StyleKeyOverride => typeof(TextEditor);

0 commit comments

Comments
 (0)