diff --git a/src/history.rs b/src/history.rs index 7acc021..5eea309 100644 --- a/src/history.rs +++ b/src/history.rs @@ -14,27 +14,26 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -use super::canvas::Canvas; use im::Vector; #[derive(Clone, druid::Data)] -pub struct VersionedCanvas { +pub struct Versioned { // We internally guarantee that this vector // is never empty - versions: Vector, + versions: Vector, curr_version: usize, } -impl VersionedCanvas { - pub fn new(canvas: Canvas) -> VersionedCanvas { - VersionedCanvas { - versions: im::vector![canvas], +impl Versioned { + pub fn new(first_version: T) -> Versioned { + Versioned { + versions: im::vector![first_version], curr_version: 0, } } - // Get current canvas version - pub fn get(&self) -> &Canvas { + // Get current version + pub fn get(&self) -> &T { self.versions.get(self.curr_version).unwrap() } @@ -58,10 +57,10 @@ impl VersionedCanvas { } } - pub fn update(&mut self, update_fn: impl FnOnce(&mut Canvas)) { - // Make a new copy of the current canvas version, + pub fn update(&mut self, update_fn: impl FnOnce(&mut T)) { + // Make a new copy of the current version, // so that we can safely modify it without losing - // the previous canvas version + // the previous version let mut new_version = self.get().clone(); update_fn(&mut new_version); @@ -77,8 +76,8 @@ impl VersionedCanvas { } // 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 + pub fn irreversible_update(&mut self, update_fn: impl FnOnce(&mut T)) { + // Do the update directly on the current version update_fn(self.versions.back_mut().unwrap()); // This is a linear history, diff --git a/src/lib.rs b/src/lib.rs index 222ad17..5fc5d88 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,3 +36,5 @@ pub struct DocumentSnapshot { pub format_version_minor: u16, pub canvas_elements: Vec, } + +pub type VersionedCanvas = history::Versioned; diff --git a/src/widget/canvas.rs b/src/widget/canvas.rs index f29cce4..47d2e26 100644 --- a/src/widget/canvas.rs +++ b/src/widget/canvas.rs @@ -18,8 +18,7 @@ use im::Vector; use super::tool_ctx::{CanvasToolCtx}; use crate::canvas::Canvas; -use crate::history::VersionedCanvas; -use crate::DocumentSnapshot; +use crate::{DocumentSnapshot, VersionedCanvas}; use druid::widget::prelude::*; use druid::{Color, Data, Env, Event, PointerType}; diff --git a/src/widget/tool_ctx.rs b/src/widget/tool_ctx.rs index 7f7f1cb..203e993 100644 --- a/src/widget/tool_ctx.rs +++ b/src/widget/tool_ctx.rs @@ -16,7 +16,7 @@ use crate::tool::{CanvasToolParams, CanvasToolType}; use crate::canvas::{Canvas, CanvasElement}; -use crate::history::VersionedCanvas; +use crate::VersionedCanvas; use druid::kurbo::BezPath; use druid::{Color, Data, Env, Event, EventCtx, MouseButton, MouseEvent, PaintCtx, RenderContext};