Skip to main content

Mountain/IPC/WindServiceHandlers/NativeDialog/
ShowOpenDialog.rs

1#![allow(non_snake_case)]
2//! `nativeHost:showOpenDialog` handler. Wires VS Code's
3//! `nativeHostService.showOpenDialog(options)` contract to Tauri's
4//! dialog plugin.
5//!
6//! Contract:
7//!   - `properties: ["openDirectory" | "openFile" | "multiSelections" |
8//!     "createDirectory" | "showHiddenFiles"]`
9//!   - `filters: [{ name, extensions: ["vsix", …] }, …]`
10//!   - `title`, `buttonLabel`, `defaultPath`
11//!   - returns `{ canceled: bool, filePaths: string[] }`.
12//!
13//! The "Install from VSIX…" flow relies on `filters` to narrow the picker
14//! to `.vsix` and on `openFile + multiSelections` so the user can pick
15//! multiple archives at once.
16
17use serde_json::{Value, json};
18use tauri::AppHandle;
19
20use crate::{IPC::WindServiceHandlers::NativeDialog::ParseDialogFilters::ParseDialogFilters, dev_log};
21
22pub async fn ShowOpenDialog(ApplicationHandle:AppHandle, Args:Vec<Value>) -> Result<Value, String> {
23	use tauri_plugin_dialog::DialogExt;
24
25	dev_log!("folder", "showOpenDialog: {:?}", Args);
26
27	// Electron passes `(windowId, options)`; `options` is always the last
28	// element regardless of how the renderer was invoked. Searching by
29	// shape (`first object with a 'properties' or 'filters' field`) keeps
30	// us robust against VS Code versions that pass an extra prefix arg.
31	let Options = Args.iter().rev().find(|V| V.is_object()).cloned().unwrap_or(Value::Null);
32	let Properties:Vec<String> = Options
33		.get("properties")
34		.and_then(Value::as_array)
35		.map(|Array| Array.iter().filter_map(|V| V.as_str().map(str::to_string)).collect())
36		.unwrap_or_default();
37	let IsFolder = Properties.iter().any(|P| P == "openDirectory");
38	let IsMultiple = Properties.iter().any(|P| P == "multiSelections");
39	let Title = Options
40		.get("title")
41		.and_then(Value::as_str)
42		.unwrap_or(if IsFolder { "Open Folder" } else { "Open File" })
43		.to_string();
44	let DefaultPath = Options.get("defaultPath").and_then(Value::as_str).map(str::to_string);
45	let Filters = ParseDialogFilters(&Options);
46
47	let Handle = ApplicationHandle.clone();
48	let FiltersForThread = Filters.clone();
49	let Selected = tokio::task::spawn_blocking(move || -> Vec<String> {
50		let mut Builder = Handle.dialog().file().set_title(&Title);
51		if let Some(Path) = DefaultPath.as_deref() {
52			Builder = Builder.set_directory(Path);
53		}
54		// Apply filters only for file pickers - Tauri returns an error on
55		// folder pickers if filters are set on some platforms.
56		if !IsFolder {
57			for Filter in &FiltersForThread {
58				let ExtRefs:Vec<&str> = Filter.Extensions.iter().map(String::as_str).collect();
59				Builder = Builder.add_filter(&Filter.Name, &ExtRefs);
60			}
61		}
62		if IsFolder {
63			if IsMultiple {
64				Builder
65					.blocking_pick_folders()
66					.unwrap_or_default()
67					.into_iter()
68					.map(|P| P.to_string())
69					.collect()
70			} else {
71				Builder.blocking_pick_folder().map(|P| vec![P.to_string()]).unwrap_or_default()
72			}
73		} else if IsMultiple {
74			Builder
75				.blocking_pick_files()
76				.unwrap_or_default()
77				.into_iter()
78				.map(|P| P.to_string())
79				.collect()
80		} else {
81			Builder.blocking_pick_file().map(|P| vec![P.to_string()]).unwrap_or_default()
82		}
83	})
84	.await
85	.map_err(|Error| format!("showOpenDialog join error: {}", Error))?;
86
87	if Selected.is_empty() {
88		dev_log!("folder", "showOpenDialog cancelled by user");
89		Ok(json!({ "canceled": true, "filePaths": [] }))
90	} else {
91		dev_log!(
92			"folder",
93			"showOpenDialog selected {} path(s) (folder={}, multi={}, filters={})",
94			Selected.len(),
95			IsFolder,
96			IsMultiple,
97			Filters.len()
98		);
99		Ok(json!({ "canceled": false, "filePaths": Selected }))
100	}
101}