Skip to content

Commit 90dd88a

Browse files
committed
refactor: use a custom indent icon instead of tree depth margin for worktrees
Signed-off-by: leo <longshuang@msn.cn>
1 parent 242ab5b commit 90dd88a

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

src/ViewModels/Worktree.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ public class Worktree : ObservableObject
1010
public Models.Worktree Backend { get; private set; }
1111
public bool IsMain { get; private set; }
1212
public bool IsCurrent { get; private set; }
13+
public bool IsLast { get; private set; }
1314
public string DisplayPath { get; private set; }
1415
public string Name { get; private set; }
1516
public string Branch { get; private set; }
16-
public int Depth { get; private set; }
1717

1818
public bool IsLocked
1919
{
@@ -24,15 +24,15 @@ public bool IsLocked
2424
public string FullPath => Backend.FullPath;
2525
public string Head => Backend.Head;
2626

27-
public Worktree(DirectoryInfo repo, Models.Worktree wt, bool isMain)
27+
public Worktree(DirectoryInfo repo, Models.Worktree wt, bool isMain, bool isLast)
2828
{
2929
Backend = wt;
3030
IsMain = isMain;
3131
IsCurrent = IsCurrentWorktree(repo, wt);
32+
IsLast = isLast;
3233
DisplayPath = IsCurrent ? string.Empty : Path.GetRelativePath(repo.FullName, wt.FullPath);
3334
Name = GenerateName();
3435
Branch = GenerateBranchName();
35-
Depth = isMain ? 0 : 1;
3636
IsLocked = wt.IsLocked;
3737
}
3838

@@ -43,9 +43,9 @@ public static List<Worktree> Build(string repo, List<Models.Worktree> worktrees)
4343

4444
var repoDir = new DirectoryInfo(repo);
4545
var nodes = new List<Worktree>();
46-
nodes.Add(new(repoDir, worktrees[0], true));
46+
nodes.Add(new(repoDir, worktrees[0], true, false));
4747
for (int i = 1; i < worktrees.Count; i++)
48-
nodes.Add(new(repoDir, worktrees[i], false));
48+
nodes.Add(new(repoDir, worktrees[i], false, i == worktrees.Count - 1));
4949

5050
return nodes;
5151
}

src/Views/Repository.axaml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@
412412
Height="0"
413413
Margin="12,0,4,0"
414414
Classes="repo_left_content_list"
415-
ItemsSource="{Binding Worktrees}"
415+
ItemsSource="{Binding Worktrees, Mode=OneWay}"
416416
SelectionMode="Single"
417417
PropertyChanged="OnWorktreeListPropertyChanged"
418418
IsVisible="{Binding IsWorktreeGroupExpanded, Mode=OneWay}">
@@ -473,13 +473,12 @@
473473
</Grid>
474474
</StackPanel>
475475
</ToolTip.Tip>
476-
<Grid ColumnDefinitions="Auto,*,22"
477-
Margin="{Binding Depth, Converter={x:Static c:IntConverters.ToTreeMargin}}"
478-
VerticalAlignment="Center">
479-
<v:WorktreeIcon Grid.Column="0"
476+
<Grid ColumnDefinitions="Auto,Auto,*,22" VerticalAlignment="Center">
477+
<v:WorktreeDepthIcon Grid.Column="0" Brush="{DynamicResource Brush.Border1}"/>
478+
<v:WorktreeIcon Grid.Column="1"
480479
Width="10" Height="10"
481480
Margin="8,0,0,0"/>
482-
<TextBlock Grid.Column="1"
481+
<TextBlock Grid.Column="2"
483482
Margin="8,0,0,0"
484483
Foreground="{DynamicResource Brush.FG2}"
485484
TextTrimming="CharacterEllipsis">
@@ -488,7 +487,7 @@
488487
Foreground="{DynamicResource Brush.FG1}"/>
489488
<Run Text="{Binding DisplayPath, Mode=OneWay}"/>
490489
</TextBlock>
491-
<Path Grid.Column="2"
490+
<Path Grid.Column="3"
492491
Width="10" Height="10"
493492
Margin="4,0,0,0"
494493
Data="{StaticResource Icons.Lock}"

src/Views/WorktreeDepthIcon.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
3+
using Avalonia;
4+
using Avalonia.Controls;
5+
using Avalonia.Media;
6+
7+
namespace SourceGit.Views
8+
{
9+
public class WorktreeDepthIcon : Control
10+
{
11+
public static readonly StyledProperty<IBrush> BrushProperty =
12+
AvaloniaProperty.Register<WorktreeDepthIcon, IBrush>(nameof(Brush), Brushes.Transparent);
13+
14+
public IBrush Brush
15+
{
16+
get => GetValue(BrushProperty);
17+
set => SetValue(BrushProperty, value);
18+
}
19+
20+
public override void Render(DrawingContext context)
21+
{
22+
if (DataContext is ViewModels.Worktree wt)
23+
{
24+
if (wt.IsMain)
25+
return;
26+
27+
var pen = new Pen(Brush);
28+
var h = Bounds.Height;
29+
var halfH = h * 0.5;
30+
31+
if (wt.IsLast)
32+
context.DrawLine(pen, new Point(12.5, 0), new Point(12.5, halfH));
33+
else
34+
context.DrawLine(pen, new Point(12.5, 0), new Point(12.5, h));
35+
36+
context.DrawLine(pen, new Point(12.5, halfH), new Point(18, halfH));
37+
}
38+
}
39+
40+
protected override void OnDataContextChanged(EventArgs e)
41+
{
42+
base.OnDataContextChanged(e);
43+
InvalidateMeasure();
44+
}
45+
46+
protected override Size MeasureOverride(Size availableSize)
47+
{
48+
if (DataContext is ViewModels.Worktree wt)
49+
{
50+
if (wt.IsMain)
51+
return new Size(0, 0);
52+
53+
return new Size(18, availableSize.Height);
54+
}
55+
56+
return new Size(0, 0);
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)