rlobkit_dialogs/
blocking.rs1#[cfg(not(any(target_os = "android", target_arch = "wasm32")))]
2use crate::RlobKit;
3#[cfg(not(any(target_os = "android", target_arch = "wasm32")))]
4use crate::RlobKitType;
5#[cfg(not(any(target_os = "android", target_arch = "wasm32")))]
6use crate::mode::RlobKitMode;
7#[cfg(not(any(target_os = "android", target_arch = "wasm32")))]
8use crate::picker::{OpenDirectoryOptions, OpenFileOptions, SaveFileOptions};
9#[cfg(not(any(target_os = "android", target_arch = "wasm32")))]
10use std::path::PathBuf;
11
12#[cfg(not(any(target_os = "android", target_arch = "wasm32")))]
13fn block_on_runtime<T>(future: impl std::future::Future<Output = T>) -> T {
14 pollster::block_on(future)
15}
16
17#[cfg(not(any(target_os = "android", target_arch = "wasm32")))]
18pub fn blocking_open_file(title: &str, extensions: &[&str]) -> Option<PathBuf> {
19 let exts: Vec<String> = extensions.iter().map(|s| s.to_string()).collect();
20 block_on_runtime(async {
21 let result = RlobKit::open_file_picker(OpenFileOptions {
22 file_type: RlobKitType::Custom {
23 extensions: exts,
24 mime_types: vec!["*/*".to_string()],
25 },
26 mode: RlobKitMode::Single,
27 title: Some(title.to_string()),
28 initial_directory: None,
29 })
30 .await;
31 match result {
32 Ok(Some(mut files)) if !files.is_empty() => {
33 files.pop().and_then(|f| f.path().map(|p| p.to_path_buf()))
34 }
35 _ => None,
36 }
37 })
38}
39
40#[cfg(not(any(target_os = "android", target_arch = "wasm32")))]
41pub fn blocking_save_file(title: &str, suggested_name: &str, extension: &str) -> Option<PathBuf> {
42 block_on_runtime(async {
43 let exts: Vec<String> = extension
44 .split(',')
45 .map(|s| s.trim().to_string())
46 .filter(|s| !s.is_empty())
47 .collect();
48 let result = RlobKit::open_file_saver(SaveFileOptions {
49 suggested_name: Some(suggested_name.to_string()),
50 extension: None,
51 file_type: Some(RlobKitType::Custom {
52 extensions: exts,
53 mime_types: vec![],
54 }),
55 title: Some(title.to_string()),
56 initial_directory: None,
57 })
58 .await;
59 match result {
60 Ok(Some(f)) => f.path().map(|p| p.to_path_buf()),
61 _ => None,
62 }
63 })
64}
65
66#[cfg(not(any(target_os = "android", target_arch = "wasm32")))]
67pub fn blocking_pick_files(title: &str, extensions: &[&str]) -> Vec<PathBuf> {
68 let exts: Vec<String> = extensions.iter().map(|s| s.to_string()).collect();
69 block_on_runtime(async {
70 let result = RlobKit::open_file_picker(OpenFileOptions {
71 file_type: RlobKitType::Custom {
72 extensions: exts,
73 mime_types: vec!["*/*".to_string()],
74 },
75 mode: RlobKitMode::Multiple { limit: None },
76 title: Some(title.to_string()),
77 initial_directory: None,
78 })
79 .await;
80 match result {
81 Ok(Some(files)) => files
82 .into_iter()
83 .filter_map(|f| f.path().map(|p| p.to_path_buf()))
84 .collect(),
85 _ => Vec::new(),
86 }
87 })
88}
89
90#[cfg(not(any(target_os = "android", target_arch = "wasm32")))]
91pub fn blocking_pick_directory(title: &str) -> Option<PathBuf> {
92 block_on_runtime(async {
93 let result = RlobKit::open_directory_picker(OpenDirectoryOptions {
94 title: Some(title.to_string()),
95 initial_directory: None,
96 })
97 .await;
98 match result {
99 Ok(Some(dir)) => Some(dir.path().to_path_buf()),
100 _ => None,
101 }
102 })
103}