Compare commits
3 Commits
d456725e23
...
f42db5fb69
| Author | SHA1 | Date |
|---|---|---|
|
|
f42db5fb69 | |
|
|
7925cfb2d8 | |
|
|
7f5aae2714 |
|
|
@ -0,0 +1,115 @@
|
||||||
|
// Stiletto
|
||||||
|
// Copyright (C) 2020 Stiletto Authors
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
use im::Vector;
|
||||||
|
|
||||||
|
use serde::de::{Deserializer, SeqAccess, Visitor};
|
||||||
|
use serde::ser::Serializer;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, druid::Data)]
|
||||||
|
pub struct Path {
|
||||||
|
pub kurbo_path: druid::kurbo::BezPath,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type Canvas = Vector<CanvasElement>;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, druid::Data, Serialize, Deserialize)]
|
||||||
|
pub enum CanvasElement {
|
||||||
|
Freehand { path: Path, thickness: f64 },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CanvasElement {
|
||||||
|
pub fn bounding_box(&self) -> druid::Rect {
|
||||||
|
match self {
|
||||||
|
CanvasElement::Freehand { path, thickness } => {
|
||||||
|
use druid::kurbo::Shape;
|
||||||
|
path.kurbo_path
|
||||||
|
.bounding_box()
|
||||||
|
.inflate(*thickness, *thickness)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn draw(&self, ctx: &mut druid::PaintCtx) {
|
||||||
|
match self {
|
||||||
|
CanvasElement::Freehand { path, thickness } => {
|
||||||
|
use druid::RenderContext;
|
||||||
|
let stroke_color = druid::Color::rgb8(0, 128, 0);
|
||||||
|
ctx.stroke(&path.kurbo_path, &stroke_color, *thickness);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_path_mut(&mut self) -> Option<&mut Path> {
|
||||||
|
match self {
|
||||||
|
CanvasElement::Freehand { path, .. } => Some(path),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> Deserialize<'de> for Path {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
deserializer.deserialize_seq(PathDeserializer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Serialize for Path {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
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,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct PathDeserializer;
|
||||||
|
|
||||||
|
impl<'de> Visitor<'de> for PathDeserializer {
|
||||||
|
type Value = Path;
|
||||||
|
|
||||||
|
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
write!(formatter, "A sequence of 2D points")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
|
||||||
|
where
|
||||||
|
A: SeqAccess<'de>,
|
||||||
|
{
|
||||||
|
use druid::kurbo::BezPath;
|
||||||
|
|
||||||
|
let mut kurbo_path = BezPath::new();
|
||||||
|
let mut first_element = true;
|
||||||
|
while let Some(point) = seq.next_element::<(f64, f64)>()? {
|
||||||
|
if first_element {
|
||||||
|
kurbo_path.move_to(point);
|
||||||
|
first_element = false;
|
||||||
|
} else {
|
||||||
|
kurbo_path.line_to(point);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Path { kurbo_path })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,92 @@
|
||||||
|
// Stiletto
|
||||||
|
// Copyright (C) 2020 Stiletto Authors
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
use super::canvas::Canvas;
|
||||||
|
use im::Vector;
|
||||||
|
|
||||||
|
#[derive(Clone, druid::Data)]
|
||||||
|
pub struct VersionedCanvas {
|
||||||
|
// We internally guarantee that this vector
|
||||||
|
// is never empty
|
||||||
|
versions: Vector<Canvas>,
|
||||||
|
curr_version: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VersionedCanvas {
|
||||||
|
pub fn new(canvas: Canvas) -> VersionedCanvas {
|
||||||
|
VersionedCanvas {
|
||||||
|
versions: im::vector![canvas],
|
||||||
|
curr_version: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get current canvas version
|
||||||
|
pub fn get(&self) -> &Canvas {
|
||||||
|
self.versions.get(self.curr_version).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn has_newer_versions(&self) -> bool {
|
||||||
|
self.curr_version + 1 < self.versions.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn has_older_versions(&self) -> bool {
|
||||||
|
self.curr_version > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn undo(&mut self) {
|
||||||
|
if self.has_older_versions() {
|
||||||
|
self.curr_version = self.curr_version - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn redo(&mut self) {
|
||||||
|
if self.has_newer_versions() {
|
||||||
|
self.curr_version = self.curr_version + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(&mut self, update_fn: impl FnOnce(&mut Canvas)) {
|
||||||
|
// Make a new copy of the current canvas version,
|
||||||
|
// so that we can safely modify it without losing
|
||||||
|
// the previous canvas version
|
||||||
|
let mut new_version = self.get().clone();
|
||||||
|
update_fn(&mut new_version);
|
||||||
|
|
||||||
|
// This is a linear history,
|
||||||
|
// so we first check if there are newer versions, if so
|
||||||
|
// this means we are in the past, so a change in the past destroys the future
|
||||||
|
// and creates a new future.
|
||||||
|
if self.has_newer_versions() {
|
||||||
|
self.versions = self.versions.take(self.curr_version + 1);
|
||||||
|
}
|
||||||
|
self.versions.push_back(new_version);
|
||||||
|
self.curr_version = self.curr_version + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do inplace update, which will be irreversible
|
||||||
|
pub fn irreversible_update(&mut self, update_fn: impl FnOnce(&mut Canvas)) {
|
||||||
|
// Do the update directly on the current canvas version
|
||||||
|
update_fn(self.versions.back_mut().unwrap());
|
||||||
|
|
||||||
|
// This is a linear history,
|
||||||
|
// so we first check if there are newer versions, if so
|
||||||
|
// this means we are in the past, so a change in the past destroys the future
|
||||||
|
// and creates a new future.
|
||||||
|
if self.has_newer_versions() {
|
||||||
|
self.versions = self.versions.take(self.curr_version + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
187
src/lib.rs
187
src/lib.rs
|
|
@ -12,189 +12,16 @@
|
||||||
// GNU Affero General Public License for more details.
|
// GNU Affero General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.use serde::{Serialize, Deserialize};
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use im::Vector;
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::vec::Vec;
|
||||||
|
|
||||||
use serde::{Serialize, Deserialize};
|
pub mod canvas;
|
||||||
use serde::ser::{Serializer};
|
pub mod history;
|
||||||
use serde::de::{Deserializer, Visitor, SeqAccess};
|
pub mod widget;
|
||||||
use std::vec::{Vec};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, druid::Data)]
|
|
||||||
pub struct Path {
|
|
||||||
pub kurbo_path: druid::kurbo::BezPath,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, druid::Data, Serialize, Deserialize)]
|
|
||||||
pub enum CanvasElement {
|
|
||||||
Freehand { path: Path, thickness: f64 },
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CanvasElement {
|
|
||||||
pub fn bounding_box(&self) -> druid::Rect {
|
|
||||||
match self {
|
|
||||||
CanvasElement::Freehand { path, thickness } => {
|
|
||||||
use druid::kurbo::Shape;
|
|
||||||
path.kurbo_path
|
|
||||||
.bounding_box()
|
|
||||||
.inflate(*thickness, *thickness)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn draw(&self, ctx: &mut druid::PaintCtx) {
|
|
||||||
match self {
|
|
||||||
CanvasElement::Freehand { path, thickness } => {
|
|
||||||
use druid::RenderContext;
|
|
||||||
let stroke_color = druid::Color::rgb8(0, 128, 0);
|
|
||||||
ctx.stroke(&path.kurbo_path, &stroke_color, *thickness);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_path_mut(&mut self) -> Option<&mut Path> {
|
|
||||||
match self {
|
|
||||||
CanvasElement::Freehand { path, .. } => Some(path),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A canvas contains all elements to be drawn
|
|
||||||
pub type Canvas = Vector<CanvasElement>;
|
|
||||||
|
|
||||||
#[derive(Clone, druid::Data)]
|
|
||||||
pub struct VersionedCanvas {
|
|
||||||
// We internally guarantee that this vector
|
|
||||||
// is never empty
|
|
||||||
versions: Vector<Canvas>,
|
|
||||||
curr_version: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl VersionedCanvas {
|
|
||||||
pub fn new(canvas: Canvas) -> VersionedCanvas {
|
|
||||||
VersionedCanvas {
|
|
||||||
versions: im::vector![canvas],
|
|
||||||
curr_version: 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get current canvas version
|
|
||||||
pub fn get(&self) -> &Canvas {
|
|
||||||
self.versions.get(self.curr_version).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn has_newer_versions(&self) -> bool {
|
|
||||||
self.curr_version + 1 < self.versions.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn has_older_versions(&self) -> bool {
|
|
||||||
self.curr_version > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn undo(&mut self) {
|
|
||||||
if self.has_older_versions() {
|
|
||||||
self.curr_version = self.curr_version - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn redo(&mut self) {
|
|
||||||
if self.has_newer_versions() {
|
|
||||||
self.curr_version = self.curr_version + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update(&mut self, update_fn: impl FnOnce(&mut Canvas)) {
|
|
||||||
// Make a new copy of the current canvas version,
|
|
||||||
// so that we can safely modify it without losing
|
|
||||||
// the previous canvas version
|
|
||||||
let mut new_version = self.get().clone();
|
|
||||||
update_fn(&mut new_version);
|
|
||||||
|
|
||||||
// This is a linear history,
|
|
||||||
// so we first check if there are newer versions, if so
|
|
||||||
// this means we are in the past, so a change in the past destroys the future
|
|
||||||
// and creates a new future.
|
|
||||||
if self.has_newer_versions() {
|
|
||||||
self.versions = self.versions.take(self.curr_version + 1);
|
|
||||||
}
|
|
||||||
self.versions.push_back(new_version);
|
|
||||||
self.curr_version = self.curr_version + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do inplace update, which will be irreversible
|
|
||||||
pub fn irreversible_update(&mut self, update_fn: impl FnOnce(&mut Canvas)) {
|
|
||||||
// Do the update directly on the current canvas version
|
|
||||||
update_fn(self.versions.back_mut().unwrap());
|
|
||||||
|
|
||||||
// This is a linear history,
|
|
||||||
// so we first check if there are newer versions, if so
|
|
||||||
// this means we are in the past, so a change in the past destroys the future
|
|
||||||
// and creates a new future.
|
|
||||||
if self.has_newer_versions() {
|
|
||||||
self.versions = self.versions.take(self.curr_version + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct PathDeserializer;
|
|
||||||
|
|
||||||
impl<'de> Visitor<'de> for PathDeserializer {
|
|
||||||
type Value = Path;
|
|
||||||
|
|
||||||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
||||||
write!(formatter, "A sequence of 2D points")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
|
|
||||||
where
|
|
||||||
A: SeqAccess<'de>
|
|
||||||
{
|
|
||||||
use druid::kurbo::BezPath;
|
|
||||||
|
|
||||||
let mut kurbo_path = BezPath::new();
|
|
||||||
let mut first_element = true;
|
|
||||||
while let Some(point) = seq.next_element::<(f64, f64)>()? {
|
|
||||||
if first_element {
|
|
||||||
kurbo_path.move_to(point);
|
|
||||||
first_element = false;
|
|
||||||
} else {
|
|
||||||
kurbo_path.line_to(point);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Path{ kurbo_path })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'de> Deserialize<'de> for Path {
|
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
||||||
where
|
|
||||||
D: Deserializer<'de>
|
|
||||||
{
|
|
||||||
deserializer.deserialize_seq(PathDeserializer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Serialize for Path {
|
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
||||||
where
|
|
||||||
S: Serializer,
|
|
||||||
{
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct DocumentSnapshot {
|
pub struct DocumentSnapshot {
|
||||||
pub canvas_elements: Vec<CanvasElement>,
|
pub canvas_elements: Vec<canvas::CanvasElement>,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
252
src/main.rs
252
src/main.rs
|
|
@ -16,163 +16,46 @@
|
||||||
|
|
||||||
use log::{info, 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, AppDelegate, Target, Env, DelegateCtx, FileDialogOptions, FileSpec, Command};
|
|
||||||
use druid::commands;
|
use druid::commands;
|
||||||
|
use druid::im::vector;
|
||||||
|
use druid::widget::prelude::*;
|
||||||
|
use druid::widget::{Align, Button, CrossAxisAlignment, Flex, SizedBox};
|
||||||
|
use druid::{
|
||||||
|
AppDelegate, AppLauncher, Command, DelegateCtx, Env, FileDialogOptions, FileSpec,
|
||||||
|
LocalizedString, Target, WindowDesc,
|
||||||
|
};
|
||||||
|
|
||||||
use stiletto::{CanvasElement, VersionedCanvas, Canvas, DocumentSnapshot};
|
use stiletto::history::VersionedCanvas;
|
||||||
|
use stiletto::widget::{CanvasState, CanvasWidget};
|
||||||
|
use stiletto::DocumentSnapshot;
|
||||||
|
|
||||||
struct Delegate;
|
pub fn main() {
|
||||||
|
let window = WindowDesc::new(build_ui)
|
||||||
#[derive(Clone, Data)]
|
.window_size((1024.0, 1400.0))
|
||||||
struct CanvasData {
|
.title(
|
||||||
current_element: Option<CanvasElement>,
|
LocalizedString::new("custom-widget-demo-window-title").with_placeholder("Stiletto"),
|
||||||
elements: VersionedCanvas,
|
);
|
||||||
|
let canvas_data = CanvasState {
|
||||||
|
current_element: None,
|
||||||
|
elements: VersionedCanvas::new(vector![]),
|
||||||
|
};
|
||||||
|
AppLauncher::with_window(window)
|
||||||
|
.use_simple_logger()
|
||||||
|
.delegate(Delegate)
|
||||||
|
.launch(canvas_data)
|
||||||
|
.expect("launch failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CanvasData {
|
fn build_ui() -> impl Widget<CanvasState> {
|
||||||
fn is_drawing(&self) -> bool {
|
let history_buttons =
|
||||||
self.current_element.is_some()
|
Flex::row()
|
||||||
}
|
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||||
|
.with_child(Button::new("Undo").on_click(
|
||||||
fn perform_undo(&mut self) {
|
|_ctx: &mut EventCtx, data: &mut CanvasState, _env: &Env| data.perform_undo(),
|
||||||
if !self.is_drawing() {
|
))
|
||||||
self.elements.undo();
|
.with_child(Button::new("Redo").on_click(
|
||||||
}
|
|_ctx: &mut EventCtx, data: &mut CanvasState, _env: &Env| data.perform_redo(),
|
||||||
}
|
));
|
||||||
|
|
||||||
fn perform_redo(&mut self) {
|
|
||||||
if !self.is_drawing() {
|
|
||||||
self.elements.redo();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_document_snapshot(&self) -> DocumentSnapshot {
|
|
||||||
DocumentSnapshot {
|
|
||||||
canvas_elements: self.elements.get().iter().cloned().collect()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_from_snapshot(&mut self, snapshot: DocumentSnapshot) {
|
|
||||||
self.current_element = None;
|
|
||||||
self.elements = VersionedCanvas::new(Vector::from(snapshot.canvas_elements));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct CanvasWidget;
|
|
||||||
|
|
||||||
impl Widget<CanvasData> for CanvasWidget {
|
|
||||||
fn event(&mut self, _ctx: &mut EventCtx, event: &Event, data: &mut CanvasData, _env: &Env) {
|
|
||||||
match event {
|
|
||||||
Event::MouseDown(mouse_event) => {
|
|
||||||
let mut kurbo_path = BezPath::new();
|
|
||||||
kurbo_path.move_to((mouse_event.pos.x, mouse_event.pos.y));
|
|
||||||
data.current_element = Some(CanvasElement::Freehand {
|
|
||||||
path: stiletto::Path { kurbo_path },
|
|
||||||
thickness: 2.0,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Event::MouseMove(mouse_event) => {
|
|
||||||
if data.is_drawing() {
|
|
||||||
if let Some(current_element) = data.current_element.as_mut() {
|
|
||||||
current_element
|
|
||||||
.get_path_mut()
|
|
||||||
.unwrap()
|
|
||||||
.kurbo_path
|
|
||||||
.line_to((mouse_event.pos.x, mouse_event.pos.y));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lifecycle(
|
|
||||||
&mut self,
|
|
||||||
_ctx: &mut LifeCycleCtx,
|
|
||||||
_event: &LifeCycle,
|
|
||||||
_data: &CanvasData,
|
|
||||||
_env: &Env,
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update(
|
|
||||||
&mut self,
|
|
||||||
ctx: &mut UpdateCtx,
|
|
||||||
old_data: &CanvasData,
|
|
||||||
data: &CanvasData,
|
|
||||||
_env: &Env,
|
|
||||||
) {
|
|
||||||
// the current_element is moved to the elements array, no need to repaint
|
|
||||||
if old_data.is_drawing() && !data.is_drawing() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if data.is_drawing() {
|
|
||||||
if let Some(e) = data.current_element.as_ref() {
|
|
||||||
ctx.request_paint_rect(e.bounding_box());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ctx.request_paint();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn layout(
|
|
||||||
&mut self,
|
|
||||||
_layout_ctx: &mut LayoutCtx,
|
|
||||||
bc: &BoxConstraints,
|
|
||||||
_data: &CanvasData,
|
|
||||||
_env: &Env,
|
|
||||||
) -> Size {
|
|
||||||
// BoxConstraints are passed by the parent widget.
|
|
||||||
// This method can return any Size within those constraints:
|
|
||||||
// bc.constrain(my_size)
|
|
||||||
//
|
|
||||||
// To check if a dimension is infinite or not (e.g. scrolling):
|
|
||||||
// bc.is_width_bounded() / bc.is_height_bounded()
|
|
||||||
bc.max()
|
|
||||||
}
|
|
||||||
|
|
||||||
// The paint method gets called last, after an event flow.
|
|
||||||
// It goes event -> update -> layout -> paint, and each method can influence the next.
|
|
||||||
// Basically, anything that changes the appearance of a widget causes a paint.
|
|
||||||
fn paint(&mut self, ctx: &mut PaintCtx, data: &CanvasData, _env: &Env) {
|
|
||||||
// (ctx.size() returns the size of the layout rect we're painting in)
|
|
||||||
let size = ctx.size();
|
|
||||||
let rect = size.to_rect();
|
|
||||||
// Note: ctx also has a `clear` method, but that clears the whole context,
|
|
||||||
// and we only want to clear this widget's area.
|
|
||||||
ctx.fill(rect, &Color::WHITE);
|
|
||||||
|
|
||||||
for element in data.elements.get().iter() {
|
|
||||||
element.draw(ctx);
|
|
||||||
}
|
|
||||||
if let Some(element) = &data.current_element {
|
|
||||||
element.draw(ctx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build_ui() -> impl Widget<CanvasData> {
|
|
||||||
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 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,29 +65,24 @@ 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")
|
ctx.submit_command(
|
||||||
.on_click(move |ctx, _, _| {
|
Command::new(
|
||||||
ctx.submit_command(
|
druid::commands::SHOW_OPEN_PANEL,
|
||||||
Command::new(
|
open_dialog_options.clone(),
|
||||||
druid::commands::SHOW_OPEN_PANEL,
|
),
|
||||||
open_dialog_options.clone(),
|
None,
|
||||||
),
|
)
|
||||||
None,
|
}))
|
||||||
)
|
.with_child(Button::new("Save").on_click(move |ctx, _, _| {
|
||||||
}))
|
ctx.submit_command(
|
||||||
.with_child(
|
Command::new(
|
||||||
Button::new("Save")
|
druid::commands::SHOW_SAVE_PANEL,
|
||||||
.on_click(move |ctx, _, _| {
|
save_dialog_options.clone(),
|
||||||
ctx.submit_command(
|
),
|
||||||
Command::new(
|
None,
|
||||||
druid::commands::SHOW_SAVE_PANEL,
|
)
|
||||||
save_dialog_options.clone(),
|
}));
|
||||||
),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
}));
|
|
||||||
|
|
||||||
|
|
||||||
let toolbar = Flex::row()
|
let toolbar = Flex::row()
|
||||||
.cross_axis_alignment(CrossAxisAlignment::Center)
|
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||||
|
|
@ -220,30 +98,15 @@ fn build_ui() -> impl Widget<CanvasData> {
|
||||||
.with_flex_child(CanvasWidget {}, 1.0)
|
.with_flex_child(CanvasWidget {}, 1.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
struct Delegate;
|
||||||
let window = WindowDesc::new(build_ui)
|
|
||||||
.window_size((1024.0, 1400.0))
|
|
||||||
.title(
|
|
||||||
LocalizedString::new("custom-widget-demo-window-title").with_placeholder("Stiletto"),
|
|
||||||
);
|
|
||||||
let canvas_data = CanvasData {
|
|
||||||
current_element: None,
|
|
||||||
elements: VersionedCanvas::new(vector![]),
|
|
||||||
};
|
|
||||||
AppLauncher::with_window(window)
|
|
||||||
.use_simple_logger()
|
|
||||||
.delegate(Delegate)
|
|
||||||
.launch(canvas_data)
|
|
||||||
.expect("launch failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AppDelegate<CanvasData> for Delegate {
|
impl AppDelegate<CanvasState> for Delegate {
|
||||||
fn command(
|
fn command(
|
||||||
&mut self,
|
&mut self,
|
||||||
_ctx: &mut DelegateCtx,
|
_ctx: &mut DelegateCtx,
|
||||||
_target: Target,
|
_target: Target,
|
||||||
cmd: &Command,
|
cmd: &Command,
|
||||||
data: &mut CanvasData,
|
data: &mut CanvasState,
|
||||||
_env: &Env,
|
_env: &Env,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
|
@ -264,7 +127,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());
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,163 @@
|
||||||
|
// Stiletto
|
||||||
|
// Copyright (C) 2020 Stiletto Authors
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
use im::Vector;
|
||||||
|
|
||||||
|
use super::canvas;
|
||||||
|
use super::canvas::{Canvas, CanvasElement};
|
||||||
|
use super::history::VersionedCanvas;
|
||||||
|
use super::DocumentSnapshot;
|
||||||
|
|
||||||
|
use druid::kurbo::BezPath;
|
||||||
|
use druid::widget::prelude::*;
|
||||||
|
use druid::{Color, Data, Env, Event};
|
||||||
|
|
||||||
|
#[derive(Clone, Data)]
|
||||||
|
pub struct CanvasState {
|
||||||
|
pub current_element: Option<CanvasElement>,
|
||||||
|
pub elements: VersionedCanvas,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CanvasState {
|
||||||
|
pub fn is_drawing(&self) -> bool {
|
||||||
|
self.current_element.is_some()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn perform_undo(&mut self) {
|
||||||
|
if !self.is_drawing() {
|
||||||
|
self.elements.undo();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn perform_redo(&mut self) {
|
||||||
|
if !self.is_drawing() {
|
||||||
|
self.elements.redo();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_document_snapshot(&self) -> DocumentSnapshot {
|
||||||
|
DocumentSnapshot {
|
||||||
|
canvas_elements: self.elements.get().iter().cloned().collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_from_snapshot(&mut self, snapshot: DocumentSnapshot) {
|
||||||
|
self.current_element = None;
|
||||||
|
self.elements = VersionedCanvas::new(Vector::from(snapshot.canvas_elements));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CanvasWidget;
|
||||||
|
|
||||||
|
impl Widget<CanvasState> for CanvasWidget {
|
||||||
|
fn event(&mut self, _ctx: &mut EventCtx, event: &Event, data: &mut CanvasState, _env: &Env) {
|
||||||
|
match event {
|
||||||
|
Event::MouseDown(mouse_event) => {
|
||||||
|
let mut kurbo_path = BezPath::new();
|
||||||
|
kurbo_path.move_to((mouse_event.pos.x, mouse_event.pos.y));
|
||||||
|
data.current_element = Some(CanvasElement::Freehand {
|
||||||
|
path: canvas::Path { kurbo_path },
|
||||||
|
thickness: 2.0,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Event::MouseMove(mouse_event) => {
|
||||||
|
if data.is_drawing() {
|
||||||
|
if let Some(current_element) = data.current_element.as_mut() {
|
||||||
|
current_element
|
||||||
|
.get_path_mut()
|
||||||
|
.unwrap()
|
||||||
|
.kurbo_path
|
||||||
|
.line_to((mouse_event.pos.x, mouse_event.pos.y));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lifecycle(
|
||||||
|
&mut self,
|
||||||
|
_ctx: &mut LifeCycleCtx,
|
||||||
|
_event: &LifeCycle,
|
||||||
|
_data: &CanvasState,
|
||||||
|
_env: &Env,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(
|
||||||
|
&mut self,
|
||||||
|
ctx: &mut UpdateCtx,
|
||||||
|
old_data: &CanvasState,
|
||||||
|
data: &CanvasState,
|
||||||
|
_env: &Env,
|
||||||
|
) {
|
||||||
|
// the current_element is moved to the elements array, no need to repaint
|
||||||
|
if old_data.is_drawing() && !data.is_drawing() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if data.is_drawing() {
|
||||||
|
if let Some(e) = data.current_element.as_ref() {
|
||||||
|
ctx.request_paint_rect(e.bounding_box());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ctx.request_paint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn layout(
|
||||||
|
&mut self,
|
||||||
|
_layout_ctx: &mut LayoutCtx,
|
||||||
|
bc: &BoxConstraints,
|
||||||
|
_data: &CanvasState,
|
||||||
|
_env: &Env,
|
||||||
|
) -> Size {
|
||||||
|
// BoxConstraints are passed by the parent widget.
|
||||||
|
// This method can return any Size within those constraints:
|
||||||
|
// bc.constrain(my_size)
|
||||||
|
//
|
||||||
|
// To check if a dimension is infinite or not (e.g. scrolling):
|
||||||
|
// bc.is_width_bounded() / bc.is_height_bounded()
|
||||||
|
bc.max()
|
||||||
|
}
|
||||||
|
|
||||||
|
// The paint method gets called last, after an event flow.
|
||||||
|
// It goes event -> update -> layout -> paint, and each method can influence the next.
|
||||||
|
// Basically, anything that changes the appearance of a widget causes a paint.
|
||||||
|
fn paint(&mut self, ctx: &mut PaintCtx, data: &CanvasState, _env: &Env) {
|
||||||
|
// (ctx.size() returns the size of the layout rect we're painting in)
|
||||||
|
let size = ctx.size();
|
||||||
|
let rect = size.to_rect();
|
||||||
|
// Note: ctx also has a `clear` method, but that clears the whole context,
|
||||||
|
// and we only want to clear this widget's area.
|
||||||
|
ctx.fill(rect, &Color::WHITE);
|
||||||
|
|
||||||
|
for element in data.elements.get().iter() {
|
||||||
|
element.draw(ctx);
|
||||||
|
}
|
||||||
|
if let Some(element) = &data.current_element {
|
||||||
|
element.draw(ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue