Skip to content

Commit e33c075

Browse files
committed
Refactor ClipboardHelper for clarity and platform-agnosticism
Refactored the `CanPasteFromClipboard` method in the `ClipboardHelper` class to improve clarity, error handling, and functionality. Removed Windows-specific logic and legacy code, consolidating clipboard handling into a unified, platform-agnostic approach. Key changes include: - Ensured `_topLevel.Clipboard` is not null using `ArgumentNullException.ThrowIfNull`. - Unified clipboard data retrieval using `_topLevel.Clipboard.TryGetDataAsync`. - Added support for `ClipboardXmlFormatA` and plain text retrieval. - Improved error handling with `try-catch` blocks and logging via `EditorLogger.Error`. - Ensured clipboard operations are invoked on the UI thread using `Dispatcher.UIThread.InvokeAsync`. - Simplified code by removing redundant and nested blocks. This refactor improves maintainability, readability, and robustness of the clipboard handling logic.
1 parent 56e6c47 commit e33c075

File tree

1 file changed

+22
-54
lines changed

1 file changed

+22
-54
lines changed

Editor/Helper/ClipboardHelper.cs

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -90,74 +90,42 @@ private async void Timer_Elapsed(object? sender, ElapsedEventArgs e)
9090
object? data = null;
9191
try
9292
{
93-
// Windows: ClipboardAvalonia
94-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
93+
ArgumentNullException.ThrowIfNull(_topLevel.Clipboard, nameof(_topLevel.Clipboard));
94+
95+
await Dispatcher.UIThread.InvokeAsync(async () =>
9596
{
96-
using var handle = await ClipboardAvalonia.OpenAsync();
97-
if (handle == null)
97+
try
9898
{
99-
data = null;
100-
}
101-
else
102-
{
103-
if (handle.ContainsFormat(ClipboardXmlFormat))
99+
using var transfer = await _topLevel.Clipboard.TryGetDataAsync();
100+
if (transfer == null)
104101
{
105-
var xmlString = handle.GetFormatType(ClipboardXmlFormat);
106-
if (!string.IsNullOrEmpty(xmlString))
107-
{
108-
data = new MathEditorData { XmlString = xmlString };
109-
}
102+
data = null;
110103
}
111104
else
112105
{
113-
var textString = handle.GetText();
114-
if (!string.IsNullOrEmpty(textString))
115-
{
116-
data = textString;
117-
}
118-
}
119-
}
120-
}
121-
// Others: Fallback to Avalonia IClipboard
122-
else
123-
{
124-
ArgumentNullException.ThrowIfNull(_topLevel.Clipboard, nameof(_topLevel.Clipboard));
125-
126-
await Dispatcher.UIThread.InvokeAsync(async () =>
127-
{
128-
try
129-
{
130-
using var transfer = await _topLevel.Clipboard.TryGetDataAsync();
131-
if (transfer == null)
106+
if (transfer.Contains(ClipboardXmlFormatA))
132107
{
133-
data = null;
108+
var xmlString = await transfer.TryGetValueAsync(ClipboardXmlFormatA);
109+
if (!string.IsNullOrEmpty(xmlString))
110+
{
111+
data = new MathEditorData { XmlString = xmlString };
112+
}
134113
}
135114
else
136115
{
137-
if (transfer.Contains(ClipboardXmlFormatA))
116+
var textString = await _topLevel.Clipboard.TryGetTextAsync();
117+
if (!string.IsNullOrEmpty(textString))
138118
{
139-
var xmlString = await transfer.TryGetValueAsync(ClipboardXmlFormatA);
140-
if (!string.IsNullOrEmpty(xmlString))
141-
{
142-
data = new MathEditorData { XmlString = xmlString };
143-
}
144-
}
145-
else
146-
{
147-
var textString = await _topLevel.Clipboard.TryGetTextAsync();
148-
if (!string.IsNullOrEmpty(textString))
149-
{
150-
data = textString;
151-
}
119+
data = textString;
152120
}
153121
}
154122
}
155-
catch (Exception e)
156-
{
157-
EditorLogger.Error(ClassName, "Failed to check clipboard data", e);
158-
}
159-
});
160-
}
123+
}
124+
catch (Exception e)
125+
{
126+
EditorLogger.Error(ClassName, "Failed to check clipboard data", e);
127+
}
128+
});
161129
}
162130
catch (Exception e)
163131
{

0 commit comments

Comments
 (0)