use crate::FileHandle;
use std::path::Path;
use std::path::PathBuf;
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
#[derive(Debug, Clone)]
pub(crate) struct Filter {
pub name: String,
pub extensions: Vec<String>,
}
#[derive(Default, Debug, Clone)]
pub struct FileDialog {
pub(crate) filters: Vec<Filter>,
pub(crate) starting_directory: Option<PathBuf>,
pub(crate) file_name: Option<String>,
pub(crate) title: Option<String>,
pub(crate) parent: Option<RawWindowHandle>,
}
unsafe impl Send for FileDialog {}
impl FileDialog {
pub fn new() -> Self {
Default::default()
}
pub fn add_filter(mut self, name: &str, extensions: &[&str]) -> Self {
self.filters.push(Filter {
name: name.into(),
extensions: extensions.iter().map(|e| e.to_string()).collect(),
});
self
}
pub fn set_directory<P: AsRef<Path>>(mut self, path: P) -> Self {
self.starting_directory = Some(path.as_ref().into());
self
}
pub fn set_file_name(mut self, file_name: &str) -> Self {
self.file_name = Some(file_name.into());
self
}
pub fn set_title(mut self, title: &str) -> Self {
self.title = Some(title.into());
self
}
pub fn set_parent<W: HasRawWindowHandle>(mut self, parent: &W) -> Self {
self.parent = Some(parent.raw_window_handle());
self
}
}
use crate::backend::{FilePickerDialogImpl, FileSaveDialogImpl, FolderPickerDialogImpl};
#[cfg(not(target_arch = "wasm32"))]
impl FileDialog {
pub fn pick_file(self) -> Option<PathBuf> {
FilePickerDialogImpl::pick_file(self)
}
pub fn pick_files(self) -> Option<Vec<PathBuf>> {
FilePickerDialogImpl::pick_files(self)
}
pub fn pick_folder(self) -> Option<PathBuf> {
FolderPickerDialogImpl::pick_folder(self)
}
pub fn save_file(self) -> Option<PathBuf> {
FileSaveDialogImpl::save_file(self)
}
}
#[derive(Default, Debug, Clone)]
pub struct AsyncFileDialog {
file_dialog: FileDialog,
}
impl AsyncFileDialog {
pub fn new() -> Self {
Default::default()
}
pub fn add_filter(mut self, name: &str, extensions: &[&str]) -> Self {
self.file_dialog = self.file_dialog.add_filter(name, extensions);
self
}
pub fn set_directory<P: AsRef<Path>>(mut self, path: P) -> Self {
self.file_dialog = self.file_dialog.set_directory(path);
self
}
pub fn set_file_name(mut self, file_name: &str) -> Self {
self.file_dialog = self.file_dialog.set_file_name(file_name);
self
}
pub fn set_title(mut self, title: &str) -> Self {
self.file_dialog = self.file_dialog.set_title(title);
self
}
pub fn set_parent<W: HasRawWindowHandle>(mut self, parent: &W) -> Self {
self.file_dialog = self.file_dialog.set_parent(parent);
self
}
}
use crate::backend::{
AsyncFilePickerDialogImpl, AsyncFileSaveDialogImpl, AsyncFolderPickerDialogImpl,
};
use std::future::Future;
impl AsyncFileDialog {
pub fn pick_file(self) -> impl Future<Output = Option<FileHandle>> {
AsyncFilePickerDialogImpl::pick_file_async(self.file_dialog)
}
pub fn pick_files(self) -> impl Future<Output = Option<Vec<FileHandle>>> {
AsyncFilePickerDialogImpl::pick_files_async(self.file_dialog)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn pick_folder(self) -> impl Future<Output = Option<FileHandle>> {
AsyncFolderPickerDialogImpl::pick_folder_async(self.file_dialog)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn save_file(self) -> impl Future<Output = Option<FileHandle>> {
AsyncFileSaveDialogImpl::save_file_async(self.file_dialog)
}
}