1
- using System ;
1
+ using System ;
2
2
using System . Collections . Generic ;
3
3
using System . IO ;
4
4
5
5
using Avalonia ;
6
6
using Avalonia . Collections ;
7
+ using Avalonia . Media ;
7
8
8
9
using CommunityToolkit . Mvvm . ComponentModel ;
9
10
10
11
namespace SourceGit . ViewModels
11
12
{
12
- public enum BranchTreeNodeType
13
- {
14
- DetachedHead ,
15
- Remote ,
16
- Folder ,
17
- Branch ,
18
- }
19
-
20
13
public class BranchTreeNode : ObservableObject
21
14
{
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
41
22
{
42
- get => Type == BranchTreeNodeType . Remote ;
23
+ get => _isExpanded ;
24
+ set => SetProperty ( ref _isExpanded , value ) ;
43
25
}
44
-
45
- public bool IsFolder
26
+
27
+ public CornerRadius CornerRadius
46
28
{
47
- get => Type == BranchTreeNodeType . Folder ;
29
+ get => _cornerRadius ;
30
+ set => SetProperty ( ref _cornerRadius , value ) ;
48
31
}
49
-
32
+
50
33
public bool IsBranch
51
34
{
52
- get => Type == BranchTreeNodeType . Branch ;
35
+ get => Backend is Models . Branch ;
53
36
}
54
37
55
- public bool IsDetachedHead
56
- {
57
- get => Type == BranchTreeNodeType . DetachedHead ;
58
- }
59
-
60
- public bool IsCurrent
38
+ public bool IsUpstreamTrackStatusVisible
61
39
{
62
- get => IsBranch && ( Backend as Models . Branch ) . IsCurrent ;
40
+ get => Backend is Models . Branch { IsLocal : true } branch && ! string . IsNullOrEmpty ( branch . UpstreamTrackStatus ) ;
63
41
}
64
42
65
- public bool IsSelected
43
+ public string UpstreamTrackStatus
66
44
{
67
- get => _isSelected ;
68
- set => SetProperty ( ref _isSelected , value ) ;
45
+ get => Backend is Models . Branch branch ? branch . UpstreamTrackStatus : "" ;
69
46
}
70
47
71
- public bool IsExpanded
48
+ public FontWeight NameFontWeight
72
49
{
73
- get => _isExpanded ;
74
- set => SetProperty ( ref _isExpanded , value ) ;
50
+ get => Backend is Models . Branch { IsCurrent : true } ? FontWeight . Bold : FontWeight . Regular ;
75
51
}
76
52
77
53
public string Tooltip
78
54
{
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 ;
92
56
}
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
+
118
58
private bool _isExpanded = false ;
119
- private CornerRadius _cornerRadius = new CornerRadius ( DEFAULT_CORNER ) ;
59
+ private CornerRadius _cornerRadius = new CornerRadius ( 4 ) ;
120
60
121
61
public class Builder
122
62
{
@@ -133,7 +73,6 @@ public void Run(List<Models.Branch> branches, List<Models.Remote> remotes, bool
133
73
var node = new BranchTreeNode ( )
134
74
{
135
75
Name = remote . Name ,
136
- Type = BranchTreeNodeType . Remote ,
137
76
Backend = remote ,
138
77
IsExpanded = bForceExpanded || _expanded . Contains ( path ) ,
139
78
} ;
@@ -176,9 +115,13 @@ private void CollectExpandedNodes(List<BranchTreeNode> nodes, string prefix)
176
115
{
177
116
foreach ( var node in nodes )
178
117
{
118
+ if ( node . Backend is Models . Branch )
119
+ continue ;
120
+
179
121
var path = prefix + "/" + node . Name ;
180
- if ( node . Type != BranchTreeNodeType . Branch && node . IsExpanded )
122
+ if ( node . IsExpanded )
181
123
_expanded . Add ( path ) ;
124
+
182
125
CollectExpandedNodes ( node . Children , path ) ;
183
126
}
184
127
}
@@ -191,7 +134,6 @@ private void MakeBranchNode(Models.Branch branch, List<BranchTreeNode> roots, Di
191
134
roots . Add ( new BranchTreeNode ( )
192
135
{
193
136
Name = branch . Name ,
194
- Type = BranchTreeNodeType . Branch ,
195
137
Backend = branch ,
196
138
IsExpanded = false ,
197
139
IsFiltered = isFiltered ,
@@ -215,7 +157,6 @@ private void MakeBranchNode(Models.Branch branch, List<BranchTreeNode> roots, Di
215
157
lastFolder = new BranchTreeNode ( )
216
158
{
217
159
Name = name ,
218
- Type = BranchTreeNodeType . Folder ,
219
160
IsExpanded = bForceExpanded || branch . IsCurrent || _expanded . Contains ( folder ) ,
220
161
} ;
221
162
roots . Add ( lastFolder ) ;
@@ -226,7 +167,6 @@ private void MakeBranchNode(Models.Branch branch, List<BranchTreeNode> roots, Di
226
167
var cur = new BranchTreeNode ( )
227
168
{
228
169
Name = name ,
229
- Type = BranchTreeNodeType . Folder ,
230
170
IsExpanded = bForceExpanded || branch . IsCurrent || _expanded . Contains ( folder ) ,
231
171
} ;
232
172
lastFolder . Children . Add ( cur ) ;
@@ -238,10 +178,9 @@ private void MakeBranchNode(Models.Branch branch, List<BranchTreeNode> roots, Di
238
178
sepIdx = branch . Name . IndexOf ( '/' , start ) ;
239
179
}
240
180
241
- lastFolder . Children . Add ( new BranchTreeNode ( )
181
+ lastFolder ? . Children . Add ( new BranchTreeNode ( )
242
182
{
243
183
Name = Path . GetFileName ( branch . Name ) ,
244
- Type = branch . IsHead ? BranchTreeNodeType . DetachedHead : BranchTreeNodeType . Branch ,
245
184
Backend = branch ,
246
185
IsExpanded = false ,
247
186
IsFiltered = isFiltered ,
@@ -252,16 +191,13 @@ private void SortNodes(List<BranchTreeNode> nodes)
252
191
{
253
192
nodes . Sort ( ( l , r ) =>
254
193
{
255
- if ( l . Type == BranchTreeNodeType . DetachedHead )
256
- {
194
+ if ( l . Backend is Models . Branch { IsHead : true } )
257
195
return - 1 ;
258
- }
259
- if ( l . Type == r . Type )
260
- {
261
- return l . Name . CompareTo ( r . Name ) ;
262
- }
263
196
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 ) ;
265
201
} ) ;
266
202
267
203
foreach ( var node in nodes )
0 commit comments