Skip to content

Commit 8e3a8f4

Browse files
committed
optimize: new way to update corner radius of branch tree node to improve performance (#137)
1 parent 476f926 commit 8e3a8f4

File tree

5 files changed

+128
-208
lines changed

5 files changed

+128
-208
lines changed

src/Converters/BranchTreeNodeConverters.cs

Lines changed: 0 additions & 99 deletions
This file was deleted.

src/ViewModels/BranchTreeNode.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
5+
using Avalonia;
46
using Avalonia.Collections;
57

8+
using CommunityToolkit.Mvvm.ComponentModel;
9+
610
namespace SourceGit.ViewModels
711
{
812
public enum BranchTreeNodeType
@@ -12,8 +16,10 @@ public enum BranchTreeNodeType
1216
Branch,
1317
}
1418

15-
public class BranchTreeNode
19+
public class BranchTreeNode : ObservableObject
1620
{
21+
public const double DEFAULT_CORNER = 4.0;
22+
1723
public string Name { get; set; }
1824
public BranchTreeNodeType Type { get; set; }
1925
public object Backend { get; set; }
@@ -51,6 +57,43 @@ public bool IsCurrent
5157
get => IsBranch && (Backend as Models.Branch).IsCurrent;
5258
}
5359

60+
public bool IsSelected
61+
{
62+
get => _isSelected;
63+
set => SetProperty(ref _isSelected, value);
64+
}
65+
66+
public CornerRadius CornerRadius
67+
{
68+
get => _cornerRadius;
69+
set => SetProperty(ref _cornerRadius, value);
70+
}
71+
72+
public void UpdateCornerRadius(ref BranchTreeNode prev)
73+
{
74+
if (_isSelected && prev != null && prev.IsSelected)
75+
{
76+
var prevTop = prev.CornerRadius.TopLeft;
77+
prev.CornerRadius = new CornerRadius(prevTop, 0);
78+
CornerRadius = new CornerRadius(0, DEFAULT_CORNER);
79+
}
80+
else if (CornerRadius.TopLeft != DEFAULT_CORNER ||
81+
CornerRadius.BottomLeft != DEFAULT_CORNER)
82+
{
83+
CornerRadius = new CornerRadius(DEFAULT_CORNER);
84+
}
85+
86+
prev = this;
87+
88+
if (!IsBranch && IsExpanded)
89+
{
90+
foreach (var child in Children)
91+
child.UpdateCornerRadius(ref prev);
92+
}
93+
}
94+
private bool _isSelected = false;
95+
private CornerRadius _cornerRadius = new CornerRadius(DEFAULT_CORNER);
96+
5497
public class Builder
5598
{
5699
public List<BranchTreeNode> Locals => _locals;

src/Views/DeleteMultipleBranches.axaml

Lines changed: 59 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -14,75 +14,69 @@
1414
Classes="bold"
1515
Text="{DynamicResource Text.DeleteMultiBranch}" />
1616

17-
<Grid Margin="0,16,8,0" RowDefinitions="Auto,Auto" ColumnDefinitions="120,*">
18-
<TextBlock Grid.Row="0" Grid.Column="0"
19-
HorizontalAlignment="Right" VerticalAlignment="Top"
20-
Text="{DynamicResource Text.DeleteMultiBranch.Targets}" />
21-
<Border Grid.Row="0" Grid.Column="1"
22-
Background="{DynamicResource Brush.Contents}"
23-
BorderThickness="1"
24-
BorderBrush="{DynamicResource Brush.Border1}"
25-
CornerRadius="4"
26-
Padding="4">
27-
<DataGrid MaxHeight="200"
28-
Background="Transparent"
29-
BorderThickness="0"
30-
ItemsSource="{Binding Targets}"
31-
SelectionMode="Single"
32-
CanUserReorderColumns="False"
33-
CanUserResizeColumns="False"
34-
CanUserSortColumns="False"
35-
IsReadOnly="True"
36-
HeadersVisibility="None"
37-
Focusable="False"
38-
RowHeight="26"
39-
HorizontalScrollBarVisibility="Auto"
40-
VerticalScrollBarVisibility="Auto">
41-
<DataGrid.Styles>
42-
<Style Selector="DataGridRow">
43-
<Setter Property="CornerRadius" Value="4" />
44-
</Style>
17+
<Border Margin="4,16,0,0"
18+
Background="{DynamicResource Brush.Contents}"
19+
BorderThickness="1"
20+
BorderBrush="{DynamicResource Brush.Border1}"
21+
CornerRadius="4"
22+
Padding="4">
23+
<DataGrid MaxHeight="200"
24+
Background="Transparent"
25+
BorderThickness="0"
26+
ItemsSource="{Binding Targets}"
27+
SelectionMode="Single"
28+
CanUserReorderColumns="False"
29+
CanUserResizeColumns="False"
30+
CanUserSortColumns="False"
31+
IsReadOnly="True"
32+
HeadersVisibility="None"
33+
Focusable="False"
34+
RowHeight="26"
35+
HorizontalScrollBarVisibility="Auto"
36+
VerticalScrollBarVisibility="Auto">
37+
<DataGrid.Styles>
38+
<Style Selector="DataGridRow">
39+
<Setter Property="CornerRadius" Value="4" />
40+
</Style>
4541

46-
<Style Selector="DataGridRow /template/ Border#RowBorder">
47-
<Setter Property="ClipToBounds" Value="True" />
48-
</Style>
42+
<Style Selector="DataGridRow /template/ Border#RowBorder">
43+
<Setter Property="ClipToBounds" Value="True" />
44+
</Style>
4945

50-
<Style Selector="DataGridRow:pointerover /template/ Rectangle#BackgroundRectangle">
51-
<Setter Property="Fill" Value="{DynamicResource Brush.AccentHovered}" />
52-
</Style>
46+
<Style Selector="DataGridRow:pointerover /template/ Rectangle#BackgroundRectangle">
47+
<Setter Property="Fill" Value="{DynamicResource Brush.AccentHovered}" />
48+
</Style>
5349

54-
<Style Selector="DataGridRow:selected /template/ Rectangle#BackgroundRectangle">
55-
<Setter Property="Fill" Value="{DynamicResource Brush.Accent}" />
56-
</Style>
57-
</DataGrid.Styles>
50+
<Style Selector="DataGridRow:selected /template/ Rectangle#BackgroundRectangle">
51+
<Setter Property="Fill" Value="{DynamicResource Brush.Accent}" />
52+
</Style>
53+
</DataGrid.Styles>
5854

59-
<DataGrid.Columns>
60-
<DataGridTemplateColumn Header="ICON">
61-
<DataGridTemplateColumn.CellTemplate>
62-
<DataTemplate>
63-
<Path Width="10" Height="10" Margin="4,0,8,0" Data="{StaticResource Icons.Branch}" />
64-
</DataTemplate>
65-
</DataGridTemplateColumn.CellTemplate>
66-
</DataGridTemplateColumn>
55+
<DataGrid.Columns>
56+
<DataGridTemplateColumn Header="ICON">
57+
<DataGridTemplateColumn.CellTemplate>
58+
<DataTemplate>
59+
<Path Width="10" Height="10" Margin="4,0,8,0" Data="{StaticResource Icons.Branch}" />
60+
</DataTemplate>
61+
</DataGridTemplateColumn.CellTemplate>
62+
</DataGridTemplateColumn>
6763

68-
<DataGridTemplateColumn Width="*" Header="NAME">
69-
<DataGridTemplateColumn.CellTemplate>
70-
<DataTemplate>
71-
<TextBlock Text="{Binding Converter={x:Static c:BranchConverters.ToName}}" ClipToBounds="True"
72-
Classes="monospace" />
73-
</DataTemplate>
74-
</DataGridTemplateColumn.CellTemplate>
75-
</DataGridTemplateColumn>
76-
</DataGrid.Columns>
77-
</DataGrid>
78-
</Border>
79-
80-
<TextBlock Grid.Row="1" Grid.Column="1"
81-
Margin="0,8,0,0"
82-
Text="{DynamicResource Text.DeleteMultiBranch.Tip}"
83-
TextWrapping="Wrap"
84-
Foreground="{DynamicResource Brush.FG2}"
85-
FontStyle="Italic" />
86-
</Grid>
64+
<DataGridTemplateColumn Width="*" Header="NAME">
65+
<DataGridTemplateColumn.CellTemplate>
66+
<DataTemplate>
67+
<TextBlock Text="{Binding Converter={x:Static c:BranchConverters.ToName}}" ClipToBounds="True"
68+
Classes="monospace" />
69+
</DataTemplate>
70+
</DataGridTemplateColumn.CellTemplate>
71+
</DataGridTemplateColumn>
72+
</DataGrid.Columns>
73+
</DataGrid>
74+
</Border>
75+
76+
<TextBlock Margin="4,8,0,0"
77+
Text="{DynamicResource Text.DeleteMultiBranch.Tip}"
78+
TextWrapping="Wrap"
79+
Foreground="{DynamicResource Brush.FG2}"
80+
FontStyle="Italic" />
8781
</StackPanel>
8882
</UserControl>

src/Views/Repository.axaml

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,8 @@
208208
<TreeView.Styles>
209209
<Style Selector="TreeViewItem" x:DataType="vm:BranchTreeNode">
210210
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
211-
<Setter Property="CornerRadius">
212-
<Setter.Value>
213-
<MultiBinding Converter="{x:Static c:BranchTreeNodeConverters.ToCornerRadius}">
214-
<Binding Path="$parent[v:Repository].RefereshLocalBranchSelectionToken"/>
215-
<Binding Path="$self"/>
216-
</MultiBinding>
217-
</Setter.Value>
218-
</Setter>
211+
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
212+
<Setter Property="CornerRadius" Value="{Binding CornerRadius}"/>
219213
</Style>
220214

221215
<Style Selector="Grid.repository_leftpanel TreeViewItem:selected /template/ Border#PART_LayoutRoot">
@@ -278,14 +272,8 @@
278272
<TreeView.Styles>
279273
<Style Selector="TreeViewItem" x:DataType="vm:BranchTreeNode">
280274
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
281-
<Setter Property="CornerRadius">
282-
<Setter.Value>
283-
<MultiBinding Converter="{x:Static c:BranchTreeNodeConverters.ToCornerRadius}">
284-
<Binding Path="$parent[v:Repository].RefereshRemoteBranchSelectionToken"/>
285-
<Binding Path="$self"/>
286-
</MultiBinding>
287-
</Setter.Value>
288-
</Setter>
275+
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
276+
<Setter Property="CornerRadius" Value="{Binding CornerRadius}"/>
289277
</Style>
290278

291279
<Style Selector="Grid.repository_leftpanel TreeViewItem:selected /template/ Border#PART_LayoutRoot">

0 commit comments

Comments
 (0)