move logic to view model

This commit is contained in:
Jeremy 2022-01-27 16:35:45 +11:00
parent 48dbfc240b
commit c5be5b89cb
3 changed files with 47 additions and 28 deletions

View file

@ -29,8 +29,12 @@ namespace Flow.Launcher.Plugin
/// </summary>
public string ActionKeywordAssigned { get; set; }
public string CopyText { get; set; } = String.Empty;
public string FileCopy { get; set; } = String.Empty;
/// <summary>
/// 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.
/// </summary>
public string CopyText { get; set; } = string.Empty;
/// <summary>
/// This holds the text which can be provided by plugin to help Flow autocomplete text

View file

@ -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)
{

View file

@ -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);
}
/// <summary>
/// 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
/// </summary>
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
}
}