Mountain/IPC/WindServiceHandlers/NativeDialog/
ShowOpenDialog.rs1#![allow(non_snake_case)]
2use 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 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 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}