Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_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		if !Title.is_empty() {
40			Builder = Builder.title(&Title);
41		}
42		if let Some(DetailText) = Detail.as_deref() {
43			// MessageDialogBuilder has no .body() method. Append the detail
44			// text to the message so it appears in the dialog body rather
45			// than overwriting the title (the original bug).
46			let Combined = format!("{}\n\n{}", &Message, DetailText);
47			Builder = Handle.dialog().message(&Combined).kind(Kind);
48			if !Title.is_empty() {
49				Builder = Builder.title(&Title);
50			}
51		}
52		Builder.blocking_show()
53	})
54	.await;
55
56	match Joined {
57		Ok(Answered) => Ok(json!({ "response": if Answered { 0 } else { 1 } })),
58
59		Err(Error) => Err(format!("showMessageBox join error: {}", Error)),
60	}
61}