mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2025-02-05 14:49:06 +00:00
Added Rust service class to use system clipboard
This commit is contained in:
parent
c43ad32fb1
commit
0859b1f2ec
46
app/MindWork AI Studio/Tools/Rust.cs
Normal file
46
app/MindWork AI Studio/Tools/Rust.cs
Normal 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,
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
8
app/MindWork AI Studio/Tools/SetClipboardResponse.cs
Normal file
8
app/MindWork AI Studio/Tools/SetClipboardResponse.cs
Normal 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);
|
7
app/MindWork AI Studio/Tools/SetClipboardText.cs
Normal file
7
app/MindWork AI Studio/Tools/SetClipboardText.cs
Normal 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);
|
Loading…
Reference in New Issue
Block a user