Mountain/Track/Effect/CreateEffectForRequest/
Clipboard.rs1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3use std::{future::Future, pin::Pin, sync::Arc};
4
5use serde_json::{Value, json};
6use tauri::Runtime;
7
8use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, Track::Effect::MappedEffectType::MappedEffect, dev_log};
9
10pub fn CreateEffect<R:Runtime>(MethodName:&str, Parameters:Value) -> Option<Result<MappedEffect, String>> {
11 match MethodName {
12 "Clipboard.Read" => {
13 let effect =
14 move |_run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
15
16 Box::pin(async move {
17 let result = tokio::task::spawn_blocking(|| -> Result<String, String> {
18 let mut Clipboard = arboard::Clipboard::new().map_err(|e| e.to_string())?;
19 Clipboard.get_text().map_err(|e| e.to_string())
20 })
21 .await
22 .map_err(|e| format!("Clipboard.Read join error: {}", e))?;
23 match result {
24 Ok(text) => Ok(json!(text)),
25 Err(e) => {
26 if e.contains("empty") || e.contains("Empty") {
27 Ok(json!(""))
28 } else {
29 dev_log!("ipc", "warn: [Clipboard.Read] {}", e);
30 Err(e)
31 }
32 },
33 }
34 })
35 };
36
37 Some(Ok(Box::new(effect)))
38 },
39
40 "Clipboard.Write" => {
41 let effect =
42 move |_run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
43
44 Box::pin(async move {
45 let text =
46 Parameters.get(0).and_then(Value::as_str).unwrap_or("").to_string();
47 let text_len = text.len();
48 let result = tokio::task::spawn_blocking(move || -> Result<(), String> {
49 let mut Clipboard =
50 arboard::Clipboard::new().map_err(|e| e.to_string())?;
51 Clipboard.set_text(text).map_err(|e| e.to_string())
52 })
53 .await
54 .map_err(|e| format!("Clipboard.Write join error: {}", e))?;
55 result.map(|()| {
56 dev_log!("ipc", "[Clipboard.Write] wrote {} byte(s)", text_len);
57 json!(null)
58 })
59 })
60 };
61
62 Some(Ok(Box::new(effect)))
63 },
64
65 _ => None,
66 }
67}