Skip to content

Commit

Permalink
optimize: new way to update corner radius of branch tree node to impr…
Browse files Browse the repository at this point in the history
…ove performance (#137)
  • Loading branch information
love-linger committed May 25, 2024
1 parent 476f926 commit 8e3a8f4
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 208 deletions.
99 changes: 0 additions & 99 deletions src/Converters/BranchTreeNodeConverters.cs

This file was deleted.

45 changes: 44 additions & 1 deletion src/ViewModels/BranchTreeNode.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;

using Avalonia;
using Avalonia.Collections;

using CommunityToolkit.Mvvm.ComponentModel;

namespace SourceGit.ViewModels
{
public enum BranchTreeNodeType
Expand All @@ -12,8 +16,10 @@ public enum BranchTreeNodeType
Branch,
}

public class BranchTreeNode
public class BranchTreeNode : ObservableObject
{
public const double DEFAULT_CORNER = 4.0;

public string Name { get; set; }
public BranchTreeNodeType Type { get; set; }
public object Backend { get; set; }
Expand Down Expand Up @@ -51,6 +57,43 @@ public bool IsCurrent
get => IsBranch && (Backend as Models.Branch).IsCurrent;
}

public bool IsSelected
{
get => _isSelected;
set => SetProperty(ref _isSelected, value);
}

public CornerRadius CornerRadius
{
get => _cornerRadius;
set => SetProperty(ref _cornerRadius, value);
}

public void UpdateCornerRadius(ref BranchTreeNode prev)
{
if (_isSelected && prev != null && prev.IsSelected)
{
var prevTop = prev.CornerRadius.TopLeft;
prev.CornerRadius = new CornerRadius(prevTop, 0);
CornerRadius = new CornerRadius(0, DEFAULT_CORNER);
}
else if (CornerRadius.TopLeft != DEFAULT_CORNER ||
CornerRadius.BottomLeft != DEFAULT_CORNER)
{
CornerRadius = new CornerRadius(DEFAULT_CORNER);
}

prev = this;

if (!IsBranch && IsExpanded)
{
foreach (var child in Children)
child.UpdateCornerRadius(ref prev);
}
}
private bool _isSelected = false;
private CornerRadius _cornerRadius = new CornerRadius(DEFAULT_CORNER);

public class Builder
{
public List<BranchTreeNode> Locals => _locals;
Expand Down
124 changes: 59 additions & 65 deletions src/Views/DeleteMultipleBranches.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,75 +14,69 @@
Classes="bold"
Text="{DynamicResource Text.DeleteMultiBranch}" />

<Grid Margin="0,16,8,0" RowDefinitions="Auto,Auto" ColumnDefinitions="120,*">
<TextBlock Grid.Row="0" Grid.Column="0"
HorizontalAlignment="Right" VerticalAlignment="Top"
Text="{DynamicResource Text.DeleteMultiBranch.Targets}" />
<Border Grid.Row="0" Grid.Column="1"
Background="{DynamicResource Brush.Contents}"
BorderThickness="1"
BorderBrush="{DynamicResource Brush.Border1}"
CornerRadius="4"
Padding="4">
<DataGrid MaxHeight="200"
Background="Transparent"
BorderThickness="0"
ItemsSource="{Binding Targets}"
SelectionMode="Single"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserSortColumns="False"
IsReadOnly="True"
HeadersVisibility="None"
Focusable="False"
RowHeight="26"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<DataGrid.Styles>
<Style Selector="DataGridRow">
<Setter Property="CornerRadius" Value="4" />
</Style>
<Border Margin="4,16,0,0"
Background="{DynamicResource Brush.Contents}"
BorderThickness="1"
BorderBrush="{DynamicResource Brush.Border1}"
CornerRadius="4"
Padding="4">
<DataGrid MaxHeight="200"
Background="Transparent"
BorderThickness="0"
ItemsSource="{Binding Targets}"
SelectionMode="Single"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserSortColumns="False"
IsReadOnly="True"
HeadersVisibility="None"
Focusable="False"
RowHeight="26"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<DataGrid.Styles>
<Style Selector="DataGridRow">
<Setter Property="CornerRadius" Value="4" />
</Style>

<Style Selector="DataGridRow /template/ Border#RowBorder">
<Setter Property="ClipToBounds" Value="True" />
</Style>
<Style Selector="DataGridRow /template/ Border#RowBorder">
<Setter Property="ClipToBounds" Value="True" />
</Style>

<Style Selector="DataGridRow:pointerover /template/ Rectangle#BackgroundRectangle">
<Setter Property="Fill" Value="{DynamicResource Brush.AccentHovered}" />
</Style>
<Style Selector="DataGridRow:pointerover /template/ Rectangle#BackgroundRectangle">
<Setter Property="Fill" Value="{DynamicResource Brush.AccentHovered}" />
</Style>

<Style Selector="DataGridRow:selected /template/ Rectangle#BackgroundRectangle">
<Setter Property="Fill" Value="{DynamicResource Brush.Accent}" />
</Style>
</DataGrid.Styles>
<Style Selector="DataGridRow:selected /template/ Rectangle#BackgroundRectangle">
<Setter Property="Fill" Value="{DynamicResource Brush.Accent}" />
</Style>
</DataGrid.Styles>

<DataGrid.Columns>
<DataGridTemplateColumn Header="ICON">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Path Width="10" Height="10" Margin="4,0,8,0" Data="{StaticResource Icons.Branch}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGrid.Columns>
<DataGridTemplateColumn Header="ICON">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Path Width="10" Height="10" Margin="4,0,8,0" Data="{StaticResource Icons.Branch}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

<DataGridTemplateColumn Width="*" Header="NAME">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={x:Static c:BranchConverters.ToName}}" ClipToBounds="True"
Classes="monospace" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Border>

<TextBlock Grid.Row="1" Grid.Column="1"
Margin="0,8,0,0"
Text="{DynamicResource Text.DeleteMultiBranch.Tip}"
TextWrapping="Wrap"
Foreground="{DynamicResource Brush.FG2}"
FontStyle="Italic" />
</Grid>
<DataGridTemplateColumn Width="*" Header="NAME">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={x:Static c:BranchConverters.ToName}}" ClipToBounds="True"
Classes="monospace" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Border>

