Skip to content

Commit

Permalink
Merge pull request #67 from gabm/fix-undo
Browse files Browse the repository at this point in the history
When the text tool is active, undo does remove other drawables
  • Loading branch information
gabm authored Mar 22, 2024
2 parents d8ec206 + 77ad598 commit 94b33af
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/sketch_board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,19 @@ 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
}
}

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
Expand Down
12 changes: 12 additions & 0 deletions src/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
}

Expand Down
43 changes: 37 additions & 6 deletions src/tools/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down

0 comments on commit 94b33af

Please sign in to comment.