From e0f0bed8223203e8676e16ecf9d2756a5fc59be1 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sat, 4 Jan 2025 00:33:24 +0800 Subject: [PATCH] Revert "Fix clipboard action under sta thread issue" --- .../NativeMethods.txt | 5 +- .../UserSettings/Settings.cs | 2 +- Flow.Launcher.Infrastructure/Win32Helper.cs | 138 ------------------ Flow.Launcher/PublicAPIInstance.cs | 39 +++-- 4 files changed, 20 insertions(+), 164 deletions(-) diff --git a/Flow.Launcher.Infrastructure/NativeMethods.txt b/Flow.Launcher.Infrastructure/NativeMethods.txt index d8777ff277c..f117534a1ff 100644 --- a/Flow.Launcher.Infrastructure/NativeMethods.txt +++ b/Flow.Launcher.Infrastructure/NativeMethods.txt @@ -16,7 +16,4 @@ WM_KEYUP WM_SYSKEYDOWN WM_SYSKEYUP -EnumWindows - -OleInitialize -OleUninitialize \ No newline at end of file +EnumWindows \ No newline at end of file diff --git a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs index 83f06279c3b..0bcc9368d22 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs @@ -230,7 +230,7 @@ public SearchPrecisionScore QuerySearchPrecision [JsonIgnore] public ObservableCollection BuiltinShortcuts { get; set; } = new() { - new BuiltinShortcutModel("{clipboard}", "shortcut_clipboard_description", () => Win32Helper.StartSTATaskAsync(Clipboard.GetText).Result), + new BuiltinShortcutModel("{clipboard}", "shortcut_clipboard_description", Clipboard.GetText), new BuiltinShortcutModel("{active_explorer_path}", "shortcut_active_explorer_path", FileExplorerHelper.GetActiveExplorerPath) }; diff --git a/Flow.Launcher.Infrastructure/Win32Helper.cs b/Flow.Launcher.Infrastructure/Win32Helper.cs index 6d6c7286412..867fef4f509 100644 --- a/Flow.Launcher.Infrastructure/Win32Helper.cs +++ b/Flow.Launcher.Infrastructure/Win32Helper.cs @@ -1,150 +1,12 @@ using System; using System.Runtime.InteropServices; -using System.Threading; -using System.Threading.Tasks; using System.Windows.Interop; using System.Windows; -using Windows.Win32; namespace Flow.Launcher.Infrastructure { public static class Win32Helper { - #region STA Thread - - /* - Found on https://github.com/files-community/Files - */ - - public static Task StartSTATaskAsync(Action action) - { - var taskCompletionSource = new TaskCompletionSource(); - Thread thread = new(() => - { - PInvoke.OleInitialize(); - - try - { - action(); - taskCompletionSource.SetResult(); - } - catch (System.Exception) - { - taskCompletionSource.SetResult(); - } - finally - { - PInvoke.OleUninitialize(); - } - }) - { - IsBackground = true, - Priority = ThreadPriority.Normal - }; - - thread.SetApartmentState(ApartmentState.STA); - thread.Start(); - - return taskCompletionSource.Task; - } - - public static Task StartSTATaskAsync(Func func) - { - var taskCompletionSource = new TaskCompletionSource(); - Thread thread = new(async () => - { - PInvoke.OleInitialize(); - - try - { - await func(); - taskCompletionSource.SetResult(); - } - catch (System.Exception) - { - taskCompletionSource.SetResult(); - } - finally - { - PInvoke.OleUninitialize(); - } - }) - { - IsBackground = true, - Priority = ThreadPriority.Normal - }; - - thread.SetApartmentState(ApartmentState.STA); - thread.Start(); - - return taskCompletionSource.Task; - } - - public static Task StartSTATaskAsync(Func func) - { - var taskCompletionSource = new TaskCompletionSource(); - - Thread thread = new(() => - { - PInvoke.OleInitialize(); - - try - { - taskCompletionSource.SetResult(func()); - } - catch (System.Exception) - { - taskCompletionSource.SetResult(default); - } - finally - { - PInvoke.OleUninitialize(); - } - }) - { - IsBackground = true, - Priority = ThreadPriority.Normal - }; - - thread.SetApartmentState(ApartmentState.STA); - thread.Start(); - - return taskCompletionSource.Task; - } - - public static Task StartSTATaskAsync(Func> func) - { - var taskCompletionSource = new TaskCompletionSource(); - - Thread thread = new(async () => - { - PInvoke.OleInitialize(); - try - { - taskCompletionSource.SetResult(await func()); - } - catch (System.Exception) - { - taskCompletionSource.SetResult(default); - } - finally - { - PInvoke.OleUninitialize(); - } - }) - { - IsBackground = true, - Priority = ThreadPriority.Normal - }; - - thread.SetApartmentState(ApartmentState.STA); - thread.Start(); - - return taskCompletionSource.Task; - } - - #endregion - #region Blur Handling /* diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index dcdb798fff2..f0295cf244a 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -117,38 +117,35 @@ public void ShellRun(string cmd, string filename = "cmd.exe") ShellCommand.Execute(startInfo); } - public async void CopyToClipboard(string stringToCopy, bool directCopy = false, bool showDefaultNotification = true) + public void CopyToClipboard(string stringToCopy, bool directCopy = false, bool showDefaultNotification = true) { if (string.IsNullOrEmpty(stringToCopy)) return; - await Win32Helper.StartSTATaskAsync(() => + var isFile = File.Exists(stringToCopy); + if (directCopy && (isFile || Directory.Exists(stringToCopy))) { - var isFile = File.Exists(stringToCopy); - if (directCopy && (isFile || Directory.Exists(stringToCopy))) - { - var paths = new StringCollection + var paths = new StringCollection { stringToCopy }; - Clipboard.SetFileDropList(paths); + Clipboard.SetFileDropList(paths); - if (showDefaultNotification) - ShowMsg( - $"{GetTranslation("copy")} {(isFile ? GetTranslation("fileTitle") : GetTranslation("folderTitle"))}", - GetTranslation("completedSuccessfully")); - } - else - { - Clipboard.SetDataObject(stringToCopy); + if (showDefaultNotification) + ShowMsg( + $"{GetTranslation("copy")} {(isFile ? GetTranslation("fileTitle") : GetTranslation("folderTitle"))}", + GetTranslation("completedSuccessfully")); + } + else + { + Clipboard.SetDataObject(stringToCopy); - if (showDefaultNotification) - ShowMsg( - $"{GetTranslation("copy")} {GetTranslation("textTitle")}", - GetTranslation("completedSuccessfully")); - } - }); + if (showDefaultNotification) + ShowMsg( + $"{GetTranslation("copy")} {GetTranslation("textTitle")}", + GetTranslation("completedSuccessfully")); + } } public void StartLoadingBar() => _mainVM.ProgressBarVisibility = Visibility.Visible;