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

View file

@ -449,6 +449,10 @@
<Grid> <Grid>
<Grid x:Name="previewEditor" SizeChanged="OnPreviewSizeChanged" TextElement.FontFamily="Consolas" TextElement.Foreground="{StaticResource Brush.FG1}"/> <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"> <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}"/> <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}"/> <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.FrozenColumnCount = 1;
grid.ContextMenuOpening += OnPreviewContextMenuOpening; grid.ContextMenuOpening += OnPreviewContextMenuOpening;
grid.RowStyle = FindResource("Style.DataGridRow.NoBringIntoView") as Style; 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(); var colLineNumber = new DataGridTextColumn();
colLineNumber.IsReadOnly = true; colLineNumber.IsReadOnly = true;
@ -491,6 +476,10 @@ namespace SourceGit.UI {
previewEditor.Children.Add(splitter); 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) { private void OnPreviewContextMenuOpening(object sender, ContextMenuEventArgs e) {
var grid = sender as DataGrid; var grid = sender as DataGrid;
if (grid == null) return; if (grid == null) return;
@ -539,6 +528,7 @@ namespace SourceGit.UI {
private async void FileTreeItemSelected(object sender, RoutedPropertyChangedEventArgs<object> e) { private async void FileTreeItemSelected(object sender, RoutedPropertyChangedEventArgs<object> e) {
previewEditor.Children.Clear(); previewEditor.Children.Clear();
previewImage.Visibility = Visibility.Collapsed;
maskPreviewNotSupported.Visibility = Visibility.Collapsed; maskPreviewNotSupported.Visibility = Visibility.Collapsed;
maskRevision.Visibility = Visibility.Collapsed; maskRevision.Visibility = Visibility.Collapsed;
@ -547,15 +537,20 @@ namespace SourceGit.UI {
switch (node.CommitObject.Kind) { switch (node.CommitObject.Kind) {
case Git.Commit.Object.Type.Blob: 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); var obj = repo.GetLFSObject(commit.SHA, node.FilePath);
maskRevision.Visibility = Visibility.Visible; maskRevision.Visibility = Visibility.Visible;
iconPreviewRevision.Data = FindResource("Icon.LFS") as Geometry; iconPreviewRevision.Data = FindResource("Icon.LFS") as Geometry;
txtPreviewRevision.Content = "LFS SIZE:" + App.Format("Bytes", obj.Size); txtPreviewRevision.Content = "LFS SIZE:" + App.Format("Bytes", obj.Size);
} else { } else {
await Task.Run(() => { await Task.Run(() => {
var isBinary = false; var data = new List<Git.Commit.Line>();
var data = commit.GetTextFileContent(repo, node.FilePath, out isBinary); var isBinary = commit.GetTextFileContent(repo, node.FilePath, data);
if (isBinary) { if (isBinary) {
Dispatcher.Invoke(() => maskPreviewNotSupported.Visibility = Visibility.Visible); Dispatcher.Invoke(() => maskPreviewNotSupported.Visibility = Visibility.Visible);