<TextBlock Margin="4,8,0,0"
Text="{DynamicResource Text.DeleteMultiBranch.Tip}"
TextWrapping="Wrap"
Foreground="{DynamicResource Brush.FG2}"
FontStyle="Italic" />
</StackPanel>
</UserControl>
20 changes: 4 additions & 16 deletions src/Views/Repository.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,8 @@
<TreeView.Styles>
<Style Selector="TreeViewItem" x:DataType="vm:BranchTreeNode">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
<Setter Property="CornerRadius">
<Setter.Value>
<MultiBinding Converter="{x:Static c:BranchTreeNodeConverters.ToCornerRadius}">
<Binding Path="$parent[v:Repository].RefereshLocalBranchSelectionToken"/>
<Binding Path="$self"/>
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
<Setter Property="CornerRadius" Value="{Binding CornerRadius}"/>
</Style>

<Style Selector="Grid.repository_leftpanel TreeViewItem:selected /template/ Border#PART_LayoutRoot">
Expand Down Expand Up @@ -278,14 +272,8 @@
<TreeView.Styles>
<Style Selector="TreeViewItem" x:DataType="vm:BranchTreeNode">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
<Setter Property="CornerRadius">
<Setter.Value>
<MultiBinding Converter="{x:Static c:BranchTreeNodeConverters.ToCornerRadius}">
<Binding Path="$parent[v:Repository].RefereshRemoteBranchSelectionToken"/>
<Binding Path="$self"/>
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
<Setter Property="CornerRadius" Value="{Binding CornerRadius}"/>
</Style>

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

0 comments on commit 8e3a8f4

Please sign in to comment.