Added Rust service class to use system clipboard

This commit is contained in:
Thorsten Sommer 2024-05-04 10:50:04 +02:00
parent c43ad32fb1
commit 0859b1f2ec
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
3 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,46 @@
using Microsoft.JSInterop;
using MudBlazor;
namespace AIStudio.Tools;
/// <summary>
/// Calling Rust functions.
/// </summary>
public sealed class Rust
{
/// <summary>
/// Tries to copy the given text to the clipboard.
/// </summary>
/// <param name="jsRuntime">The JS runtime to access the Rust code.</param>
/// <param name="snackbar">The snackbar to show the result.</param>
/// <param name="text">The text to copy to the clipboard.</param>
public async Task CopyText2Clipboard(IJSRuntime jsRuntime, ISnackbar snackbar, string text)
{
var response = await jsRuntime.InvokeAsync<SetClipboardResponse>("window.__TAURI__.invoke", "set_clipboard", new SetClipboardText(text));
var msg = response.Success switch
{
true => "Successfully copied text to clipboard!",
false => $"Failed to copy text to clipboard: {response.Issue}",
};
var severity = response.Success switch
{
true => Severity.Success,
false => Severity.Error,
};
snackbar.Add(msg, severity, config =>
{
config.Icon = Icons.Material.Filled.ContentCopy;
config.IconSize = Size.Large;
config.IconColor = severity switch
{
Severity.Success => Color.Success,
Severity.Error => Color.Error,
_ => Color.Default,
};
});
}
}

View File

@ -0,0 +1,8 @@
namespace AIStudio.Tools;
/// <summary>
/// The response from the set clipboard operation.
/// </summary>
/// <param name="Success">True when the operation was successful.</param>
/// <param name="Issue">The issue that occurred during the operation, empty when successful.</param>
public readonly record struct SetClipboardResponse(bool Success, string Issue);

View File

@ -0,0 +1,7 @@
namespace AIStudio.Tools;
/// <summary>
/// Model for setting clipboard text.
/// </summary>
/// <param name="Text">The text to set to the clipboard.</param>
public record SetClipboardText(string Text);