From 1ff328be031a7b18a2ce6c81b350047b48803d30 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 8 Jun 2023 22:46:37 +1000 Subject: [PATCH 1/4] update copy calls to use API CopyToClipboard method --- Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs | 3 ++- Flow.Launcher/MainWindow.xaml.cs | 2 +- Flow.Launcher/ViewModel/MainViewModel.cs | 7 +++--- .../Main.cs | 2 +- .../Flow.Launcher.Plugin.Calculator/Main.cs | 2 +- .../ContextMenu.cs | 2 +- .../Helper/ContextMenuHelper.cs | 22 +------------------ 7 files changed, 11 insertions(+), 29 deletions(-) diff --git a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs index d0fdf136b..24d4c9a73 100644 --- a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs +++ b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs @@ -40,7 +40,8 @@ namespace Flow.Launcher.Plugin void ShellRun(string cmd, string filename = "cmd.exe"); /// - /// Copy Text to clipboard + /// If the passed in text is the path to a file or directory, the actual file/directory will + /// be copied to clipboard. Otherwise the text itself will be copied to clipboard. /// /// Text to save on clipboard public void CopyToClipboard(string text); diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 1e7735fb2..6ee0e24c1 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -68,7 +68,7 @@ namespace Flow.Launcher } else if (!string.IsNullOrEmpty(QueryTextBox.Text)) { - System.Windows.Clipboard.SetText(QueryTextBox.SelectedText); + System.Windows.Clipboard.SetDataObject(QueryTextBox.SelectedText); } } diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 8529df7b3..354b625a4 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -1112,11 +1112,10 @@ namespace Flow.Launcher.ViewModel public void ResultCopy(string stringToCopy) { if (string.IsNullOrEmpty(stringToCopy)) - { return; - } + var isFile = File.Exists(stringToCopy); - var isFolder = Directory.Exists(stringToCopy); + var isFolder = isFile ? false : Directory.Exists(stringToCopy); // No need to eval directory exists if determined that file exists if (isFile || isFolder) { var paths = new StringCollection @@ -1125,6 +1124,7 @@ namespace Flow.Launcher.ViewModel }; Clipboard.SetFileDropList(paths); + App.API.ShowMsg( $"{App.API.GetTranslation("copy")} {(isFile ? App.API.GetTranslation("fileTitle") : App.API.GetTranslation("folderTitle"))}", App.API.GetTranslation("completedSuccessfully")); @@ -1132,6 +1132,7 @@ namespace Flow.Launcher.ViewModel else { Clipboard.SetDataObject(stringToCopy); + App.API.ShowMsg( $"{App.API.GetTranslation("copy")} {App.API.GetTranslation("textTitle")}", App.API.GetTranslation("completedSuccessfully")); diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs index d9a719272..3ac12dc2e 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs @@ -174,7 +174,7 @@ namespace Flow.Launcher.Plugin.BrowserBookmark { try { - Clipboard.SetDataObject(((BookmarkAttributes)selectedResult.ContextData).Url); + context.API.CopyToClipboard(((BookmarkAttributes)selectedResult.ContextData).Url); return true; } diff --git a/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs b/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs index d5dcdacea..e2aa5860c 100644 --- a/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs @@ -95,7 +95,7 @@ namespace Flow.Launcher.Plugin.Caculator { try { - Clipboard.SetDataObject(newResult); + Context.API.CopyToClipboard(newResult); return true; } catch (ExternalException) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/ContextMenu.cs b/Plugins/Flow.Launcher.Plugin.Explorer/ContextMenu.cs index f5733bbb5..5e5cab2f5 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/ContextMenu.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/ContextMenu.cs @@ -124,7 +124,7 @@ namespace Flow.Launcher.Plugin.Explorer { try { - Clipboard.SetText(record.FullPath); + Clipboard.SetDataObject(record.FullPath); return true; } catch (Exception e) diff --git a/Plugins/Flow.Launcher.Plugin.WindowsSettings/Helper/ContextMenuHelper.cs b/Plugins/Flow.Launcher.Plugin.WindowsSettings/Helper/ContextMenuHelper.cs index e123e2d2f..ca09df9de 100644 --- a/Plugins/Flow.Launcher.Plugin.WindowsSettings/Helper/ContextMenuHelper.cs +++ b/Plugins/Flow.Launcher.Plugin.WindowsSettings/Helper/ContextMenuHelper.cs @@ -22,26 +22,6 @@ namespace Flow.Launcher.Plugin.WindowsSettings.Helper internal static List GetContextMenu(in Result result, in string assemblyName) { return new List(0); - } - - /// - /// Copy the given text to the clipboard - /// - /// The text to copy to the clipboard - /// The text successful copy to the clipboard, otherwise - private static bool TryToCopyToClipBoard(in string text) - { - try - { - Clipboard.Clear(); - Clipboard.SetText(text); - return true; - } - catch (Exception exception) - { - Log.Exception("Can't copy to clipboard", exception, typeof(Main)); - return false; - } - } + } } } From 2b862f702a700e432aba7244117dc5f472eaff2e Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Fri, 9 Jun 2023 19:59:49 +1000 Subject: [PATCH 2/4] Make CopyToClipboard generic --- Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs | 10 +++-- Flow.Launcher/MainWindow.xaml.cs | 3 +- Flow.Launcher/PublicAPIInstance.cs | 30 ++++++++++++++- Flow.Launcher/ViewModel/MainViewModel.cs | 37 ------------------- .../ContextMenu.cs | 7 +--- Plugins/Flow.Launcher.Plugin.Explorer/Main.cs | 2 +- Plugins/Flow.Launcher.Plugin.Shell/Main.cs | 2 +- 7 files changed, 39 insertions(+), 52 deletions(-) diff --git a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs index 24d4c9a73..591bbdea3 100644 --- a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs +++ b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs @@ -38,13 +38,15 @@ namespace Flow.Launcher.Plugin /// Thrown when unable to find the file specified in the command /// Thrown when error occurs during the execution of the command void ShellRun(string cmd, string filename = "cmd.exe"); - + /// - /// If the passed in text is the path to a file or directory, the actual file/directory will - /// be copied to clipboard. Otherwise the text itself will be copied to clipboard. + /// Copies the passed in text and shows a message indicating whether the operation was completed successfully. + /// When directCopy is set to true and passed in text is the path to a file or directory, + /// the actual file/directory will be copied to clipboard. Otherwise the text itself will still be copied to clipboard. /// /// Text to save on clipboard - public void CopyToClipboard(string text); + /// When true it will directly copy the file/folder from the path specified in text + public void CopyToClipboard(string text, bool directCopy = false); /// /// Save everything, all of Flow Launcher and plugins' data and settings diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 6ee0e24c1..b4417eb59 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -63,8 +63,7 @@ namespace Flow.Launcher if (QueryTextBox.SelectionLength == 0 && result != null) { string copyText = result.CopyText; - _viewModel.ResultCopy(copyText); - + App.API.CopyToClipboard(copyText, directCopy: true); } else if (!string.IsNullOrEmpty(QueryTextBox.Text)) { diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index b61e22d5e..ff1619b0a 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -24,6 +24,7 @@ using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.Storage; using System.Collections.Concurrent; using System.Diagnostics; +using System.Collections.Specialized; namespace Flow.Launcher { @@ -116,10 +117,35 @@ namespace Flow.Launcher ShellCommand.Execute(startInfo); } - public void CopyToClipboard(string text) + public void CopyToClipboard(string stringToCopy, bool directCopy = false) { - _mainVM.ResultCopy(text); + if (string.IsNullOrEmpty(stringToCopy)) + return; + + var isFile = File.Exists(stringToCopy); + if (directCopy && (isFile || Directory.Exists(stringToCopy))) + { + var paths = new StringCollection + { + stringToCopy + }; + + Clipboard.SetFileDropList(paths); + + ShowMsg( + $"{GetTranslation("copy")} {(isFile ? GetTranslation("fileTitle") : GetTranslation("folderTitle"))}", + GetTranslation("completedSuccessfully")); + } + else + { + Clipboard.SetDataObject(stringToCopy); + + ShowMsg( + $"{GetTranslation("copy")} {GetTranslation("textTitle")}", + GetTranslation("completedSuccessfully")); + } } + public void StartLoadingBar() => _mainVM.ProgressBarVisibility = Visibility.Visible; diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 354b625a4..84c13442d 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -1103,43 +1103,6 @@ namespace Flow.Launcher.ViewModel Results.AddResults(resultsForUpdates, token); } - /// - /// Copies the specified file or folder path to the clipboard, or the specified text if it is not a valid file or folder path. - /// Shows a message indicating whether the operation was completed successfully. - /// - /// The file or folder path, or text to copy to the clipboard. - /// Nothing. - public void ResultCopy(string stringToCopy) - { - if (string.IsNullOrEmpty(stringToCopy)) - return; - - var isFile = File.Exists(stringToCopy); - var isFolder = isFile ? false : Directory.Exists(stringToCopy); // No need to eval directory exists if determined that file exists - if (isFile || isFolder) - { - var paths = new StringCollection - { - stringToCopy - }; - - Clipboard.SetFileDropList(paths); - - App.API.ShowMsg( - $"{App.API.GetTranslation("copy")} {(isFile ? App.API.GetTranslation("fileTitle") : App.API.GetTranslation("folderTitle"))}", - App.API.GetTranslation("completedSuccessfully")); - } - else - { - Clipboard.SetDataObject(stringToCopy); - - App.API.ShowMsg( - $"{App.API.GetTranslation("copy")} {App.API.GetTranslation("textTitle")}", - App.API.GetTranslation("completedSuccessfully")); - } - return; - } - #endregion } } diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/ContextMenu.cs b/Plugins/Flow.Launcher.Plugin.Explorer/ContextMenu.cs index 5e5cab2f5..4f6e84654 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/ContextMenu.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/ContextMenu.cs @@ -124,7 +124,7 @@ namespace Flow.Launcher.Plugin.Explorer { try { - Clipboard.SetDataObject(record.FullPath); + Context.API.CopyToClipboard(record.FullPath); return true; } catch (Exception e) @@ -147,10 +147,7 @@ namespace Flow.Launcher.Plugin.Explorer { try { - Clipboard.SetFileDropList(new System.Collections.Specialized.StringCollection - { - record.FullPath - }); + Context.API.CopyToClipboard(record.FullPath, directCopy: true); return true; } catch (Exception e) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Main.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Main.cs index 82a5d5441..e4056131d 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Main.cs @@ -79,7 +79,7 @@ namespace Flow.Launcher.Plugin.Explorer ? action : _ => { - Clipboard.SetDataObject(e.ToString()); + Context.API.CopyToClipboard(e.ToString()); return new ValueTask(true); } } diff --git a/Plugins/Flow.Launcher.Plugin.Shell/Main.cs b/Plugins/Flow.Launcher.Plugin.Shell/Main.cs index f64f5d376..46290b345 100644 --- a/Plugins/Flow.Launcher.Plugin.Shell/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Shell/Main.cs @@ -406,7 +406,7 @@ namespace Flow.Launcher.Plugin.Shell Title = context.API.GetTranslation("flowlauncher_plugin_cmd_copy"), Action = c => { - Clipboard.SetDataObject(selectedResult.Title); + context.API.CopyToClipboard(selectedResult.Title); return true; }, IcoPath = "Images/copy.png", From 6f7c3eba4dbc6de6bd3d99f46c7b93325b8fe1e1 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Sun, 11 Jun 2023 21:59:08 +1000 Subject: [PATCH 3/4] add option to suppress notification when calling copy --- Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs | 5 ++++- Flow.Launcher/MainWindow.xaml.cs | 2 +- Flow.Launcher/PublicAPIInstance.cs | 17 +++++++++-------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs index 591bbdea3..a07975f3c 100644 --- a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs +++ b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs @@ -46,7 +46,10 @@ namespace Flow.Launcher.Plugin /// /// Text to save on clipboard /// When true it will directly copy the file/folder from the path specified in text - public void CopyToClipboard(string text, bool directCopy = false); + /// Whether to show the default notification from this method after copy is done. + /// It will show file/folder/text is copied successfully. + /// Turn this off to show your own notification after copy is done.> + public void CopyToClipboard(string text, bool directCopy = false, bool showDefaultNotification = true); /// /// Save everything, all of Flow Launcher and plugins' data and settings diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index b4417eb59..4adfccff4 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -67,7 +67,7 @@ namespace Flow.Launcher } else if (!string.IsNullOrEmpty(QueryTextBox.Text)) { - System.Windows.Clipboard.SetDataObject(QueryTextBox.SelectedText); + App.API.CopyToClipboard(QueryTextBox.SelectedText, showDefaultNotification: false); } } diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index ff1619b0a..c1a9d8cd2 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -117,7 +117,7 @@ namespace Flow.Launcher ShellCommand.Execute(startInfo); } - public void CopyToClipboard(string stringToCopy, bool directCopy = false) + public void CopyToClipboard(string stringToCopy, bool directCopy = false, bool showDefaultNotification = true) { if (string.IsNullOrEmpty(stringToCopy)) return; @@ -132,20 +132,21 @@ namespace Flow.Launcher Clipboard.SetFileDropList(paths); - ShowMsg( - $"{GetTranslation("copy")} {(isFile ? GetTranslation("fileTitle") : GetTranslation("folderTitle"))}", - GetTranslation("completedSuccessfully")); + if (showDefaultNotification) + ShowMsg( + $"{GetTranslation("copy")} {(isFile ? GetTranslation("fileTitle") : GetTranslation("folderTitle"))}", + GetTranslation("completedSuccessfully")); } else { Clipboard.SetDataObject(stringToCopy); - ShowMsg( - $"{GetTranslation("copy")} {GetTranslation("textTitle")}", - GetTranslation("completedSuccessfully")); + if (showDefaultNotification) + ShowMsg( + $"{GetTranslation("copy")} {GetTranslation("textTitle")}", + GetTranslation("completedSuccessfully")); } } - public void StartLoadingBar() => _mainVM.ProgressBarVisibility = Visibility.Visible; From e078de557bac40db9fba4eb1f822f5c9c6d0f3ba Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Sun, 11 Jun 2023 21:59:46 +1000 Subject: [PATCH 4/4] add CopyText for Shell plugin --- Plugins/Flow.Launcher.Plugin.Shell/Main.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Shell/Main.cs b/Plugins/Flow.Launcher.Plugin.Shell/Main.cs index 46290b345..bf8f9f4d0 100644 --- a/Plugins/Flow.Launcher.Plugin.Shell/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Shell/Main.cs @@ -84,7 +84,8 @@ namespace Flow.Launcher.Plugin.Shell Execute(Process.Start, PrepareProcessStartInfo(m, runAsAdministrator)); return true; - } + }, + CopyText = m })); } } @@ -123,7 +124,8 @@ namespace Flow.Launcher.Plugin.Shell Execute(Process.Start, PrepareProcessStartInfo(m.Key, runAsAdministrator)); return true; - } + }, + CopyText = m.Key }; return ret; }).Where(o => o != null); @@ -152,7 +154,8 @@ namespace Flow.Launcher.Plugin.Shell Execute(Process.Start, PrepareProcessStartInfo(cmd, runAsAdministrator)); return true; - } + }, + CopyText = cmd }; return result; @@ -176,7 +179,8 @@ namespace Flow.Launcher.Plugin.Shell Execute(Process.Start, PrepareProcessStartInfo(m.Key, runAsAdministrator)); return true; - } + }, + CopyText = m.Key }); if (_settings.ShowOnlyMostUsedCMDs)