enhance: reduce memory usage by commit detail view

This commit is contained in:
leo 2024-06-07 16:32:06 +08:00
parent 78c7168a46
commit bacc1c85ad
No known key found for this signature in database
GPG key ID: B528468E49CD0E58
7 changed files with 385 additions and 496 deletions

View file

@ -5,22 +5,23 @@ namespace SourceGit.Commands
{
public partial class QueryRevisionObjects : Command
{
[GeneratedRegex(@"^\d+\s+(\w+)\s+([0-9a-f]+)\s+(.*)$")]
private static partial Regex REG_FORMAT();
private readonly List<Models.Object> objects = new List<Models.Object>();
public QueryRevisionObjects(string repo, string sha)
public QueryRevisionObjects(string repo, string sha, string parentFolder)
{
WorkingDirectory = repo;
Context = repo;
Args = $"ls-tree -r {sha}";
Args = $"ls-tree {sha}";
if (!string.IsNullOrEmpty(parentFolder))
Args += $" -- \"{parentFolder}\"";
}
public List<Models.Object> Result()
{
Exec();
return objects;
return _objects;
}
protected override void OnReadline(string line)
@ -50,7 +51,9 @@ namespace SourceGit.Commands
break;
}
objects.Add(obj);
_objects.Add(obj);
}
private List<Models.Object> _objects = new List<Models.Object>();
}
}

View file

@ -1,185 +0,0 @@
using System;
using System.Collections.Generic;
namespace SourceGit.Models
{
public class FileTreeNode
{
public string FullPath { get; set; } = string.Empty;
public bool IsFolder { get; set; } = false;
public bool IsExpanded { get; set; } = false;
public object Backend { get; set; } = null;
public List<FileTreeNode> Children { get; set; } = new List<FileTreeNode>();
public static List<FileTreeNode> Build(List<Change> changes, bool expanded)
{
var nodes = new List<FileTreeNode>();
var folders = new Dictionary<string, FileTreeNode>();
foreach (var c in changes)
{
var sepIdx = c.Path.IndexOf('/', StringComparison.Ordinal);
if (sepIdx == -1)
{
nodes.Add(new FileTreeNode()
{
FullPath = c.Path,
Backend = c,
IsFolder = false,
IsExpanded = false
});
}
else
{
FileTreeNode lastFolder = null;
var start = 0;
while (sepIdx != -1)
{
var folder = c.Path.Substring(0, sepIdx);
if (folders.TryGetValue(folder, out var value))
{
lastFolder = value;
}
else if (lastFolder == null)
{
lastFolder = new FileTreeNode()
{
FullPath = folder,
Backend = null,
IsFolder = true,
IsExpanded = expanded
};
nodes.Add(lastFolder);
folders.Add(folder, lastFolder);
}
else
{
var cur = new FileTreeNode()
{
FullPath = folder,
Backend = null,
IsFolder = true,
IsExpanded = expanded
};
folders.Add(folder, cur);
lastFolder.Children.Add(cur);
lastFolder = cur;
}
start = sepIdx + 1;
sepIdx = c.Path.IndexOf('/', start);
}
lastFolder.Children.Add(new FileTreeNode()
{
FullPath = c.Path,
Backend = c,
IsFolder = false,
IsExpanded = false
});
}
}
folders.Clear();
Sort(nodes);
return nodes;
}
public static List<FileTreeNode> Build(List<Object> files, bool expanded)
{
var nodes = new List<FileTreeNode>();
var folders = new Dictionary<string, FileTreeNode>();
foreach (var f in files)
{
var sepIdx = f.Path.IndexOf('/', StringComparison.Ordinal);
if (sepIdx == -1)
{
nodes.Add(new FileTreeNode()
{
FullPath = f.Path,
Backend = f,
IsFolder = false,
IsExpanded = false
});
}
else
{
FileTreeNode lastFolder = null;
var start = 0;
while (sepIdx != -1)
{
var folder = f.Path.Substring(0, sepIdx);
if (folders.TryGetValue(folder, out var value))
{
lastFolder = value;
}
else if (lastFolder == null)
{
lastFolder = new FileTreeNode()
{
FullPath = folder,
Backend = null,
IsFolder = true,
IsExpanded = expanded
};
nodes.Add(lastFolder);
folders.Add(folder, lastFolder);
}
else
{
var cur = new FileTreeNode()
{
FullPath = folder,
Backend = null,
IsFolder = true,
IsExpanded = expanded
};
folders.Add(folder, cur);
lastFolder.Children.Add(cur);
lastFolder = cur;
}
start = sepIdx + 1;
sepIdx = f.Path.IndexOf('/', start);
}
lastFolder.Children.Add(new FileTreeNode()
{
FullPath = f.Path,
Backend = f,
IsFolder = false,
IsExpanded = false
});
}
}
folders.Clear();
Sort(nodes);
return nodes;
}
private static void Sort(List<FileTreeNode> nodes)
{
nodes.Sort((l, r) =>
{
if (l.IsFolder == r.IsFolder)
{
return l.FullPath.CompareTo(r.FullPath);
}
else
{
return l.IsFolder ? -1 : 1;
}
});
foreach (var node in nodes)
{
if (node.Children.Count > 1)
Sort(node.Children);
}
}
}
}

