Skip to content

Commit f1f54bf

Browse files
committed
fix: force re-create the tree instead of only rebuild the source for the tree (#157)
1 parent 9d88ae4 commit f1f54bf

File tree

2 files changed

+62
-55
lines changed

2 files changed

+62
-55
lines changed

src/Views/ChangeCollectionView.axaml

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,33 @@
33
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:m="using:SourceGit.Models"
6-
xmlns:vm="using:SourceGit.ViewModels"
76
xmlns:v="using:SourceGit.Views"
87
xmlns:c="using:SourceGit.Converters"
98
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
109
x:Class="SourceGit.Views.ChangeCollectionView"
11-
x:Name="me">
12-
<TreeDataGrid x:Name="tree"
13-
AutoDragDropRows="False"
14-
ShowColumnHeaders="False"
15-
CanUserResizeColumns="False"
16-
CanUserSortColumns="False"
17-
ScrollViewer.BringIntoViewOnFocusChange="True">
18-
<TreeDataGrid.Resources>
19-
<DataTemplate x:Key="TreeModeTemplate" DataType="m:FileTreeNode">
20-
<Grid HorizontalAlignment="Stretch" Height="24" ColumnDefinitions="Auto,*">
21-
<Path Grid.Column="0" Classes="folder_icon" Width="14" Height="14" Margin="0,2,0,0" IsVisible="{Binding IsFolder}" Fill="Goldenrod" VerticalAlignment="Center"/>
22-
<v:ChangeStatusIcon Grid.Column="0" Width="14" Height="14" IsWorkingCopyChange="{Binding #me.IsWorkingCopyChange}" Change="{Binding Backend}" IsVisible="{Binding !IsFolder}"/>
23-
<TextBlock Grid.Column="1" Classes="monospace" Text="{Binding FullPath, Converter={x:Static c:PathConverters.PureFileName}}" Margin="6,0,0,0"/>
24-
</Grid>
25-
</DataTemplate>
10+
x:Name="ThisControl">
11+
<UserControl.Resources>
12+
<DataTemplate x:Key="TreeModeTemplate" DataType="m:FileTreeNode">
13+
<Grid HorizontalAlignment="Stretch" Height="24" ColumnDefinitions="Auto,*">
14+
<Path Grid.Column="0" Classes="folder_icon" Width="14" Height="14" Margin="0,2,0,0" IsVisible="{Binding IsFolder}" Fill="Goldenrod" VerticalAlignment="Center"/>
15+
<v:ChangeStatusIcon Grid.Column="0" Width="14" Height="14" IsWorkingCopyChange="{Binding #ThisControl.IsWorkingCopyChange}" Change="{Binding Backend}" IsVisible="{Binding !IsFolder}"/>
16+
<TextBlock Grid.Column="1" Classes="monospace" Text="{Binding FullPath, Converter={x:Static c:PathConverters.PureFileName}}" Margin="6,0,0,0"/>
17+
</Grid>
18+
</DataTemplate>
2619

27-
<DataTemplate x:Key="ListModeTemplate" DataType="m:Change">
28-
<StackPanel Orientation="Horizontal">
29-
<v:ChangeStatusIcon Width="14" Height="14" IsWorkingCopyChange="{Binding #me.IsWorkingCopyChange}" Change="{Binding}" Margin="4,0,0,0"/>
30-
<TextBlock Classes="monospace" Text="{Binding Path}" Margin="4,0"/>
31-
</StackPanel>
32-
</DataTemplate>
20+
<DataTemplate x:Key="ListModeTemplate" DataType="m:Change">
21+
<StackPanel Orientation="Horizontal">
22+
<v:ChangeStatusIcon Width="14" Height="14" IsWorkingCopyChange="{Binding #ThisControl.IsWorkingCopyChange}" Change="{Binding}" Margin="4,0,0,0"/>
23+
<TextBlock Classes="monospace" Text="{Binding Path}" Margin="4,0"/>
24+
</StackPanel>
25+
</DataTemplate>
3326

34-
<DataTemplate x:Key="GridModeTemplate" DataType="m:Change">
35-
<StackPanel Orientation="Horizontal">
36-
<v:ChangeStatusIcon Width="14" Height="14" IsWorkingCopyChange="{Binding #me.IsWorkingCopyChange}" Change="{Binding}" Margin="4,0,0,0"/>
37-
<TextBlock Classes="monospace" Text="{Binding Path, Converter={x:Static c:PathConverters.PureFileName}}" Margin="4,0"/>
38-
<TextBlock Classes="monospace" Text="{Binding Path, Converter={x:Static c:PathConverters.PureDirectoryName}}" Foreground="{DynamicResource Brush.FG2}"/>
39-
</StackPanel>
40-
</DataTemplate>
41-
</TreeDataGrid.Resources>
42-
</TreeDataGrid>
27+
<DataTemplate x:Key="GridModeTemplate" DataType="m:Change">
28+
<StackPanel Orientation="Horizontal">
29+
<v:ChangeStatusIcon Width="14" Height="14" IsWorkingCopyChange="{Binding #ThisControl.IsWorkingCopyChange}" Change="{Binding}" Margin="4,0,0,0"/>
30+
<TextBlock Classes="monospace" Text="{Binding Path, Converter={x:Static c:PathConverters.PureFileName}}" Margin="4,0"/>
31+
<TextBlock Classes="monospace" Text="{Binding Path, Converter={x:Static c:PathConverters.PureDirectoryName}}" Foreground="{DynamicResource Brush.FG2}"/>
32+
</StackPanel>
33+
</DataTemplate>
34+
</UserControl.Resources>
4335
</UserControl>

src/Views/ChangeCollectionView.axaml.cs

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Avalonia;
55
using Avalonia.Controls;
66
using Avalonia.Controls.Models.TreeDataGrid;
7+
using Avalonia.Controls.Templates;
78
using Avalonia.Interactivity;
89

910
namespace SourceGit.Views
@@ -78,11 +79,10 @@ public ChangeCollectionView()
7879

7980
private void UpdateSource()
8081
{
81-
if (tree.Source is IDisposable disposable)
82-
{
82+
if (Content is TreeDataGrid tree && tree.Source is IDisposable disposable)
8383
disposable.Dispose();
84-
tree.Source = null;
85-
}
84+
85+
Content = null;
8686

8787
var changes = Changes;
8888
if (changes == null)
@@ -92,12 +92,13 @@ private void UpdateSource()
9292
if (viewMode == Models.ChangeViewMode.Tree)
9393
{
9494
var filetree = Models.FileTreeNode.Build(changes, true);
95+
var template = this.FindResource("TreeModeTemplate") as IDataTemplate;
9596
var source = new HierarchicalTreeDataGridSource<Models.FileTreeNode>(filetree)
9697
{
9798
Columns =
9899
{
99100
new HierarchicalExpanderColumn<Models.FileTreeNode>(
100-
new TemplateColumn<Models.FileTreeNode>(null, "TreeModeTemplate", null, GridLength.Auto),
101+
new TemplateColumn<Models.FileTreeNode>(null, template, null, GridLength.Auto),
101102
x => x.Children,
102103
x => x.Children.Count > 0,
103104
x => x.IsExpanded)
@@ -111,24 +112,25 @@ private void UpdateSource()
111112
{
112113
if (!_isSelecting && s is Models.TreeDataGridSelectionModel<Models.FileTreeNode> model)
113114
{
114-
var selection = new List<Models.Change>();
115+
var selected = new List<Models.Change>();
115116
foreach (var c in model.SelectedItems)
116-
CollectChangesInNode(selection, c);
117+
CollectChangesInNode(selected, c);
117118

118119
_isSelecting = true;
119-
SetCurrentValue(SelectedChangesProperty, selection);
120+
SetCurrentValue(SelectedChangesProperty, selected);
120121
_isSelecting = false;
121122
}
122123
};
123124

124125
source.Selection = selection;
125-
tree.Source = source;
126+
CreateTreeDataGrid(source);
126127
}
127128
else if (viewMode == Models.ChangeViewMode.List)
128129
{
130+
var template = this.FindResource("ListModeTemplate") as IDataTemplate;
129131
var source = new FlatTreeDataGridSource<Models.Change>(changes)
130132
{
131-
Columns = { new TemplateColumn<Models.Change>(null, "ListModeTemplate", null, GridLength.Auto) }
133+
Columns = { new TemplateColumn<Models.Change>(null, template, null, GridLength.Auto) }
132134
};
133135

134136
var selection = new Models.TreeDataGridSelectionModel<Models.Change>(source, null);
@@ -138,24 +140,25 @@ private void UpdateSource()
138140
{
139141
if (!_isSelecting && s is Models.TreeDataGridSelectionModel<Models.Change> model)
140142
{
141-
var selection = new List<Models.Change>();
143+
var selected = new List<Models.Change>();
142144
foreach (var c in model.SelectedItems)
143-
selection.Add(c);
145+
selected.Add(c);
144146

145147
_isSelecting = true;
146-
SetCurrentValue(SelectedChangesProperty, selection);
148+
SetCurrentValue(SelectedChangesProperty, selected);
147149
_isSelecting = false;
148150
}
149151
};
150152

151153
source.Selection = selection;
152-
tree.Source = source;
154+
CreateTreeDataGrid(source);
153155
}
154156
else
155157
{
158+
var template = this.FindResource("GridModeTemplate") as IDataTemplate;
156159
var source = new FlatTreeDataGridSource<Models.Change>(changes)
157160
{
158-
Columns = { new TemplateColumn<Models.Change>(null, "GridModeTemplate", null, GridLength.Auto) },
161+
Columns = { new TemplateColumn<Models.Change>(null, template, null, GridLength.Auto) },
159162
};
160163

161164
var selection = new Models.TreeDataGridSelectionModel<Models.Change>(source, null);
@@ -165,24 +168,28 @@ private void UpdateSource()
165168
{
166169
if (!_isSelecting && s is Models.TreeDataGridSelectionModel<Models.Change> model)
167170
{
168-
var selection = new List<Models.Change>();
171+
var selected = new List<Models.Change>();
169172
foreach (var c in model.SelectedItems)
170-
selection.Add(c);
173+
selected.Add(c);
171174

172175
_isSelecting = true;
173-
SetCurrentValue(SelectedChangesProperty, selection);
176+
SetCurrentValue(SelectedChangesProperty, selected);
174177
_isSelecting = false;
175178
}
176179
};
177180

178181
source.Selection = selection;
179-
tree.Source = source;
182+
CreateTreeDataGrid(source);
180183
}
181184
}
182185

183186
private void UpdateSelected()
184187
{
185-
if (_isSelecting || tree.Source == null)
188+
if (_isSelecting || Content == null)
189+
return;
190+
191+
var tree = Content as TreeDataGrid;
192+
if (tree == null)
186193
return;
187194

188195
_isSelecting = true;
@@ -212,17 +219,25 @@ private void UpdateSelected()
212219
CollectSelectedNodeByChange(nodes, node as Models.FileTreeNode, set);
213220

214221
if (nodes.Count == 0)
215-
{
216222
treeSelection.Clear();
217-
}
218223
else
219-
{
220224
treeSelection.Select(nodes);
221-
}
222225
}
223226
_isSelecting = false;
224227
}
225228

229+
private void CreateTreeDataGrid(ITreeDataGridSource source)
230+
{
231+
Content = new TreeDataGrid()
232+
{
233+
AutoDragDropRows = false,
234+
ShowColumnHeaders = false,
235+
CanUserResizeColumns = false,
236+
CanUserSortColumns = false,
237+
Source = source,
238+
};
239+
}
240+
226241
private void CollectChangesInNode(List<Models.Change> outs, Models.FileTreeNode node)
227242
{
228243
if (node.IsFolder)

0 commit comments

Comments
 (0)