Suppress exceptions when setting clipboard data

This commit is contained in:
Joshua Shearer 2020-06-09 09:29:49 -04:00
parent 6c2894e5d4
commit 8d8bf90088
3 changed files with 34 additions and 3 deletions

View file

@ -439,5 +439,30 @@ namespace ModAssistant
ShowMessageBoxDelegate caller = new ShowMessageBoxDelegate(ShowMessageBox);
caller.BeginInvoke(Message, null, null, null);
}
/// <summary>
/// Attempts to write the specified string to the <see cref="System.Windows.Clipboard"/>.
/// </summary>
/// <param name="text">The string to be written</param>
public static void SetClipboard(string text)
{
bool success = false;
try
{
Clipboard.SetText(text);
success = true;
}
catch (Exception)
{
// Swallow exceptions relating to writing data to clipboard.
}
// This could be placed in the try/catch block but we don't
// want to suppress exceptions for non-clipboard operations
if (success)
{
Utils.SendNotify($"Copied text to clipboard");
}
}
}
}

View file

@ -716,8 +716,14 @@ namespace ModAssistant.Pages
private void CopyText(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
System.Windows.Clipboard.SetText(((TextBlock)sender).Text);
Utils.SendNotify("Copied text to clipboard");
var textBlock = sender as TextBlock;
if (textBlock == null) { return; }
var text = textBlock.Text;
// Ensure there's text to be copied
if (string.IsNullOrWhiteSpace(text)) { return; }
Utils.SetClipboard(text);
}
private void SearchButton_Click(object sender, RoutedEventArgs e)

View file

@ -206,7 +206,7 @@ namespace ModAssistant.Pages
await Task.Run(async () => await UploadLog());
System.Diagnostics.Process.Start(LogURL);
Clipboard.SetText(LogURL);
Utils.SetClipboard(LogURL);
MainWindow.Instance.MainText = (string)Application.Current.FindResource("Options:LogUrlCopied");
}
catch (Exception exception)