View file

@ -4,7 +4,6 @@ using System.IO;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Controls.Models.TreeDataGrid;
using Avalonia.Media.Imaging;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
@ -76,24 +75,6 @@ namespace SourceGit.ViewModels
}
}
public HierarchicalTreeDataGridSource<Models.FileTreeNode> RevisionFiles
{
get => _revisionFiles;
private set => SetProperty(ref _revisionFiles, value);
}
public string SearchFileFilter
{
get => _searchFileFilter;
set
{
if (SetProperty(ref _searchFileFilter, value))
{
RefreshVisibleFiles();
}
}
}
public object ViewRevisionFileContent
{
get => _viewRevisionFileContent;
@ -117,11 +98,6 @@ namespace SourceGit.ViewModels
_selectedChanges.Clear();
_searchChangeFilter = null;
_diffContext = null;
if (_revisionFilesBackup != null)
_revisionFilesBackup.Clear();
if (_revisionFiles != null)
_revisionFiles.Dispose();
_searchFileFilter = null;
_viewRevisionFileContent = null;
_cancelToken = null;
}
@ -138,9 +114,93 @@ namespace SourceGit.ViewModels
SearchChangeFilter = string.Empty;
}
public void ClearSearchFileFilter()
public List<Models.Object> GetRevisionFilesUnderFolder(string parentFolder)
{
SearchFileFilter = string.Empty;
return new Commands.QueryRevisionObjects(_repo, _commit.SHA, parentFolder).Result();
}
public void ViewRevisionFile(Models.Object file)
{
if (file == null)
{
ViewRevisionFileContent = null;
return;
}
switch (file.Type)
{
case Models.ObjectType.Blob:
Task.Run(() =>
{
var isBinary = new Commands.IsBinary(_repo, _commit.SHA, file.Path).Result();
if (isBinary)
{
var ext = Path.GetExtension(file.Path);
if (IMG_EXTS.Contains(ext))
{
var stream = Commands.QueryFileContent.Run(_repo, _commit.SHA, file.Path);
var bitmap = stream.Length > 0 ? new Bitmap(stream) : null;
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;
}
var contentStream = Commands.QueryFileContent.Run(_repo, _commit.SHA, file.Path);
var content = new StreamReader(contentStream).ReadToEnd();
if (content.StartsWith("version https://git-lfs.github.com/spec/", StringComparison.Ordinal))
{
var obj = new Models.RevisionLFSObject() { Object = new Models.LFSObject() };
var lines = content.Split('\n', StringSplitOptions.RemoveEmptyEntries);
if (lines.Length == 3)
{
foreach (var line in lines)
{
if (line.StartsWith("oid sha256:", StringComparison.Ordinal))
{
obj.Object.Oid = line.Substring(11);
}
else if (line.StartsWith("size ", StringComparison.Ordinal))
{
obj.Object.Size = long.Parse(line.Substring(5));
}
}
Dispatcher.UIThread.Invoke(() =>
{
ViewRevisionFileContent = obj;
});
return;
}
}
Dispatcher.UIThread.Invoke(() =>
{
ViewRevisionFileContent = new Models.RevisionTextFile()
{
FileName = file.Path,
Content = content
};
});
});
break;
case Models.ObjectType.Commit:
ViewRevisionFileContent = new Models.RevisionSubmodule() { SHA = file.SHA };
break;
default:
ViewRevisionFileContent = null;
break;
}
}
public ContextMenu CreateChangeContextMenu(Models.Change change)
@ -319,29 +379,19 @@ namespace SourceGit.ViewModels
VisibleChanges = null;
SelectedChanges = null;
if (_revisionFiles != null)
{
_revisionFiles.Dispose();
_revisionFiles = null;
}
if (_commit == null)
return;
if (_cancelToken != null)
_cancelToken.Requested = true;
_cancelToken = new Commands.Command.CancelToken();
var parent = _commit.Parents.Count == 0 ? "4b825dc642cb6eb9a060e54bf8d69288fbee4904" : _commit.Parents[0];
var cmdChanges = new Commands.CompareRevisions(_repo, parent, _commit.SHA) { Cancel = _cancelToken };
var cmdRevisionFiles = new Commands.QueryRevisionObjects(_repo, _commit.SHA) { Cancel = _cancelToken };
Task.Run(() =>
{
var parent = _commit.Parents.Count == 0 ? "4b825dc642cb6eb9a060e54bf8d69288fbee4904" : _commit.Parents[0];
var cmdChanges = new Commands.CompareRevisions(_repo, parent, _commit.SHA) { Cancel = _cancelToken };
var changes = cmdChanges.Result();
if (cmdChanges.Cancel.Requested)
return;
var visible = changes;
if (!string.IsNullOrWhiteSpace(_searchChangeFilter))
{
@ -349,39 +399,18 @@ namespace SourceGit.ViewModels
foreach (var c in changes)
{
if (c.Path.Contains(_searchChangeFilter, StringComparison.OrdinalIgnoreCase))
{
visible.Add(c);
}
}
}
Dispatcher.UIThread.Invoke(() =>
if (!cmdChanges.Cancel.Requested)
{
Changes = changes;
VisibleChanges = visible;
});
});
Task.Run(() =>
{
_revisionFilesBackup = cmdRevisionFiles.Result();
if (cmdRevisionFiles.Cancel.Requested)
return;
var visible = _revisionFilesBackup;
var isSearching = !string.IsNullOrWhiteSpace(_searchFileFilter);
if (isSearching)
{
visible = new List<Models.Object>();
foreach (var f in _revisionFilesBackup)
Dispatcher.UIThread.Post(() =>
{
if (f.Path.Contains(_searchFileFilter, StringComparison.OrdinalIgnoreCase))
visible.Add(f);
}
Changes = changes;
VisibleChanges = visible;
});
}
var tree = Models.FileTreeNode.Build(visible, isSearching || visible.Count <= 100);
Dispatcher.UIThread.Invoke(() => BuildRevisionFilesSource(tree));
});
}
@ -407,140 +436,6 @@ namespace SourceGit.ViewModels
}
}
private void RefreshVisibleFiles()
{
if (_revisionFiles == null)
return;
var visible = _revisionFilesBackup;
var isSearching = !string.IsNullOrWhiteSpace(_searchFileFilter);
if (isSearching)
{
visible = new List<Models.Object>();
foreach (var f in _revisionFilesBackup)
{
if (f.Path.Contains(_searchFileFilter, StringComparison.OrdinalIgnoreCase))
visible.Add(f);
}
}
BuildRevisionFilesSource(Models.FileTreeNode.Build(visible, isSearching || visible.Count < 100));
}
private void RefreshViewRevisionFile(Models.Object file)
{
if (file == null)
{
ViewRevisionFileContent = null;
return;
}
switch (file.Type)
{
case Models.ObjectType.Blob:
Task.Run(() =>
{
var isBinary = new Commands.IsBinary(_repo, _commit.SHA, file.Path).Result();
if (isBinary)
{
var ext = Path.GetExtension(file.Path);
if (IMG_EXTS.Contains(ext))
{
var stream = Commands.QueryFileContent.Run(_repo, _commit.SHA, file.Path);
var bitmap = stream.Length > 0 ? new Bitmap(stream) : null;
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;
}
var contentStream = Commands.QueryFileContent.Run(_repo, _commit.SHA, file.Path);
var content = new StreamReader(contentStream).ReadToEnd();
if (content.StartsWith("version https://git-lfs.github.com/spec/", StringComparison.Ordinal))
{
var obj = new Models.RevisionLFSObject() { Object = new Models.LFSObject() };
var lines = content.Split('\n', StringSplitOptions.RemoveEmptyEntries);
if (lines.Length == 3)
{
foreach (var line in lines)
{
if (line.StartsWith("oid sha256:", StringComparison.Ordinal))
{
obj.Object.Oid = line.Substring(11);
}
else if (line.StartsWith("size ", StringComparison.Ordinal))
{
obj.Object.Size = long.Parse(line.Substring(5));
}
}
Dispatcher.UIThread.Invoke(() =>
{
ViewRevisionFileContent = obj;
});
return;
}
}
Dispatcher.UIThread.Invoke(() =>
{
ViewRevisionFileContent = new Models.RevisionTextFile()
{
FileName = file.Path,
Content = content
};
});
});
break;
case Models.ObjectType.Commit:
ViewRevisionFileContent = new Models.RevisionSubmodule() { SHA = file.SHA };
break;
default:
ViewRevisionFileContent = null;
break;
}
}
private void BuildRevisionFilesSource(List<Models.FileTreeNode> tree)
{
var source = new HierarchicalTreeDataGridSource<Models.FileTreeNode>(tree)
{
Columns =
{
new HierarchicalExpanderColumn<Models.FileTreeNode>(
new TemplateColumn<Models.FileTreeNode>("Icon", "FileTreeNodeExpanderTemplate", null, GridLength.Auto),
x => x.Children,
x => x.Children.Count > 0,
x => x.IsExpanded),
new TextColumn<Models.FileTreeNode, string>(
null,
x => string.Empty,
GridLength.Star)
}
};
var selection = new Models.TreeDataGridSelectionModel<Models.FileTreeNode>(source, x => x.Children);
selection.SingleSelect = true;
selection.SelectionChanged += (s, _) =>
{
if (s is Models.TreeDataGridSelectionModel<Models.FileTreeNode> selection)
RefreshViewRevisionFile(selection.SelectedItem?.Backend as Models.Object);
};
source.Selection = selection;
RevisionFiles = source;
}
private static readonly HashSet<string> IMG_EXTS = new HashSet<string>()
{
".ico", ".bmp", ".jpg", ".png", ".jpeg"
@ -554,9 +449,6 @@ namespace SourceGit.ViewModels
private List<Models.Change> _selectedChanges = null;
private string _searchChangeFilter = string.Empty;
private DiffContext _diffContext = null;
private List<Models.Object> _revisionFilesBackup = null;
private HierarchicalTreeDataGridSource<Models.FileTreeNode> _revisionFiles = null;
private string _searchFileFilter = string.Empty;
private object _viewRevisionFileContent = null;
private Commands.Command.CancelToken _cancelToken = null;
}

View file

@ -9,10 +9,10 @@
x:Class="SourceGit.Views.ChangeCollectionView"
x:Name="ThisControl">
<UserControl.Resources>
<DataTemplate x:Key="TreeModeTemplate" DataType="m:FileTreeNode">
<DataTemplate x:Key="TreeModeTemplate" DataType="v:ChangeTreeNode">
<Grid HorizontalAlignment="Stretch" Height="24" ColumnDefinitions="Auto,*">
<Path Grid.Column="0" Classes="folder_icon" Width="14" Height="14" Margin="0,2,0,0" IsVisible="{Binding IsFolder}" Fill="Goldenrod" VerticalAlignment="Center"/>
<v:ChangeStatusIcon Grid.Column="0" Width="14" Height="14" IsWorkingCopyChange="{Binding #ThisControl.IsWorkingCopyChange}" Change="{Binding Backend}" IsVisible="{Binding !IsFolder}"/>
<v:ChangeStatusIcon Grid.Column="0" Width="14" Height="14" IsWorkingCopyChange="{Binding #ThisControl.IsWorkingCopyChange}" Change="{Binding Change}" IsVisible="{Binding !IsFolder}"/>
<TextBlock Grid.Column="1" Classes="monospace" Text="{Binding FullPath, Converter={x:Static c:PathConverters.PureFileName}}" Margin="6,0,0,0"/>
</Grid>
</DataTemplate>

View file

@ -9,6 +9,101 @@ using Avalonia.Interactivity;
namespace SourceGit.Views
{
public class ChangeTreeNode
{
public string FullPath { get; set; } = string.Empty;
public bool IsFolder { get; set; } = false;
public bool IsExpanded { get; set; } = false;
public Models.Change Change { get; set; } = null;
public List<ChangeTreeNode> Children { get; set; } = new List<ChangeTreeNode>();
public static List<ChangeTreeNode> Build(IList<Models.Change> changes, bool expanded)
{
var nodes = new List<ChangeTreeNode>();
var folders = new Dictionary<string, ChangeTreeNode>();
foreach (var c in changes)
{
var sepIdx = c.Path.IndexOf('/', StringComparison.Ordinal);
if (sepIdx == -1)
{
nodes.Add(new ChangeTreeNode()
{
FullPath = c.Path,
Change = c,
IsFolder = false,
IsExpanded = false
});
}
else
{
ChangeTreeNode lastFolder = null;
var start = 0;
while (sepIdx != -1)
{
var folder = c.Path.Substring(0, sepIdx);
if (folders.TryGetValue(folder, out var value))
{
lastFolder = value;
}
else if (lastFolder == null)
{
lastFolder = new ChangeTreeNode()
{
FullPath = folder,
IsFolder = true,
IsExpanded = expanded
};
folders.Add(folder, lastFolder);
InsertFolder(nodes, lastFolder);
}
else
{
var cur = new ChangeTreeNode()
{
FullPath = folder,
IsFolder = true,
IsExpanded = expanded
};
folders.Add(folder, cur);
InsertFolder(lastFolder.Children, cur);
lastFolder = cur;
}
start = sepIdx + 1;
sepIdx = c.Path.IndexOf('/', start);
}
lastFolder.Children.Add(new ChangeTreeNode()
{
FullPath = c.Path,
Change = c,
IsFolder = false,
IsExpanded = false
});
}
}
folders.Clear();
return nodes;
}
private static void InsertFolder(List<ChangeTreeNode> collection, ChangeTreeNode subFolder)
{
for (int i = 0; i < collection.Count; i++)
{
if (!collection[i].IsFolder)
{
collection.Insert(i, subFolder);
return;
}
}
collection.Add(subFolder);
}
}
public partial class ChangeCollectionView : UserControl
{
public static readonly StyledProperty<bool> IsWorkingCopyChangeProperty =
@ -91,26 +186,26 @@ namespace SourceGit.Views
var viewMode = ViewMode;
if (viewMode == Models.ChangeViewMode.Tree)
{
var filetree = Models.FileTreeNode.Build(changes, true);
var filetree = ChangeTreeNode.Build(changes, true);
var template = this.FindResource("TreeModeTemplate") as IDataTemplate;
var source = new HierarchicalTreeDataGridSource<Models.FileTreeNode>(filetree)
var source = new HierarchicalTreeDataGridSource<ChangeTreeNode>(filetree)
{
Columns =
{
new HierarchicalExpanderColumn<Models.FileTreeNode>(
new TemplateColumn<Models.FileTreeNode>(null, template, null, GridLength.Auto),
new HierarchicalExpanderColumn<ChangeTreeNode>(
new TemplateColumn<ChangeTreeNode>(null, template, null, GridLength.Auto),
x => x.Children,
x => x.Children.Count > 0,
x => x.IsExpanded)
}
};
var selection = new Models.TreeDataGridSelectionModel<Models.FileTreeNode>(source, x => x.Children);
var selection = new Models.TreeDataGridSelectionModel<ChangeTreeNode>(source, x => x.Children);
selection.SingleSelect = SingleSelect;
selection.RowDoubleTapped += (_, e) => RaiseEvent(new RoutedEventArgs(ChangeDoubleTappedEvent));
selection.SelectionChanged += (s, _) =>
{
if (!_isSelecting && s is Models.TreeDataGridSelectionModel<Models.FileTreeNode> model)
if (!_isSelecting && s is Models.TreeDataGridSelectionModel<ChangeTreeNode> model)
{
var selected = new List<Models.Change>();
foreach (var c in model.SelectedItems)
@ -195,7 +290,7 @@ namespace SourceGit.Views
else
changeSelection.Select(selected);
}
else if (tree.Source.Selection is Models.TreeDataGridSelectionModel<Models.FileTreeNode> treeSelection)
else if (tree.Source.Selection is Models.TreeDataGridSelectionModel<ChangeTreeNode> treeSelection)
{
if (selected == null || selected.Count == 0)
{
@ -208,9 +303,9 @@ namespace SourceGit.Views
foreach (var c in selected)
set.Add(c);
var nodes = new List<Models.FileTreeNode>();
var nodes = new List<ChangeTreeNode>();
foreach (var node in tree.Source.Items)
CollectSelectedNodeByChange(nodes, node as Models.FileTreeNode, set);
CollectSelectedNodeByChange(nodes, node as ChangeTreeNode, set);
if (nodes.Count == 0)
treeSelection.Clear();
@ -232,22 +327,20 @@ namespace SourceGit.Views
};
}
private void CollectChangesInNode(List<Models.Change> outs, Models.FileTreeNode node)
private void CollectChangesInNode(List<Models.Change> outs, ChangeTreeNode node)
{
if (node.IsFolder)
{
foreach (var child in node.Children)
CollectChangesInNode(outs, child);
}
else
else if (!outs.Contains(node.Change))
{
var change = node.Backend as Models.Change;
if (change != null && !outs.Contains(change))
outs.Add(change);
outs.Add(node.Change);
}
}
private void CollectSelectedNodeByChange(List<Models.FileTreeNode> outs, Models.FileTreeNode node, HashSet<object> selected)
private void CollectSelectedNodeByChange(List<ChangeTreeNode> outs, ChangeTreeNode node, HashSet<object> selected)
{
if (node == null)
return;
@ -257,7 +350,7 @@ namespace SourceGit.Views
foreach (var child in node.Children)
CollectSelectedNodeByChange(outs, child, selected);
}
else if (node.Backend != null && selected.Contains(node.Backend))
else if (node.Change != null && selected.Contains(node.Change))
{
outs.Add(node);
}

View file

@ -17,48 +17,20 @@
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" RowDefinitions="26,*">
<!-- Search -->
<TextBox Grid.Row="0"
Height="26"
BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}"
Background="Transparent"
CornerRadius="4"
Watermark="{DynamicResource Text.CommitDetail.Files.Search}"
Text="{Binding SearchFileFilter, Mode=TwoWay}">
<TextBox.InnerLeftContent>
<Path Width="14" Height="14" Margin="4,0,0,0" Fill="{DynamicResource Brush.FG2}" Data="{StaticResource Icons.Search}"/>
</TextBox.InnerLeftContent>
<TextBox.InnerRightContent>
<Button Classes="icon_button"
IsVisible="{Binding SearchFileFilter, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
Command="{Binding ClearSearchFileFilter}">
<Path Width="14" Height="14" Fill="{DynamicResource Brush.FG2}" Data="{StaticResource Icons.Clear}"/>
</Button>
</TextBox.InnerRightContent>
</TextBox>
<!-- File Tree -->
<Border Grid.Row="1" Margin="0,4,0,0" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}">
<TreeDataGrid AutoDragDropRows="False"
ShowColumnHeaders="False"
CanUserResizeColumns="False"
CanUserSortColumns="False"
Source="{Binding RevisionFiles}"
ContextRequested="OnFileContextRequested">
<TreeDataGrid.Resources>
<DataTemplate x:Key="FileTreeNodeExpanderTemplate" DataType="m:FileTreeNode">
<Grid HorizontalAlignment="Stretch" Height="24" ColumnDefinitions="Auto,*">
<Path Grid.Column="0" Classes="folder_icon" Width="14" Height="14" Margin="0,2,0,0" IsVisible="{Binding IsFolder}" Fill="Goldenrod" VerticalAlignment="Center"/>
<Path Grid.Column="0" Width="14" Height="14" IsVisible="{Binding !IsFolder}" Data="{StaticResource Icons.File}" VerticalAlignment="Center"/>
<TextBlock Grid.Column="1" Classes="monospace" Text="{Binding FullPath, Converter={x:Static c:PathConverters.PureFileName}}" Margin="6,0,0,0"/>
</Grid>
</DataTemplate>
</TreeDataGrid.Resources>
</TreeDataGrid>
</Border>
</Grid>
<!-- File Tree -->
<Border Grid.Column="0" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}">
<v:RevisionFileTreeView Revision="{Binding Commit.SHA}" ContextRequested="OnRevisionFileTreeViewContextRequested">
<v:RevisionFileTreeView.Resources>
<DataTemplate x:Key="RevisionFileTreeNodeTemplate" DataType="v:RevisionFileTreeNode">
<Grid HorizontalAlignment="Stretch" Height="24" ColumnDefinitions="Auto,*">
<Path Grid.Column="0" Classes="folder_icon" Width="14" Height="14" Margin="0,2,0,0" IsVisible="{Binding IsFolder}" Fill="Goldenrod" VerticalAlignment="Center"/>
<Path Grid.Column="0" Width="14" Height="14" IsVisible="{Binding !IsFolder}" Data="{StaticResource Icons.File}" VerticalAlignment="Center"/>
<TextBlock Grid.Column="1" Classes="monospace" Text="{Binding Name}" Margin="6,0,0,0"/>
</Grid>
</DataTemplate>
</v:RevisionFileTreeView.Resources>
</v:RevisionFileTreeView>
</Border>
<GridSplitter Grid.Column="1"
MinWidth="1"

View file

@ -1,8 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Models.TreeDataGrid;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Media.Imaging;
@ -11,10 +15,155 @@ using Avalonia.Styling;
using AvaloniaEdit;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.TextMate;
namespace SourceGit.Views
{
public class RevisionFileTreeNode
{
public Models.Object Backend { get; set; } = null;
public bool IsExpanded { get; set; } = false;
public List<RevisionFileTreeNode> Children { get; set; } = new List<RevisionFileTreeNode>();
public bool IsFolder => Backend != null && Backend.Type == Models.ObjectType.Tree;
public string Name => Backend != null ? Path.GetFileName(Backend.Path) : string.Empty;
}
public class RevisionFileTreeView : UserControl
{
public static readonly StyledProperty<string> RevisionProperty =
AvaloniaProperty.Register<RevisionFileTreeView, string>(nameof(Revision), null);
public string Revision
{
get => GetValue(RevisionProperty);
set => SetValue(RevisionProperty, value);
}
public Models.Object SelectedObject
{
get;
private set;
} = null;
protected override Type StyleKeyOverride => typeof(UserControl);
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == RevisionProperty)
{
SelectedObject = null;
if (Content is TreeDataGrid tree && tree.Source is IDisposable disposable)
disposable.Dispose();
var vm = DataContext as ViewModels.CommitDetail;
if (vm == null)
{
Content = null;
GC.Collect();
return;
}
var objects = vm.GetRevisionFilesUnderFolder(null);
if (objects == null || objects.Count == 0)
{
Content = null;
GC.Collect();
return;
}
var toplevelObjects = new List<RevisionFileTreeNode>();
foreach (var obj in objects)
toplevelObjects.Add(new RevisionFileTreeNode() { Backend = obj });
toplevelObjects.Sort((l, r) =>
{
if (l.IsFolder == r.IsFolder)
return l.Name.CompareTo(r.Name);
return l.IsFolder ? -1 : 1;
});
var template = this.FindResource("RevisionFileTreeNodeTemplate") as IDataTemplate;
var source = new HierarchicalTreeDataGridSource<RevisionFileTreeNode>(toplevelObjects)
{
Columns =
{
new HierarchicalExpanderColumn<RevisionFileTreeNode>(
new TemplateColumn<RevisionFileTreeNode>(null, template, null, GridLength.Auto),
GetChildrenOfTreeNode,
x => x.IsFolder,
x => x.IsExpanded)
}
};
var selection = new Models.TreeDataGridSelectionModel<RevisionFileTreeNode>(source, GetChildrenOfTreeNode);
selection.SingleSelect = true;
selection.SelectionChanged += (s, _) =>
{
if (s is Models.TreeDataGridSelectionModel<RevisionFileTreeNode> model)
{
var node = model.SelectedItem;
var detail = DataContext as ViewModels.CommitDetail;
if (node != null && !node.IsFolder)
{
SelectedObject = node.Backend;
detail.ViewRevisionFile(node.Backend);
}
else
{
SelectedObject = null;
detail.ViewRevisionFile(null);
}
}
};
source.Selection = selection;
Content = new TreeDataGrid()
{
AutoDragDropRows = false,
ShowColumnHeaders = false,
CanUserResizeColumns = false,
CanUserSortColumns = false,
Source = source,
};
GC.Collect();
}
}
private List<RevisionFileTreeNode> GetChildrenOfTreeNode(RevisionFileTreeNode node)
{
if (!node.IsFolder)
return null;
if (node.Children.Count > 0)
return node.Children;
var vm = DataContext as ViewModels.CommitDetail;
if (vm == null)
return null;
var objects = vm.GetRevisionFilesUnderFolder(node.Backend.Path + "/");
if (objects == null || objects.Count == 0)
return null;
foreach (var obj in objects)
node.Children.Add(new RevisionFileTreeNode() { Backend = obj });
node.Children.Sort((l, r) =>
{
if (l.IsFolder == r.IsFolder)
return l.Name.CompareTo(r.Name);
return l.IsFolder ? -1 : 1;
});
return node.Children;
}
}
public class RevisionImageFileView : Control
{
public static readonly StyledProperty<Bitmap> SourceProperty =
@ -59,9 +208,7 @@ namespace SourceGit.Views
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)
@ -79,9 +226,7 @@ namespace SourceGit.Views
{
var source = Source;
if (source == null)
{
return availableSize;
}
var w = availableSize.Width - 16;
var h = availableSize.Height - 16;
@ -89,13 +234,9 @@ namespace SourceGit.Views
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
{
@ -130,12 +271,6 @@ namespace SourceGit.Views
base.OnLoaded(e);
TextArea.TextView.ContextRequested += OnTextViewContextRequested;
_textMate = Models.TextMateHelper.CreateForEditor(this);
if (DataContext is Models.RevisionTextFile source)
{
Models.TextMateHelper.SetGrammarByFileName(_textMate, source.FileName);
}
}
protected override void OnUnloaded(RoutedEventArgs e)
@ -143,13 +278,6 @@ namespace SourceGit.Views
base.OnUnloaded(e);
TextArea.TextView.ContextRequested -= OnTextViewContextRequested;
if (_textMate != null)
{
_textMate.Dispose();
_textMate = null;
}
GC.Collect();
}
@ -159,20 +287,9 @@ namespace SourceGit.Views
var source = DataContext as Models.RevisionTextFile;
if (source != null)
{
Text = source.Content;
Models.TextMateHelper.SetGrammarByFileName(_textMate, source.FileName);
}
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property.Name == "ActualThemeVariant" && change.NewValue != null)
{
Models.TextMateHelper.SetThemeByApp(_textMate);
}
else
Text = string.Empty;
}
private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e)
@ -202,8 +319,6 @@ namespace SourceGit.Views
TextArea.TextView.OpenContextMenu(menu);
e.Handled = true;
}
private TextMate.Installation _textMate = null;
}
public partial class RevisionFiles : UserControl
@ -213,15 +328,14 @@ namespace SourceGit.Views
InitializeComponent();
}
private void OnFileContextRequested(object sender, ContextRequestedEventArgs e)
private void OnRevisionFileTreeViewContextRequested(object sender, ContextRequestedEventArgs e)
{
if (DataContext is ViewModels.CommitDetail vm && sender is TreeDataGrid tree)
if (DataContext is ViewModels.CommitDetail vm && sender is RevisionFileTreeView view)
{
var selected = tree.RowSelection.SelectedItem as Models.FileTreeNode;
if (selected != null && !selected.IsFolder && selected.Backend is Models.Object obj)
if (view.SelectedObject != null && view.SelectedObject.Type != Models.ObjectType.Tree)
{
var menu = vm.CreateRevisionFileContextMenu(obj);
tree.OpenContextMenu(menu);
var menu = vm.CreateRevisionFileContextMenu(view.SelectedObject);
view.OpenContextMenu(menu);
}
}