Use async file picker API
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
use std::{
|
||||
path::PathBuf,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
use winit::window::Window;
|
||||
|
||||
pub struct FilePicker {
|
||||
window: Option<Weak<Window>>,
|
||||
}
|
||||
|
||||
impl FilePicker {
|
||||
pub fn new() -> Self {
|
||||
Self { window: None }
|
||||
}
|
||||
|
||||
pub fn set_window(&mut self, window: &Arc<Window>) {
|
||||
self.window = Some(Arc::downgrade(window));
|
||||
}
|
||||
|
||||
pub fn new_dialog(&self) -> FileDialogBuilder {
|
||||
let mut dialog = rfd::AsyncFileDialog::new();
|
||||
if let Some(window) = self.window.as_ref().and_then(|w| w.upgrade()) {
|
||||
dialog = dialog.set_parent(&window);
|
||||
}
|
||||
FileDialogBuilder { dialog }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FileDialogBuilder {
|
||||
dialog: rfd::AsyncFileDialog,
|
||||
}
|
||||
|
||||
impl FileDialogBuilder {
|
||||
pub fn add_filter(self, name: impl Into<String>, extensions: &[impl ToString]) -> Self {
|
||||
Self {
|
||||
dialog: self.dialog.add_filter(name, extensions),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_file_name(self, file_name: impl Into<String>) -> Self {
|
||||
Self {
|
||||
dialog: self.dialog.set_file_name(file_name),
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user