Implement Undo/Redo #4
18
src/lib.rs
18
src/lib.rs
|
|
@ -42,20 +42,24 @@ use im::Vector;
|
|||
pub type Canvas = Vector<CanvasElement>;
|
||||
|
||||
#[derive(Clone, druid::Data)]
|
||||
pub struct History {
|
||||
pub struct VersionedCanvas {
|
||||
// We internally guarantee that this vector
|
||||
// is never empty
|
||||
versions: Vector<Canvas>,
|
||||
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,
|
||||
|
Franciman marked this conversation as resolved
|
||||
// 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);
|
||||
}
|
||||
|
|
|
|||
14
src/main.rs
14
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<CanvasElement>,
|
||||
//elements: Vector<CanvasElement>,
|
||||
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<CanvasData> 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<CanvasData> 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()
|
||||
|
|
|
|||
Loading…
Reference in New Issue
I agree, I’ll leave out this consideration before pushing