1#![windows_subsystem = "windows"]
19
20use druid::widget::{Align, Button, Flex, TextBox};
21use druid::{
22 commands, AppDelegate, AppLauncher, Command, DelegateCtx, Env, FileDialogOptions, FileSpec,
23 Handled, LocalizedString, Target, Widget, WindowDesc,
24};
25
26struct Delegate;
27
28pub fn main() {
29 let main_window = WindowDesc::new(ui_builder())
30 .title(LocalizedString::new("open-save-demo").with_placeholder("Opening/Saving Demo"));
31 let data = "Type here.".to_owned();
32 AppLauncher::with_window(main_window)
33 .delegate(Delegate)
34 .log_to_console()
35 .launch(data)
36 .expect("launch failed");
37}
38
39fn ui_builder() -> impl Widget<String> {
40 let rs = FileSpec::new("Rust source", &["rs"]);
41 let txt = FileSpec::new("Text file", &["txt"]);
42 let other = FileSpec::new("Bogus file", &["foo", "bar", "baz"]);
43 let default_save_name = String::from("MyFile.txt");
46 let save_dialog_options = FileDialogOptions::new()
47 .allowed_types(vec![rs, txt, other])
48 .default_type(txt)
49 .default_name(default_save_name)
50 .name_label("Target")
51 .title("Choose a target for this lovely file")
52 .button_text("Export");
53 let open_dialog_options = save_dialog_options
54 .clone()
55 .default_name("MySavedFile.txt")
56 .name_label("Source")
57 .title("Where did you put that file?")
58 .button_text("Import");
59
60 let input = TextBox::new();
61 let save = Button::new("Save").on_click(move |ctx, _, _| {
62 ctx.submit_command(druid::commands::SHOW_SAVE_PANEL.with(save_dialog_options.clone()))
63 });
64 let open = Button::new("Open").on_click(move |ctx, _, _| {
65 ctx.submit_command(druid::commands::SHOW_OPEN_PANEL.with(open_dialog_options.clone()))
66 });
67
68 let mut col = Flex::column();
69 col.add_child(input);
70 col.add_spacer(8.0);
71 col.add_child(save);
72 col.add_child(open);
73 Align::centered(col)
74}
75
76impl AppDelegate<String> for Delegate {
77 fn command(
78 &mut self,
79 _ctx: &mut DelegateCtx,
80 _target: Target,
81 cmd: &Command,
82 data: &mut String,
83 _env: &Env,
84 ) -> Handled {
85 if let Some(file_info) = cmd.get(commands::SAVE_FILE_AS) {
86 if let Err(e) = std::fs::write(file_info.path(), &data[..]) {
87 println!("Error writing file: {e}");
88 }
89 return Handled::Yes;
90 }
91 if let Some(file_info) = cmd.get(commands::OPEN_FILE) {
92 match std::fs::read_to_string(file_info.path()) {
93 Ok(s) => {
94 let first_line = s.lines().next().unwrap_or("");
95 *data = first_line.to_owned();
96 }
97 Err(e) => {
98 println!("Error opening file: {e}");
99 }
100 }
101 return Handled::Yes;
102 }
103 Handled::No
104 }
105}