2024-03-27 06:38:38 -07:00
|
|
|
using System;
|
|
|
|
|
|
|
|
using Avalonia;
|
2024-02-05 23:08:37 -08:00
|
|
|
using Avalonia.Controls;
|
2024-03-27 06:38:38 -07:00
|
|
|
using Avalonia.Media;
|
|
|
|
using Avalonia.Media.Imaging;
|
2024-03-28 00:47:40 -07:00
|
|
|
using Avalonia.Styling;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
namespace SourceGit.Views
|
|
|
|
{
|
2024-03-27 06:38:38 -07:00
|
|
|
public class ImageDiffView : Control
|
|
|
|
{
|
|
|
|
public static readonly StyledProperty<double> AlphaProperty =
|
|
|
|
AvaloniaProperty.Register<ImageDiffView, double>(nameof(Alpha), 0.5);
|
|
|
|
|
|
|
|
public double Alpha
|
|
|
|
{
|
|
|
|
get => GetValue(AlphaProperty);
|
|
|
|
set => SetValue(AlphaProperty, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly StyledProperty<Bitmap> OldImageProperty =
|
|
|
|
AvaloniaProperty.Register<ImageDiffView, Bitmap>(nameof(OldImage), null);
|
|
|
|
|
|
|
|
public Bitmap OldImage
|
|
|
|
{
|
|
|
|
get => GetValue(OldImageProperty);
|
|
|
|
set => SetValue(OldImageProperty, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly StyledProperty<Bitmap> NewImageProperty =
|
|
|
|
AvaloniaProperty.Register<ImageDiffView, Bitmap>(nameof(NewImage), null);
|
|
|
|
|
|
|
|
public Bitmap NewImage
|
|
|
|
{
|
|
|
|
get => GetValue(NewImageProperty);
|
|
|
|
set => SetValue(NewImageProperty, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ImageDiffView()
|
|
|
|
{
|
|
|
|
AffectsMeasure<ImageDiffView>(OldImageProperty, NewImageProperty);
|
|
|
|
AffectsRender<ImageDiffView>(AlphaProperty);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Render(DrawingContext context)
|
|
|
|
{
|
|
|
|
var alpha = Alpha;
|
2024-03-28 00:47:40 -07:00
|
|
|
var bgMaskBrush = new SolidColorBrush(ActualThemeVariant == ThemeVariant.Dark ? 0xFF404040 : 0xFFBBBBBB);
|
|
|
|
|
|
|
|
var bg = new DrawingGroup()
|
|
|
|
{
|
|
|
|
Children =
|
|
|
|
{
|
|
|
|
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)) },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
context.FillRectangle(brushBG, new Rect(Bounds.Size));
|
2024-03-27 06:38:38 -07:00
|
|
|
|
|
|
|
var left = OldImage;
|
|
|
|
if (left != null && alpha > 0)
|
|
|
|
{
|
|
|
|
var src = new Rect(0, 0, left.Size.Width * Alpha, left.Size.Height);
|
2024-03-28 00:47:40 -07:00
|
|
|
var dst = new Rect(8, 8, (Bounds.Width - 16) * Alpha, Bounds.Height - 16);
|
2024-03-27 06:38:38 -07:00
|
|
|
context.DrawImage(left, src, dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
var right = NewImage;
|
|
|
|
if (right != null)
|
|
|
|
{
|
|
|
|
var src = new Rect(right.Size.Width * Alpha, 0, right.Size.Width - right.Size.Width * Alpha, right.Size.Height);
|
2024-03-28 00:47:40 -07:00
|
|
|
var dst = new Rect((Bounds.Width - 16) * Alpha + 8, 8, (Bounds.Width - 16) * (1 - Alpha), Bounds.Height - 16);
|
2024-03-27 06:38:38 -07:00
|
|
|
context.DrawImage(right, src, dst);
|
|
|
|
}
|
2024-03-28 00:47:40 -07:00
|
|
|
|
|
|
|
var x = (Bounds.Width - 16) * Alpha + 8;
|
2024-03-27 06:38:38 -07:00
|
|
|
context.DrawLine(new Pen(Brushes.DarkGreen, 2), new Point(x, 0), new Point(x, Bounds.Height));
|
|
|
|
}
|
|
|
|
|
2024-03-28 00:47:40 -07:00
|
|
|
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
|
|
|
|
{
|
|
|
|
base.OnPropertyChanged(change);
|
|
|
|
if (change.Property.Name == "ActualThemeVariant") InvalidateVisual();
|
|
|
|
}
|
|
|
|
|
2024-03-27 06:38:38 -07:00
|
|
|
protected override Size MeasureOverride(Size availableSize)
|
|
|
|
{
|
|
|
|
var left = OldImage;
|
|
|
|
var right = NewImage;
|
|
|
|
|
|
|
|
if (left != null)
|
|
|
|
{
|
|
|
|
return GetDesiredSize(left.Size, availableSize);
|
2024-03-28 02:46:03 -07:00
|
|
|
}
|
2024-03-27 06:38:38 -07:00
|
|
|
else if (right != null)
|
|
|
|
{
|
|
|
|
return GetDesiredSize(right.Size, availableSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return availableSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Size GetDesiredSize(Size img, Size available)
|
|
|
|
{
|
2024-03-28 00:47:40 -07:00
|
|
|
var w = available.Width - 16;
|
|
|
|
var h = available.Height - 16;
|
|
|
|
|
|
|
|
if (img.Width <= w)
|
2024-03-27 06:38:38 -07:00
|
|
|
{
|
2024-03-28 00:47:40 -07:00
|
|
|
if (img.Height <= h)
|
2024-03-27 06:38:38 -07:00
|
|
|
{
|
2024-03-28 00:47:40 -07:00
|
|
|
return new Size(img.Width + 16, img.Height + 16);
|
2024-03-27 06:38:38 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-28 00:47:40 -07:00
|
|
|
return new Size(h * img.Width / img.Height + 16, available.Height);
|
2024-03-27 06:38:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-28 00:47:40 -07:00
|
|
|
var s = Math.Max(img.Width / w, img.Height / h);
|
|
|
|
return new Size(img.Width / s + 16, img.Height / s + 16);
|
2024-03-27 06:38:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public partial class DiffView : UserControl
|
|
|
|
{
|
|
|
|
public DiffView()
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
InitializeComponent();
|
|
|
|
}
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|