Skip to main content

cranpose_services/
file_picker.rs

1//! Native, cross-platform file and folder picker.
2//!
3//! Applications pick through the [`FilePicker`] obtained from
4//! [`local_file_picker`] and receive an opaque [`PickedEntry`] handle, not a
5//! filesystem path. This is deliberate: on Android (Storage Access Framework
6//! `content://` trees), iOS (`UIDocumentPicker` security-scoped URLs) and the
7//! web (File System Access handles) the user can choose folders served by the
8//! *system* document providers — a mounted WebDAV share, cloud storage, etc. —
9//! which do not map to a local path. [`PickedEntry::read_bytes`] and
10//! [`PickedEntry::list`] read through the originating provider on every
11//! platform.
12//!
13//! The picker is asynchronous and must run on the UI thread, so callers drive
14//! it from [`cranpose_core::LaunchedEffectAsync`].
15
16use cranpose_core::compositionLocalOfWithPolicy;
17use cranpose_core::CompositionLocal;
18use cranpose_core::CompositionLocalProvider;
19use cranpose_macros::composable;
20use std::cell::RefCell;
21use std::future::Future;
22use std::pin::Pin;
23use std::rc::Rc;
24
25/// Errors produced while presenting a picker or reading a picked entry.
26#[derive(thiserror::Error, Debug, Clone)]
27pub enum FilePickerError {
28    /// Presenting the picker failed.
29    #[error("file picker failed: {0}")]
30    Failed(String),
31    /// Reading or listing a picked entry failed.
32    #[error("reading picked entry failed: {0}")]
33    ReadFailed(String),
34    /// `list` was called on a file, or `read_bytes` on a folder.
35    #[error("picked entry is a {actual}, expected a {expected}")]
36    WrongKind {
37        /// The kind the entry actually is.
38        actual: &'static str,
39        /// The kind the operation expected.
40        expected: &'static str,
41    },
42    /// The picker requires a cranpose-services feature that is not enabled.
43    #[error("{operation} requires cranpose-services feature `{feature}`")]
44    UnsupportedFeature {
45        /// The attempted operation.
46        operation: &'static str,
47        /// The feature that enables it.
48        feature: &'static str,
49    },
50    /// No picker is available on this platform/build.
51    #[error("file picking is not available on this platform")]
52    UnsupportedPlatform,
53}
54
55/// A `'static` future returned by picker operations, polled on the UI thread.
56pub type PickerFuture<T> = Pin<Box<dyn Future<Output = T>>>;
57
58/// Whether a [`PickedEntry`] is a single file or a folder/tree.
59#[derive(Clone, Copy, Debug, PartialEq, Eq)]
60pub enum PickedKind {
61    /// A single file.
62    File,
63    /// A folder (directory tree).
64    Folder,
65}
66
67/// A named filter limiting the file types offered by the picker.
68#[derive(Clone, Debug, Default)]
69pub struct FileFilter {
70    /// Human-readable group name, for example `"Audio"`.
71    pub label: String,
72    /// Accepted extensions without the leading dot, for example `["mp3", "flac"]`.
73    pub extensions: Vec<String>,
74}
75
76impl FileFilter {
77    /// Creates a filter from a label and a set of extensions.
78    pub fn new(label: impl Into<String>, extensions: &[&str]) -> Self {
79        Self {
80            label: label.into(),
81            extensions: extensions.iter().map(|ext| (*ext).to_string()).collect(),
82        }
83    }
84}
85
86/// Options controlling a pick request.
87#[derive(Clone, Debug, Default)]
88pub struct FilePickerOptions {
89    /// Dialog title.
90    pub title: Option<String>,
91    /// File-type filters (ignored by folder pickers and some platforms).
92    pub filters: Vec<FileFilter>,
93}
94
95impl FilePickerOptions {
96    /// Sets the dialog title.
97    pub fn with_title(mut self, title: impl Into<String>) -> Self {
98        self.title = Some(title.into());
99        self
100    }
101
102    /// Adds a file-type filter.
103    pub fn with_filter(mut self, filter: FileFilter) -> Self {
104        self.filters.push(filter);
105        self
106    }
107}
108
109/// An opaque handle to a picked file or folder.
110///
111/// This is **not** guaranteed to be a filesystem path. Use [`read_bytes`] to
112/// read a file and [`list`] to enumerate a folder's immediate children; both
113/// go through the originating system provider.
114///
115/// [`read_bytes`]: PickedEntry::read_bytes
116/// [`list`]: PickedEntry::list
117pub trait PickedEntry {
118    /// The display name (last path/URI component).
119    fn name(&self) -> String;
120
121    /// Whether this entry is a file or a folder.
122    fn kind(&self) -> PickedKind;
123
124    /// A user-facing identifier (a path or a `content://`/`file://` URI). Not
125    /// guaranteed to be a usable filesystem path.
126    fn display_path(&self) -> String;
127
128    /// Reads the full contents of a [`PickedKind::File`] entry.
129    fn read_bytes(&self) -> PickerFuture<Result<Vec<u8>, FilePickerError>>;
130
131    /// Lists the immediate children of a [`PickedKind::Folder`] entry.
132    fn list(&self) -> PickerFuture<Result<Vec<PickedEntryRef>, FilePickerError>>;
133}
134
135/// Shared handle to a [`PickedEntry`].
136pub type PickedEntryRef = Rc<dyn PickedEntry>;
137
138/// A folder being enumerated, yielding its files as the provider discovers
139/// them.
140///
141/// Walking a deep tree from a slow provider (cloud storage, a mounted WebDAV
142/// share) can take a long time, so the files are delivered incrementally rather
143/// than all at once: poll [`take_ready`] each frame to drain newly-found files,
144/// and [`is_finished`] to know when the walk is done. Callers can show the
145/// running count and start playing the first file without waiting for the rest.
146///
147/// [`take_ready`]: FolderStream::take_ready
148/// [`is_finished`]: FolderStream::is_finished
149pub trait FolderStream {
150    /// Files discovered since the previous call (drains the ready queue).
151    fn take_ready(&self) -> Vec<PickedEntryRef>;
152
153    /// Whether enumeration has finished (no more files will be discovered).
154    fn is_finished(&self) -> bool;
155
156    /// Takes the error that ended enumeration early, if any.
157    fn take_error(&self) -> Option<FilePickerError>;
158}
159
160/// Shared handle to a [`FolderStream`].
161pub type FolderStreamRef = Rc<dyn FolderStream>;
162
163/// Recursively collects every [`PickedKind::File`] under `entry`.
164fn collect_files(entry: PickedEntryRef) -> PickerFuture<Vec<PickedEntryRef>> {
165    Box::pin(async move {
166        match entry.kind() {
167            PickedKind::File => vec![entry],
168            PickedKind::Folder => {
169                let mut files = Vec::new();
170                if let Ok(children) = entry.list().await {
171                    for child in children {
172                        files.extend(collect_files(child).await);
173                    }
174                }
175                files
176            }
177        }
178    })
179}
180
181/// A [`FolderStream`] whose files were all gathered up front (the default for
182/// providers that enumerate eagerly, e.g. the local filesystem). It yields
183/// everything on the first poll and is immediately finished.
184struct ReadyFolderStream {
185    ready: RefCell<Vec<PickedEntryRef>>,
186}
187
188impl FolderStream for ReadyFolderStream {
189    fn take_ready(&self) -> Vec<PickedEntryRef> {
190        std::mem::take(&mut self.ready.borrow_mut())
191    }
192
193    fn is_finished(&self) -> bool {
194        true
195    }
196
197    fn take_error(&self) -> Option<FilePickerError> {
198        None
199    }
200}
201
202/// Wraps an eager [`FilePicker::pick_folder`] as a [`FolderStream`] by walking
203/// the whole tree up front. Used as the default for every backend that does not
204/// stream natively.
205fn eager_folder_stream(
206    folder: PickerFuture<Result<Option<PickedEntryRef>, FilePickerError>>,
207) -> PickerFuture<Result<Option<FolderStreamRef>, FilePickerError>> {
208    Box::pin(async move {
209        match folder.await? {
210            None => Ok(None),
211            Some(entry) => {
212                let files = collect_files(entry).await;
213                Ok(Some(Rc::new(ReadyFolderStream {
214                    ready: RefCell::new(files),
215                }) as FolderStreamRef))
216            }
217        }
218    })
219}
220
221/// Presents native file and folder pickers.
222pub trait FilePicker {
223    /// Presents a single-file picker. Resolves to `None` if cancelled.
224    fn pick_file(
225        &self,
226        options: FilePickerOptions,
227    ) -> PickerFuture<Result<Option<PickedEntryRef>, FilePickerError>>;
228
229    /// Presents a folder/tree picker. Resolves to `None` if cancelled.
230    fn pick_folder(
231        &self,
232        options: FilePickerOptions,
233    ) -> PickerFuture<Result<Option<PickedEntryRef>, FilePickerError>>;
234
235    /// Presents a folder picker and streams the tree's files as they are
236    /// discovered (see [`FolderStream`]).
237    ///
238    /// The default walks the picked folder eagerly via [`pick_folder`] and
239    /// yields every file at once; backends served by a slow provider (Android's
240    /// Storage Access Framework) override this to stream during the walk.
241    /// Resolves to `None` if cancelled.
242    ///
243    /// [`pick_folder`]: FilePicker::pick_folder
244    fn pick_folder_streaming(
245        &self,
246        options: FilePickerOptions,
247    ) -> PickerFuture<Result<Option<FolderStreamRef>, FilePickerError>> {
248        eager_folder_stream(self.pick_folder(options))
249    }
250}
251
252/// Shared handle to a [`FilePicker`].
253pub type FilePickerRef = Rc<dyn FilePicker>;
254
255thread_local! {
256    static PLATFORM_FILE_PICKER: RefCell<Option<FilePickerRef>> = const { RefCell::new(None) };
257}
258
259/// Registers the platform-provided picker (Android SAF / iOS UIDocumentPicker).
260///
261/// The cranpose crate's Android and iOS backends call this during startup, when
262/// they have access to the Activity / root view controller. Once registered it
263/// takes precedence over the built-in desktop/web pickers.
264pub fn set_platform_file_picker(picker: FilePickerRef) {
265    PLATFORM_FILE_PICKER.with(|cell| *cell.borrow_mut() = Some(picker));
266}
267
268/// Removes any registered platform picker (used in tests and teardown).
269pub fn clear_platform_file_picker() {
270    PLATFORM_FILE_PICKER.with(|cell| *cell.borrow_mut() = None);
271}
272
273fn registered_platform_file_picker() -> Option<FilePickerRef> {
274    PLATFORM_FILE_PICKER.with(|cell| cell.borrow().clone())
275}
276
277/// The picker installed by [`ProvideFilePicker`]: a registered platform picker
278/// if present, otherwise the built-in backend for this target.
279struct PlatformFilePicker;
280
281impl FilePicker for PlatformFilePicker {
282    fn pick_file(
283        &self,
284        options: FilePickerOptions,
285    ) -> PickerFuture<Result<Option<PickedEntryRef>, FilePickerError>> {
286        if let Some(picker) = registered_platform_file_picker() {
287            return picker.pick_file(options);
288        }
289        builtin_pick(options, PickedKind::File)
290    }
291
292    fn pick_folder(
293        &self,
294        options: FilePickerOptions,
295    ) -> PickerFuture<Result<Option<PickedEntryRef>, FilePickerError>> {
296        if let Some(picker) = registered_platform_file_picker() {
297            return picker.pick_folder(options);
298        }
299        builtin_pick(options, PickedKind::Folder)
300    }
301
302    fn pick_folder_streaming(
303        &self,
304        options: FilePickerOptions,
305    ) -> PickerFuture<Result<Option<FolderStreamRef>, FilePickerError>> {
306        if let Some(picker) = registered_platform_file_picker() {
307            return picker.pick_folder_streaming(options);
308        }
309        eager_folder_stream(builtin_pick(options, PickedKind::Folder))
310    }
311}
312
313#[cfg_attr(
314    not(any(feature = "file-picker-native", feature = "file-picker-web")),
315    allow(unused_variables)
316)]
317fn builtin_pick(
318    options: FilePickerOptions,
319    kind: PickedKind,
320) -> PickerFuture<Result<Option<PickedEntryRef>, FilePickerError>> {
321    #[cfg(all(
322        not(target_arch = "wasm32"),
323        not(target_os = "android"),
324        not(target_os = "ios"),
325        feature = "file-picker-native"
326    ))]
327    {
328        return desktop::pick(options, kind);
329    }
330
331    #[cfg(all(target_arch = "wasm32", feature = "file-picker-web"))]
332    {
333        return web::pick(options, kind);
334    }
335
336    #[allow(unreachable_code)]
337    Box::pin(async move { Err(FilePickerError::UnsupportedPlatform) })
338}
339
340/// The default picker (the platform backend).
341pub fn default_file_picker() -> FilePickerRef {
342    Rc::new(PlatformFilePicker)
343}
344
345/// The [`CompositionLocal`] carrying the active [`FilePicker`].
346pub fn local_file_picker() -> CompositionLocal<FilePickerRef> {
347    thread_local! {
348        static LOCAL_FILE_PICKER: RefCell<Option<CompositionLocal<FilePickerRef>>> = const { RefCell::new(None) };
349    }
350
351    LOCAL_FILE_PICKER.with(|cell| {
352        cell.borrow_mut()
353            .get_or_insert_with(|| compositionLocalOfWithPolicy(default_file_picker, Rc::ptr_eq))
354            .clone()
355    })
356}
357
358/// Provides the default [`FilePicker`] to descendant composables.
359#[allow(non_snake_case)]
360#[composable]
361pub fn ProvideFilePicker(content: impl FnOnce()) {
362    let picker = cranpose_core::remember(default_file_picker).with(|state| state.clone());
363    let picker_local = local_file_picker();
364
365    CompositionLocalProvider(vec![picker_local.provides(picker)], move || {
366        content();
367    });
368}
369
370#[cfg(all(
371    not(target_arch = "wasm32"),
372    not(target_os = "android"),
373    not(target_os = "ios"),
374    feature = "file-picker-native"
375))]
376mod desktop;
377
378#[cfg(all(target_arch = "wasm32", feature = "file-picker-web"))]
379mod web;
380
381#[cfg(test)]
382mod tests {
383    use super::*;
384
385    #[test]
386    fn options_builder_sets_title_and_filters() {
387        let options = FilePickerOptions::default()
388            .with_title("Pick audio")
389            .with_filter(FileFilter::new("Audio", &["mp3", "flac"]));
390        assert_eq!(options.title.as_deref(), Some("Pick audio"));
391        assert_eq!(options.filters.len(), 1);
392        assert_eq!(options.filters[0].extensions, vec!["mp3", "flac"]);
393    }
394
395    #[test]
396    fn default_picker_is_created() {
397        let picker = default_file_picker();
398        assert_eq!(Rc::strong_count(&picker), 1);
399    }
400
401    #[test]
402    fn registered_platform_picker_takes_precedence() {
403        struct Marker;
404        impl FilePicker for Marker {
405            fn pick_file(
406                &self,
407                _options: FilePickerOptions,
408            ) -> PickerFuture<Result<Option<PickedEntryRef>, FilePickerError>> {
409                Box::pin(async { Err(FilePickerError::Failed("marker".into())) })
410            }
411            fn pick_folder(
412                &self,
413                _options: FilePickerOptions,
414            ) -> PickerFuture<Result<Option<PickedEntryRef>, FilePickerError>> {
415                Box::pin(async { Err(FilePickerError::Failed("marker".into())) })
416            }
417        }
418
419        clear_platform_file_picker();
420        assert!(registered_platform_file_picker().is_none());
421        set_platform_file_picker(Rc::new(Marker));
422        assert!(registered_platform_file_picker().is_some());
423
424        let result =
425            pollster::block_on(default_file_picker().pick_file(FilePickerOptions::default()));
426        assert!(matches!(result, Err(FilePickerError::Failed(_))));
427        clear_platform_file_picker();
428    }
429}