refactor: use the same control between image preview view and image diff view

This commit is contained in:
leo 2024-06-22 18:56:49 +08:00
parent 45b93a117e
commit dbce8eebde
No known key found for this signature in database
3 changed files with 14 additions and 93 deletions

View file

@ -164,90 +164,6 @@ namespace SourceGit.Views
}
}
public class RevisionImageFileView : Control
{
public static readonly StyledProperty<Bitmap> SourceProperty =
AvaloniaProperty.Register<RevisionImageFileView, Bitmap>(nameof(Source), null);
public Bitmap Source
{
get => GetValue(SourceProperty);
set => SetValue(SourceProperty, value);
}
static RevisionImageFileView()
{
AffectsMeasure<RevisionImageFileView>(SourceProperty);
}
public override void Render(DrawingContext context)
{
if (_bgBrush == null)
{
var maskBrush = new SolidColorBrush(ActualThemeVariant == ThemeVariant.Dark ? 0xFF404040 : 0xFFBBBBBB);
var bg = new DrawingGroup()
{
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)) },
}
};
_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(_bgBrush, new Rect(Bounds.Size));
var source = Source;
if (source != null)
context.DrawImage(source, new Rect(source.Size), new Rect(8, 8, Bounds.Width - 16, Bounds.Height - 16));
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property.Name == "ActualThemeVariant")
{
_bgBrush = null;
InvalidateVisual();
}
}
protected override Size MeasureOverride(Size availableSize)
{
var source = Source;
if (source == null)
return availableSize;
var w = availableSize.Width - 16;
var h = availableSize.Height - 16;
var size = source.Size;
if (size.Width <= w)
{
if (size.Height <= h)
return new Size(size.Width + 16, size.Height + 16);
else
return new Size(h * size.Width / size.Height + 16, availableSize.Height);
}
else
{
var scale = Math.Max(size.Width / w, size.Height / h);
return new Size(size.Width / scale + 16, size.Height / scale + 16);
}
}
private DrawingBrush _bgBrush = null;
}
public class RevisionTextFileView : TextEditor
{
protected override Type StyleKeyOverride => typeof(TextEditor);