Skip to content

Commit c5be5b8

Browse files
committed
move logic to view model
1 parent 48dbfc2 commit c5be5b8

File tree

3 files changed

+47
-28
lines changed

3 files changed

+47
-28
lines changed

Flow.Launcher.Plugin/Result.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ public class Result
2929
/// </summary>
3030
public string ActionKeywordAssigned { get; set; }
3131

32-
public string CopyText { get; set; } = String.Empty;
33-
public string FileCopy { get; set; } = String.Empty;
32+
/// <summary>
33+
/// This holds the text which can be provided by plugin to be copied to the
34+
/// user's clipboard when Ctrl + C is pressed on a result. If the text is a file/directory path
35+
/// flow will copy the actual file/folder instead of just the path text.
36+
/// </summary>
37+
public string CopyText { get; set; } = string.Empty;
3438

3539
/// <summary>
3640
/// This holds the text which can be provided by plugin to help Flow autocomplete text

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
using System.Windows.Media;
2121
using Flow.Launcher.Infrastructure.Hotkey;
2222
using Flow.Launcher.Plugin.SharedCommands;
23-
using System.IO;
2423

2524
namespace Flow.Launcher
2625
{
@@ -55,37 +54,15 @@ public MainWindow()
5554
}
5655
private void OnCopy(object sender, ExecutedRoutedEventArgs e)
5756
{
58-
5957
if (QueryTextBox.SelectionLength == 0)
6058
{
61-
var results = _viewModel.Results;
62-
var result = results.SelectedItem?.Result;
63-
if (result != null)
64-
{
65-
string copyText = String.IsNullOrEmpty(result.CopyText) ? result.SubTitle : result.CopyText;
66-
if (File.Exists(copyText) || Directory.Exists(copyText))
67-
{
68-
69-
System.Collections.Specialized.StringCollection paths = new System.Collections.Specialized.StringCollection();
70-
paths.Add(copyText);
71-
72-
System.Windows.Clipboard.SetFileDropList(paths);
73-
74-
}
75-
else
76-
{
77-
System.Windows.Clipboard.SetDataObject(copyText.ToString());
78-
}
79-
80-
e.Handled = true;
81-
}
59+
_viewModel.ResultCopy(string.Empty);
8260

8361
}
84-
else if (!String.IsNullOrEmpty(QueryTextBox.Text))
62+
else if (!string.IsNullOrEmpty(QueryTextBox.Text))
8563
{
86-
System.Windows.Clipboard.SetDataObject(QueryTextBox.SelectedText);
64+
_viewModel.ResultCopy(QueryTextBox.SelectedText);
8765
}
88-
e.Handled = false;
8966
}
9067
private async void OnClosing(object sender, CancelEventArgs e)
9168
{

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
using Microsoft.VisualStudio.Threading;
2020
using System.Threading.Channels;
2121
using ISavable = Flow.Launcher.Plugin.ISavable;
22+
using System.IO;
23+
using System.Collections.Specialized;
2224

2325
namespace Flow.Launcher.ViewModel
2426
{
@@ -873,6 +875,42 @@ public void UpdateResultView(IEnumerable<ResultsForUpdate> resultsForUpdates)
873875
Results.AddResults(resultsForUpdates, token);
874876
}
875877

878+
/// <summary>
879+
/// This is the global copy method for an individual result. If no text is passed,
880+
/// the method will work out what is to be copied based on the result, so plugin can offer the text
881+
/// to be copied via the result model. If the text is a directory/file path,
882+
/// then actual file/folder will be copied instead.
883+
/// The result's subtitle text is the default text to be copied
884+
/// </summary>
885+
public void ResultCopy(string stringToCopy)
886+
{
887+
if (string.IsNullOrEmpty(stringToCopy))
888+
{
889+
var result = Results.SelectedItem?.Result;
890+
if (result != null)
891+
{
892+
string copyText = string.IsNullOrEmpty(result.CopyText) ? result.SubTitle : result.CopyText;
893+
if (File.Exists(copyText) || Directory.Exists(copyText))
894+
{
895+
896+
var paths = new StringCollection();
897+
paths.Add(copyText);
898+
899+
Clipboard.SetFileDropList(paths);
900+
901+
}
902+
else
903+
{
904+
Clipboard.SetDataObject(copyText.ToString());
905+
}
906+
}
907+
908+
return;
909+
}
910+
911+
Clipboard.SetDataObject(stringToCopy);
912+
}
913+
876914
#endregion
877915
}
878916
}

0 commit comments

Comments
 (0)