diff --git a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs index d0fdf136b68..a07975f3c76 100644 --- a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs +++ b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs @@ -38,12 +38,18 @@ public interface IPublicAPI /// 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"); - + /// - /// Copy Text 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 + /// 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 1e7735fb29c..4adfccff482 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -63,12 +63,11 @@ private void OnCopy(object sender, ExecutedRoutedEventArgs e) 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)) { - System.Windows.Clipboard.SetText(QueryTextBox.SelectedText); + App.API.CopyToClipboard(QueryTextBox.SelectedText, showDefaultNotification: false); } } diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index b61e22d5e23..c1a9d8cd20b 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -24,6 +24,7 @@ using Flow.Launcher.Infrastructure.Storage; using System.Collections.Concurrent; using System.Diagnostics; +using System.Collections.Specialized; namespace Flow.Launcher { @@ -116,9 +117,35 @@ public void ShellRun(string cmd, string filename = "cmd.exe") ShellCommand.Execute(startInfo); } - public void CopyToClipboard(string text) + public void CopyToClipboard(string stringToCopy, bool directCopy = false, bool showDefaultNotification = true) { - _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); + + 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")); + } } public void StartLoadingBar() => _mainVM.ProgressBarVisibility = Visibility.Visible; diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 8529df7b313..84c13442d50 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -1103,42 +1103,6 @@ public void UpdateResultView(IEnumerable resultsForUpdates) 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 = Directory.Exists(stringToCopy); - 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.BrowserBookmark/Main.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs index d9a719272b5..3ac12dc2e2e 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs @@ -174,7 +174,7 @@ public List LoadContextMenus(Result selectedResult) { 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 d5dcdacea5c..e2aa5860c99 100644 --- a/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs @@ -95,7 +95,7 @@ public List Query(Query query) { 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 f5733bbb555..4f6e8465473 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/ContextMenu.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/ContextMenu.cs @@ -124,7 +124,7 @@ public List LoadContextMenus(Result selectedResult) { try { - Clipboard.SetText(record.FullPath); + Context.API.CopyToClipboard(record.FullPath); return true; } catch (Exception e) @@ -147,10 +147,7 @@ public List LoadContextMenus(Result selectedResult) { 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 82a5d544122..e4056131d4b 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Main.cs @@ -79,7 +79,7 @@ public async Task> QueryAsync(Query query, CancellationToken token) ? 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 f64f5d37675..bf8f9f4d0ec 100644 --- a/Plugins/Flow.Launcher.Plugin.Shell/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Shell/Main.cs @@ -84,7 +84,8 @@ public List Query(Query query) Execute(Process.Start, PrepareProcessStartInfo(m, runAsAdministrator)); return true; - } + }, + CopyText = m })); } } @@ -123,7 +124,8 @@ private List GetHistoryCmds(string cmd, Result result) Execute(Process.Start, PrepareProcessStartInfo(m.Key, runAsAdministrator)); return true; - } + }, + CopyText = m.Key }; return ret; }).Where(o => o != null); @@ -152,7 +154,8 @@ private Result GetCurrentCmd(string cmd) Execute(Process.Start, PrepareProcessStartInfo(cmd, runAsAdministrator)); return true; - } + }, + CopyText = cmd }; return result; @@ -176,7 +179,8 @@ private List ResultsFromHistory() Execute(Process.Start, PrepareProcessStartInfo(m.Key, runAsAdministrator)); return true; - } + }, + CopyText = m.Key }); if (_settings.ShowOnlyMostUsedCMDs) @@ -406,7 +410,7 @@ public List LoadContextMenus(Result selectedResult) 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", diff --git a/Plugins/Flow.Launcher.Plugin.WindowsSettings/Helper/ContextMenuHelper.cs b/Plugins/Flow.Launcher.Plugin.WindowsSettings/Helper/ContextMenuHelper.cs index e123e2d2fc6..ca09df9ded0 100644 --- a/Plugins/Flow.Launcher.Plugin.WindowsSettings/Helper/ContextMenuHelper.cs +++ b/Plugins/Flow.Launcher.Plugin.WindowsSettings/Helper/ContextMenuHelper.cs @@ -22,26 +22,6 @@ internal static class ContextMenuHelper 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; - } - } + } } }