Use sync API on macos
This commit is contained in:
+38
-10
@@ -18,39 +18,67 @@ impl FilePicker {
|
||||
self.window = Some(Arc::downgrade(window));
|
||||
}
|
||||
|
||||
pub fn new_dialog(&self) -> FileDialogBuilder {
|
||||
#[allow(unused_mut)]
|
||||
pub fn pick_file_dialog(&self) -> FileDialogBuilder {
|
||||
self.new_dialog(FileDialogAction::PickFile, false)
|
||||
}
|
||||
|
||||
pub fn save_file_dialog(&self) -> FileDialogBuilder {
|
||||
self.new_dialog(FileDialogAction::SaveFile, true)
|
||||
}
|
||||
|
||||
fn new_dialog(&self, action: FileDialogAction, attach_window: bool) -> FileDialogBuilder {
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
let mut dialog = rfd::AsyncFileDialog::new();
|
||||
#[cfg(target_os = "macos")]
|
||||
if let Some(window) = self.window.as_ref().and_then(|w| w.upgrade()) {
|
||||
let mut dialog = rfd::FileDialog::new();
|
||||
|
||||
if attach_window && let Some(window) = self.window.as_ref().and_then(|w| w.upgrade()) {
|
||||
dialog = dialog.set_parent(&window);
|
||||
}
|
||||
FileDialogBuilder { dialog }
|
||||
FileDialogBuilder { dialog, action }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FileDialogBuilder {
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
dialog: rfd::AsyncFileDialog,
|
||||
#[cfg(target_os = "macos")]
|
||||
dialog: rfd::FileDialog,
|
||||
action: FileDialogAction
|
||||
}
|
||||
|
||||
impl FileDialogBuilder {
|
||||
pub fn add_filter(self, name: impl Into<String>, extensions: &[impl ToString]) -> Self {
|
||||
Self {
|
||||
dialog: self.dialog.add_filter(name, extensions),
|
||||
..self
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_file_name(self, file_name: impl Into<String>) -> Self {
|
||||
Self {
|
||||
dialog: self.dialog.set_file_name(file_name),
|
||||
..self
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pick_file(self) -> Option<PathBuf> {
|
||||
pollster::block_on(self.dialog.pick_file()).map(Into::into)
|
||||
}
|
||||
|
||||
pub fn save_file(self) -> Option<PathBuf> {
|
||||
pollster::block_on(self.dialog.save_file()).map(Into::into)
|
||||
pub fn open(self) -> Option<PathBuf> {
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
fn resolve(action: impl Future<Output = Option<rfd::FileHandle>>) -> Option<PathBuf> {
|
||||
pollster::block_on(action).map(Into::into)
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
fn resolve(action: Option<PathBuf>) -> Option<PathBuf> {
|
||||
action
|
||||
}
|
||||
match self.action {
|
||||
FileDialogAction::PickFile => resolve(self.dialog.pick_file()),
|
||||
FileDialogAction::SaveFile => resolve(self.dialog.save_file()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum FileDialogAction {
|
||||
PickFile,
|
||||
SaveFile,
|
||||
}
|
||||
Reference in New Issue
Block a user