mirror of
https://github.com/sourcegit-scm/sourcegit.git
synced 2024-12-23 20:47:25 -08:00
feature: supports preview and diff with image files
This commit is contained in:
parent
5ef542f92d
commit
6950055f24
9 changed files with 268 additions and 51 deletions
40
src/SourceGit/Commands/GetImageFileAsBitmap.cs
Normal file
40
src/SourceGit/Commands/GetImageFileAsBitmap.cs
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
using Avalonia.Media.Imaging;
|
||||||
|
|
||||||
|
namespace SourceGit.Commands
|
||||||
|
{
|
||||||
|
public static class GetImageFileAsBitmap
|
||||||
|
{
|
||||||
|
public static Bitmap Run(string repo, string revision, string file)
|
||||||
|
{
|
||||||
|
var starter = new ProcessStartInfo();
|
||||||
|
starter.WorkingDirectory = repo;
|
||||||
|
starter.FileName = Native.OS.GitInstallPath;
|
||||||
|
starter.Arguments = $"show {revision}:\"{file}\"";
|
||||||
|
starter.UseShellExecute = false;
|
||||||
|
starter.CreateNoWindow = true;
|
||||||
|
starter.WindowStyle = ProcessWindowStyle.Hidden;
|
||||||
|
starter.RedirectStandardOutput = true;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var stream = new MemoryStream();
|
||||||
|
var proc = new Process() { StartInfo = starter };
|
||||||
|
proc.Start();
|
||||||
|
proc.StandardOutput.BaseStream.CopyTo(stream);
|
||||||
|
proc.WaitForExit();
|
||||||
|
proc.Close();
|
||||||
|
|
||||||
|
stream.Position = 0;
|
||||||
|
return new Bitmap(stream);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,8 @@
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
using Avalonia.Media.Imaging;
|
||||||
|
|
||||||
namespace SourceGit.Models
|
namespace SourceGit.Models
|
||||||
{
|
{
|
||||||
public enum TextDiffLineType
|
public enum TextDiffLineType
|
||||||
|
@ -547,6 +549,12 @@ namespace SourceGit.Models
|
||||||
public long NewSize { get; set; } = 0;
|
public long NewSize { get; set; } = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ImageDiff
|
||||||
|
{
|
||||||
|
public Bitmap Old { get; set; } = null;
|
||||||
|
public Bitmap New { get; set; } = null;
|
||||||
|
}
|
||||||
|
|
||||||
public class NoOrEOLChange
|
public class NoOrEOLChange
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,17 @@
|
||||||
namespace SourceGit.Models
|
using Avalonia.Media.Imaging;
|
||||||
|
|
||||||
|
namespace SourceGit.Models
|
||||||
{
|
{
|
||||||
public class RevisionBinaryFile
|
public class RevisionBinaryFile
|
||||||
{
|
{
|
||||||
public long Size { get; set; } = 0;
|
public long Size { get; set; } = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class RevisionImageFile
|
||||||
|
{
|
||||||
|
public Bitmap Image { get; set; } = null;
|
||||||
|
}
|
||||||
|
|
||||||
public class RevisionTextFile
|
public class RevisionTextFile
|
||||||
{
|
{
|
||||||
public string FileName { get; set; }
|
public string FileName { get; set; }
|
||||||
|
|
|
@ -432,11 +432,24 @@ namespace SourceGit.ViewModels
|
||||||
var isBinary = new Commands.IsBinary(_repo, _commit.SHA, file.Path).Result();
|
var isBinary = new Commands.IsBinary(_repo, _commit.SHA, file.Path).Result();
|
||||||
if (isBinary)
|
if (isBinary)
|
||||||
{
|
{
|
||||||
var size = new Commands.QueryFileSize(_repo, file.Path, _commit.SHA).Result();
|
var ext = Path.GetExtension(file.Path);
|
||||||
Dispatcher.UIThread.Invoke(() =>
|
if (IMG_EXTS.Contains(ext))
|
||||||
{
|
{
|
||||||
ViewRevisionFileContent = new Models.RevisionBinaryFile() { Size = size };
|
var bitmap = Commands.GetImageFileAsBitmap.Run(_repo, _commit.SHA, file.Path);
|
||||||
});
|
Dispatcher.UIThread.Invoke(() =>
|
||||||
|
{
|
||||||
|
ViewRevisionFileContent = new Models.RevisionImageFile() { Image = bitmap };
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var size = new Commands.QueryFileSize(_repo, file.Path, _commit.SHA).Result();
|
||||||
|
Dispatcher.UIThread.Invoke(() =>
|
||||||
|
{
|
||||||
|
ViewRevisionFileContent = new Models.RevisionBinaryFile() { Size = size };
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -485,6 +498,11 @@ namespace SourceGit.ViewModels
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static readonly HashSet<string> IMG_EXTS = new HashSet<string>()
|
||||||
|
{
|
||||||
|
".ico", ".bmp", ".jpg", ".png", ".jpeg"
|
||||||
|
};
|
||||||
|
|
||||||
private string _repo = string.Empty;
|
private string _repo = string.Empty;
|
||||||
private int _activePageIndex = 0;
|
private int _activePageIndex = 0;
|
||||||
private Models.Commit _commit = null;
|
private Models.Commit _commit = null;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System.IO;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using Avalonia;
|
using Avalonia;
|
||||||
|
@ -46,12 +47,6 @@ namespace SourceGit.ViewModels
|
||||||
private set => SetProperty(ref _isLoading, value);
|
private set => SetProperty(ref _isLoading, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsNoChange
|
|
||||||
{
|
|
||||||
get => _isNoChange;
|
|
||||||
private set => SetProperty(ref _isNoChange, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsTextDiff
|
public bool IsTextDiff
|
||||||
{
|
{
|
||||||
get => _isTextDiff;
|
get => _isTextDiff;
|
||||||
|
@ -77,7 +72,6 @@ namespace SourceGit.ViewModels
|
||||||
|
|
||||||
if (previous != null)
|
if (previous != null)
|
||||||
{
|
{
|
||||||
_isNoChange = previous._isNoChange;
|
|
||||||
_isTextDiff = previous._isTextDiff;
|
_isTextDiff = previous._isTextDiff;
|
||||||
_content = previous._content;
|
_content = previous._content;
|
||||||
}
|
}
|
||||||
|
@ -89,53 +83,62 @@ namespace SourceGit.ViewModels
|
||||||
Task.Run(() =>
|
Task.Run(() =>
|
||||||
{
|
{
|
||||||
var latest = new Commands.Diff(repo, option).Result();
|
var latest = new Commands.Diff(repo, option).Result();
|
||||||
var binaryDiff = null as Models.BinaryDiff;
|
var rs = null as object;
|
||||||
|
|
||||||
if (latest.IsBinary)
|
if (latest.TextDiff != null)
|
||||||
|
{
|
||||||
|
latest.TextDiff.File = _option.Path;
|
||||||
|
rs = latest.TextDiff;
|
||||||
|
}
|
||||||
|
else if (latest.IsBinary)
|
||||||
{
|
{
|
||||||
binaryDiff = new Models.BinaryDiff();
|
|
||||||
|
|
||||||
var oldPath = string.IsNullOrEmpty(_option.OrgPath) ? _option.Path : _option.OrgPath;
|
var oldPath = string.IsNullOrEmpty(_option.OrgPath) ? _option.Path : _option.OrgPath;
|
||||||
if (option.Revisions.Count == 2)
|
var ext = Path.GetExtension(oldPath);
|
||||||
|
|
||||||
|
if (IMG_EXTS.Contains(ext))
|
||||||
{
|
{
|
||||||
binaryDiff.OldSize = new Commands.QueryFileSize(repo, oldPath, option.Revisions[0]).Result();
|
var imgDiff = new Models.ImageDiff();
|
||||||
binaryDiff.NewSize = new Commands.QueryFileSize(repo, _option.Path, option.Revisions[1]).Result();
|
if (option.Revisions.Count == 2)
|
||||||
|
{
|
||||||
|
imgDiff.Old = Commands.GetImageFileAsBitmap.Run(repo, option.Revisions[0], oldPath);
|
||||||
|
imgDiff.New = Commands.GetImageFileAsBitmap.Run(repo, option.Revisions[1], oldPath);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
imgDiff.Old = Commands.GetImageFileAsBitmap.Run(repo, "HEAD", oldPath);
|
||||||
|
imgDiff.New = File.Exists(_option.Path) ? new Avalonia.Media.Imaging.Bitmap(_option.Path) : null;
|
||||||
|
}
|
||||||
|
rs = imgDiff;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
binaryDiff.OldSize = new Commands.QueryFileSize(repo, oldPath, "HEAD").Result();
|
var binaryDiff = new Models.BinaryDiff();
|
||||||
binaryDiff.NewSize = new FileInfo(Path.Combine(repo, _option.Path)).Length;
|
if (option.Revisions.Count == 2)
|
||||||
}
|
{
|
||||||
|
binaryDiff.OldSize = new Commands.QueryFileSize(repo, oldPath, option.Revisions[0]).Result();
|
||||||
|
binaryDiff.NewSize = new Commands.QueryFileSize(repo, _option.Path, option.Revisions[1]).Result();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
binaryDiff.OldSize = new Commands.QueryFileSize(repo, oldPath, "HEAD").Result();
|
||||||
|
binaryDiff.NewSize = new FileInfo(Path.Combine(repo, _option.Path)).Length;
|
||||||
|
}
|
||||||
|
rs = binaryDiff;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (latest.IsLFS)
|
||||||
|
{
|
||||||
|
rs = latest.LFSDiff;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rs = new Models.NoOrEOLChange();
|
||||||
}
|
}
|
||||||
|
|
||||||
Dispatcher.UIThread.Post(() =>
|
Dispatcher.UIThread.Post(() =>
|
||||||
{
|
{
|
||||||
if (latest.IsBinary)
|
Content = rs;
|
||||||
{
|
IsTextDiff = latest.TextDiff != null;
|
||||||
Content = binaryDiff;
|
|
||||||
IsTextDiff = false;
|
|
||||||
IsNoChange = false;
|
|
||||||
}
|
|
||||||
else if (latest.IsLFS)
|
|
||||||
{
|
|
||||||
Content = latest.LFSDiff;
|
|
||||||
IsTextDiff = false;
|
|
||||||
IsNoChange = false;
|
|
||||||
}
|
|
||||||
else if (latest.TextDiff != null)
|
|
||||||
{
|
|
||||||
latest.TextDiff.File = _option.Path;
|
|
||||||
Content = latest.TextDiff;
|
|
||||||
IsTextDiff = true;
|
|
||||||
IsNoChange = false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Content = new Models.NoOrEOLChange();
|
|
||||||
IsTextDiff = false;
|
|
||||||
IsNoChange = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
IsLoading = false;
|
IsLoading = false;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -157,10 +160,14 @@ namespace SourceGit.ViewModels
|
||||||
await Task.Run(() => Commands.MergeTool.OpenForDiff(_repo, exec, args, _option));
|
await Task.Run(() => Commands.MergeTool.OpenForDiff(_repo, exec, args, _option));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static readonly HashSet<string> IMG_EXTS = new HashSet<string>()
|
||||||
|
{
|
||||||
|
".ico", ".bmp", ".jpg", ".png", ".jpeg"
|
||||||
|
};
|
||||||
|
|
||||||
private readonly string _repo = string.Empty;
|
private readonly string _repo = string.Empty;
|
||||||
private readonly Models.DiffOption _option = null;
|
private readonly Models.DiffOption _option = null;
|
||||||
private bool _isLoading = true;
|
private bool _isLoading = true;
|
||||||
private bool _isNoChange = false;
|
|
||||||
private bool _isTextDiff = false;
|
private bool _isTextDiff = false;
|
||||||
private object _content = null;
|
private object _content = null;
|
||||||
private Vector _syncScrollOffset = Vector.Zero;
|
private Vector _syncScrollOffset = Vector.Zero;
|
||||||
|
|
|
@ -109,6 +109,36 @@
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</DataTemplate>
|
</DataTemplate>
|
||||||
|
|
||||||
|
<!-- Image Diff -->
|
||||||
|
<DataTemplate DataType="m:ImageDiff">
|
||||||
|
<Grid Margin="8,8,8,8" RowDefinitions="*,Auto" HorizontalAlignment="Center">
|
||||||
|
<Border Grid.Row="0" BorderThickness="1" BorderBrush="{DynamicResource Brush.Border1}">
|
||||||
|
<v:ImageDiffView Alpha="{Binding #ImageDiffSlider.Value}"
|
||||||
|
OldImage="{Binding Old}"
|
||||||
|
NewImage="{Binding New}"/>
|
||||||
|
</Border>
|
||||||
|
|
||||||
|
<Slider Grid.Row="1"
|
||||||
|
x:Name="ImageDiffSlider"
|
||||||
|
Minimum="0" Maximum="1"
|
||||||
|
VerticalAlignment="Top"
|
||||||
|
TickPlacement="BottomRight"
|
||||||
|
TickFrequency="0.1"
|
||||||
|
Margin="0,8,0,0"
|
||||||
|
Foreground="{DynamicResource Brush.Border1}"
|
||||||
|
Value="0.5">
|
||||||
|
<Slider.Resources>
|
||||||
|
<Thickness x:Key="SliderTopHeaderMargin">0,0,0,4</Thickness>
|
||||||
|
<GridLength x:Key="SliderPreContentMargin">0</GridLength>
|
||||||
|
<GridLength x:Key="SliderPostContentMargin">0</GridLength>
|
||||||
|
<CornerRadius x:Key="SliderThumbCornerRadius">8</CornerRadius>
|
||||||
|
<x:Double x:Key="SliderHorizontalThumbWidth">16</x:Double>
|
||||||
|
<x:Double x:Key="SliderHorizontalThumbHeight">16</x:Double>
|
||||||
|
</Slider.Resources>
|
||||||
|
</Slider>
|
||||||
|
</Grid>
|
||||||
|
</DataTemplate>
|
||||||
|
|
||||||
<!-- Text Diff -->
|
<!-- Text Diff -->
|
||||||
<DataTemplate DataType="m:TextDiff">
|
<DataTemplate DataType="m:TextDiff">
|
||||||
<v:TextDiffView TextDiff="{Binding}" UseSideBySideDiff="{Binding Source={x:Static vm:Preference.Instance}, Path=UseSideBySideDiff, Mode=OneWay}"/>
|
<v:TextDiffView TextDiff="{Binding}" UseSideBySideDiff="{Binding Source={x:Static vm:Preference.Instance}, Path=UseSideBySideDiff, Mode=OneWay}"/>
|
||||||
|
|
|
@ -1,7 +1,111 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
using Avalonia;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Media;
|
||||||
|
using Avalonia.Media.Imaging;
|
||||||
|
|
||||||
namespace SourceGit.Views
|
namespace SourceGit.Views
|
||||||
{
|
{
|
||||||
|
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;
|
||||||
|
var x = Bounds.Width * 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(0, 0, x, Bounds.Height);
|
||||||
|
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);
|
||||||
|
var dst = new Rect(x, 0, Bounds.Width - x, Bounds.Height);
|
||||||
|
context.DrawImage(right, src, dst);
|
||||||
|
}
|
||||||
|
|
||||||
|
context.DrawLine(new Pen(Brushes.DarkGreen, 2), new Point(x, 0), new Point(x, Bounds.Height));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override Size MeasureOverride(Size availableSize)
|
||||||
|
{
|
||||||
|
var left = OldImage;
|
||||||
|
var right = NewImage;
|
||||||
|
|
||||||
|
if (left != null)
|
||||||
|
{
|
||||||
|
return GetDesiredSize(left.Size, availableSize);
|
||||||
|
}
|
||||||
|
else if (right != null)
|
||||||
|
{
|
||||||
|
return GetDesiredSize(right.Size, availableSize);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return availableSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Size GetDesiredSize(Size img, Size available)
|
||||||
|
{
|
||||||
|
if (img.Width <= available.Width)
|
||||||
|
{
|
||||||
|
if (img.Height <= available.Height)
|
||||||
|
{
|
||||||
|
return img;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new Size(available.Height * img.Width / img.Height, available.Height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var s = Math.Max(img.Width / available.Width, img.Height / available.Height);
|
||||||
|
return new Size(img.Width / s, img.Height / s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public partial class DiffView : UserControl
|
public partial class DiffView : UserControl
|
||||||
{
|
{
|
||||||
public DiffView()
|
public DiffView()
|
||||||
|
|
|
@ -9,7 +9,6 @@ namespace SourceGit.Views
|
||||||
{
|
{
|
||||||
public class NameHighlightedTextBlock : Control
|
public class NameHighlightedTextBlock : Control
|
||||||
{
|
{
|
||||||
|
|
||||||
public static readonly StyledProperty<string> TextProperty =
|
public static readonly StyledProperty<string> TextProperty =
|
||||||
AvaloniaProperty.Register<NameHighlightedTextBlock, string>(nameof(Text));
|
AvaloniaProperty.Register<NameHighlightedTextBlock, string>(nameof(Text));
|
||||||
|
|
||||||
|
|
|
@ -91,6 +91,10 @@
|
||||||
<v:RevisionTextFileView FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}" Background="{DynamicResource Brush.Contents}"/>
|
<v:RevisionTextFileView FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}" Background="{DynamicResource Brush.Contents}"/>
|
||||||
</DataTemplate>
|
</DataTemplate>
|
||||||
|
|
||||||
|
<DataTemplate DataType="m:RevisionImageFile">
|
||||||
|
<Image Source="{Binding Image}" Margin="8"/>
|
||||||
|
</DataTemplate>
|
||||||
|
|
||||||
<DataTemplate DataType="m:RevisionLFSObject">
|
<DataTemplate DataType="m:RevisionLFSObject">
|
||||||
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
|
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
|
||||||
<TextBlock Text="{DynamicResource Text.CommitDetail.Files.LFS}" FontSize="18" FontWeight="Bold" HorizontalAlignment="Center" Foreground="{DynamicResource Brush.FG2}"/>
|
<TextBlock Text="{DynamicResource Text.CommitDetail.Files.LFS}" FontSize="18" FontWeight="Bold" HorizontalAlignment="Center" Foreground="{DynamicResource Brush.FG2}"/>
|
||||||
|
|
Loading…
Reference in a new issue