Skip to main content

ferroui_base/platform/storage/
file_picker_save_options.rs

1use super::{FilePickerFileType, PickerOptions};
2use std::ops::{Deref, DerefMut};
3use std::rc::Rc;
4
5/// Options class for the
6/// [`IStorageProvider::save_file_picker_async`](super::IStorageProvider::save_file_picker_async)
7/// method.
8#[derive(Clone, Default)]
9pub struct FilePickerSaveOptions {
10    base: PickerOptions,
11    suggested_file_type: Option<Rc<FilePickerFileType>>,
12    default_extension: Option<String>,
13    file_type_choices: Option<Vec<Rc<FilePickerFileType>>>,
14    show_overwrite_prompt: Option<bool>,
15}
16
17impl Deref for FilePickerSaveOptions {
18    type Target = PickerOptions;
19
20    fn deref(&self) -> &PickerOptions {
21        &self.base
22    }
23}
24
25impl DerefMut for FilePickerSaveOptions {
26    fn deref_mut(&mut self) -> &mut PickerOptions {
27        &mut self.base
28    }
29}
30
31impl From<PickerOptions> for FilePickerSaveOptions {
32    fn from(base: PickerOptions) -> Self {
33        Self { base, ..Self::default() }
34    }
35}
36
37impl FilePickerSaveOptions {
38    /// Creates options with nothing set.
39    pub fn new() -> Self {
40        Self::default()
41    }
42
43    /// The file type that should be preselected when the dialog is opened.
44    ///
45    /// This value should reference one of the items in
46    /// [`file_type_choices`](Self::file_type_choices). If not set, the
47    /// first file type may be selected by default.
48    pub fn suggested_file_type(&self) -> Option<&Rc<FilePickerFileType>> {
49        self.suggested_file_type.as_ref()
50    }
51
52    pub fn set_suggested_file_type(&mut self, value: Option<Rc<FilePickerFileType>>) {
53        self.suggested_file_type = value;
54    }
55
56    /// Sets [`suggested_file_type`](Self::suggested_file_type) and returns
57    /// the options.
58    pub fn with_suggested_file_type(mut self, value: Rc<FilePickerFileType>) -> Self {
59        self.suggested_file_type = Some(value);
60        self
61    }
62
63    /// The default extension to be used to save the file.
64    pub fn default_extension(&self) -> Option<&str> {
65        self.default_extension.as_deref()
66    }
67
68    pub fn set_default_extension(&mut self, value: Option<String>) {
69        self.default_extension = value;
70    }
71
72    /// Sets [`default_extension`](Self::default_extension) and returns the
73    /// options.
74    pub fn with_default_extension(mut self, value: impl Into<String>) -> Self {
75        self.default_extension = Some(value.into());
76        self
77    }
78
79    /// The collection of valid file types that the user can choose to
80    /// assign to a file.
81    pub fn file_type_choices(&self) -> Option<&[Rc<FilePickerFileType>]> {
82        self.file_type_choices.as_deref()
83    }
84
85    pub fn set_file_type_choices(&mut self, value: Option<Vec<Rc<FilePickerFileType>>>) {
86        self.file_type_choices = value;
87    }
88
89    /// Sets [`file_type_choices`](Self::file_type_choices) and returns the
90    /// options.
91    pub fn with_file_type_choices(mut self, value: Vec<Rc<FilePickerFileType>>) -> Self {
92        self.file_type_choices = Some(value);
93        self
94    }
95
96    /// A value indicating whether the file dialog displays a warning if the
97    /// user specifies the name of a file that already exists.
98    pub fn show_overwrite_prompt(&self) -> Option<bool> {
99        self.show_overwrite_prompt
100    }
101
102    pub fn set_show_overwrite_prompt(&mut self, value: Option<bool>) {
103        self.show_overwrite_prompt = value;
104    }
105
106    /// Sets [`show_overwrite_prompt`](Self::show_overwrite_prompt) and
107    /// returns the options.
108    pub fn with_show_overwrite_prompt(mut self, value: bool) -> Self {
109        self.show_overwrite_prompt = Some(value);
110        self
111    }
112}