1
1
// Copyright (c) Files Community
2
2
// Licensed under the MIT License.
3
3
4
- using CommunityToolkit . WinUI ;
5
- using Microsoft . UI . Xaml ;
6
4
using Microsoft . UI . Xaml . Controls ;
7
- using Microsoft . UI . Xaml . Media ;
8
5
using Microsoft . UI . Xaml . Markup ;
6
+ using Microsoft . UI . Xaml . Media ;
7
+ using Microsoft . UI . Xaml . Media . Animation ;
9
8
using Microsoft . UI . Xaml . Shapes ;
10
- using Microsoft . UI . Xaml . Input ;
11
- using Microsoft . UI ;
12
- using Windows . ApplicationModel . Contacts ;
13
9
14
10
namespace Files . App . Controls
15
11
{
16
12
// Content
17
13
[ ContentProperty ( Name = nameof ( Modes ) ) ]
18
- // Template parts
19
- [ TemplatePart ( Name = "PART_ModesHostGrid" , Type = typeof ( Grid ) ) ]
20
- // Visual states
21
- [ TemplateVisualState ( Name = "Focused" , GroupName = "FocusStates" ) ]
22
- [ TemplateVisualState ( Name = "Normal" , GroupName = "FocusStates" ) ]
23
14
public partial class Omnibar : Control
24
15
{
25
- private const string ModesHostGrid = "PART_ModesHostGrid" ;
26
- private const string AutoSuggestPopup = "PART_AutoSuggestPopup" ;
27
- private const string AutoSuggestBoxBorder = "PART_AutoSuggestBoxBorder" ;
16
+ // Constants
17
+
18
+ private const string TemplatePartName_ModesHostGrid = "PART_ModesHostGrid" ;
19
+
20
+ // Fields
28
21
29
22
private Grid ? _modesHostGrid ;
30
- private Popup ? _autoSuggestPopup ;
31
- private Border ? _autoSuggestBoxBorder ;
32
- private bool _isFocused ;
33
- private bool _stillHasFocus ;
23
+
24
+ // Constructor
34
25
35
26
public Omnibar ( )
36
27
{
37
28
DefaultStyleKey = typeof ( Omnibar ) ;
38
29
39
- Modes ??= [ ] ;
30
+ Modes = [ ] ;
31
+ AutoSuggestBoxPadding = new ( 0 , 0 , 0 , 0 ) ;
40
32
}
41
33
34
+ // Methods
35
+
42
36
protected override void OnApplyTemplate ( )
43
37
{
44
- _modesHostGrid = GetTemplateChild ( ModesHostGrid ) as Grid
45
- ?? throw new MissingFieldException ( $ "Could not find { ModesHostGrid } in the given { nameof ( Omnibar ) } 's style.") ;
46
- _autoSuggestPopup = GetTemplateChild ( AutoSuggestPopup ) as Popup
47
- ?? throw new MissingFieldException ( $ "Could not find { AutoSuggestPopup } in the given { nameof ( Omnibar ) } 's style.") ;
48
- _autoSuggestBoxBorder = GetTemplateChild ( AutoSuggestBoxBorder ) as Border
49
- ?? throw new MissingFieldException ( $ "Could not find { AutoSuggestBoxBorder } in the given { nameof ( Omnibar ) } 's style.") ;
50
-
51
- if ( Modes is null )
52
- return ;
38
+ base . OnApplyTemplate ( ) ;
39
+
40
+ _modesHostGrid = GetTemplateChild ( TemplatePartName_ModesHostGrid ) as Grid
41
+ ?? throw new MissingFieldException ( $ "Could not find { TemplatePartName_ModesHostGrid } in the given { nameof ( Omnibar ) } 's style.") ;
53
42
54
- // Add shadow to the popup and set the proper width
55
- _autoSuggestBoxBorder ! . Translation = new ( 0 , 0 , 32 ) ;
56
- _autoSuggestBoxBorder ! . Width = _modesHostGrid ! . ActualWidth ;
43
+ PopulateModes ( ) ;
44
+ }
45
+
46
+ public void PopulateModes ( )
47
+ {
48
+ if ( Modes is null || _modesHostGrid is null )
49
+ return ;
57
50
58
51
// Populate the modes
59
52
foreach ( var mode in Modes )
60
53
{
61
54
// Insert a divider
62
55
if ( _modesHostGrid . Children . Count is not 0 )
63
56
{
64
- var divider = new Rectangle ( )
65
- {
66
- Fill = ( SolidColorBrush ) Application . Current . Resources [ "DividerStrokeColorDefaultBrush" ] ,
67
- Height = 20 ,
68
- Margin = new ( 2 , 0 , 2 , 0 ) ,
69
- Width = 1 ,
70
- } ;
57
+ var divider = new OmnibarModeSeparator ( ) ;
71
58
72
59
_modesHostGrid . ColumnDefinitions . Add ( new ( ) { Width = GridLength . Auto } ) ;
73
60
Grid . SetColumn ( divider , _modesHostGrid . Children . Count ) ;
@@ -78,103 +65,45 @@ protected override void OnApplyTemplate()
78
65
_modesHostGrid . ColumnDefinitions . Add ( new ( ) { Width = GridLength . Auto } ) ;
79
66
Grid . SetColumn ( mode , _modesHostGrid . Children . Count ) ;
80
67
_modesHostGrid . Children . Add ( mode ) ;
81
- mode . Host = this ;
68
+ mode . SetOwner ( this ) ;
82
69
}
83
-
84
- _modesHostGrid . SizeChanged += ModesHostGrid_SizeChanged ;
85
-
86
- GotFocus += Omnibar_GotFocus ;
87
- LostFocus += Omnibar_LostFocus ;
88
- LosingFocus += Omnibar_LosingFocus ;
89
-
90
- UpdateVisualStates ( ) ;
91
-
92
- base . OnApplyTemplate ( ) ;
93
70
}
94
71
95
- // Methods
96
-
97
72
internal void ChangeMode ( OmnibarMode modeToExpand )
98
73
{
99
74
if ( _modesHostGrid is null || Modes is null )
100
- throw new NullReferenceException ( ) ;
101
-
102
- // Reset
103
- foreach ( var column in _modesHostGrid . ColumnDefinitions )
104
- column . Width = GridLength . Auto ;
105
- foreach ( var mode in Modes )
106
- VisualStateManager . GoToState ( mode , "Unfocused" , true ) ;
107
-
108
- // Expand the given mode
109
- VisualStateManager . GoToState ( modeToExpand , "Focused" , true ) ;
110
- _modesHostGrid . ColumnDefinitions [ _modesHostGrid . Children . IndexOf ( modeToExpand ) ] . Width = new ( 1 , GridUnitType . Star ) ;
111
-
112
- CurrentSelectedMode = modeToExpand ;
113
-
114
- UpdateVisualStates ( ) ;
115
- }
116
-
117
- private void UpdateVisualStates ( )
118
- {
119
- VisualStateManager . GoToState ( this , _isFocused ? "Focused" : "Normal" , true ) ;
75
+ return ;
120
76
121
- if ( CurrentSelectedMode is not null && _autoSuggestPopup is not null )
122
- {
123
- // Close anyway
124
- if ( _autoSuggestPopup . IsOpen && CurrentSelectedMode . SuggestionItemsSource is null )
125
- VisualStateManager . GoToState ( this , "PopupClosed" , true ) ;
77
+ var index = _modesHostGrid . Children . IndexOf ( modeToExpand ) ;
126
78
127
- // Decide open or close
128
- if ( _isFocused != _autoSuggestPopup . IsOpen )
129
- VisualStateManager . GoToState ( this , _isFocused && CurrentSelectedMode . SuggestionItemsSource is not null ? "PopupOpened" : "PopupClosed" , true ) ;
130
- }
79
+ // Add the reposition transition to the old item
80
+ //foreach (var mode in Modes)
81
+ // mode.Transitions = [ new RepositionThemeTransition() ];
131
82
132
83
if ( CurrentSelectedMode is not null )
133
- VisualStateManager . GoToState (
134
- CurrentSelectedMode ,
135
- _isFocused
136
- ? "Focused"
137
- : CurrentSelectedMode . ContentOnInactive is null
138
- ? "CurrentUnfocusedWithoutInactiveMode"
139
- : "CurrentUnfocusedWithInactiveMode" ,
140
- true ) ;
141
- }
84
+ VisualStateManager . GoToState ( CurrentSelectedMode , "Unfocused" , true ) ;
142
85
143
- // Events
86
+ // Reset
87
+ foreach ( var column in _modesHostGrid . ColumnDefinitions )
88
+ column . Width = GridLength . Auto ;
144
89
145
- private void ModesHostGrid_SizeChanged ( object sender , SizeChangedEventArgs e )
146
- {
147
- _autoSuggestBoxBorder ! . Width = _modesHostGrid ! . ActualWidth ;
148
- }
90
+ // Expand the given mode
91
+ _modesHostGrid . ColumnDefinitions [ index ] . Width = new ( 1 , GridUnitType . Star ) ;
149
92
150
- private void Omnibar_GotFocus ( object sender , RoutedEventArgs e )
151
- {
152
- _isFocused = true ;
153
- UpdateVisualStates ( ) ;
154
- }
93
+ VisualStateManager . GoToState ( modeToExpand , "Focused" , true ) ;
155
94
156
- private void Omnibar_LosingFocus ( UIElement sender , LosingFocusEventArgs args )
157
- {
158
- // Ignore when user clicks on the TextBox or the button area of an OmnibarMode, Omnibar still has focus anyway
159
- if ( args . NewFocusedElement ? . GetType ( ) is not { } focusedType ||
160
- focusedType == typeof ( TextBox ) ||
161
- focusedType == typeof ( OmnibarMode ) ||
162
- focusedType == typeof ( Omnibar ) )
163
- {
164
- _stillHasFocus = true ;
165
- }
166
- }
95
+ // Remove it again
96
+ //foreach (var mode in Modes)
97
+ // mode.Transitions.Clear();
167
98
168
- private void Omnibar_LostFocus ( object sender , RoutedEventArgs e )
169
- {
170
- if ( _stillHasFocus )
171
- {
172
- _stillHasFocus = false ;
173
- return ;
174
- }
99
+ // Set the correct AutoSuggestBox cursor position
100
+ var itemCount = Modes . Count ;
101
+ var itemIndex = Modes . IndexOf ( modeToExpand ) ;
102
+ var leftPadding = ( itemIndex + 1 ) * modeToExpand . ActualWidth + 9 * itemIndex + 4 ;
103
+ var rightPadding = ( itemCount - itemIndex - 1 ) * modeToExpand . ActualWidth + 9 * ( itemCount - itemIndex - 1 ) + 8 ;
104
+ AutoSuggestBoxPadding = new ( leftPadding , 5 , rightPadding , 6 ) ;
175
105
176
- _isFocused = false ;
177
- UpdateVisualStates ( ) ;
106
+ CurrentSelectedMode = modeToExpand ;
178
107
}
179
108
}
180
109
}
0 commit comments