Skip to main content

Mountain/IPC/WindServiceHandlers/UI/
QuickInput.rs

1#![allow(non_snake_case, unused_variables)]
2//! QuickPick / InputBox dialog handlers. Routes through
3//! `UserInterfaceProvider` so the actual dialog rendering stays
4//! platform-agnostic (Tauri-webview on desktop; extensible to a future
5//! browser preview).
6
7use std::sync::Arc;
8
9use serde_json::{Value, json};
10
11use crate::RunTime::ApplicationRunTime::ApplicationRunTime;
12
13pub async fn QuickInputShowQuickPick(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
14	use CommonLibrary::UserInterface::{
15		DTO::{QuickPickItemDTO::QuickPickItemDTO, QuickPickOptionsDTO::QuickPickOptionsDTO},
16		UserInterfaceProvider::UserInterfaceProvider,
17	};
18
19	let Items:Vec<QuickPickItemDTO> = Arguments
20		.first()
21		.and_then(|V| V.as_array())
22		.map(|Arr| {
23			Arr.iter()
24				.filter_map(|Item| {
25					let Label = Item.get("label").and_then(|L| L.as_str()).unwrap_or("").to_string();
26					let Description = Item.get("description").and_then(|D| D.as_str()).map(|S| S.to_string());
27					let Detail = Item.get("detail").and_then(|D| D.as_str()).map(|S| S.to_string());
28					let Picked = Item.get("picked").and_then(|P| P.as_bool()).unwrap_or(false);
29					Some(QuickPickItemDTO { Label, Description, Detail, Picked:Some(Picked), AlwaysShow:Some(false) })
30				})
31				.collect()
32		})
33		.unwrap_or_default();
34
35	let Options = QuickPickOptionsDTO {
36		PlaceHolder:Arguments
37			.get(1)
38			.and_then(|V| V.get("placeholder"))
39			.and_then(|P| P.as_str())
40			.map(|S| S.to_string()),
41		CanPickMany:Some(
42			Arguments
43				.get(1)
44				.and_then(|V| V.get("canPickMany"))
45				.and_then(|B| B.as_bool())
46				.unwrap_or(false),
47		),
48		Title:Arguments
49			.get(1)
50			.and_then(|V| V.get("title"))
51			.and_then(|T| T.as_str())
52			.map(|S| S.to_string()),
53		..Default::default()
54	};
55
56	let Result = RunTime
57		.Environment
58		.ShowQuickPick(Items, Some(Options))
59		.await
60		.map_err(|Error| format!("quickInput:showQuickPick failed: {}", Error))?;
61
62	match Result {
63		Some(Labels) => Ok(Labels.into_iter().next().map(|S| json!(S)).unwrap_or(Value::Null)),
64		None => Ok(Value::Null),
65	}
66}
67
68pub async fn QuickInputShowInputBox(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
69	use CommonLibrary::UserInterface::{
70		DTO::InputBoxOptionsDTO::InputBoxOptionsDTO,
71		UserInterfaceProvider::UserInterfaceProvider,
72	};
73
74	let Opts = Arguments.first();
75	let Options = InputBoxOptionsDTO {
76		Prompt:Opts
77			.and_then(|V| V.get("prompt"))
78			.and_then(|P| P.as_str())
79			.map(|S| S.to_string()),
80		PlaceHolder:Opts
81			.and_then(|V| V.get("placeholder"))
82			.and_then(|P| P.as_str())
83			.map(|S| S.to_string()),
84		IsPassword:Some(Opts.and_then(|V| V.get("password")).and_then(|B| B.as_bool()).unwrap_or(false)),
85		Value:Opts
86			.and_then(|V| V.get("value"))
87			.and_then(|V| V.as_str())
88			.map(|S| S.to_string()),
89		Title:Opts
90			.and_then(|V| V.get("title"))
91			.and_then(|T| T.as_str())
92			.map(|S| S.to_string()),
93		IgnoreFocusOut:None,
94	};
95
96	let Result = RunTime
97		.Environment
98		.ShowInputBox(Some(Options))
99		.await
100		.map_err(|Error| format!("quickInput:showInputBox failed: {}", Error))?;
101
102	Ok(Result.map(|S| json!(S)).unwrap_or(Value::Null))
103}