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/// Presents native file and folder pickers.
139pub trait FilePicker {
140    /// Presents a single-file picker. Resolves to `None` if cancelled.
141    fn pick_file(
142        &self,
143        options: FilePickerOptions,
144    ) -> PickerFuture<Result<Option<PickedEntryRef>, FilePickerError>>;
145
146    /// Presents a folder/tree picker. Resolves to `None` if cancelled.
147    fn pick_folder(
148        &self,
149        options: FilePickerOptions,
150    ) -> PickerFuture<Result<Option<PickedEntryRef>, FilePickerError>>;
151}
152
153/// Shared handle to a [`FilePicker`].
154pub type FilePickerRef = Rc<dyn FilePicker>;
155
156thread_local! {
157    static PLATFORM_FILE_PICKER: RefCell<Option<FilePickerRef>> = const { RefCell::new(None) };
158}
159
160/// Registers the platform-provided picker (Android SAF / iOS UIDocumentPicker).
161///
162/// The cranpose crate's Android and iOS backends call this during startup, when
163/// they have access to the Activity / root view controller. Once registered it
164/// takes precedence over the built-in desktop/web pickers.
165pub fn set_platform_file_picker(picker: FilePickerRef) {
166    PLATFORM_FILE_PICKER.with(|cell| *cell.borrow_mut() = Some(picker));
167}
168
169/// Removes any registered platform picker (used in tests and teardown).
170pub fn clear_platform_file_picker() {
171    PLATFORM_FILE_PICKER.with(|cell| *cell.borrow_mut() = None);
172}
173
174fn registered_platform_file_picker() -> Option<FilePickerRef> {
175    PLATFORM_FILE_PICKER.with(|cell| cell.borrow().clone())
176}
177
178/// The picker installed by [`ProvideFilePicker`]: a registered platform picker
179/// if present, otherwise the built-in backend for this target.
180struct PlatformFilePicker;
181
182impl FilePicker for PlatformFilePicker {
183    fn pick_file(
184        &self,
185        options: FilePickerOptions,
186    ) -> PickerFuture<Result<Option<PickedEntryRef>, FilePickerError>> {
187        if let Some(picker) = registered_platform_file_picker() {
188            return picker.pick_file(options);
189        }
190        builtin_pick(options, PickedKind::File)
191    }
192
193    fn pick_folder(
194        &self,
195        options: FilePickerOptions,
196    ) -> PickerFuture<Result<Option<PickedEntryRef>, FilePickerError>> {
197        if let Some(picker) = registered_platform_file_picker() {
198            return picker.pick_folder(options);
199        }
200        builtin_pick(options, PickedKind::Folder)
201    }
202}
203
204#[cfg_attr(
205    not(any(feature = "file-picker-native", feature = "file-picker-web")),
206    allow(unused_variables)
207)]
208fn builtin_pick(
209    options: FilePickerOptions,
210    kind: PickedKind,
211) -> PickerFuture<Result<Option<PickedEntryRef>, FilePickerError>> {
212    #[cfg(all(
213        not(target_arch = "wasm32"),
214        not(target_os = "android"),
215        not(target_os = "ios"),
216        feature = "file-picker-native"
217    ))]
218    {
219        return desktop::pick(options, kind);
220    }
221
222    #[cfg(all(target_arch = "wasm32", feature = "file-picker-web"))]
223    {
224        return web::pick(options, kind);
225    }
226
227    #[allow(unreachable_code)]
228    Box::pin(async move { Err(FilePickerError::UnsupportedPlatform) })
229}
230
231/// The default picker (the platform backend).
232pub fn default_file_picker() -> FilePickerRef {
233    Rc::new(PlatformFilePicker)
234}
235
236/// The [`CompositionLocal`] carrying the active [`FilePicker`].
237pub fn local_file_picker() -> CompositionLocal<FilePickerRef> {
238    thread_local! {
239        static LOCAL_FILE_PICKER: RefCell<Option<CompositionLocal<FilePickerRef>>> = const { RefCell::new(None) };
240    }
241
242    LOCAL_FILE_PICKER.with(|cell| {
243        cell.borrow_mut()
244            .get_or_insert_with(|| compositionLocalOfWithPolicy(default_file_picker, Rc::ptr_eq))
245            .clone()
246    })
247}
248
249/// Provides the default [`FilePicker`] to descendant composables.
250#[allow(non_snake_case)]
251#[composable]
252pub fn ProvideFilePicker(content: impl FnOnce()) {
253    let picker = cranpose_core::remember(default_file_picker).with(|state| state.clone());
254    let picker_local = local_file_picker();
255
256    CompositionLocalProvider(vec![picker_local.provides(picker)], move || {
257        content();
258    });
259}
260
261#[cfg(all(
262    not(target_arch = "wasm32"),
263    not(target_os = "android"),
264    not(target_os = "ios"),
265    feature = "file-picker-native"
266))]
267mod desktop;
268
269#[cfg(all(target_arch = "wasm32", feature = "file-picker-web"))]
270mod web;
271
272#[cfg(test)]
273mod tests {
274    use super::*;
275
276    #[test]
277    fn options_builder_sets_title_and_filters() {
278        let options = FilePickerOptions::default()
279            .with_title("Pick audio")
280            .with_filter(FileFilter::new("Audio", &["mp3", "flac"]));
281        assert_eq!(options.title.as_deref(), Some("Pick audio"));
282        assert_eq!(options.filters.len(), 1);
283        assert_eq!(options.filters[0].extensions, vec!["mp3", "flac"]);
284    }
285
286    #[test]
287    fn default_picker_is_created() {
288        let picker = default_file_picker();
289        assert_eq!(Rc::strong_count(&picker), 1);
290    }
291
292    #[test]
293    fn registered_platform_picker_takes_precedence() {
294        struct Marker;
295        impl FilePicker for Marker {
296            fn pick_file(
297                &self,
298                _options: FilePickerOptions,
299            ) -> PickerFuture<Result<Option<PickedEntryRef>, FilePickerError>> {
300                Box::pin(async { Err(FilePickerError::Failed("marker".into())) })
301            }
302            fn pick_folder(
303                &self,
304                _options: FilePickerOptions,
305            ) -> PickerFuture<Result<Option<PickedEntryRef>, FilePickerError>> {
306                Box::pin(async { Err(FilePickerError::Failed("marker".into())) })
307            }
308        }
309
310        clear_platform_file_picker();
311        assert!(registered_platform_file_picker().is_none());
312        set_platform_file_picker(Rc::new(Marker));
313        assert!(registered_platform_file_picker().is_some());
314
315        let result =
316            pollster::block_on(default_file_picker().pick_file(FilePickerOptions::default()));
317        assert!(matches!(result, Err(FilePickerError::Failed(_))));
318        clear_platform_file_picker();
319    }
320}