Merge pull request #2143 from Flow-Launcher/expand-resultcopy

Consistent handling of clipboard copying
This commit is contained in:
Garulf 2023-06-04 19:34:52 -04:00 committed by GitHub
commit fa2c894ac0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 37 deletions

View file

@ -59,14 +59,16 @@ namespace Flow.Launcher
private void OnCopy(object sender, ExecutedRoutedEventArgs e)
{
if (QueryTextBox.SelectionLength == 0)
var result = _viewModel.Results.SelectedItem?.Result;
if (QueryTextBox.SelectionLength == 0 && result != null)
{
_viewModel.ResultCopy(string.Empty);
string copyText = result.CopyText;
_viewModel.ResultCopy(copyText);
}
else if (!string.IsNullOrEmpty(QueryTextBox.Text))
{
_viewModel.ResultCopy(QueryTextBox.SelectedText);
System.Windows.Clipboard.SetText(QueryTextBox.SelectedText);
}
}

View file

@ -118,7 +118,7 @@ namespace Flow.Launcher
public void CopyToClipboard(string text)
{
Clipboard.SetDataObject(text);
_mainVM.ResultCopy(text);
}
public void StartLoadingBar() => _mainVM.ProgressBarVisibility = Visibility.Visible;

View file

@ -1104,47 +1104,39 @@ namespace Flow.Launcher.ViewModel
}
/// <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
/// Copies the specified file or folder path to the clipboard, or the specified text if it is not a valid file or folder path.
/// Shows a message indicating whether the operation was completed successfully.
/// </summary>
/// <param name="stringToCopy">The file or folder path, or text to copy to the clipboard.</param>
/// <returns>Nothing.</returns>
public void ResultCopy(string stringToCopy)
{
if (string.IsNullOrEmpty(stringToCopy))
{
var result = Results.SelectedItem?.Result;
if (result != null)
{
string copyText = result.CopyText;
var isFile = File.Exists(copyText);
var isFolder = Directory.Exists(copyText);
if (isFile || isFolder)
{
var paths = new StringCollection
{
copyText
};
Clipboard.SetFileDropList(paths);
App.API.ShowMsg(
$"{App.API.GetTranslation("copy")} {(isFile ? App.API.GetTranslation("fileTitle") : App.API.GetTranslation("folderTitle"))}",
App.API.GetTranslation("completedSuccessfully"));
}
else
{
Clipboard.SetDataObject(copyText);
App.API.ShowMsg(
$"{App.API.GetTranslation("copy")} {App.API.GetTranslation("textTitle")}",
App.API.GetTranslation("completedSuccessfully"));
}
}
return;
}
var isFile = File.Exists(stringToCopy);
var isFolder = Directory.Exists(stringToCopy);
if (isFile || isFolder)
{
var paths = new StringCollection
{
stringToCopy
};
Clipboard.SetDataObject(stringToCopy);
Clipboard.SetFileDropList(paths);
App.API.ShowMsg(
$"{App.API.GetTranslation("copy")} {(isFile ? App.API.GetTranslation("fileTitle") : App.API.GetTranslation("folderTitle"))}",
App.API.GetTranslation("completedSuccessfully"));
}
else
{
Clipboard.SetDataObject(stringToCopy);
App.API.ShowMsg(
$"{App.API.GetTranslation("copy")} {App.API.GetTranslation("textTitle")}",
App.API.GetTranslation("completedSuccessfully"));
}
return;
}
#endregion