From f1f54bf6fecd7c701dfd9de0576225ca395c7509 Mon Sep 17 00:00:00 2001 From: leo Date: Sun, 2 Jun 2024 14:06:27 +0800 Subject: [PATCH] fix: force re-create the tree instead of only rebuild the source for the tree (#157) --- src/Views/ChangeCollectionView.axaml | 54 +++++++++------------ src/Views/ChangeCollectionView.axaml.cs | 63 +++++++++++++++---------- 2 files changed, 62 insertions(+), 55 deletions(-) diff --git a/src/Views/ChangeCollectionView.axaml b/src/Views/ChangeCollectionView.axaml index 87c729e7..c364080e 100644 --- a/src/Views/ChangeCollectionView.axaml +++ b/src/Views/ChangeCollectionView.axaml @@ -3,41 +3,33 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:m="using:SourceGit.Models" - xmlns:vm="using:SourceGit.ViewModels" xmlns:v="using:SourceGit.Views" xmlns:c="using:SourceGit.Converters" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="SourceGit.Views.ChangeCollectionView" - x:Name="me"> - - - - - - - - - + x:Name="ThisControl"> + + + + + + + + - - - - - - + + + + + + - - - - - - - - - + + + + + + + + diff --git a/src/Views/ChangeCollectionView.axaml.cs b/src/Views/ChangeCollectionView.axaml.cs index 5c42b07c..05e418c1 100644 --- a/src/Views/ChangeCollectionView.axaml.cs +++ b/src/Views/ChangeCollectionView.axaml.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Models.TreeDataGrid; +using Avalonia.Controls.Templates; using Avalonia.Interactivity; namespace SourceGit.Views @@ -78,11 +79,10 @@ namespace SourceGit.Views private void UpdateSource() { - if (tree.Source is IDisposable disposable) - { + if (Content is TreeDataGrid tree && tree.Source is IDisposable disposable) disposable.Dispose(); - tree.Source = null; - } + + Content = null; var changes = Changes; if (changes == null) @@ -92,12 +92,13 @@ namespace SourceGit.Views if (viewMode == Models.ChangeViewMode.Tree) { var filetree = Models.FileTreeNode.Build(changes, true); + var template = this.FindResource("TreeModeTemplate") as IDataTemplate; var source = new HierarchicalTreeDataGridSource(filetree) { Columns = { new HierarchicalExpanderColumn( - new TemplateColumn(null, "TreeModeTemplate", null, GridLength.Auto), + new TemplateColumn(null, template, null, GridLength.Auto), x => x.Children, x => x.Children.Count > 0, x => x.IsExpanded) @@ -111,24 +112,25 @@ namespace SourceGit.Views { if (!_isSelecting && s is Models.TreeDataGridSelectionModel model) { - var selection = new List(); + var selected = new List(); foreach (var c in model.SelectedItems) - CollectChangesInNode(selection, c); + CollectChangesInNode(selected, c); _isSelecting = true; - SetCurrentValue(SelectedChangesProperty, selection); + SetCurrentValue(SelectedChangesProperty, selected); _isSelecting = false; } }; source.Selection = selection; - tree.Source = source; + CreateTreeDataGrid(source); } else if (viewMode == Models.ChangeViewMode.List) { + var template = this.FindResource("ListModeTemplate") as IDataTemplate; var source = new FlatTreeDataGridSource(changes) { - Columns = { new TemplateColumn(null, "ListModeTemplate", null, GridLength.Auto) } + Columns = { new TemplateColumn(null, template, null, GridLength.Auto) } }; var selection = new Models.TreeDataGridSelectionModel(source, null); @@ -138,24 +140,25 @@ namespace SourceGit.Views { if (!_isSelecting && s is Models.TreeDataGridSelectionModel model) { - var selection = new List(); + var selected = new List(); foreach (var c in model.SelectedItems) - selection.Add(c); + selected.Add(c); _isSelecting = true; - SetCurrentValue(SelectedChangesProperty, selection); + SetCurrentValue(SelectedChangesProperty, selected); _isSelecting = false; } }; source.Selection = selection; - tree.Source = source; + CreateTreeDataGrid(source); } else { + var template = this.FindResource("GridModeTemplate") as IDataTemplate; var source = new FlatTreeDataGridSource(changes) { - Columns = { new TemplateColumn(null, "GridModeTemplate", null, GridLength.Auto) }, + Columns = { new TemplateColumn(null, template, null, GridLength.Auto) }, }; var selection = new Models.TreeDataGridSelectionModel(source, null); @@ -165,24 +168,28 @@ namespace SourceGit.Views { if (!_isSelecting && s is Models.TreeDataGridSelectionModel model) { - var selection = new List(); + var selected = new List(); foreach (var c in model.SelectedItems) - selection.Add(c); + selected.Add(c); _isSelecting = true; - SetCurrentValue(SelectedChangesProperty, selection); + SetCurrentValue(SelectedChangesProperty, selected); _isSelecting = false; } }; source.Selection = selection; - tree.Source = source; + CreateTreeDataGrid(source); } } private void UpdateSelected() { - if (_isSelecting || tree.Source == null) + if (_isSelecting || Content == null) + return; + + var tree = Content as TreeDataGrid; + if (tree == null) return; _isSelecting = true; @@ -212,17 +219,25 @@ namespace SourceGit.Views CollectSelectedNodeByChange(nodes, node as Models.FileTreeNode, set); if (nodes.Count == 0) - { treeSelection.Clear(); - } else - { treeSelection.Select(nodes); - } } _isSelecting = false; } + private void CreateTreeDataGrid(ITreeDataGridSource source) + { + Content = new TreeDataGrid() + { + AutoDragDropRows = false, + ShowColumnHeaders = false, + CanUserResizeColumns = false, + CanUserSortColumns = false, + Source = source, + }; + } + private void CollectChangesInNode(List outs, Models.FileTreeNode node) { if (node.IsFolder)