From 77ad598d05efa75a158d95f85e6f11a1d5d12efd Mon Sep 17 00:00:00 2001 From: Matthias Gabriel Date: Fri, 22 Mar 2024 09:04:57 +0100 Subject: [PATCH] fix undo behaviour --- src/sketch_board.rs | 8 ++++++-- src/tools/mod.rs | 12 ++++++++++++ src/tools/text.rs | 43 +++++++++++++++++++++++++++++++++++++------ 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/sketch_board.rs b/src/sketch_board.rs index fec1f71..b83ec34 100644 --- a/src/sketch_board.rs +++ b/src/sketch_board.rs @@ -273,7 +273,9 @@ impl SketchBoard { } fn handle_undo(&mut self) -> ToolUpdateResult { - if self.renderer.undo() { + if self.active_tool.borrow().active() { + self.active_tool.borrow_mut().handle_undo() + } else if self.renderer.undo() { ToolUpdateResult::Redraw } else { ToolUpdateResult::Unmodified @@ -281,7 +283,9 @@ impl SketchBoard { } fn handle_redo(&mut self) -> ToolUpdateResult { - if self.renderer.redo() { + if self.active_tool.borrow().active() { + self.active_tool.borrow_mut().handle_redo() + } else if self.renderer.redo() { ToolUpdateResult::Redraw } else { ToolUpdateResult::Unmodified diff --git a/src/tools/mod.rs b/src/tools/mod.rs index 3b010bd..8c444ba 100644 --- a/src/tools/mod.rs +++ b/src/tools/mod.rs @@ -79,6 +79,18 @@ pub trait Tool { ToolUpdateResult::Unmodified } + fn active(&self) -> bool { + false + } + + fn handle_undo(&mut self) -> ToolUpdateResult { + ToolUpdateResult::Unmodified + } + + fn handle_redo(&mut self) -> ToolUpdateResult { + ToolUpdateResult::Unmodified + } + fn get_drawable(&self) -> Option<&dyn Drawable>; } diff --git a/src/tools/text.rs b/src/tools/text.rs index 99ff584..a9043c9 100644 --- a/src/tools/text.rs +++ b/src/tools/text.rs @@ -23,6 +23,20 @@ pub struct Text { style: Style, } +impl Text { + fn new(pos: Vec2D, style: Style) -> Self { + let text_buffer = TextBuffer::new(None); + text_buffer.set_enable_undo(true); + + Self { + pos, + text_buffer, + editing: true, + style, + } + } +} + impl Drawable for Text { fn draw( &self, @@ -274,12 +288,7 @@ impl Tool for TextTool { }; // create a new Text - self.text = Some(Text { - pos: event.pos, - text_buffer: TextBuffer::new(None), - editing: true, - style: self.style, - }); + self.text = Some(Text::new(event.pos, self.style)); return_value } else { @@ -300,6 +309,28 @@ impl Tool for TextTool { ToolUpdateResult::Unmodified } } + + fn active(&self) -> bool { + self.text.is_some() + } + + fn handle_undo(&mut self) -> ToolUpdateResult { + if let Some(t) = &self.text { + t.text_buffer.undo(); + ToolUpdateResult::Redraw + } else { + ToolUpdateResult::Unmodified + } + } + + fn handle_redo(&mut self) -> ToolUpdateResult { + if let Some(t) = &self.text { + t.text_buffer.redo(); + ToolUpdateResult::Redraw + } else { + ToolUpdateResult::Unmodified + } + } } enum ActionScope { ForwardChar,