1- using System ;
1+ using System ;
22using System . Collections . Generic ;
33using System . IO ;
44
55using Avalonia ;
66using Avalonia . Collections ;
7+ using Avalonia . Media ;
78
89using CommunityToolkit . Mvvm . ComponentModel ;
910
1011namespace SourceGit . ViewModels
1112{
12- public enum BranchTreeNodeType
13- {
14- DetachedHead ,
15- Remote ,
16- Folder ,
17- Branch ,
18- }
19-
2013 public class BranchTreeNode : ObservableObject
2114 {
22- public const double DEFAULT_CORNER = 4.0 ;
23-
24- public string Name { get ; set ; }
25- public BranchTreeNodeType Type { get ; set ; }
26- public object Backend { get ; set ; }
27- public bool IsFiltered { get ; set ; }
28- public List < BranchTreeNode > Children { get ; set ; } = new List < BranchTreeNode > ( ) ;
29-
30- public bool IsUpstreamTrackStatusVisible
31- {
32- get => IsBranch && ! string . IsNullOrEmpty ( ( Backend as Models . Branch ) . UpstreamTrackStatus ) ;
33- }
34-
35- public string UpstreamTrackStatus
36- {
37- get => Type == BranchTreeNodeType . Branch ? ( Backend as Models . Branch ) . UpstreamTrackStatus : "" ;
38- }
39-
40- public bool IsRemote
15+ public string Name { get ; private set ; } = string . Empty ;
16+ public object Backend { get ; private set ; } = null ;
17+ public int Depth { get ; set ; } = 0 ;
18+ public bool IsFiltered { get ; set ; } = false ;
19+ public List < BranchTreeNode > Children { get ; private set ; } = new List < BranchTreeNode > ( ) ;
20+
21+ public bool IsExpanded
4122 {
42- get => Type == BranchTreeNodeType . Remote ;
23+ get => _isExpanded ;
24+ set => SetProperty ( ref _isExpanded , value ) ;
4325 }
44-
45- public bool IsFolder
26+
27+ public CornerRadius CornerRadius
4628 {
47- get => Type == BranchTreeNodeType . Folder ;
29+ get => _cornerRadius ;
30+ set => SetProperty ( ref _cornerRadius , value ) ;
4831 }
49-
32+
5033 public bool IsBranch
5134 {
52- get => Type == BranchTreeNodeType . Branch ;
35+ get => Backend is Models . Branch ;
5336 }
5437
55- public bool IsDetachedHead
56- {
57- get => Type == BranchTreeNodeType . DetachedHead ;
58- }
59-
60- public bool IsCurrent
38+ public bool IsUpstreamTrackStatusVisible
6139 {
62- get => IsBranch && ( Backend as Models . Branch ) . IsCurrent ;
40+ get => Backend is Models . Branch { IsLocal : true } branch && ! string . IsNullOrEmpty ( branch . UpstreamTrackStatus ) ;
6341 }
6442
65- public bool IsSelected
43+ public string UpstreamTrackStatus
6644 {
67- get => _isSelected ;
68- set => SetProperty ( ref _isSelected , value ) ;
45+ get => Backend is Models . Branch branch ? branch . UpstreamTrackStatus : "" ;
6946 }
7047
71- public bool IsExpanded
48+ public FontWeight NameFontWeight
7249 {
73- get => _isExpanded ;
74- set => SetProperty ( ref _isExpanded , value ) ;
50+ get => Backend is Models . Branch { IsCurrent : true } ? FontWeight . Bold : FontWeight . Regular ;
7551 }
7652
7753 public string Tooltip
7854 {
79- get
80- {
81- if ( Backend is Models . Branch b )
82- return b . FriendlyName ;
83-
84- return null ;
85- }
86- }
87-
88- public CornerRadius CornerRadius
89- {
90- get => _cornerRadius ;
91- set => SetProperty ( ref _cornerRadius , value ) ;
55+ get => Backend is Models . Branch b ? b . FriendlyName : null ;
9256 }
93-
94- public void UpdateCornerRadius ( ref BranchTreeNode prev )
95- {
96- if ( _isSelected && prev != null && prev . IsSelected )
97- {
98- var prevTop = prev . CornerRadius . TopLeft ;
99- prev . CornerRadius = new CornerRadius ( prevTop , 0 ) ;
100- CornerRadius = new CornerRadius ( 0 , DEFAULT_CORNER ) ;
101- }
102- else if ( CornerRadius . TopLeft != DEFAULT_CORNER ||
103- CornerRadius . BottomLeft != DEFAULT_CORNER )
104- {
105- CornerRadius = new CornerRadius ( DEFAULT_CORNER ) ;
106- }
107-
108- prev = this ;
109-
110- if ( ! IsBranch && IsExpanded )
111- {
112- foreach ( var child in Children )
113- child . UpdateCornerRadius ( ref prev ) ;
114- }
115- }
116-
117- private bool _isSelected = false ;
57+
11858 private bool _isExpanded = false ;
119- private CornerRadius _cornerRadius = new CornerRadius ( DEFAULT_CORNER ) ;
59+ private CornerRadius _cornerRadius = new CornerRadius ( 4 ) ;
12060
12161 public class Builder
12262 {
@@ -133,7 +73,6 @@ public void Run(List<Models.Branch> branches, List<Models.Remote> remotes, bool
13373 var node = new BranchTreeNode ( )
13474 {
13575 Name = remote . Name ,
136- Type = BranchTreeNodeType . Remote ,
13776 Backend = remote ,
13877 IsExpanded = bForceExpanded || _expanded . Contains ( path ) ,
13978 } ;
@@ -176,9 +115,13 @@ private void CollectExpandedNodes(List<BranchTreeNode> nodes, string prefix)
176115 {
177116 foreach ( var node in nodes )
178117 {
118+ if ( node . Backend is Models . Branch )
119+ continue ;
120+
179121 var path = prefix + "/" + node . Name ;
180- if ( node . Type != BranchTreeNodeType . Branch && node . IsExpanded )
122+ if ( node . IsExpanded )
181123 _expanded . Add ( path ) ;
124+
182125 CollectExpandedNodes ( node . Children , path ) ;
183126 }
184127 }
@@ -191,7 +134,6 @@ private void MakeBranchNode(Models.Branch branch, List<BranchTreeNode> roots, Di
191134 roots . Add ( new BranchTreeNode ( )
192135 {
193136 Name = branch . Name ,
194- Type = BranchTreeNodeType . Branch ,
195137 Backend = branch ,
196138 IsExpanded = false ,
197139 IsFiltered = isFiltered ,
@@ -215,7 +157,6 @@ private void MakeBranchNode(Models.Branch branch, List<BranchTreeNode> roots, Di
215157 lastFolder = new BranchTreeNode ( )
216158 {
217159 Name = name ,
218- Type = BranchTreeNodeType . Folder ,
219160 IsExpanded = bForceExpanded || branch . IsCurrent || _expanded . Contains ( folder ) ,
220161 } ;
221162 roots . Add ( lastFolder ) ;
@@ -226,7 +167,6 @@ private void MakeBranchNode(Models.Branch branch, List<BranchTreeNode> roots, Di
226167 var cur = new BranchTreeNode ( )
227168 {
228169 Name = name ,
229- Type = BranchTreeNodeType . Folder ,
230170 IsExpanded = bForceExpanded || branch . IsCurrent || _expanded . Contains ( folder ) ,
231171 } ;
232172 lastFolder . Children . Add ( cur ) ;
@@ -238,10 +178,9 @@ private void MakeBranchNode(Models.Branch branch, List<BranchTreeNode> roots, Di
238178 sepIdx = branch . Name . IndexOf ( '/' , start ) ;
239179 }
240180
241- lastFolder . Children . Add ( new BranchTreeNode ( )
181+ lastFolder ? . Children . Add ( new BranchTreeNode ( )
242182 {
243183 Name = Path . GetFileName ( branch . Name ) ,
244- Type = branch . IsHead ? BranchTreeNodeType . DetachedHead : BranchTreeNodeType . Branch ,
245184 Backend = branch ,
246185 IsExpanded = false ,
247186 IsFiltered = isFiltered ,
@@ -252,16 +191,13 @@ private void SortNodes(List<BranchTreeNode> nodes)
252191 {
253192 nodes . Sort ( ( l , r ) =>
254193 {
255- if ( l . Type == BranchTreeNodeType . DetachedHead )
256- {
194+ if ( l . Backend is Models . Branch { IsHead : true } )
257195 return - 1 ;
258- }
259- if ( l . Type == r . Type )
260- {
261- return l . Name . CompareTo ( r . Name ) ;
262- }
263196
264- return ( int ) l . Type - ( int ) r . Type ;
197+ if ( l . Backend is Models . Branch )
198+ return r . Backend is Models . Branch ? string . Compare ( l . Name , r . Name , StringComparison . Ordinal ) : 1 ;
199+
200+ return r . Backend is Models . Branch ? - 1 : string . Compare ( l . Name , r . Name , StringComparison . Ordinal ) ;
265201 } ) ;
266202
267203 foreach ( var node in nodes )
0 commit comments