11// Copyright (c) Files Community
22// Licensed under the MIT License.
33
4- using CommunityToolkit . WinUI ;
5- using Microsoft . UI . Xaml ;
64using Microsoft . UI . Xaml . Controls ;
7- using Microsoft . UI . Xaml . Media ;
85using Microsoft . UI . Xaml . Markup ;
6+ using Microsoft . UI . Xaml . Media ;
7+ using Microsoft . UI . Xaml . Media . Animation ;
98using Microsoft . UI . Xaml . Shapes ;
10- using Microsoft . UI . Xaml . Input ;
11- using Microsoft . UI ;
12- using Windows . ApplicationModel . Contacts ;
139
1410namespace Files . App . Controls
1511{
1612 // Content
1713 [ 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" ) ]
2314 public partial class Omnibar : Control
2415 {
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
2821
2922 private Grid ? _modesHostGrid ;
30- private Popup ? _autoSuggestPopup ;
31- private Border ? _autoSuggestBoxBorder ;
32- private bool _isFocused ;
33- private bool _stillHasFocus ;
23+
24+ // Constructor
3425
3526 public Omnibar ( )
3627 {
3728 DefaultStyleKey = typeof ( Omnibar ) ;
3829
39- Modes ??= [ ] ;
30+ Modes = [ ] ;
31+ AutoSuggestBoxPadding = new ( 0 , 0 , 0 , 0 ) ;
4032 }
4133
34+ // Methods
35+
4236 protected override void OnApplyTemplate ( )
4337 {
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.") ;
5342
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 ;
5750
5851 // Populate the modes
5952 foreach ( var mode in Modes )
6053 {
6154 // Insert a divider
6255 if ( _modesHostGrid . Children . Count is not 0 )
6356 {
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 ( ) ;
7158
7259 _modesHostGrid . ColumnDefinitions . Add ( new ( ) { Width = GridLength . Auto } ) ;
7360 Grid . SetColumn ( divider , _modesHostGrid . Children . Count ) ;
@@ -78,103 +65,45 @@ protected override void OnApplyTemplate()
7865 _modesHostGrid . ColumnDefinitions . Add ( new ( ) { Width = GridLength . Auto } ) ;
7966 Grid . SetColumn ( mode , _modesHostGrid . Children . Count ) ;
8067 _modesHostGrid . Children . Add ( mode ) ;
81- mode . Host = this ;
68+ mode . SetOwner ( this ) ;
8269 }
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 ( ) ;
9370 }
9471
95- // Methods
96-
9772 internal void ChangeMode ( OmnibarMode modeToExpand )
9873 {
9974 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 ;
12076
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 ) ;
12678
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() ];
13182
13283 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 ) ;
14285
143- // Events
86+ // Reset
87+ foreach ( var column in _modesHostGrid . ColumnDefinitions )
88+ column . Width = GridLength . Auto ;
14489
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 ) ;
14992
150- private void Omnibar_GotFocus ( object sender , RoutedEventArgs e )
151- {
152- _isFocused = true ;
153- UpdateVisualStates ( ) ;
154- }
93+ VisualStateManager . GoToState ( modeToExpand , "Focused" , true ) ;
15594
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();
16798
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 ) ;
175105
176- _isFocused = false ;
177- UpdateVisualStates ( ) ;
106+ CurrentSelectedMode = modeToExpand ;
178107 }
179108 }
180109}
0 commit comments