diff --git a/src/lib.rs b/src/lib.rs index 2140d20..74401b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,10 +16,10 @@ use im::Vector; -use serde::{Serialize, Deserialize}; -use serde::ser::{Serializer}; -use serde::de::{Deserializer, Visitor, SeqAccess}; -use std::vec::{Vec}; +use serde::de::{Deserializer, SeqAccess, Visitor}; +use serde::ser::Serializer; +use serde::{Deserialize, Serialize}; +use std::vec::Vec; #[derive(Debug, Clone, druid::Data)] pub struct Path { @@ -147,7 +147,7 @@ impl<'de> Visitor<'de> for PathDeserializer { fn visit_seq(self, mut seq: A) -> Result where - A: SeqAccess<'de> + A: SeqAccess<'de>, { use druid::kurbo::BezPath; @@ -162,14 +162,14 @@ impl<'de> Visitor<'de> for PathDeserializer { } } - Ok(Path{ kurbo_path }) + Ok(Path { kurbo_path }) } } impl<'de> Deserialize<'de> for Path { fn deserialize(deserializer: D) -> Result where - D: Deserializer<'de> + D: Deserializer<'de>, { deserializer.deserialize_seq(PathDeserializer) } @@ -180,17 +180,13 @@ impl Serialize for Path { where S: Serializer, { - use druid::kurbo::{PathEl}; + use druid::kurbo::PathEl; - serializer.collect_seq( - self.kurbo_path.iter() - .filter_map(|path_el| { - match path_el { - PathEl::MoveTo(pt) => Some(Into::<(f64, f64)>::into(pt)), - PathEl::LineTo(pt) => Some(Into::<(f64, f64)>::into(pt)), - _ => None - } - })) + serializer.collect_seq(self.kurbo_path.iter().filter_map(|path_el| match path_el { + PathEl::MoveTo(pt) => Some(Into::<(f64, f64)>::into(pt)), + PathEl::LineTo(pt) => Some(Into::<(f64, f64)>::into(pt)), + _ => None, + })) } } diff --git a/src/main.rs b/src/main.rs index 618fa99..89d7424 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,13 +16,16 @@ use log::{info, warn}; +use druid::commands; use druid::im::{vector, Vector}; use druid::kurbo::BezPath; use druid::widget::prelude::*; -use druid::{AppLauncher, Color, Data, Event, LocalizedString, WindowDesc, AppDelegate, Target, Env, DelegateCtx, FileDialogOptions, FileSpec, Command}; -use druid::commands; +use druid::{ + AppDelegate, AppLauncher, Color, Command, Data, DelegateCtx, Env, Event, FileDialogOptions, + FileSpec, LocalizedString, Target, WindowDesc, +}; -use stiletto::{CanvasElement, VersionedCanvas, Canvas, DocumentSnapshot}; +use stiletto::{Canvas, CanvasElement, DocumentSnapshot, VersionedCanvas}; struct Delegate; @@ -51,7 +54,7 @@ impl CanvasData { fn get_document_snapshot(&self) -> DocumentSnapshot { DocumentSnapshot { - canvas_elements: self.elements.get().iter().cloned().collect() + canvas_elements: self.elements.get().iter().cloned().collect(), } } @@ -88,11 +91,9 @@ impl Widget for CanvasWidget { Event::MouseUp(_) => { if data.is_drawing() { if let Some(current_element) = data.current_element.take() { - data.elements.update(move |canvas: &mut Canvas| { canvas.push_back(current_element); }); - } } } @@ -167,12 +168,15 @@ impl Widget for CanvasWidget { fn build_ui() -> impl Widget { use druid::widget::{Align, Button, CrossAxisAlignment, Flex, SizedBox}; - let history_buttons = Flex::row() - .cross_axis_alignment(CrossAxisAlignment::Center) - .with_child( - Button::new("Undo").on_click(|_ctx: &mut EventCtx, data: &mut CanvasData, _env: &Env| data.perform_undo()) - ) - .with_child(Button::new("Redo").on_click(|_ctx: &mut EventCtx, data: &mut CanvasData, _env: &Env| data.perform_redo())); + let history_buttons = + Flex::row() + .cross_axis_alignment(CrossAxisAlignment::Center) + .with_child(Button::new("Undo").on_click( + |_ctx: &mut EventCtx, data: &mut CanvasData, _env: &Env| data.perform_undo(), + )) + .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() @@ -182,29 +186,24 @@ fn build_ui() -> impl Widget { let save_buttons = Flex::row() .cross_axis_alignment(CrossAxisAlignment::Center) - .with_child( - Button::new("Open") - .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(move |ctx, _, _| { - ctx.submit_command( - Command::new( - druid::commands::SHOW_SAVE_PANEL, - save_dialog_options.clone(), - ), - None, - ) - })); - + .with_child(Button::new("Open").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(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) @@ -264,7 +263,8 @@ impl AppDelegate for Delegate { } 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); + 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());