1use floem::{
2 action::{open_file, save_as},
3 file::{FileDialogOptions, FileInfo, FileSpec},
4 reactive::{create_rw_signal, SignalGet, SignalUpdate},
5 text::Weight,
6 views::{button, h_stack, label, v_stack, Decorators},
7 IntoView,
8};
9
10pub fn files_view() -> impl IntoView {
11 let files = create_rw_signal("".to_string());
12 let view = h_stack((
13 button("Select file").on_click_cont(move |_| {
14 open_file(
15 FileDialogOptions::new()
16 .force_starting_directory("/")
17 .title("Select file")
18 .allowed_types(vec![FileSpec {
19 name: "text",
20 extensions: &["txt", "rs", "md"],
21 }]),
22 move |file_info| {
23 if let Some(file) = file_info {
24 println!("Selected file: {:?}", file.path);
25 files.set(display_files(file));
26 }
27 },
28 );
29 }),
30 button("Select multiple files").on_click_cont(move |_| {
31 open_file(
32 FileDialogOptions::new()
33 .multi_selection()
34 .title("Select file")
35 .allowed_types(vec![FileSpec {
36 name: "text",
37 extensions: &["txt", "rs", "md"],
38 }]),
39 move |file_info| {
40 if let Some(file) = file_info {
41 println!("Selected file: {:?}", file.path);
42 files.set(display_files(file));
43 }
44 },
45 );
46 }),
47 button("Select folder").on_click_cont(move |_| {
48 open_file(
49 FileDialogOptions::new()
50 .select_directories()
51 .title("Select Folder"),
52 move |file_info| {
53 if let Some(file) = file_info {
54 println!("Selected folder: {:?}", file.path);
55 files.set(display_files(file));
56 }
57 },
58 );
59 }),
60 button("Select multiple folder").on_click_cont(move |_| {
61 open_file(
62 FileDialogOptions::new()
63 .select_directories()
64 .multi_selection()
65 .title("Select multiple Folder"),
66 move |file_info| {
67 if let Some(file) = file_info {
68 println!("Selected folder: {:?}", file.path);
69 files.set(display_files(file));
70 }
71 },
72 );
73 }),
74 button("Save file").on_click_cont(move |_| {
75 save_as(
76 FileDialogOptions::new()
77 .default_name("floem.file")
78 .title("Save file"),
79 move |file_info| {
80 if let Some(file) = file_info {
81 println!("Save file to: {:?}", file.path);
82 files.set(display_files(file));
83 }
84 },
85 );
86 }),
87 ))
88 .style(|s| s.justify_center());
89
90 v_stack((
91 view,
92 h_stack((
93 "Path(s): ".style(|s| s.font_weight(Weight::BOLD)),
94 label(move || files.get()),
95 )),
96 ))
97 .style(|s| {
98 s.col_gap(5)
99 .width_full()
100 .height_full()
101 .items_center()
102 .justify_center()
103 })
104}
105
106fn display_files(file: FileInfo) -> String {
107 let paths: Vec<&str> = file.path.iter().filter_map(|p| p.to_str()).collect();
108 paths.join("\n")
109}