diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index 7b80ec480..66e11f881 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -2,6 +2,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.Specialized; +using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Linq; @@ -10,6 +11,7 @@ using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using System.Windows; +using System.Windows.Input; using System.Windows.Media; using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.Core; @@ -32,8 +34,6 @@ using Flow.Launcher.ViewModel; using JetBrains.Annotations; using Squirrel; using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch; -using System.ComponentModel; -using System.Windows.Input; namespace Flow.Launcher { @@ -93,18 +93,8 @@ namespace Flow.Launcher } public void ShowMainWindow() => _mainVM.Show(); - - public void FocusQueryTextBox() - { - Application.Current.Dispatcher.Invoke(new Action(() => - { - if (Application.Current.MainWindow is MainWindow mw) - { - mw.QueryTextBox.Focus(); - Keyboard.Focus(mw.QueryTextBox); - } - })); - } + + public void FocusQueryTextBox() => _mainVM.FocusQueryTextBox(); public void HideMainWindow() => _mainVM.Hide(); diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 807275fcb..6e1b0dd93 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -1926,6 +1926,21 @@ namespace Flow.Launcher.ViewModel Results.AddResults(resultsForUpdates, token, reSelect); } + [System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "")] + public void FocusQueryTextBox() + { + // When application is exiting, the Application.Current will be null + Application.Current?.Dispatcher.Invoke(() => + { + // When application is exiting, the Application.Current will be null + if (Application.Current?.MainWindow is MainWindow window) + { + window.QueryTextBox.Focus(); + Keyboard.Focus(window.QueryTextBox); + } + }); + } + #endregion #region IDisposable diff --git a/Plugins/Flow.Launcher.Plugin.Shell/Main.cs b/Plugins/Flow.Launcher.Plugin.Shell/Main.cs index 758ad09d5..2613c770b 100644 --- a/Plugins/Flow.Launcher.Plugin.Shell/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Shell/Main.cs @@ -382,10 +382,13 @@ namespace Flow.Launcher.Plugin.Shell // show the main window and set focus to the query box _ = Task.Run(async () => { - await Task.Delay(50); // 💡 키보드 이벤트 처리가 끝난 뒤 - Context.API.FocusQueryTextBox(); - Context.API.ChangeQuery($"{Context.CurrentPluginMetadata.ActionKeywords[0]}{Plugin.Query.TermSeparator}"); + + // Win+R is a system-reserved shortcut, and though the plugin intercepts the keyboard event and + // shows the main window, Windows continues to process the Win key and briefly reclaims focus. + // So we need to wait until the keyboard event processing is completed and then set focus + await Task.Delay(50); + Context.API.FocusQueryTextBox(); }); }