Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed up file dialog #326

Merged
merged 2 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/files/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "fils"
version = "0.1.0"
edition = "2021"

[dependencies]
im = "15.1.0"
floem = { path = "../.." }
100 changes: 100 additions & 0 deletions examples/files/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use floem::{
action::{open_file, save_as},
file::{FileDialogOptions, FileSpec},
keyboard::{Key, ModifiersState, NamedKey},
view::View,
views::{h_stack, Decorators},
widgets::button,
};

fn app_view() -> impl View {
let view = h_stack((
button(|| "Select file").on_click_cont(|_| {
open_file(
FileDialogOptions::new()
.force_starting_directory("/")
.title("Select file")
.allowed_types(vec![FileSpec {
name: "text",
extensions: &["txt", "rs", "md"],
}]),
move |file_info| {
if let Some(file) = file_info {
println!("Selected file: {:?}", file.path);
}
},
);
}),
button(|| "Select multiple files").on_click_cont(|_| {
open_file(
FileDialogOptions::new()
.multi_selection()
.title("Select file")
.allowed_types(vec![FileSpec {
name: "text",
extensions: &["txt", "rs", "md"],
}]),
move |file_info| {
if let Some(file) = file_info {
println!("Selected file: {:?}", file.path);
}
},
);
}),
button(|| "Select folder").on_click_cont(|_| {
open_file(
FileDialogOptions::new()
.select_directories()
.title("Select Folder"),
move |file_info| {
if let Some(file) = file_info {
println!("Selected folder: {:?}", file.path);
}
},
);
}),
button(|| "Select multiple folder").on_click_cont(|_| {
open_file(
FileDialogOptions::new()
.select_directories()
.multi_selection()
.title("Select multiple Folder"),
move |file_info| {
if let Some(file) = file_info {
println!("Selected folder: {:?}", file.path);
}
},
);
}),
button(|| "Save file").on_click_cont(|_| {
save_as(
FileDialogOptions::new()
.default_name("floem.file")
.title("Save file"),
move |file_info| {
if let Some(file) = file_info {
println!("Save file to: {:?}", file.path);
}
},
);
}),
))
.style(|s| {
s.gap(5, 0)
.width_full()
.height_full()
.items_center()
.justify_center()
});

let id = view.id();
view.on_key_up(
Key::Named(NamedKey::F11),
ModifiersState::empty(),
move |_| id.inspect(),
)
}

fn main() {
floem::launch(app_view);
}
52 changes: 43 additions & 9 deletions src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,32 +109,66 @@ pub fn open_file(
options: FileDialogOptions,
file_info_action: impl Fn(Option<FileInfo>) + 'static,
) {
let send = create_ext_action(Scope::new(), move |path: Option<PathBuf>| {
file_info_action(path.map(|path| FileInfo { path, format: None }))
});
let send = create_ext_action(
Scope::new(),
move |(path, paths): (Option<PathBuf>, Option<Vec<PathBuf>>)| {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to juggle the two different types you get with multi_selection enabled. This was the best I could come up with.

if paths.is_some() {
file_info_action(paths.map(|paths| FileInfo {
path: paths,
format: None,
}))
} else {
file_info_action(path.map(|path| FileInfo {
path: vec![path],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trade off is we always return a Vec even when you can only select a single thing. I personally think that's easier to handle in user land but happy to hear others to chime in

format: None,
}))
}
},
);
std::thread::spawn(move || {
let mut dialog = rfd::FileDialog::new();
if let Some(path) = options.starting_directory.as_ref() {
dialog = dialog.set_directory(path);
}
let path = if options.select_directories {
dialog.pick_folder()
if let Some(title) = options.title.as_ref() {
dialog = dialog.set_title(title);
}
if let Some(allowed_types) = options.allowed_types.as_ref() {
dialog = allowed_types.iter().fold(dialog, |dialog, filter| {
dialog.add_filter(filter.name, filter.extensions)
});
}

if options.select_directories && options.multi_selection {
send((None, dialog.pick_folders()));
} else if options.select_directories && !options.multi_selection {
send((dialog.pick_folder(), None));
} else if !options.select_directories && options.multi_selection {
send((None, dialog.pick_files()));
} else {
dialog.pick_file()
};
send(path);
send((dialog.pick_file(), None));
}
});
}

pub fn save_as(options: FileDialogOptions, file_info_action: impl Fn(Option<FileInfo>) + 'static) {
let send = create_ext_action(Scope::new(), move |path: Option<PathBuf>| {
file_info_action(path.map(|path| FileInfo { path, format: None }))
file_info_action(path.map(|path| FileInfo {
path: vec![path],
format: None,
}))
});
std::thread::spawn(move || {
let mut dialog = rfd::FileDialog::new();
if let Some(path) = options.starting_directory.as_ref() {
dialog = dialog.set_directory(path);
}
if let Some(name) = options.default_name.as_ref() {
dialog = dialog.set_file_name(name);
}
if let Some(title) = options.title.as_ref() {
dialog = dialog.set_title(title);
}
let path = dialog.save_file();
send(path);
});
Expand Down
6 changes: 3 additions & 3 deletions src/file.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::{Path, PathBuf};
use std::path::PathBuf;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FileSpec {
Expand All @@ -23,7 +23,7 @@ pub struct FileInfo {
///
/// On macOS, this is already rewritten to use the extension that the user selected
/// with the `file format` property.
pub path: PathBuf,
pub path: Vec<PathBuf>,
/// The selected file format.
///
/// If there're multiple different formats available
Expand All @@ -36,7 +36,7 @@ pub struct FileInfo {

impl FileInfo {
/// Returns the underlying path.
pub fn path(&self) -> &Path {
pub fn path(&self) -> &Vec<PathBuf> {
&self.path
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Path here would break the compiler. Since we already type is as PathBuf above I saw no reasons to fight the compiler here.

}
}
Expand Down
Loading