Skip to content

Commit c2eedbd

Browse files
committed
performance: do NOT re-create background brush if there exists one
1 parent eb4f38b commit c2eedbd

File tree

2 files changed

+66
-48
lines changed

2 files changed

+66
-48
lines changed

src/SourceGit/Views/DiffView.axaml.cs

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,53 +45,62 @@ static ImageDiffView()
4545

4646
public override void Render(DrawingContext context)
4747
{
48-
var alpha = Alpha;
49-
var bgMaskBrush = new SolidColorBrush(ActualThemeVariant == ThemeVariant.Dark ? 0xFF404040 : 0xFFBBBBBB);
50-
51-
var bg = new DrawingGroup()
48+
if (_bgBrush == null)
5249
{
53-
Children =
50+
var maskBrush = new SolidColorBrush(ActualThemeVariant == ThemeVariant.Dark ? 0xFF404040 : 0xFFBBBBBB);
51+
var bg = new DrawingGroup()
5452
{
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-
};
53+
Children =
54+
{
55+
new GeometryDrawing() { Brush = maskBrush, Geometry = new RectangleGeometry(new Rect(0, 0, 12, 12)) },
56+
new GeometryDrawing() { Brush = maskBrush, Geometry = new RectangleGeometry(new Rect(12, 12, 12, 12)) },
57+
}
58+
};
59+
60+
_bgBrush = 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+
}
6869

69-
context.FillRectangle(brushBG, new Rect(Bounds.Size));
70+
context.FillRectangle(_bgBrush, new Rect(Bounds.Size));
7071

72+
var alpha = Alpha;
73+
var w = Bounds.Width - 16;
74+
var h = Bounds.Height - 16;
75+
var x = w * alpha;
7176
var left = OldImage;
7277
if (left != null && alpha > 0)
7378
{
74-
var src = new Rect(0, 0, left.Size.Width * Alpha, left.Size.Height);
75-
var dst = new Rect(8, 8, (Bounds.Width - 16) * Alpha, Bounds.Height - 16);
79+
var src = new Rect(0, 0, left.Size.Width * alpha, left.Size.Height);
80+
var dst = new Rect(8, 8, x, h);
7681
context.DrawImage(left, src, dst);
7782
}
7883

7984
var right = NewImage;
80-
if (right != null)
85+
if (right != null && alpha < 1)
8186
{
82-
var src = new Rect(right.Size.Width * Alpha, 0, right.Size.Width - right.Size.Width * Alpha, right.Size.Height);
83-
var dst = new Rect((Bounds.Width - 16) * Alpha + 8, 8, (Bounds.Width - 16) * (1 - Alpha), Bounds.Height - 16);
87+
var src = new Rect(right.Size.Width * alpha, 0, right.Size.Width * (1 - alpha), right.Size.Height);
88+
var dst = new Rect(x + 8, 8, w - x, h);
8489
context.DrawImage(right, src, dst);
8590
}
8691

87-
var x = (Bounds.Width - 16) * Alpha + 8;
88-
context.DrawLine(new Pen(Brushes.DarkGreen, 2), new Point(x, 0), new Point(x, Bounds.Height));
92+
context.DrawLine(new Pen(Brushes.DarkGreen, 2), new Point(x + 8, 0), new Point(x + 8, Bounds.Height));
8993
}
9094

9195
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
9296
{
9397
base.OnPropertyChanged(change);
94-
if (change.Property.Name == "ActualThemeVariant") InvalidateVisual();
98+
99+
if (change.Property.Name == "ActualThemeVariant")
100+
{
101+
_bgBrush = null;
102+
InvalidateVisual();
103+
}
95104
}
96105

97106
protected override Size MeasureOverride(Size availableSize)
@@ -135,6 +144,8 @@ private Size GetDesiredSize(Size img, Size available)
135144
return new Size(img.Width / s + 16, img.Height / s + 16);
136145
}
137146
}
147+
148+
private DrawingBrush _bgBrush = null;
138149
}
139150

140151
public partial class DiffView : UserControl

src/SourceGit/Views/RevisionFiles.axaml.cs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,29 @@ static RevisionImageFileView()
3333

3434
public override void Render(DrawingContext context)
3535
{
36-
base.Render(context);
37-
38-
var bgMaskBrush = new SolidColorBrush(ActualThemeVariant == ThemeVariant.Dark ? 0xFF404040 : 0xFFBBBBBB);
39-
40-
var bg = new DrawingGroup()
36+
if (_bgBrush == null)
4137
{
42-
Children =
38+
var maskBrush = new SolidColorBrush(ActualThemeVariant == ThemeVariant.Dark ? 0xFF404040 : 0xFFBBBBBB);
39+
var bg = new DrawingGroup()
4340
{
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-
};
41+
Children =
42+
{
43+
new GeometryDrawing() { Brush = maskBrush, Geometry = new RectangleGeometry(new Rect(0, 0, 12, 12)) },
44+
new GeometryDrawing() { Brush = maskBrush, Geometry = new RectangleGeometry(new Rect(12, 12, 12, 12)) },
45+
}
46+
};
47+
48+
_bgBrush = new DrawingBrush(bg)
49+
{
50+
AlignmentX = AlignmentX.Left,
51+
AlignmentY = AlignmentY.Top,
52+
DestinationRect = new RelativeRect(new Size(24, 24), RelativeUnit.Absolute),
53+
Stretch = Stretch.None,
54+
TileMode = TileMode.Tile,
55+
};
56+
}
5757

58-
context.FillRectangle(brushBG, new Rect(Bounds.Size));
58+
context.FillRectangle(_bgBrush, new Rect(Bounds.Size));
5959

6060
var source = Source;
6161
if (source != null)
@@ -67,7 +67,12 @@ public override void Render(DrawingContext context)
6767
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
6868
{
6969
base.OnPropertyChanged(change);
70-
if (change.Property.Name == "ActualThemeVariant") InvalidateVisual();
70+
71+
if (change.Property.Name == "ActualThemeVariant")
72+
{
73+
_bgBrush = null;
74+
InvalidateVisual();
75+
}
7176
}
7277

7378
protected override Size MeasureOverride(Size availableSize)
@@ -98,6 +103,8 @@ protected override Size MeasureOverride(Size availableSize)
98103
return new Size(size.Width / scale + 16, size.Height / scale + 16);
99104
}
100105
}
106+
107+
private DrawingBrush _bgBrush = null;
101108
}
102109

103110
public class RevisionTextFileView : TextEditor

0 commit comments

Comments
 (0)