performance: do NOT re-create background brush if there exists one

This commit is contained in:
leo 2024-03-28 21:06:12 +08:00
parent eb4f38b676
commit c2eedbdbf2
2 changed files with 64 additions and 46 deletions

View file

@ -45,53 +45,62 @@ namespace SourceGit.Views
public override void Render(DrawingContext context) public override void Render(DrawingContext context)
{ {
var alpha = Alpha; if (_bgBrush == null)
var bgMaskBrush = new SolidColorBrush(ActualThemeVariant == ThemeVariant.Dark ? 0xFF404040 : 0xFFBBBBBB);
var bg = new DrawingGroup()
{ {
Children = var maskBrush = new SolidColorBrush(ActualThemeVariant == ThemeVariant.Dark ? 0xFF404040 : 0xFFBBBBBB);
var bg = new DrawingGroup()
{ {
new GeometryDrawing() { Brush = bgMaskBrush, Geometry = new RectangleGeometry(new Rect(0, 0, 12, 12)) }, Children =
new GeometryDrawing() { Brush = bgMaskBrush, Geometry = new RectangleGeometry(new Rect(12, 12, 12, 12)) }, {
} 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)) },
}
};
var brushBG = new DrawingBrush(bg) _bgBrush = new DrawingBrush(bg)
{ {
AlignmentX = AlignmentX.Left, AlignmentX = AlignmentX.Left,
AlignmentY = AlignmentY.Top, AlignmentY = AlignmentY.Top,
DestinationRect = new RelativeRect(new Size(24, 24), RelativeUnit.Absolute), DestinationRect = new RelativeRect(new Size(24, 24), RelativeUnit.Absolute),
Stretch = Stretch.None, Stretch = Stretch.None,
TileMode = TileMode.Tile, TileMode = TileMode.Tile,
}; };
}
context.FillRectangle(brushBG, new Rect(Bounds.Size)); 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; var left = OldImage;
if (left != null && alpha > 0) if (left != null && alpha > 0)
{ {
var src = new Rect(0, 0, left.Size.Width * Alpha, left.Size.Height); var src = new Rect(0, 0, left.Size.Width * alpha, left.Size.Height);
var dst = new Rect(8, 8, (Bounds.Width - 16) * Alpha, Bounds.Height - 16); var dst = new Rect(8, 8, x, h);
context.DrawImage(left, src, dst); context.DrawImage(left, src, dst);
} }
var right = NewImage; var right = NewImage;
if (right != null) if (right != null && alpha < 1)
{ {
var src = new Rect(right.Size.Width * Alpha, 0, right.Size.Width - right.Size.Width * Alpha, right.Size.Height); var src = new Rect(right.Size.Width * alpha, 0, right.Size.Width * (1 - alpha), right.Size.Height);
var dst = new Rect((Bounds.Width - 16) * Alpha + 8, 8, (Bounds.Width - 16) * (1 - Alpha), Bounds.Height - 16); var dst = new Rect(x + 8, 8, w - x, h);
context.DrawImage(right, src, dst); context.DrawImage(right, src, dst);
} }
var x = (Bounds.Width - 16) * Alpha + 8; context.DrawLine(new Pen(Brushes.DarkGreen, 2), new Point(x + 8, 0), new Point(x + 8, Bounds.Height));
context.DrawLine(new Pen(Brushes.DarkGreen, 2), new Point(x, 0), new Point(x, Bounds.Height));
} }
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{ {
base.OnPropertyChanged(change); base.OnPropertyChanged(change);
if (change.Property.Name == "ActualThemeVariant") InvalidateVisual();
if (change.Property.Name == "ActualThemeVariant")
{
_bgBrush = null;
InvalidateVisual();
}
} }
protected override Size MeasureOverride(Size availableSize) protected override Size MeasureOverride(Size availableSize)
@ -135,6 +144,8 @@ namespace SourceGit.Views
return new Size(img.Width / s + 16, img.Height / s + 16); return new Size(img.Width / s + 16, img.Height / s + 16);
} }
} }
private DrawingBrush _bgBrush = null;
} }
public partial class DiffView : UserControl public partial class DiffView : UserControl

View file

@ -33,29 +33,29 @@ namespace SourceGit.Views
public override void Render(DrawingContext context) public override void Render(DrawingContext context)
{ {
base.Render(context); if (_bgBrush == null)
var bgMaskBrush = new SolidColorBrush(ActualThemeVariant == ThemeVariant.Dark ? 0xFF404040 : 0xFFBBBBBB);
var bg = new DrawingGroup()
{ {
Children = var maskBrush = new SolidColorBrush(ActualThemeVariant == ThemeVariant.Dark ? 0xFF404040 : 0xFFBBBBBB);
var bg = new DrawingGroup()
{ {
new GeometryDrawing() { Brush = bgMaskBrush, Geometry = new RectangleGeometry(new Rect(0, 0, 12, 12)) }, Children =
new GeometryDrawing() { Brush = bgMaskBrush, Geometry = new RectangleGeometry(new Rect(12, 12, 12, 12)) }, {
} 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)) },
}
};
var brushBG = new DrawingBrush(bg) _bgBrush = new DrawingBrush(bg)
{ {
AlignmentX = AlignmentX.Left, AlignmentX = AlignmentX.Left,
AlignmentY = AlignmentY.Top, AlignmentY = AlignmentY.Top,
DestinationRect = new RelativeRect(new Size(24, 24), RelativeUnit.Absolute), DestinationRect = new RelativeRect(new Size(24, 24), RelativeUnit.Absolute),
Stretch = Stretch.None, Stretch = Stretch.None,
TileMode = TileMode.Tile, TileMode = TileMode.Tile,
}; };
}
context.FillRectangle(brushBG, new Rect(Bounds.Size)); context.FillRectangle(_bgBrush, new Rect(Bounds.Size));
var source = Source; var source = Source;
if (source != null) if (source != null)
@ -67,7 +67,12 @@ namespace SourceGit.Views
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{ {
base.OnPropertyChanged(change); base.OnPropertyChanged(change);
if (change.Property.Name == "ActualThemeVariant") InvalidateVisual();
if (change.Property.Name == "ActualThemeVariant")
{
_bgBrush = null;
InvalidateVisual();
}
} }
protected override Size MeasureOverride(Size availableSize) protected override Size MeasureOverride(Size availableSize)
@ -98,6 +103,8 @@ namespace SourceGit.Views
return new Size(size.Width / scale + 16, size.Height / scale + 16); return new Size(size.Width / scale + 16, size.Height / scale + 16);
} }
} }
private DrawingBrush _bgBrush = null;
} }
public class RevisionTextFileView : TextEditor public class RevisionTextFileView : TextEditor