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

View file

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