Skip to main content

Mountain/IPC/WindServiceHandlers/NativeHost/
ShowMessageBox.rs

1//! Wire method: `nativeHost:showMessageBox`.
2//! Surfaces a blocking modal message dialog via `tauri_plugin_dialog`.
3//! Returns `{ response: 0 }` (OK pressed) or `{ response: 1 }` (dismissed).
4//! VS Code destructures `result.response` to determine which button was chosen.
5
6use serde_json::{Value, json};
7use tauri::AppHandle;
8use tauri_plugin_dialog::{DialogExt, MessageDialogKind};
9
10use crate::IPC::WindServiceHandlers::Utilities::JsonValueHelpers::arg_val;
11
12pub async fn Fn(ApplicationHandle:AppHandle, Arguments:Vec<Value>) -> Result<Value, String> {
13	let Options = arg_val(&Arguments, 0);
14
15	let Message = Options.get("message").and_then(Value::as_str).unwrap_or("").to_string();
16
17	let Detail = Options.get("detail").and_then(Value::as_str).map(str::to_string);
18
19	let DialogType = Options
20		.get("type")
21		.and_then(Value::as_str)
22		.map(|S| S.to_lowercase())
23		.unwrap_or_default();
24
25	let Title = Options.get("title").and_then(Value::as_str).unwrap_or("").to_string();
26
27	let Kind = match DialogType.as_str() {
28		"warning" | "warn" => MessageDialogKind::Warning,
29
30		"error" => MessageDialogKind::Error,
31
32		_ => MessageDialogKind::Info,
33	};
34
35	let Handle = ApplicationHandle.clone();
36
37	let Joined = tokio::task::spawn_blocking(move || -> bool {
38		let mut Builder = Handle.dialog().message(&Message).kind(Kind);
39
40		if !Title.is_empty() {
41			Builder = Builder.title(&Title);
42		}
43
44		if let Some(DetailText) = Detail.as_deref() {
45			// MessageDialogBuilder has no .body() method. Append the detail
46			// text to the message so it appears in the dialog body rather
47			// than overwriting the title (the original bug).
48			let Combined = format!("{}\n\n{}", &Message, DetailText);
49
50			Builder = Handle.dialog().message(&Combined).kind(Kind);
51
52			if !Title.is_empty() {
53				Builder = Builder.title(&Title);
54			}
55		}
56
57		Builder.blocking_show()
58	})
59	.await;
60
61	match Joined {
62		Ok(Answered) => Ok(json!({ "response": if Answered { 0 } else { 1 } })),
63
64		Err(Error) => Err(format!("showMessageBox join error: {}", Error)),
65	}
66}