Skip to main content

Mountain/IPC/WindServiceHandlers/UI/
Notification.rs

1#![allow(non_snake_case, unused_variables)]
2//! Notification toast handlers. Both the plain-message and the
3//! progress-bar variants go through here; each emits on an
4//! `SkyEvent::Notification*` channel so Sky's toast stack renders
5//! without a round-trip back through Mountain.
6//!
7//! Note: these are the Wind-facing IPC invocations (called from the
8//! renderer's `INotificationService`). The Cocoon-side notification
9//! path for extensions lives in `Vine::Server::Notification::*`.
10
11use serde_json::{Value, json};
12use tauri::AppHandle;
13use CommonLibrary::IPC::SkyEvent::SkyEvent;
14
15fn NewId(Prefix:&str) -> String {
16	format!(
17		"{}-{}",
18		Prefix,
19		std::time::SystemTime::now()
20			.duration_since(std::time::UNIX_EPOCH)
21			.map(|D| D.as_millis())
22			.unwrap_or(0)
23	)
24}
25
26pub async fn NotificationShow(ApplicationHandle:AppHandle, Arguments:Vec<Value>) -> Result<Value, String> {
27	use tauri::Emitter;
28
29	let Message = Arguments.first().and_then(|V| V.as_str()).unwrap_or("").to_string();
30
31	let Severity = Arguments.get(1).and_then(|V| V.as_str()).unwrap_or("info").to_string();
32
33	let Actions = Arguments.get(2).cloned().unwrap_or(json!([]));
34
35	let Id = NewId("notification");
36
37	let _ = ApplicationHandle.emit(
38		SkyEvent::NotificationShow.AsStr(),
39		json!({
40			"id": Id,
41			"message": Message,
42			"severity": Severity,
43			"actions": Actions,
44		}),
45	);
46
47	Ok(json!(Id))
48}
49
50pub async fn NotificationShowProgress(ApplicationHandle:AppHandle, Arguments:Vec<Value>) -> Result<Value, String> {
51	use tauri::Emitter;
52
53	let Title = Arguments.first().and_then(|V| V.as_str()).unwrap_or("").to_string();
54
55	let Cancellable = Arguments.get(1).and_then(|V| V.as_bool()).unwrap_or(false);
56
57	let Id = NewId("progress");
58
59	let _ = ApplicationHandle.emit(
60		SkyEvent::NotificationProgressBegin.AsStr(),
61		json!({
62			"id": Id,
63			"title": Title,
64			"cancellable": Cancellable,
65		}),
66	);
67
68	Ok(json!(Id))
69}
70
71pub async fn NotificationUpdateProgress(ApplicationHandle:AppHandle, Arguments:Vec<Value>) -> Result<Value, String> {
72	use tauri::Emitter;
73
74	let Id = Arguments.first().and_then(|V| V.as_str()).unwrap_or("").to_string();
75
76	let Increment = Arguments.get(1).and_then(|V| V.as_f64()).unwrap_or(0.0);
77
78	let Message = Arguments.get(2).and_then(|V| V.as_str()).unwrap_or("").to_string();
79
80	let _ = ApplicationHandle.emit(
81		SkyEvent::NotificationProgressUpdate.AsStr(),
82		json!({
83			"id": Id,
84			"increment": Increment,
85			"message": Message,
86		}),
87	);
88
89	Ok(Value::Null)
90}
91
92pub async fn NotificationEndProgress(ApplicationHandle:AppHandle, Arguments:Vec<Value>) -> Result<Value, String> {
93	use tauri::Emitter;
94
95	let Id = Arguments.first().and_then(|V| V.as_str()).unwrap_or("").to_string();
96
97	let _ = ApplicationHandle.emit(SkyEvent::NotificationProgressEnd.AsStr(), json!({ "id": Id }));
98
99	Ok(Value::Null)
100}