diff --git a/src/Commands/QueryRevisionObjects.cs b/src/Commands/QueryRevisionObjects.cs index 7a3db057..bcad9129 100644 --- a/src/Commands/QueryRevisionObjects.cs +++ b/src/Commands/QueryRevisionObjects.cs @@ -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 objects = new List(); - 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 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 _objects = new List(); } } diff --git a/src/Models/FileTreeNode.cs b/src/Models/FileTreeNode.cs deleted file mode 100644 index ad1298c9..00000000 --- a/src/Models/FileTreeNode.cs +++ /dev/null @@ -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 Children { get; set; } = new List(); - - public static List Build(List changes, bool expanded) - { - var nodes = new List(); - var folders = new Dictionary(); - - 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 Build(List files, bool expanded) - { - var nodes = new List(); - var folders = new Dictionary(); - - 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 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); - } - } - } -} diff --git a/src/ViewModels/CommitDetail.cs b/src/ViewModels/CommitDetail.cs index bcacd042..c521ce5b 100644 --- a/src/ViewModels/CommitDetail.cs +++ b/src/ViewModels/CommitDetail.cs @@ -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 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 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(); - 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(); - 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 tree) - { - var source = new HierarchicalTreeDataGridSource(tree) - { - Columns = - { - new HierarchicalExpanderColumn( - new TemplateColumn("Icon", "FileTreeNodeExpanderTemplate", null, GridLength.Auto), - x => x.Children, - x => x.Children.Count > 0, - x => x.IsExpanded), - new TextColumn( - null, - x => string.Empty, - GridLength.Star) - } - }; - - var selection = new Models.TreeDataGridSelectionModel(source, x => x.Children); - selection.SingleSelect = true; - selection.SelectionChanged += (s, _) => - { - if (s is Models.TreeDataGridSelectionModel selection) - RefreshViewRevisionFile(selection.SelectedItem?.Backend as Models.Object); - }; - - source.Selection = selection; - RevisionFiles = source; - } - private static readonly HashSet IMG_EXTS = new HashSet() { ".ico", ".bmp", ".jpg", ".png", ".jpeg" @@ -554,9 +449,6 @@ namespace SourceGit.ViewModels private List _selectedChanges = null; private string _searchChangeFilter = string.Empty; private DiffContext _diffContext = null; - private List _revisionFilesBackup = null; - private HierarchicalTreeDataGridSource _revisionFiles = null; - private string _searchFileFilter = string.Empty; private object _viewRevisionFileContent = null; private Commands.Command.CancelToken _cancelToken = null; } diff --git a/src/Views/ChangeCollectionView.axaml b/src/Views/ChangeCollectionView.axaml index c364080e..634ca205 100644 --- a/src/Views/ChangeCollectionView.axaml +++ b/src/Views/ChangeCollectionView.axaml @@ -9,10 +9,10 @@ x:Class="SourceGit.Views.ChangeCollectionView" x:Name="ThisControl"> - + - + diff --git a/src/Views/ChangeCollectionView.axaml.cs b/src/Views/ChangeCollectionView.axaml.cs index 61f8de97..c9ae72b2 100644 --- a/src/Views/ChangeCollectionView.axaml.cs +++ b/src/Views/ChangeCollectionView.axaml.cs @@ -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 Children { get; set; } = new List(); + + public static List Build(IList changes, bool expanded) + { + var nodes = new List(); + var folders = new Dictionary(); + + 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 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 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(filetree) + var source = new HierarchicalTreeDataGridSource(filetree) { Columns = { - new HierarchicalExpanderColumn( - new TemplateColumn(null, template, null, GridLength.Auto), + new HierarchicalExpanderColumn( + new TemplateColumn(null, template, null, GridLength.Auto), x => x.Children, x => x.Children.Count > 0, x => x.IsExpanded) } }; - var selection = new Models.TreeDataGridSelectionModel(source, x => x.Children); + var selection = new Models.TreeDataGridSelectionModel(source, x => x.Children); selection.SingleSelect = SingleSelect; selection.RowDoubleTapped += (_, e) => RaiseEvent(new RoutedEventArgs(ChangeDoubleTappedEvent)); selection.SelectionChanged += (s, _) => { - if (!_isSelecting && s is Models.TreeDataGridSelectionModel model) + if (!_isSelecting && s is Models.TreeDataGridSelectionModel model) { var selected = new List(); 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 treeSelection) + else if (tree.Source.Selection is Models.TreeDataGridSelectionModel 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(); + var nodes = new List(); 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 outs, Models.FileTreeNode node) + private void CollectChangesInNode(List 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 outs, Models.FileTreeNode node, HashSet selected) + private void CollectSelectedNodeByChange(List outs, ChangeTreeNode node, HashSet 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); } diff --git a/src/Views/RevisionFiles.axaml b/src/Views/RevisionFiles.axaml index 82d2651c..9c999061 100644 --- a/src/Views/RevisionFiles.axaml +++ b/src/Views/RevisionFiles.axaml @@ -17,48 +17,20 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + Children { get; set; } = new List(); + + 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 RevisionProperty = + AvaloniaProperty.Register(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(); + 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(toplevelObjects) + { + Columns = + { + new HierarchicalExpanderColumn( + new TemplateColumn(null, template, null, GridLength.Auto), + GetChildrenOfTreeNode, + x => x.IsFolder, + x => x.IsExpanded) + } + }; + + var selection = new Models.TreeDataGridSelectionModel(source, GetChildrenOfTreeNode); + selection.SingleSelect = true; + selection.SelectionChanged += (s, _) => + { + if (s is Models.TreeDataGridSelectionModel 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 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 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); } }