Skip to main content

ferroui_base/platform/storage/
file_picker_open_options.rs

1use super::{FilePickerFileType, PickerOptions};
2use std::ops::{Deref, DerefMut};
3use std::rc::Rc;
4
5/// Options class for the
6/// [`IStorageProvider::open_file_picker_async`](super::IStorageProvider::open_file_picker_async)
7/// method.
8#[derive(Clone, Default)]
9pub struct FilePickerOpenOptions {
10    base: PickerOptions,
11    suggested_file_type: Option<Rc<FilePickerFileType>>,
12    allow_multiple: bool,
13    file_type_filter: Option<Vec<Rc<FilePickerFileType>>>,
14}
15
16impl Deref for FilePickerOpenOptions {
17    type Target = PickerOptions;
18
19    fn deref(&self) -> &PickerOptions {
20        &self.base
21    }
22}
23
24impl DerefMut for FilePickerOpenOptions {
25    fn deref_mut(&mut self) -> &mut PickerOptions {
26        &mut self.base
27    }
28}
29
30impl From<PickerOptions> for FilePickerOpenOptions {
31    fn from(base: PickerOptions) -> Self {
32        Self { base, ..Self::default() }
33    }
34}
35
36impl FilePickerOpenOptions {
37    /// Creates options with nothing set.
38    pub fn new() -> Self {
39        Self::default()
40    }
41
42    /// The file type that should be preselected when the dialog is opened.
43    ///
44    /// This value should reference one of the items in
45    /// [`file_type_filter`](Self::file_type_filter). If not set, the first
46    /// file type may be selected by default.
47    pub fn suggested_file_type(&self) -> Option<&Rc<FilePickerFileType>> {
48        self.suggested_file_type.as_ref()
49    }
50
51    pub fn set_suggested_file_type(&mut self, value: Option<Rc<FilePickerFileType>>) {
52        self.suggested_file_type = value;
53    }
54
55    /// Sets [`suggested_file_type`](Self::suggested_file_type) and returns
56    /// the options.
57    pub fn with_suggested_file_type(mut self, value: Rc<FilePickerFileType>) -> Self {
58        self.suggested_file_type = Some(value);
59        self
60    }
61
62    /// An option indicating whether the open picker allows users to select
63    /// multiple files.
64    pub fn allow_multiple(&self) -> bool {
65        self.allow_multiple
66    }
67
68    pub fn set_allow_multiple(&mut self, value: bool) {
69        self.allow_multiple = value;
70    }
71
72    /// Sets [`allow_multiple`](Self::allow_multiple) and returns the
73    /// options.
74    pub fn with_allow_multiple(mut self, value: bool) -> Self {
75        self.allow_multiple = value;
76        self
77    }
78
79    /// The collection of file types that the file open picker displays.
80    pub fn file_type_filter(&self) -> Option<&[Rc<FilePickerFileType>]> {
81        self.file_type_filter.as_deref()
82    }
83
84    pub fn set_file_type_filter(&mut self, value: Option<Vec<Rc<FilePickerFileType>>>) {
85        self.file_type_filter = value;
86    }
87
88    /// Sets [`file_type_filter`](Self::file_type_filter) and returns the
89    /// options.
90    pub fn with_file_type_filter(mut self, value: Vec<Rc<FilePickerFileType>>) -> Self {
91        self.file_type_filter = Some(value);
92        self
93    }
94}