diff --git a/CHANGELOG.md b/CHANGELOG.md
index a1b9b5a..29139ef 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -67,6 +67,7 @@ Improvements
- Added trimming and AOT annotations and made MLEM.Ui trimmable
- Ensure paragraphs display up-to-date versions of their text callbacks
- Set cornflower blue as the default link color
+- Added TextField.OnCopyPasteException to allow handling exceptions thrown by TextCopy
Fixes
- Fixed parents of elements that prevent spill not being notified properly
diff --git a/MLEM.Ui/Elements/TextField.cs b/MLEM.Ui/Elements/TextField.cs
index a8ab9e6..307fae5 100644
--- a/MLEM.Ui/Elements/TextField.cs
+++ b/MLEM.Ui/Elements/TextField.cs
@@ -1,3 +1,4 @@
+using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Font;
@@ -32,6 +33,14 @@ namespace MLEM.Ui.Elements {
///
public static readonly Rule FileNames = (field, add) => TextInput.FileNames(field.textInput, add);
+#if NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER
+ ///
+ /// An event that is raised when an exception is thrown while trying to copy or paste clipboard contents using TextCopy.
+ /// If no event handlers are added, the exception is ignored.
+ ///
+ public static event Action OnCopyPasteException;
+#endif
+
///
/// The color that this text field's text should display with
///
@@ -147,7 +156,20 @@ namespace MLEM.Ui.Elements {
public TextField(Anchor anchor, Vector2 size, Rule rule = null, GenericFont font = null, string text = null, bool multiline = false) : base(anchor, size) {
this.textInput = new TextInput(null, Vector2.Zero, 1
#if NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER
- , null, ClipboardService.SetText, ClipboardService.GetText
+ , null, s => {
+ try {
+ ClipboardService.SetText(s);
+ } catch (Exception e) {
+ TextField.OnCopyPasteException?.Invoke(e);
+ }
+ }, () => {
+ try {
+ return ClipboardService.GetText();
+ } catch (Exception e) {
+ TextField.OnCopyPasteException?.Invoke(e);
+ return null;
+ }
+ }
#endif
) {
OnTextChange = (i, s) => this.OnTextChange?.Invoke(this, s),