Skip to main content

Mountain/Track/Effect/CreateEffectForRequest/
Clipboard.rs

1#![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					Box::pin(async move {
16						let result = tokio::task::spawn_blocking(|| -> Result<String, String> {
17							let mut Clipboard = arboard::Clipboard::new().map_err(|e| e.to_string())?;
18							Clipboard.get_text().map_err(|e| e.to_string())
19						})
20						.await
21						.map_err(|e| format!("Clipboard.Read join error: {}", e))?;
22						match result {
23							Ok(text) => Ok(json!(text)),
24							Err(e) => {
25								if e.contains("empty") || e.contains("Empty") {
26									Ok(json!(""))
27								} else {
28									dev_log!("ipc", "warn: [Clipboard.Read] {}", e);
29									Err(e)
30								}
31							},
32						}
33					})
34				};
35			Some(Ok(Box::new(effect)))
36		},
37
38		"Clipboard.Write" => {
39			let effect =
40				move |_run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
41					Box::pin(async move {
42						let text =
43							Parameters.get(0).and_then(Value::as_str).unwrap_or("").to_string();
44						let text_len = text.len();
45						let result = tokio::task::spawn_blocking(move || -> Result<(), String> {
46							let mut Clipboard =
47								arboard::Clipboard::new().map_err(|e| e.to_string())?;
48							Clipboard.set_text(text).map_err(|e| e.to_string())
49						})
50						.await
51						.map_err(|e| format!("Clipboard.Write join error: {}", e))?;
52						result.map(|()| {
53							dev_log!("ipc", "[Clipboard.Write] wrote {} byte(s)", text_len);
54							json!(null)
55						})
56					})
57				};
58			Some(Ok(Box::new(effect)))
59		},
60
61		_ => None,
62	}
63}