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
// 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 {
pub struct Versioned<T: druid::Data + Clone> {
// We internally guarantee that this vector
// is never empty
versions: Vector<Canvas>,
versions: Vector<T>,
curr_version: usize,
}
impl VersionedCanvas {
pub fn new(canvas: Canvas) -> VersionedCanvas {
VersionedCanvas {
versions: im::vector![canvas],
impl<T: druid::Data + Clone> Versioned<T> {
pub fn new(first_version: T) -> Versioned<T> {
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,

View File

@ -46,3 +46,5 @@ impl DocumentSnapshot {
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 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};

View File

@ -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};