use crate::{app::WindowKey, files::FileFilter};
use std::path::{Path, PathBuf};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FileDialogKind {
OpenFile,
OpenFiles,
SaveFile,
PickFolder,
PickFolders,
}
impl FileDialogKind {
pub fn is_multiple(self) -> bool {
matches!(self, Self::OpenFiles | Self::PickFolders)
}
}
#[derive(Clone, Debug)]
pub struct FileDialog {
kind: FileDialogKind,
title: Option<String>,
directory: Option<PathBuf>,
file_name: Option<String>,
filters: Vec<FileFilter>,
parent: Option<WindowKey>,
}
impl FileDialog {
fn new(kind: FileDialogKind) -> Self {
Self {
kind,
title: None,
directory: None,
file_name: None,
filters: Vec::new(),
parent: None,
}
}
pub fn open_file() -> Self {
Self::new(FileDialogKind::OpenFile)
}
pub fn open_files() -> Self {
Self::new(FileDialogKind::OpenFiles)
}
pub fn save_file() -> Self {
Self::new(FileDialogKind::SaveFile)
}
pub fn pick_folder() -> Self {
Self::new(FileDialogKind::PickFolder)
}
pub fn pick_folders() -> Self {
Self::new(FileDialogKind::PickFolders)
}
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
pub fn directory(mut self, directory: impl Into<PathBuf>) -> Self {
self.directory = Some(directory.into());
self
}
pub fn file_name(mut self, name: impl Into<String>) -> Self {
self.file_name = Some(name.into());
self
}
pub fn filter(mut self, filter: FileFilter) -> Self {
self.filters.push(filter);
self
}
pub fn parent(mut self, window: impl Into<WindowKey>) -> Self {
self.parent = Some(window.into());
self
}
pub fn kind(&self) -> FileDialogKind {
self.kind
}
pub fn title_text(&self) -> Option<&str> {
self.title.as_deref()
}
pub fn start_directory(&self) -> Option<&Path> {
self.directory.as_deref()
}
pub fn default_file_name(&self) -> Option<&str> {
self.file_name.as_deref()
}
pub fn filters(&self) -> &[FileFilter] {
&self.filters
}
pub fn parent_window(&self) -> Option<&WindowKey> {
self.parent.as_ref()
}
}