Skip to content

Commit 2b862f7

Browse files
committed
Make CopyToClipboard generic
1 parent 1ff328b commit 2b862f7

File tree

7 files changed

+39
-52
lines changed

7 files changed

+39
-52
lines changed

Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ public interface IPublicAPI
3838
/// <exception cref="FileNotFoundException">Thrown when unable to find the file specified in the command </exception>
3939
/// <exception cref="Win32Exception">Thrown when error occurs during the execution of the command </exception>
4040
void ShellRun(string cmd, string filename = "cmd.exe");
41-
41+
4242
/// <summary>
43-
/// If the passed in text is the path to a file or directory, the actual file/directory will
44-
/// be copied to clipboard. Otherwise the text itself will be copied to clipboard.
43+
/// Copies the passed in text and shows a message indicating whether the operation was completed successfully.
44+
/// When directCopy is set to true and passed in text is the path to a file or directory,
45+
/// the actual file/directory will be copied to clipboard. Otherwise the text itself will still be copied to clipboard.
4546
/// </summary>
4647
/// <param name="text">Text to save on clipboard</param>
47-
public void CopyToClipboard(string text);
48+
/// <param name="directCopy">When true it will directly copy the file/folder from the path specified in text</param>
49+
public void CopyToClipboard(string text, bool directCopy = false);
4850

4951
/// <summary>
5052
/// Save everything, all of Flow Launcher and plugins' data and settings

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ private void OnCopy(object sender, ExecutedRoutedEventArgs e)
6363
if (QueryTextBox.SelectionLength == 0 && result != null)
6464
{
6565
string copyText = result.CopyText;
66-
_viewModel.ResultCopy(copyText);
67-
66+
App.API.CopyToClipboard(copyText, directCopy: true);
6867
}
6968
else if (!string.IsNullOrEmpty(QueryTextBox.Text))
7069
{

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using Flow.Launcher.Infrastructure.Storage;
2525
using System.Collections.Concurrent;
2626
using System.Diagnostics;
27+
using System.Collections.Specialized;
2728

2829
namespace Flow.Launcher
2930
{
@@ -116,10 +117,35 @@ public void ShellRun(string cmd, string filename = "cmd.exe")
116117
ShellCommand.Execute(startInfo);
117118
}
118119

119-
public void CopyToClipboard(string text)
120+
public void CopyToClipboard(string stringToCopy, bool directCopy = false)
120121
{
121-
_mainVM.ResultCopy(text);
122+
if (string.IsNullOrEmpty(stringToCopy))
123+
return;
124+
125+
var isFile = File.Exists(stringToCopy);
126+
if (directCopy && (isFile || Directory.Exists(stringToCopy)))
127+
{
128+
var paths = new StringCollection
129+
{
130+
stringToCopy
131+
};
132+
133+
Clipboard.SetFileDropList(paths);
134+
135+
ShowMsg(
136+
$"{GetTranslation("copy")} {(isFile ? GetTranslation("fileTitle") : GetTranslation("folderTitle"))}",
137+
GetTranslation("completedSuccessfully"));
138+
}
139+
else
140+
{
141+
Clipboard.SetDataObject(stringToCopy);
142+
143+
ShowMsg(
144+
$"{GetTranslation("copy")} {GetTranslation("textTitle")}",
145+
GetTranslation("completedSuccessfully"));
146+
}
122147
}
148+
123149

124150
public void StartLoadingBar() => _mainVM.ProgressBarVisibility = Visibility.Visible;
125151

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,43 +1103,6 @@ public void UpdateResultView(IEnumerable<ResultsForUpdate> resultsForUpdates)
11031103
Results.AddResults(resultsForUpdates, token);
11041104
}
11051105

1106-
/// <summary>
1107-
/// Copies the specified file or folder path to the clipboard, or the specified text if it is not a valid file or folder path.
1108-
/// Shows a message indicating whether the operation was completed successfully.
1109-
/// </summary>
1110-
/// <param name="stringToCopy">The file or folder path, or text to copy to the clipboard.</param>
1111-
/// <returns>Nothing.</returns>
1112-
public void ResultCopy(string stringToCopy)
1113-
{
1114-
if (string.IsNullOrEmpty(stringToCopy))
1115-
return;
1116-
1117-
var isFile = File.Exists(stringToCopy);
1118-
var isFolder = isFile ? false : Directory.Exists(stringToCopy); // No need to eval directory exists if determined that file exists
1119-
if (isFile || isFolder)
1120-
{
1121-
var paths = new StringCollection
1122-
{
1123-
stringToCopy
1124-
};
1125-
1126-
Clipboard.SetFileDropList(paths);
1127-
1128-
App.API.ShowMsg(
1129-
$"{App.API.GetTranslation("copy")} {(isFile ? App.API.GetTranslation("fileTitle") : App.API.GetTranslation("folderTitle"))}",
1130-
App.API.GetTranslation("completedSuccessfully"));
1131-
}
1132-
else
1133-
{
1134-
Clipboard.SetDataObject(stringToCopy);
1135-
1136-
App.API.ShowMsg(
1137-
$"{App.API.GetTranslation("copy")} {App.API.GetTranslation("textTitle")}",
1138-
App.API.GetTranslation("completedSuccessfully"));
1139-
}
1140-
return;
1141-
}
1142-
11431106
#endregion
11441107
}
11451108
}

Plugins/Flow.Launcher.Plugin.Explorer/ContextMenu.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public List<Result> LoadContextMenus(Result selectedResult)
124124
{
125125
try
126126
{
127-
Clipboard.SetDataObject(record.FullPath);
127+
Context.API.CopyToClipboard(record.FullPath);
128128
return true;
129129
}
130130
catch (Exception e)
@@ -147,10 +147,7 @@ public List<Result> LoadContextMenus(Result selectedResult)
147147
{
148148
try
149149
{
150-
Clipboard.SetFileDropList(new System.Collections.Specialized.StringCollection
151-
{
152-
record.FullPath
153-
});
150+
Context.API.CopyToClipboard(record.FullPath, directCopy: true);
154151
return true;
155152
}
156153
catch (Exception e)

Plugins/Flow.Launcher.Plugin.Explorer/Main.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
7979
? action
8080
: _ =>
8181
{
82-
Clipboard.SetDataObject(e.ToString());
82+
Context.API.CopyToClipboard(e.ToString());
8383
return new ValueTask<bool>(true);
8484
}
8585
}

Plugins/Flow.Launcher.Plugin.Shell/Main.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ public List<Result> LoadContextMenus(Result selectedResult)
406406
Title = context.API.GetTranslation("flowlauncher_plugin_cmd_copy"),
407407
Action = c =>
408408
{
409-
Clipboard.SetDataObject(selectedResult.Title);
409+
context.API.CopyToClipboard(selectedResult.Title);
410410
return true;
411411
},
412412
IcoPath = "Images/copy.png",

0 commit comments

Comments
 (0)