refactor(clipboard): refactor clipboard function arguments for better clarity about the needed type (#1218)

This commit is contained in:
Amr Bashir
2024-04-24 16:44:48 +02:00
committed by GitHub
parent 9c7eb35967
commit 1f16c64d67
9 changed files with 145 additions and 157 deletions
+30 -12
View File
@@ -2,37 +2,54 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use tauri::{command, AppHandle, Manager, ResourceId, Runtime, State, Webview};
use tauri::{command, image::JsImage, AppHandle, Manager, ResourceId, Runtime, State, Webview};
use crate::{ClipKind, Clipboard, ClipboardContents, Result};
use crate::{Clipboard, Result};
#[command]
#[cfg(desktop)]
pub(crate) async fn write_text<R: Runtime>(
_app: AppHandle<R>,
clipboard: State<'_, Clipboard<R>>,
data: ClipKind,
text: &str,
#[allow(unused)] label: Option<String>,
) -> Result<()> {
clipboard.write_text(data)
clipboard.write_text(text)
}
#[command]
pub(crate) async fn write_image<R: Runtime>(
webview: Webview<R>,
#[cfg(not(desktop))]
pub(crate) async fn write_text<R: Runtime>(
_app: AppHandle<R>,
clipboard: State<'_, Clipboard<R>>,
data: ClipKind,
text: &str,
#[allow(unused)] label: Option<&str>,
) -> Result<()> {
let resources_table = webview.resources_table();
clipboard.write_image_inner(data, &resources_table)
match label {
Some(label) => clipboard.write_text_with_label(text, label),
None => clipboard.write_text(text),
}
}
#[command]
pub(crate) async fn read_text<R: Runtime>(
_app: AppHandle<R>,
clipboard: State<'_, Clipboard<R>>,
) -> Result<ClipboardContents> {
) -> Result<String> {
clipboard.read_text()
}
#[command]
pub(crate) async fn write_image<R: Runtime>(
webview: Webview<R>,
clipboard: State<'_, Clipboard<R>>,
image: JsImage,
) -> Result<()> {
let resources_table = webview.resources_table();
let image = image.into_img(&resources_table)?;
clipboard.write_image(&image)
}
#[command]
pub(crate) async fn read_image<R: Runtime>(
webview: Webview<R>,
@@ -48,9 +65,10 @@ pub(crate) async fn read_image<R: Runtime>(
pub(crate) async fn write_html<R: Runtime>(
_app: AppHandle<R>,
clipboard: State<'_, Clipboard<R>>,
data: ClipKind,
html: &str,
alt_text: Option<&str>,
) -> Result<()> {
clipboard.write_html(data)
clipboard.write_html(html, alt_text)
}
#[command]