From b25ae3d5dfaf7fee4005e912cc1ef11b78a6d621 Mon Sep 17 00:00:00 2001 From: Vitalii Mikhailov Date: Sat, 15 Jul 2023 15:32:19 +0300 Subject: [PATCH] Better Clipboard handling --- build/common.props | 2 +- changelog.txt | 4 +++ .../WinForms/HtmlCrashReportForm.cs | 30 ++++++++++++------- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/build/common.props b/build/common.props index f1dc52d7..4e846263 100644 --- a/build/common.props +++ b/build/common.props @@ -4,7 +4,7 @@ - 2.8.9 + 2.8.10 2.2.2 3.2.0.77 diff --git a/changelog.txt b/changelog.txt index e6ecffff..a009ea9e 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,4 +1,8 @@ --------------------------------------------------------------------------------------------------- +Version: 2.8.10 +Game Versions: v1.0.0,v1.0.1,v1.0.2,v1.0.3,v1.1.0,v1.1.1,v1.1.2,v1.1.3,v1.1.4,v1.1.5,v1.2.0,v1.2.1 +* Better Clipboard handling +--------------------------------------------------------------------------------------------------- Version: 2.8.9 Game Versions: v1.0.0,v1.0.1,v1.0.2,v1.0.3,v1.1.0,v1.1.1,v1.1.2,v1.1.3,v1.1.4,v1.1.5,v1.2.0,v1.2.1 * Fixed unintentional crash with Distance Matrix diff --git a/src/Bannerlord.ButterLib/ExceptionHandler/WinForms/HtmlCrashReportForm.cs b/src/Bannerlord.ButterLib/ExceptionHandler/WinForms/HtmlCrashReportForm.cs index 3656fe25..2f1190af 100644 --- a/src/Bannerlord.ButterLib/ExceptionHandler/WinForms/HtmlCrashReportForm.cs +++ b/src/Bannerlord.ButterLib/ExceptionHandler/WinForms/HtmlCrashReportForm.cs @@ -212,7 +212,8 @@ internal HtmlCrashReportForm(CrashReport crashReport) public async void CopyAsHTML() { - await SetClipboardTextAsync(ReportInHtml); + if (!await SetClipboardTextAsync(ReportInHtml)) + MessageBox.Show("Failed to copy the HTML content to the clipboard!", "Error!"); } public async void UploadReport() @@ -229,8 +230,10 @@ public async void UploadReport() { case CrashUploaderStatus.Success: { - await SetClipboardTextAsync(result.Url ?? string.Empty); - MessageBox.Show($"Report available at\n{result.Url}\nThe url was copied to the clipboard!", "Success!"); + if (await SetClipboardTextAsync(result.Url ?? string.Empty)) + MessageBox.Show($"Report available at\n{result.Url}\nThe url was copied to the clipboard!", "Success!"); + else + MessageBox.Show($"Report available at\n{result.Url}\nFailed to copy the url to the clipboard!", "Success!"); break; } case CrashUploaderStatus.MetadataNotFound: @@ -319,19 +322,26 @@ private void HtmlRender_Navigating(object sender, WebBrowserNavigatingEventArgs private static bool UriIsValid(string url) => Uri.TryCreate(url, UriKind.Absolute, out var uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps); - private static async Task SetClipboardTextAsync(string text) + private static async Task SetClipboardTextAsync(string text) { - var completionSource = new TaskCompletionSource(); + var completionSource = new TaskCompletionSource(); var staThread = new Thread(() => { - var dataObject = new DataObject(); - dataObject.SetText(text, TextDataFormat.Text); - Clipboard.SetDataObject(dataObject, true, 10, 100); - completionSource.SetResult(null); + try + { + var dataObject = new DataObject(); + dataObject.SetText(text, TextDataFormat.Text); + Clipboard.SetDataObject(dataObject, true, 10, 100); + completionSource.SetResult(true); + } + catch (Exception) + { + completionSource.SetResult(false); + } }); staThread.SetApartmentState(ApartmentState.STA); staThread.Start(); - await completionSource.Task; + return await completionSource.Task; } } }