Skip to content

Commit 144fdad

Browse files
authored
Merge pull request #2177 from Flow-Launcher/refactor_copy
Refactor clipboard copying
2 parents 6655d83 + e078de5 commit 144fdad

File tree

10 files changed

+55
-78
lines changed

10 files changed

+55
-78
lines changed

Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,18 @@ 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-
/// Copy Text 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.
4446
/// </summary>
4547
/// <param name="text">Text to save on clipboard</param>
46-
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+
/// <param name="showDefaultNotification">Whether to show the default notification from this method after copy is done.
50+
/// It will show file/folder/text is copied successfully.
51+
/// Turn this off to show your own notification after copy is done.</param>>
52+
public void CopyToClipboard(string text, bool directCopy = false, bool showDefaultNotification = true);
4753

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

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,11 @@ 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
{
71-
System.Windows.Clipboard.SetText(QueryTextBox.SelectedText);
70+
App.API.CopyToClipboard(QueryTextBox.SelectedText, showDefaultNotification: false);
7271
}
7372
}
7473

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 29 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,9 +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, bool showDefaultNotification = true)
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+
if (showDefaultNotification)
136+
ShowMsg(
137+
$"{GetTranslation("copy")} {(isFile ? GetTranslation("fileTitle") : GetTranslation("folderTitle"))}",
138+
GetTranslation("completedSuccessfully"));
139+
}
140+
else
141+
{
142+
Clipboard.SetDataObject(stringToCopy);
143+
144+
if (showDefaultNotification)
145+
ShowMsg(
146+
$"{GetTranslation("copy")} {GetTranslation("textTitle")}",
147+
GetTranslation("completedSuccessfully"));
148+
}
122149
}
123150

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

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,42 +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-
{
1116-
return;
1117-
}
1118-
var isFile = File.Exists(stringToCopy);
1119-
var isFolder = Directory.Exists(stringToCopy);
1120-
if (isFile || isFolder)
1121-
{
1122-
var paths = new StringCollection
1123-
{
1124-
stringToCopy
1125-
};
1126-
1127-
Clipboard.SetFileDropList(paths);
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-
App.API.ShowMsg(
1136-
$"{App.API.GetTranslation("copy")} {App.API.GetTranslation("textTitle")}",
1137-
App.API.GetTranslation("completedSuccessfully"));
1138-
}
1139-
return;
1140-
}
1141-
11421106
#endregion
11431107
}
11441108
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public List<Result> LoadContextMenus(Result selectedResult)
174174
{
175175
try
176176
{
177-
Clipboard.SetDataObject(((BookmarkAttributes)selectedResult.ContextData).Url);
177+
context.API.CopyToClipboard(((BookmarkAttributes)selectedResult.ContextData).Url);
178178

179179
return true;
180180
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public List<Result> Query(Query query)
9595
{
9696
try
9797
{
98-
Clipboard.SetDataObject(newResult);
98+
Context.API.CopyToClipboard(newResult);
9999
return true;
100100
}
101101
catch (ExternalException)

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.SetText(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: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ public List<Result> Query(Query query)
8484

8585
Execute(Process.Start, PrepareProcessStartInfo(m, runAsAdministrator));
8686
return true;
87-
}
87+
},
88+
CopyText = m
8889
}));
8990
}
9091
}
@@ -123,7 +124,8 @@ private List<Result> GetHistoryCmds(string cmd, Result result)
123124

124125
Execute(Process.Start, PrepareProcessStartInfo(m.Key, runAsAdministrator));
125126
return true;
126-
}
127+
},
128+
CopyText = m.Key
127129
};
128130
return ret;
129131
}).Where(o => o != null);
@@ -152,7 +154,8 @@ private Result GetCurrentCmd(string cmd)
152154

153155
Execute(Process.Start, PrepareProcessStartInfo(cmd, runAsAdministrator));
154156
return true;
155-
}
157+
},
158+
CopyText = cmd
156159
};
157160

158161
return result;
@@ -176,7 +179,8 @@ private List<Result> ResultsFromHistory()
176179

177180
Execute(Process.Start, PrepareProcessStartInfo(m.Key, runAsAdministrator));
178181
return true;
179-
}
182+
},
183+
CopyText = m.Key
180184
});
181185

182186
if (_settings.ShowOnlyMostUsedCMDs)
@@ -406,7 +410,7 @@ public List<Result> LoadContextMenus(Result selectedResult)
406410
Title = context.API.GetTranslation("flowlauncher_plugin_cmd_copy"),
407411
Action = c =>
408412
{
409-
Clipboard.SetDataObject(selectedResult.Title);
413+
context.API.CopyToClipboard(selectedResult.Title);
410414
return true;
411415
},
412416
IcoPath = "Images/copy.png",

Plugins/Flow.Launcher.Plugin.WindowsSettings/Helper/ContextMenuHelper.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,6 @@ internal static class ContextMenuHelper
2222
internal static List<Result> GetContextMenu(in Result result, in string assemblyName)
2323
{
2424
return new List<Result>(0);
25-
}
26-
27-
/// <summary>
28-
/// Copy the given text to the clipboard
29-
/// </summary>
30-
/// <param name="text">The text to copy to the clipboard</param>
31-
/// <returns><see langword="true"/>The text successful copy to the clipboard, otherwise <see langword="false"/></returns>
32-
private static bool TryToCopyToClipBoard(in string text)
33-
{
34-
try
35-
{
36-
Clipboard.Clear();
37-
Clipboard.SetText(text);
38-
return true;
39-
}
40-
catch (Exception exception)
41-
{
42-
Log.Exception("Can't copy to clipboard", exception, typeof(Main));
43-
return false;
44-
}
45-
}
25+
}
4626
}
4727
}

0 commit comments

Comments
 (0)