diff --git a/src/lib.rs b/src/lib.rs index bbeacdf..bf2baa7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,20 +42,24 @@ use im::Vector; pub type Canvas = Vector; #[derive(Clone, druid::Data)] -pub struct History { +pub struct VersionedCanvas { + // We internally guarantee that this vector + // is never empty versions: Vector, curr_version: usize, } -impl History { - pub fn new(canvas: Canvas) -> History { - History { +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 { + let focus = self.versions.focus(); self.versions.get(self.curr_version).unwrap() } @@ -83,8 +87,7 @@ impl History { // 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 we want to keep all ramifications, we should use a tree, - // but honestly, this sounds like overkill. + // and creates a new future. if self.has_newer_versions() { self.versions = self.versions.take(self.curr_version + 1); } @@ -98,8 +101,7 @@ impl History { // 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 we want to keep all ramifications, we should use a tree, - // but honestly, this sounds like overkill. + // and creates a new future. if self.has_newer_versions() { self.versions = self.versions.take(self.curr_version + 1); } diff --git a/src/main.rs b/src/main.rs index ee66a65..fa001d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,13 +19,13 @@ use druid::kurbo::BezPath; use druid::widget::prelude::*; use druid::{AppLauncher, Color, Data, Event, LocalizedString, WindowDesc}; -use stiletto::{CanvasElement, History, Canvas}; +use stiletto::{CanvasElement, VersionedCanvas, Canvas}; #[derive(Clone, Data)] struct CanvasData { current_element: Option, //elements: Vector, - history: History, + elements: VersionedCanvas, } impl CanvasData { @@ -35,13 +35,13 @@ impl CanvasData { fn perform_undo(&mut self) { if !self.is_drawing() { - self.history.undo(); + self.elements.undo(); } } fn perform_redo(&mut self) { if !self.is_drawing() { - self.history.redo(); + self.elements.redo(); } } } @@ -74,7 +74,7 @@ impl Widget for CanvasWidget { if data.is_drawing() { if let Some(current_element) = data.current_element.take() { - data.history.update(move |canvas: &Canvas| -> Canvas { + data.elements.update(move |canvas: &Canvas| -> Canvas { let mut new_canvas = canvas.clone(); new_canvas.push_back(current_element); return new_canvas; @@ -143,7 +143,7 @@ impl Widget for CanvasWidget { // and we only want to clear this widget's area. ctx.fill(rect, &Color::WHITE); - for element in data.history.get().iter() { + for element in data.elements.get().iter() { element.draw(ctx); } if let Some(element) = &data.current_element { @@ -177,7 +177,7 @@ pub fn main() { ); let canvas_data = CanvasData { current_element: None, - history: History::new(vector![]), + elements: VersionedCanvas::new(vector![]), }; AppLauncher::with_window(window) .use_simple_logger()