From c5be5b89cb6ba675b23c5c0d61c0c7b05c7450fa Mon Sep 17 00:00:00 2001 From: Jeremy Date: Thu, 27 Jan 2022 16:35:45 +1100 Subject: [PATCH] move logic to view model --- Flow.Launcher.Plugin/Result.cs | 8 +++-- Flow.Launcher/MainWindow.xaml.cs | 29 ++---------------- Flow.Launcher/ViewModel/MainViewModel.cs | 38 ++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 28 deletions(-) diff --git a/Flow.Launcher.Plugin/Result.cs b/Flow.Launcher.Plugin/Result.cs index c97146fce..4a5eb39af 100644 --- a/Flow.Launcher.Plugin/Result.cs +++ b/Flow.Launcher.Plugin/Result.cs @@ -29,8 +29,12 @@ namespace Flow.Launcher.Plugin /// public string ActionKeywordAssigned { get; set; } - public string CopyText { get; set; } = String.Empty; - public string FileCopy { get; set; } = String.Empty; + /// + /// This holds the text which can be provided by plugin to be copied to the + /// user's clipboard when Ctrl + C is pressed on a result. If the text is a file/directory path + /// flow will copy the actual file/folder instead of just the path text. + /// + public string CopyText { get; set; } = string.Empty; /// /// This holds the text which can be provided by plugin to help Flow autocomplete text diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 6625a6d89..366407182 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -20,7 +20,6 @@ using Flow.Launcher.Infrastructure; using System.Windows.Media; using Flow.Launcher.Infrastructure.Hotkey; using Flow.Launcher.Plugin.SharedCommands; -using System.IO; namespace Flow.Launcher { @@ -55,37 +54,15 @@ namespace Flow.Launcher } private void OnCopy(object sender, ExecutedRoutedEventArgs e) { - if (QueryTextBox.SelectionLength == 0) { - var results = _viewModel.Results; - var result = results.SelectedItem?.Result; - if (result != null) - { - string copyText = String.IsNullOrEmpty(result.CopyText) ? result.SubTitle : result.CopyText; - if (File.Exists(copyText) || Directory.Exists(copyText)) - { - - System.Collections.Specialized.StringCollection paths = new System.Collections.Specialized.StringCollection(); - paths.Add(copyText); - - System.Windows.Clipboard.SetFileDropList(paths); - - } - else - { - System.Windows.Clipboard.SetDataObject(copyText.ToString()); - } - - e.Handled = true; - } + _viewModel.ResultCopy(string.Empty); } - else if (!String.IsNullOrEmpty(QueryTextBox.Text)) + else if (!string.IsNullOrEmpty(QueryTextBox.Text)) { - System.Windows.Clipboard.SetDataObject(QueryTextBox.SelectedText); + _viewModel.ResultCopy(QueryTextBox.SelectedText); } - e.Handled = false; } private async void OnClosing(object sender, CancelEventArgs e) { diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 47b2cba9e..e4353bbcb 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -19,6 +19,8 @@ using Flow.Launcher.Infrastructure.Logger; using Microsoft.VisualStudio.Threading; using System.Threading.Channels; using ISavable = Flow.Launcher.Plugin.ISavable; +using System.IO; +using System.Collections.Specialized; namespace Flow.Launcher.ViewModel { @@ -873,6 +875,42 @@ namespace Flow.Launcher.ViewModel Results.AddResults(resultsForUpdates, token); } + /// + /// This is the global copy method for an individual result. If no text is passed, + /// the method will work out what is to be copied based on the result, so plugin can offer the text + /// to be copied via the result model. If the text is a directory/file path, + /// then actual file/folder will be copied instead. + /// The result's subtitle text is the default text to be copied + /// + public void ResultCopy(string stringToCopy) + { + if (string.IsNullOrEmpty(stringToCopy)) + { + var result = Results.SelectedItem?.Result; + if (result != null) + { + string copyText = string.IsNullOrEmpty(result.CopyText) ? result.SubTitle : result.CopyText; + if (File.Exists(copyText) || Directory.Exists(copyText)) + { + + var paths = new StringCollection(); + paths.Add(copyText); + + Clipboard.SetFileDropList(paths); + + } + else + { + Clipboard.SetDataObject(copyText.ToString()); + } + } + + return; + } + + Clipboard.SetDataObject(stringToCopy); + } + #endregion } } \ No newline at end of file