@@ -31,6 +31,8 @@ public partial class Omnibar : Control
3131 private string _userInput = string . Empty ;
3232 private OmnibarTextChangeReason _textChangeReason = OmnibarTextChangeReason . None ;
3333
34+ private WeakReference < UIElement ? > _previouslyFocusedElement = new ( null ) ;
35+
3436 // Events
3537
3638 public event TypedEventHandler < Omnibar , OmnibarQuerySubmittedEventArgs > ? QuerySubmitted ;
@@ -67,6 +69,7 @@ protected override void OnApplyTemplate()
6769 PopulateModes ( ) ;
6870
6971 SizeChanged += Omnibar_SizeChanged ;
72+ _textBox . GettingFocus += AutoSuggestBox_GettingFocus ;
7073 _textBox . GotFocus += AutoSuggestBox_GotFocus ;
7174 _textBox . LostFocus += AutoSuggestBox_LostFocus ;
7275 _textBox . KeyDown += AutoSuggestBox_KeyDown ;
@@ -104,27 +107,23 @@ public void PopulateModes()
104107 }
105108 }
106109
107- public void ChangeMode ( OmnibarMode modeToExpand , bool shouldFocus = false , bool useTransition = true )
110+ protected void ChangeMode ( OmnibarMode ? oldMode , OmnibarMode newMode )
108111 {
109- if ( _modesHostGrid is null || Modes is null )
112+ if ( _modesHostGrid is null || Modes is null || CurrentSelectedMode is null )
110113 return ;
111114
112115 foreach ( var mode in Modes )
113116 {
114117 // Add the reposition transition to the all modes
115- if ( useTransition )
116- {
117- mode . Transitions = [ new RepositionThemeTransition ( ) ] ;
118- mode . UpdateLayout ( ) ;
119- }
120-
121- mode . OnChangingCurrentMode ( false ) ;
118+ mode . Transitions = [ new RepositionThemeTransition ( ) ] ;
119+ mode . UpdateLayout ( ) ;
120+ mode . IsTabStop = true ;
122121 }
123122
124- var index = _modesHostGrid . Children . IndexOf ( modeToExpand ) ;
123+ var index = _modesHostGrid . Children . IndexOf ( newMode ) ;
125124
126- if ( CurrentSelectedMode is not null )
127- VisualStateManager . GoToState ( CurrentSelectedMode , "Unfocused" , true ) ;
125+ if ( oldMode is not null )
126+ VisualStateManager . GoToState ( oldMode , "Unfocused" , true ) ;
128127
129128 // Reset
130129 foreach ( var column in _modesHostGrid . ColumnDefinitions )
@@ -134,8 +133,8 @@ public void ChangeMode(OmnibarMode modeToExpand, bool shouldFocus = false, bool
134133 _modesHostGrid . ColumnDefinitions [ index ] . Width = new ( 1 , GridUnitType . Star ) ;
135134
136135 var itemCount = Modes . Count ;
137- var itemIndex = Modes . IndexOf ( modeToExpand ) ;
138- var modeButtonWidth = modeToExpand . ActualWidth ;
136+ var itemIndex = Modes . IndexOf ( newMode ) ;
137+ var modeButtonWidth = newMode . ActualWidth ;
139138 var modeSeparatorWidth = itemCount is not 0 or 1 ? _modesHostGrid . Children [ 1 ] is FrameworkElement frameworkElement ? frameworkElement . ActualWidth : 0 : 0 ;
140139
141140 var leftPadding = ( itemIndex + 1 ) * modeButtonWidth + modeSeparatorWidth * itemIndex ;
@@ -144,51 +143,52 @@ public void ChangeMode(OmnibarMode modeToExpand, bool shouldFocus = false, bool
144143 // Set the correct AutoSuggestBox cursor position
145144 AutoSuggestBoxPadding = new ( leftPadding , 0 , rightPadding , 0 ) ;
146145
147- CurrentSelectedMode = modeToExpand ;
148-
149146 _textChangeReason = OmnibarTextChangeReason . ProgrammaticChange ;
150- ChangeTextBoxText ( CurrentSelectedMode . Text ?? string . Empty ) ;
151-
152- // Move cursor of the TextBox to the tail
153- _textBox . Select ( _textBox . Text . Length , 0 ) ;
147+ ChangeTextBoxText ( newMode . Text ?? string . Empty ) ;
154148
155- VisualStateManager . GoToState ( CurrentSelectedMode , "Focused" , true ) ;
156- CurrentSelectedMode . OnChangingCurrentMode ( true ) ;
157-
158- if ( IsFocused )
159- {
160- VisualStateManager . GoToState ( CurrentSelectedMode , "Focused" , true ) ;
161- VisualStateManager . GoToState ( _textBox , "InputAreaVisible" , true ) ;
162- }
163- else if ( CurrentSelectedMode ? . ContentOnInactive is not null )
149+ VisualStateManager . GoToState ( newMode , "Focused" , true ) ;
150+ newMode . IsTabStop = false ;
151+ if ( newMode . IsAutoFocusEnabled )
164152 {
165- VisualStateManager . GoToState ( CurrentSelectedMode , "CurrentUnfocused" , true ) ;
166- VisualStateManager . GoToState ( _textBox , "InputAreaCollapsed" , true ) ;
153+ _textBox . Focus ( FocusState . Pointer ) ;
167154 }
168155 else
169156 {
170- VisualStateManager . GoToState ( _textBox , "InputAreaVisible" , true ) ;
171- }
172-
173- if ( shouldFocus )
174- _textBox . Focus ( FocusState . Keyboard ) ;
157+ if ( IsFocused )
158+ {
159+ VisualStateManager . GoToState ( newMode , "Focused" , true ) ;
160+ VisualStateManager . GoToState ( _textBox , "InputAreaVisible" , true ) ;
161+ }
162+ else if ( newMode ? . ContentOnInactive is not null )
163+ {
164+ VisualStateManager . GoToState ( newMode , "CurrentUnfocused" , true ) ;
165+ VisualStateManager . GoToState ( _textBox , "InputAreaCollapsed" , true ) ;
166+ }
167+ else
168+ {
169+ VisualStateManager . GoToState ( _textBox , "InputAreaVisible" , true ) ;
170+ }
175171
176- TryToggleIsSuggestionsPopupOpen ( IsFocused && CurrentSelectedMode ? . SuggestionItemsSource is not null ) ;
172+ TryToggleIsSuggestionsPopupOpen ( true ) ;
173+ }
177174
178175 // Remove the reposition transition from the all modes
179- if ( useTransition )
176+ foreach ( var mode in Modes )
180177 {
181- foreach ( var mode in Modes )
182- {
183- mode . Transitions . Clear ( ) ;
184- mode . UpdateLayout ( ) ;
185- }
178+ mode . Transitions . Clear ( ) ;
179+ mode . UpdateLayout ( ) ;
186180 }
187181 }
188182
183+ internal protected void FocusTextBox ( )
184+ {
185+ _textBox . Focus ( FocusState . Keyboard ) ;
186+ }
187+
189188 public bool TryToggleIsSuggestionsPopupOpen ( bool wantToOpen )
190189 {
191- if ( wantToOpen && ( ! IsFocused || CurrentSelectedMode ? . SuggestionItemsSource is null || ( CurrentSelectedMode ? . SuggestionItemsSource is IList collection && collection . Count is 0 ) ) )
190+ if ( wantToOpen && ( ! IsFocused || CurrentSelectedMode ? . SuggestionItemsSource is null || ( CurrentSelectedMode ? . SuggestionItemsSource is IList collection && collection . Count is 0 ) ) ||
191+ _textBoxSuggestionsPopup is null )
192192 return false ;
193193
194194 _textBoxSuggestionsPopup . IsOpen = wantToOpen ;
0 commit comments