Skip to content
24 changes: 9 additions & 15 deletions src/Files.App.Controls/Omnibar/Omnibar.Events.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Copyright (c) Files Community
// Licensed under the MIT License.

using Microsoft.UI.Input;
using Microsoft.UI.Xaml.Input;
using Windows.System;
using Windows.UI.Core;

namespace Files.App.Controls
{
Expand Down Expand Up @@ -47,12 +45,16 @@ private void AutoSuggestBox_GotFocus(object sender, RoutedEventArgs e)
private void AutoSuggestBox_LostFocus(object sender, RoutedEventArgs e)
{
// TextBox still has focus if the context menu for selected text is open
if (_textBox.ContextFlyout.IsOpen)
return;

var element = Microsoft.UI.Xaml.Input.FocusManager.GetFocusedElement(this.XamlRoot);
if (element is FlyoutBase or Popup)
return;

GlobalHelper.WriteDebugStringForOmnibar("The TextBox lost the focus.");

IsFocused = false;

// Reset to the default mode when Omnibar loses focus
CurrentSelectedMode = Modes?.FirstOrDefault();
}

private async void AutoSuggestBox_KeyDown(object sender, KeyRoutedEventArgs e)
Expand Down Expand Up @@ -112,16 +114,6 @@ private async void AutoSuggestBox_KeyDown(object sender, KeyRoutedEventArgs e)
previouslyFocusedElement?.Focus(FocusState.Programmatic);
}
}
else if (e.Key == VirtualKey.Tab && !InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down))
{
GlobalHelper.WriteDebugStringForOmnibar("The TextBox accepted the Tab key.");

// Focus on inactive content when pressing Tab instead of moving to the next control in the tab order
e.Handled = true;
IsFocused = false;
await Task.Delay(15);
CurrentSelectedMode?.ContentOnInactive?.Focus(FocusState.Keyboard);
}
else
{
_textChangeReason = OmnibarTextChangeReason.UserInput;
Expand All @@ -141,6 +133,8 @@ private void AutoSuggestBox_TextChanged(object sender, TextChangedEventArgs e)
_textChangeReason = OmnibarTextChangeReason.UserInput;
_userInput = _textBox.Text;
}
else if (_textChangeReason is OmnibarTextChangeReason.ProgrammaticChange)
_textBox.SelectAll();

TextChanged?.Invoke(this, new(CurrentSelectedMode, _textChangeReason));

Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/UserControls/NavigationToolbar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@
x:Load="{x:Bind ViewModel.EnableOmnibar, Mode=OneWay}"
CurrentSelectedModeName="{x:Bind ViewModel.OmnibarCurrentSelectedModeName, Mode=TwoWay}"
IsFocused="{x:Bind ViewModel.IsOmnibarFocused, Mode=TwoWay}"
LostFocus="Omnibar_LostFocus"
ModeChanged="Omnibar_ModeChanged"
PreviewKeyDown="Omnibar_PreviewKeyDown"
QuerySubmitted="Omnibar_QuerySubmitted"
TextChanged="Omnibar_TextChanged">
Expand Down
20 changes: 15 additions & 5 deletions src/Files.App/UserControls/NavigationToolbar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.UI.Xaml.Navigation;
using Windows.AI.Actions.Hosting;
using Windows.System;
using Windows.UI.Core;

namespace Files.App.UserControls
{
Expand Down Expand Up @@ -427,22 +428,31 @@ private void BreadcrumbBar_ItemDropDownFlyoutClosed(object sender, BreadcrumbBar
e.Flyout.Items.Clear();
}

private void Omnibar_LostFocus(object sender, RoutedEventArgs e)
private void Omnibar_ModeChanged(object sender, OmnibarModeChangedEventArgs e)
{
// Reset the command palette text when switching modes
if (Omnibar.CurrentSelectedMode == OmnibarCommandPaletteMode)
{
Omnibar.CurrentSelectedMode = OmnibarPathMode;
ViewModel.OmnibarCommandPaletteModeText = string.Empty;
}
}

private void Omnibar_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
private async void Omnibar_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key is VirtualKey.Escape)
{
Omnibar.IsFocused = false;
(MainPageViewModel.SelectedTabItem?.TabItemContent as Control)?.Focus(FocusState.Programmatic);
}
else if (e.Key is VirtualKey.Tab && Omnibar.IsFocused && !InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down))
{
var currentSelectedMode = Omnibar.CurrentSelectedMode;
Omnibar.IsFocused = false;
await Task.Delay(15);

if (currentSelectedMode == OmnibarPathMode)
BreadcrumbBar.Focus(FocusState.Keyboard);
else if (currentSelectedMode == OmnibarCommandPaletteMode)
OmnibarCommandPaletteMode.Focus(FocusState.Keyboard);
}
}

private void NavigationButtonOverflowFlyoutButton_LosingFocus(UIElement sender, LosingFocusEventArgs args)
Expand Down
Loading