1use std::{cell::RefCell, future::Future, pin::Pin, rc::Rc};
17
18use cranpose_core::{CompositionLocal, CompositionLocalProvider, compositionLocalOfWithPolicy};
19use cranpose_macros::composable;
20
21use crate::content::{ContentError, ContentFolderRef, ContentHandle, ContentSinkRef};
22
23#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
25pub enum FilePickerError {
26 #[error("file picker failed: {0}")]
28 Failed(String),
29 #[error(transparent)]
31 Content(#[from] ContentError),
32 #[error("{operation} requires cranpose-services feature `{feature}`")]
34 UnsupportedFeature {
35 operation: &'static str,
37 feature: &'static str,
39 },
40 #[error("file picking is not available on this platform")]
42 UnsupportedPlatform,
43}
44
45pub type PickerFuture<T> = Pin<Box<dyn Future<Output = T>>>;
47
48#[derive(Clone, Debug, Default, PartialEq, Eq)]
50pub struct FileFilter {
51 pub label: String,
53 pub extensions: Vec<String>,
55 pub mime_types: Vec<String>,
59}
60
61impl FileFilter {
62 pub fn new(label: impl Into<String>, extensions: &[&str]) -> Self {
64 Self {
65 label: label.into(),
66 extensions: extensions.iter().map(|ext| (*ext).to_string()).collect(),
67 mime_types: Vec::new(),
68 }
69 }
70
71 pub fn with_mime_types(mut self, mime_types: &[&str]) -> Self {
73 self.mime_types = mime_types.iter().map(|mime| (*mime).to_string()).collect();
74 self
75 }
76}
77
78#[derive(Clone, Debug, Default, PartialEq, Eq)]
80pub struct FilePickerOptions {
81 pub title: Option<String>,
83 pub filters: Vec<FileFilter>,
85}
86
87impl FilePickerOptions {
88 pub fn with_title(mut self, title: impl Into<String>) -> Self {
90 self.title = Some(title.into());
91 self
92 }
93
94 pub fn with_filter(mut self, filter: FileFilter) -> Self {
96 self.filters.push(filter);
97 self
98 }
99
100 pub fn mime_types(&self) -> Vec<String> {
102 self.filters
103 .iter()
104 .flat_map(|filter| filter.mime_types.iter().cloned())
105 .collect()
106 }
107}
108
109#[derive(Clone, Debug, Default, PartialEq, Eq)]
111pub struct SaveDocumentRequest {
112 pub file_name: String,
114 pub mime_type: String,
117 pub title: Option<String>,
119}
120
121impl SaveDocumentRequest {
122 pub fn new(file_name: impl Into<String>, mime_type: impl Into<String>) -> Self {
124 Self {
125 file_name: file_name.into(),
126 mime_type: mime_type.into(),
127 title: None,
128 }
129 }
130
131 pub fn with_title(mut self, title: impl Into<String>) -> Self {
133 self.title = Some(title.into());
134 self
135 }
136}
137
138#[doc(hidden)]
139pub enum RecoveredPick {
140 File(ContentHandle),
141 Files(Vec<ContentHandle>),
142 Folder(ContentFolderRef),
143 WritableFolder(String),
144}
145
146pub trait FilePicker {
150 fn pick_file(
152 &self,
153 options: FilePickerOptions,
154 ) -> PickerFuture<Result<Option<ContentHandle>, FilePickerError>>;
155
156 fn pick_files(
161 &self,
162 options: FilePickerOptions,
163 ) -> PickerFuture<Result<Vec<ContentHandle>, FilePickerError>> {
164 let single = self.pick_file(options);
165 Box::pin(async move { Ok(single.await?.into_iter().collect()) })
166 }
167
168 fn pick_folder(
170 &self,
171 options: FilePickerOptions,
172 ) -> PickerFuture<Result<Option<ContentFolderRef>, FilePickerError>>;
173
174 fn save_document(
177 &self,
178 request: SaveDocumentRequest,
179 ) -> PickerFuture<Result<Option<ContentSinkRef>, FilePickerError>> {
180 let _ = request;
181 Box::pin(async { Err(FilePickerError::UnsupportedPlatform) })
182 }
183
184 fn pick_writable_folder(
188 &self,
189 options: FilePickerOptions,
190 ) -> PickerFuture<Result<Option<String>, FilePickerError>> {
191 let _ = options;
192 Box::pin(async { Err(FilePickerError::UnsupportedPlatform) })
193 }
194
195 #[doc(hidden)]
196 fn take_recovered_pick(&self) -> Option<RecoveredPick> {
197 None
198 }
199}
200
201pub type FilePickerRef = Rc<dyn FilePicker>;
203
204thread_local! {
205 static PLATFORM_FILE_PICKER: RefCell<Option<FilePickerRef>> = const { RefCell::new(None) };
206}
207
208pub fn set_platform_file_picker(picker: FilePickerRef) {
214 PLATFORM_FILE_PICKER.with(|cell| *cell.borrow_mut() = Some(picker));
215}
216
217pub fn clear_platform_file_picker() {
219 PLATFORM_FILE_PICKER.with(|cell| *cell.borrow_mut() = None);
220}
221
222fn registered_platform_file_picker() -> Option<FilePickerRef> {
223 PLATFORM_FILE_PICKER.with(|cell| cell.borrow().clone())
224}
225
226struct PlatformFilePicker;
227
228impl FilePicker for PlatformFilePicker {
229 fn pick_file(
230 &self,
231 options: FilePickerOptions,
232 ) -> PickerFuture<Result<Option<ContentHandle>, FilePickerError>> {
233 match registered_platform_file_picker() {
234 Some(picker) => picker.pick_file(options),
235 None => builtin::pick_file(options),
236 }
237 }
238
239 fn pick_files(
240 &self,
241 options: FilePickerOptions,
242 ) -> PickerFuture<Result<Vec<ContentHandle>, FilePickerError>> {
243 match registered_platform_file_picker() {
244 Some(picker) => picker.pick_files(options),
245 None => builtin::pick_files(options),
246 }
247 }
248
249 fn pick_folder(
250 &self,
251 options: FilePickerOptions,
252 ) -> PickerFuture<Result<Option<ContentFolderRef>, FilePickerError>> {
253 match registered_platform_file_picker() {
254 Some(picker) => picker.pick_folder(options),
255 None => builtin::pick_folder(options),
256 }
257 }
258
259 fn save_document(
260 &self,
261 request: SaveDocumentRequest,
262 ) -> PickerFuture<Result<Option<ContentSinkRef>, FilePickerError>> {
263 match registered_platform_file_picker() {
264 Some(picker) => picker.save_document(request),
265 None => builtin::save_document(request),
266 }
267 }
268
269 fn pick_writable_folder(
270 &self,
271 options: FilePickerOptions,
272 ) -> PickerFuture<Result<Option<String>, FilePickerError>> {
273 match registered_platform_file_picker() {
274 Some(picker) => picker.pick_writable_folder(options),
275 None => builtin::pick_writable_folder(options),
276 }
277 }
278
279 fn take_recovered_pick(&self) -> Option<RecoveredPick> {
280 registered_platform_file_picker().and_then(|picker| picker.take_recovered_pick())
281 }
282}
283
284pub fn default_file_picker() -> FilePickerRef {
286 Rc::new(PlatformFilePicker)
287}
288
289pub fn local_file_picker() -> CompositionLocal<FilePickerRef> {
291 crate::composition_locals::cached_local(
292 |locals| &locals.file_picker,
293 || compositionLocalOfWithPolicy(default_file_picker, Rc::ptr_eq),
294 )
295}
296
297#[composable]
299pub fn ProvideFilePicker(content: impl FnOnce()) {
300 let picker = cranpose_core::remember(default_file_picker).with(|state| state.clone());
301 let picker_local = local_file_picker();
302
303 CompositionLocalProvider([picker_local.provides(picker)], move || {
304 content();
305 });
306}
307
308mod builtin {
309
310 #[cfg(all(
311 not(target_arch = "wasm32"),
312 not(target_os = "android"),
313 not(target_os = "ios"),
314 feature = "file-picker-native"
315 ))]
316 pub(super) use super::desktop::{
317 pick_file, pick_files, pick_folder, pick_writable_folder, save_document,
318 };
319 #[cfg(all(target_arch = "wasm32", feature = "file-picker-web"))]
320 pub(super) use super::web::{
321 pick_file, pick_files, pick_folder, pick_writable_folder, save_document,
322 };
323
324 #[cfg(not(any(
325 all(
326 not(target_arch = "wasm32"),
327 not(target_os = "android"),
328 not(target_os = "ios"),
329 feature = "file-picker-native"
330 ),
331 all(target_arch = "wasm32", feature = "file-picker-web")
332 )))]
333 mod unsupported {
334 use crate::{
335 content::{ContentFolderRef, ContentHandle, ContentSinkRef},
336 file_picker::{FilePickerError, FilePickerOptions, PickerFuture, SaveDocumentRequest},
337 };
338
339 pub(in crate::file_picker) fn pick_file(
340 _options: FilePickerOptions,
341 ) -> PickerFuture<Result<Option<ContentHandle>, FilePickerError>> {
342 Box::pin(async { Err(FilePickerError::UnsupportedPlatform) })
343 }
344
345 pub(in crate::file_picker) fn pick_files(
346 _options: FilePickerOptions,
347 ) -> PickerFuture<Result<Vec<ContentHandle>, FilePickerError>> {
348 Box::pin(async { Err(FilePickerError::UnsupportedPlatform) })
349 }
350
351 pub(in crate::file_picker) fn pick_folder(
352 _options: FilePickerOptions,
353 ) -> PickerFuture<Result<Option<ContentFolderRef>, FilePickerError>> {
354 Box::pin(async { Err(FilePickerError::UnsupportedPlatform) })
355 }
356
357 pub(in crate::file_picker) fn save_document(
358 _request: SaveDocumentRequest,
359 ) -> PickerFuture<Result<Option<ContentSinkRef>, FilePickerError>> {
360 Box::pin(async { Err(FilePickerError::UnsupportedPlatform) })
361 }
362
363 pub(in crate::file_picker) fn pick_writable_folder(
364 _options: FilePickerOptions,
365 ) -> PickerFuture<Result<Option<String>, FilePickerError>> {
366 Box::pin(async { Err(FilePickerError::UnsupportedPlatform) })
367 }
368 }
369
370 #[cfg(not(any(
371 all(
372 not(target_arch = "wasm32"),
373 not(target_os = "android"),
374 not(target_os = "ios"),
375 feature = "file-picker-native"
376 ),
377 all(target_arch = "wasm32", feature = "file-picker-web")
378 )))]
379 pub(super) use unsupported::{
380 pick_file, pick_files, pick_folder, pick_writable_folder, save_document,
381 };
382}
383
384#[cfg(all(
385 not(target_arch = "wasm32"),
386 not(target_os = "android"),
387 not(target_os = "ios"),
388 feature = "file-picker-native"
389))]
390mod desktop;
391
392#[cfg(all(target_arch = "wasm32", feature = "file-picker-web"))]
393mod web;
394
395#[cfg(test)]
396#[path = "tests/file_picker_tests.rs"]
397mod tests;