1010
1111namespace SourceGit . Views
1212{
13+ public class BranchSelectorChoice : TextBlock
14+ {
15+ public static readonly StyledProperty < Models . Branch > BranchProperty =
16+ AvaloniaProperty . Register < BranchSelectorChoice , Models . Branch > ( nameof ( Branch ) ) ;
17+
18+ public Models . Branch Branch
19+ {
20+ get => GetValue ( BranchProperty ) ;
21+ set => SetValue ( BranchProperty , value ) ;
22+ }
23+
24+ public static readonly StyledProperty < bool > UsePureNameProperty =
25+ AvaloniaProperty . Register < BranchSelectorChoice , bool > ( nameof ( UsePureName ) ) ;
26+
27+ public bool UsePureName
28+ {
29+ get => GetValue ( UsePureNameProperty ) ;
30+ set => SetValue ( UsePureNameProperty , value ) ;
31+ }
32+
33+ protected override Type StyleKeyOverride => typeof ( TextBlock ) ;
34+
35+ protected override void OnPropertyChanged ( AvaloniaPropertyChangedEventArgs change )
36+ {
37+ base . OnPropertyChanged ( change ) ;
38+
39+ if ( change . Property == BranchProperty || change . Property == UsePureNameProperty )
40+ {
41+ if ( Branch is { } branch )
42+ Text = UsePureName ? branch . Name : branch . FriendlyName ;
43+ else
44+ Text = "---" ;
45+ }
46+ }
47+ }
48+
1349 public partial class BranchSelector : UserControl
1450 {
1551 public static readonly StyledProperty < List < Models . Branch > > BranchesProperty =
@@ -30,13 +66,16 @@ public List<Models.Branch> VisibleBranches
3066 set => SetValue ( VisibleBranchesProperty , value ) ;
3167 }
3268
33- public static readonly StyledProperty < Models . Branch > SelectedBranchProperty =
34- AvaloniaProperty . Register < BranchSelector , Models . Branch > ( nameof ( SelectedBranch ) ) ;
69+ public static readonly DirectProperty < BranchSelector , Models . Branch > SelectedBranchProperty =
70+ AvaloniaProperty . RegisterDirect < BranchSelector , Models . Branch > (
71+ nameof ( SelectedBranch ) ,
72+ o => o . SelectedBranch ,
73+ ( o , v ) => o . SelectedBranch = v ) ;
3574
3675 public Models . Branch SelectedBranch
3776 {
38- get => GetValue ( SelectedBranchProperty ) ;
39- set => SetValue ( SelectedBranchProperty , value ) ;
77+ get => _selectedBranch ;
78+ set => SetAndRaise ( SelectedBranchProperty , ref _selectedBranch , value ) ;
4079 }
4180
4281 public static readonly StyledProperty < bool > IsDropDownOpenedProperty =
@@ -57,6 +96,15 @@ public string SearchFilter
5796 set => SetValue ( SearchFilterProperty , value ) ;
5897 }
5998
99+ public static readonly StyledProperty < bool > UsePureNameProperty =
100+ AvaloniaProperty . Register < BranchSelector , bool > ( nameof ( UsePureName ) ) ;
101+
102+ public bool UsePureName
103+ {
104+ get => GetValue ( UsePureNameProperty ) ;
105+ set => SetValue ( UsePureNameProperty , value ) ;
106+ }
107+
60108 public BranchSelector ( )
61109 {
62110 Focusable = true ;
@@ -97,7 +145,7 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
97145
98146 SetCurrentValue ( VisibleBranchesProperty , visible ) ;
99147 if ( ! keepSelection && visible . Count > 0 )
100- SetCurrentValue ( SelectedBranchProperty , visible [ 0 ] ) ;
148+ SelectedBranch = visible [ 0 ] ;
101149 }
102150 }
103151 }
@@ -215,6 +263,12 @@ private void OnDropDownListKeyDown(object _, KeyEventArgs e)
215263 }
216264 }
217265
266+ private void OnDropDownListSelectionChanged ( object sender , SelectionChangedEventArgs e )
267+ {
268+ if ( e . AddedItems . Count == 1 && e . AddedItems [ 0 ] is Models . Branch branch )
269+ SelectedBranch = branch ;
270+ }
271+
218272 private void OnDropDownItemPointerPressed ( object sender , PointerPressedEventArgs e )
219273 {
220274 if ( sender is Control { DataContext : Models . Branch branch } )
@@ -225,5 +279,6 @@ private void OnDropDownItemPointerPressed(object sender, PointerPressedEventArgs
225279 }
226280
227281 private Popup _popup = null ;
282+ private Models . Branch _selectedBranch = null ;
228283 }
229284}
0 commit comments