2025-01-13 18:51:26 +00:00
|
|
|
using AIStudio.Tools.Rust;
|
|
|
|
|
|
2025-02-15 14:41:12 +00:00
|
|
|
namespace AIStudio.Tools.Services;
|
2025-01-13 18:51:26 +00:00
|
|
|
|
|
|
|
|
public sealed partial class RustService
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Tries to copy the given text to the clipboard.
|
|
|
|
|
/// </summary>
|
2026-08-02 19:41:12 +00:00
|
|
|
/// <remarks>
|
|
|
|
|
/// The outcome is reported through the message bus. Callers used to hand in their snackbar, which
|
|
|
|
|
/// was the reason most components injected one at all, and it meant this notification was styled
|
|
|
|
|
/// here instead of together with every other notification of the app.
|
|
|
|
|
/// </remarks>
|
2025-01-13 18:51:26 +00:00
|
|
|
/// <param name="text">The text to copy to the clipboard.</param>
|
2026-08-02 19:41:12 +00:00
|
|
|
public async Task CopyText2Clipboard(string text)
|
2025-01-13 18:51:26 +00:00
|
|
|
{
|
2025-05-04 12:59:30 +00:00
|
|
|
var message = TB("Successfully copied the text to your clipboard");
|
2026-08-02 19:41:12 +00:00
|
|
|
var succeeded = false;
|
2025-01-13 18:51:26 +00:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var encryptedText = await text.Encrypt(this.encryptor!);
|
|
|
|
|
var response = await this.http.PostAsync("/clipboard/set", new StringContent(encryptedText.EncryptedData));
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
this.logger!.LogError($"Failed to copy the text to the clipboard due to an network error: '{response.StatusCode}'");
|
2025-05-04 12:59:30 +00:00
|
|
|
message = TB("Failed to copy the text to your clipboard.");
|
2025-01-13 18:51:26 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var state = await response.Content.ReadFromJsonAsync<SetClipboardResponse>(this.jsonRustSerializerOptions);
|
|
|
|
|
if (!state.Success)
|
|
|
|
|
{
|
|
|
|
|
this.logger!.LogError("Failed to copy the text to the clipboard.");
|
2025-05-04 12:59:30 +00:00
|
|
|
message = TB("Failed to copy the text to your clipboard.");
|
2025-01-13 18:51:26 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2026-08-02 19:41:12 +00:00
|
|
|
|
|
|
|
|
succeeded = true;
|
2025-01-13 18:51:26 +00:00
|
|
|
this.logger!.LogDebug("Successfully copied the text to the clipboard.");
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2026-08-02 19:41:12 +00:00
|
|
|
if (succeeded)
|
|
|
|
|
await MessageBus.INSTANCE.SendSuccess(new(Icons.Material.Filled.ContentCopy, message));
|
|
|
|
|
else
|
|
|
|
|
await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.ContentCopy, message));
|
2025-01-13 18:51:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|