Skip to content

Commit 2beab53

Browse files
authored
Merge pull request #3568 from onesounds/20250522-FixWinR
Fix: Ensure QueryTextBox receives focus after Win+R
2 parents ac61469 + 3718ae5 commit 2beab53

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public interface IPublicAPI
8888
/// Show the MainWindow when hiding
8989
/// </summary>
9090
void ShowMainWindow();
91+
92+
/// <summary>
93+
/// Focus the query text box in the main window
94+
/// </summary>
95+
void FocusQueryTextBox();
9196

9297
/// <summary>
9398
/// Hide MainWindow

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Collections.Specialized;
5+
using System.ComponentModel;
56
using System.Diagnostics;
67
using System.IO;
78
using System.Linq;
@@ -10,6 +11,7 @@
1011
using System.Threading;
1112
using System.Threading.Tasks;
1213
using System.Windows;
14+
using System.Windows.Input;
1315
using System.Windows.Media;
1416
using CommunityToolkit.Mvvm.DependencyInjection;
1517
using Flow.Launcher.Core;
@@ -32,7 +34,6 @@
3234
using JetBrains.Annotations;
3335
using Squirrel;
3436
using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch;
35-
using System.ComponentModel;
3637

3738
namespace Flow.Launcher
3839
{
@@ -93,6 +94,8 @@ public async void RestartApp()
9394

9495
public void ShowMainWindow() => _mainVM.Show();
9596

97+
public void FocusQueryTextBox() => _mainVM.FocusQueryTextBox();
98+
9699
public void HideMainWindow() => _mainVM.Hide();
97100

98101
public bool IsMainWindowVisible() => _mainVM.MainWindowVisibilityStatus;

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,21 @@ public void UpdateResultView(ICollection<ResultsForUpdate> resultsForUpdates)
19261926
Results.AddResults(resultsForUpdates, token, reSelect);
19271927
}
19281928

1929+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "<Pending>")]
1930+
public void FocusQueryTextBox()
1931+
{
1932+
// When application is exiting, the Application.Current will be null
1933+
Application.Current?.Dispatcher.Invoke(() =>
1934+
{
1935+
// When application is exiting, the Application.Current will be null
1936+
if (Application.Current?.MainWindow is MainWindow window)
1937+
{
1938+
window.QueryTextBox.Focus();
1939+
Keyboard.Focus(window.QueryTextBox);
1940+
}
1941+
});
1942+
}
1943+
19291944
#endregion
19301945

19311946
#region IDisposable

Plugins/Flow.Launcher.Plugin.Shell/Main.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,17 @@ bool API_GlobalKeyboardEvent(int keyevent, int vkcode, SpecialKeyState state)
378378

379379
private void OnWinRPressed()
380380
{
381+
Context.API.ShowMainWindow();
381382
// show the main window and set focus to the query box
382-
_ = Task.Run(() =>
383+
_ = Task.Run(async () =>
383384
{
384-
Context.API.ShowMainWindow();
385385
Context.API.ChangeQuery($"{Context.CurrentPluginMetadata.ActionKeywords[0]}{Plugin.Query.TermSeparator}");
386+
387+
// Win+R is a system-reserved shortcut, and though the plugin intercepts the keyboard event and
388+
// shows the main window, Windows continues to process the Win key and briefly reclaims focus.
389+
// So we need to wait until the keyboard event processing is completed and then set focus
390+
await Task.Delay(50);
391+
Context.API.FocusQueryTextBox();
386392
});
387393
}
388394

0 commit comments

Comments
 (0)