Cargo fmt pass
This commit is contained in:
parent
d456725e23
commit
7f5aae2714
22
src/lib.rs
22
src/lib.rs
|
|
@ -16,10 +16,10 @@
|
||||||
|
|
||||||
use im::Vector;
|
use im::Vector;
|
||||||
|
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::de::{Deserializer, SeqAccess, Visitor};
|
||||||
use serde::ser::{Serializer};
|
use serde::ser::Serializer;
|
||||||
use serde::de::{Deserializer, Visitor, SeqAccess};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::vec::{Vec};
|
use std::vec::Vec;
|
||||||
|
|
||||||
#[derive(Debug, Clone, druid::Data)]
|
#[derive(Debug, Clone, druid::Data)]
|
||||||
pub struct Path {
|
pub struct Path {
|
||||||
|
|
@ -147,7 +147,7 @@ impl<'de> Visitor<'de> for PathDeserializer {
|
||||||
|
|
||||||
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
|
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
|
||||||
where
|
where
|
||||||
A: SeqAccess<'de>
|
A: SeqAccess<'de>,
|
||||||
{
|
{
|
||||||
use druid::kurbo::BezPath;
|
use druid::kurbo::BezPath;
|
||||||
|
|
||||||
|
|
@ -169,7 +169,7 @@ impl<'de> Visitor<'de> for PathDeserializer {
|
||||||
impl<'de> Deserialize<'de> for Path {
|
impl<'de> Deserialize<'de> for Path {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
where
|
where
|
||||||
D: Deserializer<'de>
|
D: Deserializer<'de>,
|
||||||
{
|
{
|
||||||
deserializer.deserialize_seq(PathDeserializer)
|
deserializer.deserialize_seq(PathDeserializer)
|
||||||
}
|
}
|
||||||
|
|
@ -180,16 +180,12 @@ impl Serialize for Path {
|
||||||
where
|
where
|
||||||
S: Serializer,
|
S: Serializer,
|
||||||
{
|
{
|
||||||
use druid::kurbo::{PathEl};
|
use druid::kurbo::PathEl;
|
||||||
|
|
||||||
serializer.collect_seq(
|
serializer.collect_seq(self.kurbo_path.iter().filter_map(|path_el| match path_el {
|
||||||
self.kurbo_path.iter()
|
|
||||||
.filter_map(|path_el| {
|
|
||||||
match path_el {
|
|
||||||
PathEl::MoveTo(pt) => Some(Into::<(f64, f64)>::into(pt)),
|
PathEl::MoveTo(pt) => Some(Into::<(f64, f64)>::into(pt)),
|
||||||
PathEl::LineTo(pt) => Some(Into::<(f64, f64)>::into(pt)),
|
PathEl::LineTo(pt) => Some(Into::<(f64, f64)>::into(pt)),
|
||||||
_ => None
|
_ => None,
|
||||||
}
|
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
38
src/main.rs
38
src/main.rs
|
|
@ -16,13 +16,16 @@
|
||||||
|
|
||||||
use log::{info, warn};
|
use log::{info, warn};
|
||||||
|
|
||||||
|
use druid::commands;
|
||||||
use druid::im::{vector, Vector};
|
use druid::im::{vector, Vector};
|
||||||
use druid::kurbo::BezPath;
|
use druid::kurbo::BezPath;
|
||||||
use druid::widget::prelude::*;
|
use druid::widget::prelude::*;
|
||||||
use druid::{AppLauncher, Color, Data, Event, LocalizedString, WindowDesc, AppDelegate, Target, Env, DelegateCtx, FileDialogOptions, FileSpec, Command};
|
use druid::{
|
||||||
use druid::commands;
|
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;
|
struct Delegate;
|
||||||
|
|
||||||
|
|
@ -51,7 +54,7 @@ impl CanvasData {
|
||||||
|
|
||||||
fn get_document_snapshot(&self) -> DocumentSnapshot {
|
fn get_document_snapshot(&self) -> DocumentSnapshot {
|
||||||
DocumentSnapshot {
|
DocumentSnapshot {
|
||||||
canvas_elements: self.elements.get().iter().cloned().collect()
|
canvas_elements: self.elements.get().iter().cloned().collect(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -88,11 +91,9 @@ impl Widget<CanvasData> for CanvasWidget {
|
||||||
Event::MouseUp(_) => {
|
Event::MouseUp(_) => {
|
||||||
if data.is_drawing() {
|
if data.is_drawing() {
|
||||||
if let Some(current_element) = data.current_element.take() {
|
if let Some(current_element) = data.current_element.take() {
|
||||||
|
|
||||||
data.elements.update(move |canvas: &mut Canvas| {
|
data.elements.update(move |canvas: &mut Canvas| {
|
||||||
canvas.push_back(current_element);
|
canvas.push_back(current_element);
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -167,12 +168,15 @@ impl Widget<CanvasData> for CanvasWidget {
|
||||||
|
|
||||||
fn build_ui() -> impl Widget<CanvasData> {
|
fn build_ui() -> impl Widget<CanvasData> {
|
||||||
use druid::widget::{Align, Button, CrossAxisAlignment, Flex, SizedBox};
|
use druid::widget::{Align, Button, CrossAxisAlignment, Flex, SizedBox};
|
||||||
let history_buttons = Flex::row()
|
let history_buttons =
|
||||||
|
Flex::row()
|
||||||
.cross_axis_alignment(CrossAxisAlignment::Center)
|
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||||
.with_child(
|
.with_child(Button::new("Undo").on_click(
|
||||||
Button::new("Undo").on_click(|_ctx: &mut EventCtx, data: &mut CanvasData, _env: &Env| data.perform_undo())
|
|_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()));
|
.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 stlt = FileSpec::new("Stiletto notebook", &["stlt"]);
|
||||||
let save_dialog_options = FileDialogOptions::new()
|
let save_dialog_options = FileDialogOptions::new()
|
||||||
|
|
@ -182,9 +186,7 @@ fn build_ui() -> impl Widget<CanvasData> {
|
||||||
|
|
||||||
let save_buttons = Flex::row()
|
let save_buttons = Flex::row()
|
||||||
.cross_axis_alignment(CrossAxisAlignment::Center)
|
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||||
.with_child(
|
.with_child(Button::new("Open").on_click(move |ctx, _, _| {
|
||||||
Button::new("Open")
|
|
||||||
.on_click(move |ctx, _, _| {
|
|
||||||
ctx.submit_command(
|
ctx.submit_command(
|
||||||
Command::new(
|
Command::new(
|
||||||
druid::commands::SHOW_OPEN_PANEL,
|
druid::commands::SHOW_OPEN_PANEL,
|
||||||
|
|
@ -193,9 +195,7 @@ fn build_ui() -> impl Widget<CanvasData> {
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
}))
|
}))
|
||||||
.with_child(
|
.with_child(Button::new("Save").on_click(move |ctx, _, _| {
|
||||||
Button::new("Save")
|
|
||||||
.on_click(move |ctx, _, _| {
|
|
||||||
ctx.submit_command(
|
ctx.submit_command(
|
||||||
Command::new(
|
Command::new(
|
||||||
druid::commands::SHOW_SAVE_PANEL,
|
druid::commands::SHOW_SAVE_PANEL,
|
||||||
|
|
@ -205,7 +205,6 @@ fn build_ui() -> impl Widget<CanvasData> {
|
||||||
)
|
)
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
||||||
let toolbar = Flex::row()
|
let toolbar = Flex::row()
|
||||||
.cross_axis_alignment(CrossAxisAlignment::Center)
|
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||||
.with_spacer(30.0)
|
.with_spacer(30.0)
|
||||||
|
|
@ -264,7 +263,8 @@ impl AppDelegate<CanvasData> for Delegate {
|
||||||
}
|
}
|
||||||
if let Some(file_info) = cmd.get(commands::OPEN_FILE) {
|
if let Some(file_info) = cmd.get(commands::OPEN_FILE) {
|
||||||
if let Ok(f) = File::open(file_info.path()) {
|
if let Ok(f) = File::open(file_info.path()) {
|
||||||
let res_snapshot: Result<DocumentSnapshot, serde_json::Error> = serde_json::from_reader(f);
|
let res_snapshot: Result<DocumentSnapshot, serde_json::Error> =
|
||||||
|
serde_json::from_reader(f);
|
||||||
if let Ok(document_snapshot) = res_snapshot {
|
if let Ok(document_snapshot) = res_snapshot {
|
||||||
data.set_from_snapshot(document_snapshot);
|
data.set_from_snapshot(document_snapshot);
|
||||||
info!("Loaded file {}", file_info.path().display());
|
info!("Loaded file {}", file_info.path().display());
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue