Skip to content

Commit a738026

Browse files
committed
Ecs to unfocus
1 parent 98951ff commit a738026

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/Files.App.Controls/Omnibar/Omnibar.Events.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ private void Omnibar_SizeChanged(object sender, SizeChangedEventArgs e)
1414
_textBoxSuggestionsContainerBorder.Width = ActualWidth;
1515
}
1616

17+
private void AutoSuggestBox_GettingFocus(UIElement sender, GettingFocusEventArgs args)
18+
{
19+
if (args.OldFocusedElement is null)
20+
return;
21+
22+
_previouslyFocusedElement = new(args.OldFocusedElement as UIElement);
23+
}
24+
1725
private void AutoSuggestBox_GotFocus(object sender, RoutedEventArgs e)
1826
{
1927
IsFocused = true;
@@ -65,12 +73,20 @@ private void AutoSuggestBox_KeyDown(object sender, KeyRoutedEventArgs e)
6573
ChooseSuggestionItem(_textBoxSuggestionsListView.SelectedItem);
6674
}
6775
}
68-
else if (e.Key == VirtualKey.Escape && _textBoxSuggestionsPopup.IsOpen)
76+
else if (e.Key == VirtualKey.Escape)
6977
{
7078
e.Handled = true;
7179

72-
RevertTextToUserInput();
73-
_textBoxSuggestionsPopup.IsOpen = false;
80+
if (_textBoxSuggestionsPopup.IsOpen)
81+
{
82+
RevertTextToUserInput();
83+
_textBoxSuggestionsPopup.IsOpen = false;
84+
}
85+
else
86+
{
87+
_previouslyFocusedElement.TryGetTarget(out var previouslyFocusedElement);
88+
previouslyFocusedElement?.Focus(FocusState.Programmatic);
89+
}
7490
}
7591
else
7692
{

src/Files.App.Controls/Omnibar/Omnibar.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

0 commit comments

Comments
 (0)