Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/RPC/CocoonService/GenericRequest/
WindowDialogs.rs

1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3//! Generic-request window/dialog handlers for `process_mountain_request`.
4//! Handles `UserInterface.ShowOpenDialog`, `UserInterface.ShowSaveDialog`,
5//! `UserInterface.ShowInputBox`, `showInformation`, `showWarning`, `showError`,
6//! `showTextDocument`, `openDocument`, `createWebviewPanel`, `setWebviewHtml`,
7//! `createStatusBarItem`, `setStatusBarText`, `saveAll`, `applyEdit`,
8//! `openExternal`.
9
10use CommonLibrary::UserInterface::UserInterfaceProvider::UserInterfaceProvider;
11use serde_json::{Value, json};
12use tauri::Emitter;
13use tonic::Response;
14
15use crate::{Environment::MountainEnvironment::MountainEnvironment, Vine::Generated::GenericResponse};
16use super::FileSystem::{ErrResponse, OkResponse};
17
18pub async fn HandleShowOpenDialog(RequestId:u64, Params:Value, Env:&MountainEnvironment) -> Response<GenericResponse> {
19	use CommonLibrary::UserInterface::DTO::OpenDialogOptionsDTO::OpenDialogOptionsDTO;
20
21	let Title = Params
22		.get(0)
23		.and_then(|V| V.get("title"))
24		.and_then(|T| T.as_str())
25		.map(|S| S.to_string());
26	let Options = OpenDialogOptionsDTO {
27		Base:CommonLibrary::UserInterface::DTO::DialogOptionsDTO::DialogOptionsDTO { Title, ..Default::default() },
28		..OpenDialogOptionsDTO::default()
29	};
30	match Env.ShowOpenDialog(Some(Options)).await {
31		Ok(Some(Paths)) => {
32			let Uris:Vec<String> = Paths.iter().map(|P| format!("file://{}", P.display())).collect();
33			OkResponse(RequestId, &json!(Uris))
34		},
35		Ok(None) => OkResponse(RequestId, &json!(serde_json::Value::Array(vec![]))),
36		Err(Error) => ErrResponse(RequestId, -32000, Error.to_string()),
37	}
38}
39
40pub async fn HandleShowSaveDialog(RequestId:u64, Params:Value, Env:&MountainEnvironment) -> Response<GenericResponse> {
41	use CommonLibrary::UserInterface::DTO::SaveDialogOptionsDTO::SaveDialogOptionsDTO;
42
43	let Title = Params
44		.get(0)
45		.and_then(|V| V.get("title"))
46		.and_then(|T| T.as_str())
47		.map(|S| S.to_string());
48	let Options = SaveDialogOptionsDTO {
49		Base:CommonLibrary::UserInterface::DTO::DialogOptionsDTO::DialogOptionsDTO { Title, ..Default::default() },
50		..SaveDialogOptionsDTO::default()
51	};
52	match Env.ShowSaveDialog(Some(Options)).await {
53		Ok(Some(Path)) => OkResponse(RequestId, &json!(format!("file://{}", Path.display()))),
54		Ok(None) => OkResponse(RequestId, &Value::Null),
55		Err(Error) => ErrResponse(RequestId, -32000, Error.to_string()),
56	}
57}
58
59pub async fn HandleShowInputBox(RequestId:u64, Params:Value, Env:&MountainEnvironment) -> Response<GenericResponse> {
60	use CommonLibrary::UserInterface::DTO::InputBoxOptionsDTO::InputBoxOptionsDTO;
61
62	let Opts = Params.get(0);
63	let Options = InputBoxOptionsDTO {
64		Prompt:Opts
65			.and_then(|V| V.get("prompt"))
66			.and_then(|P| P.as_str())
67			.map(|S| S.to_string()),
68		PlaceHolder:Opts
69			.and_then(|V| V.get("placeHolder"))
70			.and_then(|P| P.as_str())
71			.map(|S| S.to_string()),
72		IsPassword:Some(Opts.and_then(|V| V.get("password")).and_then(|B| B.as_bool()).unwrap_or(false)),
73		Value:Opts
74			.and_then(|V| V.get("value"))
75			.and_then(|V| V.as_str())
76			.map(|S| S.to_string()),
77		Title:None,
78		IgnoreFocusOut:None,
79	};
80	match Env.ShowInputBox(Some(Options)).await {
81		Ok(Some(Text)) => OkResponse(RequestId, &json!(Text)),
82		Ok(None) => OkResponse(RequestId, &Value::Null),
83		Err(Error) => ErrResponse(RequestId, -32000, Error.to_string()),
84	}
85}
86
87pub async fn HandleShowMessage(
88	RequestId:u64,
89	Params:Value,
90	Env:&MountainEnvironment,
91	Severity:CommonLibrary::UserInterface::DTO::MessageSeverity::MessageSeverity,
92) -> Response<GenericResponse> {
93	let Message = Params.get("message").and_then(|V| V.as_str()).unwrap_or("").to_string();
94	let Items:Option<Value> = Params
95		.get("items")
96		.cloned()
97		.filter(|V| V.is_array() && !V.as_array().unwrap().is_empty());
98	match Env.ShowMessage(Severity, Message, Items).await {
99		Ok(Some(Selected)) => OkResponse(RequestId, &json!({ "selectedItem": Selected })),
100		Ok(None) => OkResponse(RequestId, &Value::Null),
101		Err(Error) => ErrResponse(RequestId, -32000, Error.to_string()),
102	}
103}
104
105pub fn HandleShowTextDocument(RequestId:u64, Params:Value, Env:&MountainEnvironment) -> Response<GenericResponse> {
106	let Uri = Params
107		.get("uri")
108		.and_then(|V| V.get("value").or(Some(V)))
109		.and_then(|V| V.as_str())
110		.unwrap_or("")
111		.to_string();
112	let ViewColumn = Params.get("viewColumn").and_then(|V| V.as_i64()).map(|N| N + 2);
113	let PreserveFocus = Params.get("preserveFocus").and_then(|V| V.as_bool()).unwrap_or(false);
114	let _ = Env.ApplicationHandle.emit(
115		"sky://editor/openDocument",
116		json!({ "uri": Uri, "viewColumn": ViewColumn, "preserveFocus": PreserveFocus }),
117	);
118	OkResponse(RequestId, &json!({ "success": true }))
119}
120
121pub fn HandleOpenDocument(RequestId:u64, Params:Value, Env:&MountainEnvironment) -> Response<GenericResponse> {
122	let Uri = Params
123		.get("uri")
124		.and_then(|V| V.get("value").or(Some(V)))
125		.and_then(|V| V.as_str())
126		.unwrap_or("")
127		.to_string();
128	let ViewColumn = Params.get("viewColumn").and_then(|V| V.as_i64());
129	let _ = Env
130		.ApplicationHandle
131		.emit("sky://editor/openDocument", json!({ "uri": Uri, "viewColumn": ViewColumn }));
132	OkResponse(RequestId, &json!({ "success": true }))
133}
134
135pub fn HandleSaveAll(RequestId:u64, Params:Value, Env:&MountainEnvironment) -> Response<GenericResponse> {
136	let IncludeUntitled = Params.get("includeUntitled").and_then(|V| V.as_bool()).unwrap_or(false);
137	let _ = Env
138		.ApplicationHandle
139		.emit("sky://editor/saveAll", json!({ "includeUntitled": IncludeUntitled }));
140	OkResponse(RequestId, &json!({ "success": true }))
141}
142
143pub fn HandleApplyEdit(RequestId:u64, Params:Value, Env:&MountainEnvironment) -> Response<GenericResponse> {
144	let Uri = Params
145		.get("uri")
146		.and_then(|V| V.get("value").or(Some(V)))
147		.and_then(|V| V.as_str())
148		.unwrap_or("")
149		.to_string();
150	let Edits = Params.get("edits").cloned().unwrap_or(json!([]));
151	let _ = Env
152		.ApplicationHandle
153		.emit("sky://editor/applyEdits", json!({ "uri": Uri, "edits": Edits }));
154	OkResponse(RequestId, &json!({ "success": true }))
155}
156
157pub fn HandleOpenExternal(RequestId:u64, Params:Value, Env:&MountainEnvironment) -> Response<GenericResponse> {
158	let Url = Params
159		.as_str()
160		.or_else(|| Params.get("url").and_then(|V| V.as_str()))
161		.unwrap_or("")
162		.to_string();
163	let _ = Env.ApplicationHandle.emit("sky://native/openExternal", json!({ "url": Url }));
164	OkResponse(RequestId, &json!({ "success": true }))
165}
166
167pub fn HandleCreateStatusBarItem(RequestId:u64, Params:Value, Env:&MountainEnvironment) -> Response<GenericResponse> {
168	let Id = Params.get("id").and_then(|V| V.as_str()).unwrap_or("").to_string();
169	let Text = Params.get("text").and_then(|V| V.as_str()).unwrap_or("").to_string();
170	let Tooltip = Params.get("tooltip").and_then(|V| V.as_str()).unwrap_or("").to_string();
171	let _ = Env.ApplicationHandle.emit(
172		"sky://statusbar/set-entry",
173		json!({ "id": Id, "text": Text, "tooltip": Tooltip }),
174	);
175	OkResponse(RequestId, &json!({ "itemId": Id }))
176}
177
178pub fn HandleSetStatusBarText(RequestId:u64, Params:Value, Env:&MountainEnvironment) -> Response<GenericResponse> {
179	let ItemId = Params.get("itemId").and_then(|V| V.as_str()).unwrap_or("").to_string();
180	let Text = Params.get("text").and_then(|V| V.as_str()).unwrap_or("").to_string();
181	let _ = Env
182		.ApplicationHandle
183		.emit("sky://statusbar/update", json!({ "id": ItemId, "text": Text }));
184	OkResponse(RequestId, &json!({ "success": true }))
185}
186
187pub fn HandleCreateWebviewPanel(RequestId:u64, Params:Value, Env:&MountainEnvironment) -> Response<GenericResponse> {
188	let ViewType = Params.get("viewType").and_then(|V| V.as_str()).unwrap_or("").to_string();
189	let Title = Params.get("title").and_then(|V| V.as_str()).unwrap_or("").to_string();
190	let Handle = std::time::SystemTime::now()
191		.duration_since(std::time::UNIX_EPOCH)
192		.map(|D| D.as_millis() as u64)
193		.unwrap_or(0);
194	let _ = Env.ApplicationHandle.emit(
195		"sky://webview/create",
196		json!({
197			"handle": Handle,
198			"viewType": ViewType,
199			"title": Title,
200			"viewColumn": Params.get("viewColumn"),
201			"preserveFocus": Params.get("preserveFocus").and_then(|V| V.as_bool()).unwrap_or(false),
202		}),
203	);
204	OkResponse(RequestId, &json!({ "handle": Handle }))
205}
206
207pub fn HandleSetWebviewHtml(RequestId:u64, Params:Value, Env:&MountainEnvironment) -> Response<GenericResponse> {
208	let Handle = Params.get("handle").and_then(|V| V.as_u64()).unwrap_or(0);
209	let Html = Params.get("html").and_then(|V| V.as_str()).unwrap_or("").to_string();
210	let _ = Env
211		.ApplicationHandle
212		.emit("sky://webview/set-html", json!({ "handle": Handle, "html": Html }));
213	OkResponse(RequestId, &json!({ "success": true }))
214}