feature<CommitViewer>: support preview image files

This commit is contained in:
leo 2021-01-27 11:30:17 +08:00
parent 9ac37ccc09
commit 11d567604a
3 changed files with 22 additions and 24 deletions

View file

@ -224,9 +224,9 @@ namespace SourceGit.Git {
/// </summary>
/// <param name="repo"></param>
/// <param name="file"></param>
/// <param name="line"></param>
/// <returns></returns>
public List<Line> GetTextFileContent(Repository repo, string file, out bool isBinary) {
var data = new List<Line>();
public bool GetTextFileContent(Repository repo, string file, List<Line> lines) {
var binary = false;
var count = 0;
@ -240,19 +240,18 @@ namespace SourceGit.Git {
if (line.IndexOf('\0') >= 0) {
binary = true;
data.Clear();
lines.Clear();
return;
}
count++;
data.Add(new Line() { No = count, Content = line });
lines.Add(new Line() { No = count, Content = line });
});
if (errs != null) App.RaiseError(errs);
}
isBinary = binary;
return data;
return binary;
}
/// <summary>

View file

@ -449,6 +449,10 @@
<Grid>
<Grid x:Name="previewEditor" SizeChanged="OnPreviewSizeChanged" TextElement.FontFamily="Consolas" TextElement.Foreground="{StaticResource Brush.FG1}"/>
<ScrollViewer x:Name="previewImage" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" HorizontalAlignment="Center" VerticalAlignment="Center">
<Image x:Name="previewImageData" Width="Auto" Height="Auto"/>
</ScrollViewer>
<StackPanel x:Name="maskRevision" Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center" Visibility="Collapsed">
<Path x:Name="iconPreviewRevision" Width="64" Height="64" Style="{StaticResource Style.Icon}" Data="{StaticResource Icon.Submodule}" Fill="{StaticResource Brush.FG2}"/>
<Label x:Name="txtPreviewRevision" Margin="0,16,0,0" FontFamily="Consolas" FontSize="18" FontWeight="UltraBold" HorizontalAlignment="Center" Foreground="{StaticResource Brush.FG2}"/>

View file

@ -446,21 +446,6 @@ namespace SourceGit.UI {
grid.FrozenColumnCount = 1;
grid.ContextMenuOpening += OnPreviewContextMenuOpening;
grid.RowStyle = FindResource("Style.DataGridRow.NoBringIntoView") as Style;
grid.CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, (o, e) => {
var items = (o as DataGrid).SelectedItems;
if (items.Count == 0) return;
var builder = new StringBuilder();
foreach (var item in items) {
var line = item as Git.Commit.Line;
if (line == null) continue;
builder.Append(line.Content);
builder.AppendLine();
}
Clipboard.SetText(builder.ToString());
}));
var colLineNumber = new DataGridTextColumn();
colLineNumber.IsReadOnly = true;
@ -491,6 +476,10 @@ namespace SourceGit.UI {
previewEditor.Children.Add(splitter);
}
private bool IsImage(string path) {
return path.EndsWith(".png") || path.EndsWith(".jpg") || path.EndsWith(".jpeg") || path.EndsWith(".ico") || path.EndsWith(".bmp") || path.EndsWith(".svg");
}
private void OnPreviewContextMenuOpening(object sender, ContextMenuEventArgs e) {
var grid = sender as DataGrid;
if (grid == null) return;
@ -539,6 +528,7 @@ namespace SourceGit.UI {
private async void FileTreeItemSelected(object sender, RoutedPropertyChangedEventArgs<object> e) {
previewEditor.Children.Clear();
previewImage.Visibility = Visibility.Collapsed;
maskPreviewNotSupported.Visibility = Visibility.Collapsed;
maskRevision.Visibility = Visibility.Collapsed;
@ -547,15 +537,20 @@ namespace SourceGit.UI {
switch (node.CommitObject.Kind) {
case Git.Commit.Object.Type.Blob:
if (repo.IsLFSFiltered(node.FilePath)) {
if (IsImage(node.FilePath)) {
var tmp = Path.GetTempFileName();
commit.SaveFileTo(repo, node.FilePath, tmp);
previewImageData.Source = new BitmapImage(new Uri(tmp, UriKind.Absolute));
previewImage.Visibility = Visibility.Visible;
} else if (repo.IsLFSFiltered(node.FilePath)) {
var obj = repo.GetLFSObject(commit.SHA, node.FilePath);
maskRevision.Visibility = Visibility.Visible;
iconPreviewRevision.Data = FindResource("Icon.LFS") as Geometry;
txtPreviewRevision.Content = "LFS SIZE:" + App.Format("Bytes", obj.Size);
} else {
await Task.Run(() => {
var isBinary = false;
var data = commit.GetTextFileContent(repo, node.FilePath, out isBinary);
var data = new List<Git.Commit.Line>();
var isBinary = commit.GetTextFileContent(repo, node.FilePath, data);
if (isBinary) {
Dispatcher.Invoke(() => maskPreviewNotSupported.Visibility = Visibility.Visible);