From cfa925da2fe1da01d2abf876d89bbcf3e6cb0bc2 Mon Sep 17 00:00:00 2001 From: Enrico Lumetti Date: Mon, 9 Nov 2020 18:59:02 +0100 Subject: [PATCH] Use file dialogs to open/save files --- src/main.rs | 97 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 71 insertions(+), 26 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9d4b1d8..9bda313 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,15 +14,18 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -use log::warn; +use log::{info, warn}; use druid::im::{vector, Vector}; use druid::kurbo::BezPath; use druid::widget::prelude::*; -use druid::{AppLauncher, Color, Data, Event, LocalizedString, WindowDesc}; +use druid::{AppLauncher, Color, Data, Event, LocalizedString, WindowDesc, AppDelegate, Target, Env, DelegateCtx, FileDialogOptions, FileSpec, Command}; +use druid::commands; use stiletto::{CanvasElement, VersionedCanvas, Canvas, DocumentSnapshot}; +struct Delegate; + #[derive(Clone, Data)] struct CanvasData { current_element: Option, @@ -89,7 +92,7 @@ impl Widget for CanvasWidget { data.elements.update(move |canvas: &Canvas| -> Canvas { let mut new_canvas = canvas.clone(); new_canvas.push_back(current_element); - return new_canvas; + new_canvas }); } @@ -173,38 +176,38 @@ fn build_ui() -> impl Widget { ) .with_child(Button::new("Redo").on_click(|_ctx: &mut EventCtx, data: &mut CanvasData, _env: &Env| data.perform_redo())); + let stlt = FileSpec::new("Stiletto notebook", &["stlt"]); + let save_dialog_options = FileDialogOptions::new() + .allowed_types(vec![stlt]) + .default_type(stlt); + let open_dialog_options = save_dialog_options.clone(); + let save_buttons = Flex::row() .cross_axis_alignment(CrossAxisAlignment::Center) .with_child( Button::new("Open") - .on_click(|_ctx, data: &mut CanvasData, _env| { - use std::fs::File; - - if let Ok(f) = File::open("test.stlt") { - let res_snapshot: Result = serde_json::from_reader(f); - if let Ok(document_snapshot) = res_snapshot { - data.set_from_snapshot(document_snapshot); - } else { - warn!("didn't work: {:?}", res_snapshot.err()); - } - } + .on_click(move |ctx, _, _| { + ctx.submit_command( + Command::new( + druid::commands::SHOW_OPEN_PANEL, + open_dialog_options.clone(), + ), + None, + ) })) .with_child( Button::new("Save") - .on_click(|_ctx, data: &mut CanvasData, _env| { - use std::fs::File; - - let res_file = File::create("test.stlt"); - if let Ok(f) = res_file { - let write_res = serde_json::to_writer_pretty(f, &data.get_document_snapshot()); - if write_res.is_err() { - warn!("Error while saving: {:?}", write_res.err()); - } - } else { - warn!("Cannot create file: {:?}", res_file.err()); - } + .on_click(move |ctx, _, _| { + ctx.submit_command( + Command::new( + druid::commands::SHOW_SAVE_PANEL, + save_dialog_options.clone(), + ), + None, + ) })); + let toolbar = Flex::row() .cross_axis_alignment(CrossAxisAlignment::Center) .with_spacer(30.0) @@ -231,6 +234,48 @@ pub fn main() { }; AppLauncher::with_window(window) .use_simple_logger() + .delegate(Delegate) .launch(canvas_data) .expect("launch failed"); } + +impl AppDelegate for Delegate { + fn command( + &mut self, + _ctx: &mut DelegateCtx, + _target: Target, + cmd: &Command, + data: &mut CanvasData, + _env: &Env, + ) -> bool { + use std::fs::File; + + if let Some(Some(file_info)) = cmd.get(commands::SAVE_FILE) { + let res_file = File::create(file_info.path()); + if let Ok(f) = res_file { + let write_res = serde_json::to_writer_pretty(f, &data.get_document_snapshot()); + if write_res.is_err() { + warn!("Error while saving: {:?}", write_res.err()); + } else { + info!("Written to file: {}", file_info.path().display()); + } + } else { + warn!("Cannot create file: {:?}", res_file.err()); + } + return true; + } + if let Some(file_info) = cmd.get(commands::OPEN_FILE) { + if let Ok(f) = File::open(file_info.path()) { + let res_snapshot: Result = serde_json::from_reader(f); + if let Ok(document_snapshot) = res_snapshot { + data.set_from_snapshot(document_snapshot); + info!("Loaded file {}", file_info.path().display()); + } else { + warn!("didn't work: {:?}", res_snapshot.err()); + } + } + return true; + } + false + } +}