Skip to main content

Mountain/Track/Effect/CreateEffectForRequest/
WindowUI.rs

1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3//! Window-namespace UI commands from Cocoon's window shim. These emit Tauri
4//! events to Sky and return immediately (no reply channel wired yet).
5
6use std::{future::Future, pin::Pin, sync::Arc};
7
8use serde_json::{Value, json};
9use tauri::Runtime;
10
11use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, Track::Effect::MappedEffectType::MappedEffect, dev_log};
12
13pub fn CreateEffect<R:Runtime>(MethodName:&str, Parameters:Value) -> Option<Result<MappedEffect, String>> {
14	match MethodName {
15		"Window.ShowMessage" => {
16			let effect =
17				move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
18					Box::pin(async move {
19						use tauri::Emitter;
20						let AppHandle = run_time.Environment.ApplicationHandle.clone();
21						let Payload = if Parameters.is_array() {
22							Parameters.get(0).cloned().unwrap_or_default()
23						} else {
24							Parameters
25						};
26						let Id = format!(
27							"notification-{}",
28							std::time::SystemTime::now()
29								.duration_since(std::time::UNIX_EPOCH)
30								.map(|D| D.as_millis())
31								.unwrap_or(0)
32						);
33						let Message = Payload.get("message").and_then(Value::as_str).unwrap_or("").to_string();
34						let Level = Payload.get("level").and_then(Value::as_str).unwrap_or("info").to_string();
35						let Items = Payload.get("items").cloned().unwrap_or(json!([]));
36						let Options = Payload.get("options").cloned().unwrap_or(json!({}));
37						if let Err(Error) = AppHandle.emit(
38							"sky://notification/show",
39							json!({
40								"id": Id,
41								"message": Message,
42								"severity": Level,
43								"actions": Items,
44								"options": Options,
45							}),
46						) {
47							dev_log!(
48								"notification",
49								"warn: [Window.ShowMessage] sky://notification/show emit failed: {}",
50								Error
51							);
52						}
53						Ok(Value::Null)
54					})
55				};
56			Some(Ok(Box::new(effect)))
57		},
58
59		"Window.ShowQuickPick" | "Window.ShowInputBox" | "Window.ShowOpenDialog" | "Window.ShowSaveDialog" => {
60			let MethodNameOwned = MethodName.to_string();
61			let effect =
62				move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
63					Box::pin(async move {
64						use tauri::Emitter;
65						let Args = if Parameters.is_array() { Parameters } else { json!([Parameters]) };
66						let Channel = match MethodNameOwned.as_str() {
67							"Window.ShowQuickPick" => "sky://quickpick/show",
68							"Window.ShowInputBox" => "sky://input-box/show",
69							"Window.ShowOpenDialog" => "sky://dialog/open",
70							"Window.ShowSaveDialog" => "sky://dialog/save",
71							_ => "sky://quickpick/show",
72						};
73						let AppHandle = run_time.Environment.ApplicationHandle.clone();
74						let Nonce = format!(
75							"ui-{}",
76							std::time::SystemTime::now()
77								.duration_since(std::time::UNIX_EPOCH)
78								.map(|D| D.as_nanos())
79								.unwrap_or(0)
80						);
81						if let Err(Error) = AppHandle.emit(Channel, json!({ "nonce": Nonce, "args": Args })) {
82							dev_log!("ipc", "warn: [{}] {} emit failed: {}", MethodNameOwned, Channel, Error);
83						}
84						Ok(Value::Null)
85					})
86				};
87			Some(Ok(Box::new(effect)))
88		},
89
90		_ => None,
91	}
92}