Make VersionedCanvas update function easier to use
This commit is contained in:
parent
2ddfef8416
commit
d456725e23
14
src/lib.rs
14
src/lib.rs
|
|
@ -103,7 +103,13 @@ impl VersionedCanvas {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self, update_fn: impl FnOnce(&Canvas) -> Canvas) {
|
||||
pub fn update(&mut self, update_fn: impl FnOnce(&mut Canvas)) {
|
||||
// Make a new copy of the current canvas version,
|
||||
// so that we can safely modify it without losing
|
||||
// the previous canvas version
|
||||
let mut new_version = self.get().clone();
|
||||
update_fn(&mut new_version);
|
||||
|
||||
// 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
|
||||
|
|
@ -111,13 +117,15 @@ impl VersionedCanvas {
|
|||
if self.has_newer_versions() {
|
||||
self.versions = self.versions.take(self.curr_version + 1);
|
||||
}
|
||||
let new_version = update_fn(self.get());
|
||||
self.versions.push_back(new_version);
|
||||
self.curr_version = self.curr_version + 1;
|
||||
}
|
||||
|
||||
// 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
|
||||
update_fn(self.versions.back_mut().unwrap());
|
||||
|
||||
// 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
|
||||
|
|
@ -125,8 +133,6 @@ impl VersionedCanvas {
|
|||
if self.has_newer_versions() {
|
||||
self.versions = self.versions.take(self.curr_version + 1);
|
||||
}
|
||||
|
||||
update_fn(self.versions.back_mut().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -89,10 +89,8 @@ impl Widget<CanvasData> for CanvasWidget {
|
|||
if data.is_drawing() {
|
||||
if let Some(current_element) = data.current_element.take() {
|
||||
|
||||
data.elements.update(move |canvas: &Canvas| -> Canvas {
|
||||
let mut new_canvas = canvas.clone();
|
||||
new_canvas.push_back(current_element);
|
||||
new_canvas
|
||||
data.elements.update(move |canvas: &mut Canvas| {
|
||||
canvas.push_back(current_element);
|
||||
});
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue