Make 'VersionedCanvas' into a generic 'Versioned<T>' data structure

This commit is contained in:
Enrico Lumetti 2021-03-05 09:02:17 +01:00
parent b3748a6faa
commit 13bac91dbc
4 changed files with 17 additions and 17 deletions

View File

@ -14,27 +14,26 @@
// 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/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
use super::canvas::Canvas;
use im::Vector; use im::Vector;
#[derive(Clone, druid::Data)] #[derive(Clone, druid::Data)]
pub struct VersionedCanvas { pub struct Versioned<T: druid::Data + Clone> {
// We internally guarantee that this vector // We internally guarantee that this vector
// is never empty // is never empty
versions: Vector<Canvas>, versions: Vector<T>,
curr_version: usize, curr_version: usize,
} }
impl VersionedCanvas { impl<T: druid::Data + Clone> Versioned<T> {
pub fn new(canvas: Canvas) -> VersionedCanvas { pub fn new(first_version: T) -> Versioned<T> {
VersionedCanvas { Versioned {
versions: im::vector![canvas], versions: im::vector![first_version],
curr_version: 0, curr_version: 0,
} }
} }
// Get current canvas version // Get current version
pub fn get(&self) -> &Canvas { pub fn get(&self) -> &T {
self.versions.get(self.curr_version).unwrap() self.versions.get(self.curr_version).unwrap()
} }
@ -58,10 +57,10 @@ impl VersionedCanvas {
} }
} }
pub fn update(&mut self, update_fn: impl FnOnce(&mut Canvas)) { pub fn update(&mut self, update_fn: impl FnOnce(&mut T)) {
// Make a new copy of the current canvas version, // Make a new copy of the current version,
// so that we can safely modify it without losing // so that we can safely modify it without losing
// the previous canvas version // the previous version
let mut new_version = self.get().clone(); let mut new_version = self.get().clone();
update_fn(&mut new_version); update_fn(&mut new_version);
@ -77,8 +76,8 @@ impl VersionedCanvas {
} }
// Do inplace update, which will be irreversible // Do inplace update, which will be irreversible
pub fn irreversible_update(&mut self, update_fn: impl FnOnce(&mut Canvas)) { pub fn irreversible_update(&mut self, update_fn: impl FnOnce(&mut T)) {
// Do the update directly on the current canvas version // Do the update directly on the current version
update_fn(self.versions.back_mut().unwrap()); update_fn(self.versions.back_mut().unwrap());
// This is a linear history, // This is a linear history,

View File

@ -46,3 +46,5 @@ impl DocumentSnapshot {
serde_bare::to_writer(writer, &self) serde_bare::to_writer(writer, &self)
} }
} }
pub type VersionedCanvas = history::Versioned<canvas::Canvas>;

View File

@ -18,8 +18,7 @@ use im::Vector;
use super::tool_ctx::{CanvasToolCtx}; use super::tool_ctx::{CanvasToolCtx};
use crate::canvas::Canvas; use crate::canvas::Canvas;
use crate::history::VersionedCanvas; use crate::{DocumentSnapshot, VersionedCanvas};
use crate::DocumentSnapshot;
use druid::widget::prelude::*; use druid::widget::prelude::*;
use druid::{Color, Data, Env, Event, PointerType}; use druid::{Color, Data, Env, Event, PointerType};

View File

@ -16,7 +16,7 @@
use crate::tool::{CanvasToolParams, CanvasToolType}; use crate::tool::{CanvasToolParams, CanvasToolType};
use crate::canvas::{Canvas, CanvasElement}; use crate::canvas::{Canvas, CanvasElement};
use crate::history::VersionedCanvas; use crate::VersionedCanvas;
use druid::kurbo::BezPath; use druid::kurbo::BezPath;
use druid::{Color, Data, Env, Event, EventCtx, MouseButton, MouseEvent, PaintCtx, RenderContext}; use druid::{Color, Data, Env, Event, EventCtx, MouseButton, MouseEvent, PaintCtx, RenderContext};