Skip to content

Commit

Permalink
remember state of update_text_position in undo history & fix redo ig
Browse files Browse the repository at this point in the history
  • Loading branch information
justDeeevin committed Apr 2, 2024
1 parent 9293015 commit 7362fbf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/nuhxboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ pub enum Change {
MoveElement{
index: usize,
delta: geo::Coord,
move_text: bool,
},
}

Expand Down Expand Up @@ -710,21 +711,23 @@ impl Application for NuhxBoard {
self.edit_history.push(change);
}
Message::Undo => {
println!("Undo");
if self.history_depth < self.edit_history.len() {
self.history_depth += 1;
match self.edit_history[self.edit_history.len() - self.history_depth] {
Change::MoveElement { index, delta } => {
self.config.elements[index].translate(-delta, self.update_text_position);
Change::MoveElement { index, delta, move_text } => {
self.config.elements[index].translate(-delta, move_text);
}
}
}
}
Message::Redo => {
println!("Redo");
if self.history_depth > 0 {
self.history_depth -= 1;
match self.edit_history[self.edit_history.len() - self.history_depth - 1] {
Change::MoveElement { index, delta } => {
self.config.elements[index].translate(delta, self.update_text_position);
Change::MoveElement { index, delta, move_text} => {
self.config.elements[index].translate(delta, move_text);
}
}
}
Expand Down Expand Up @@ -822,9 +825,11 @@ impl Application for NuhxBoard {
iced::Event::Keyboard(iced::keyboard::Event::KeyPressed { key, location: _, modifiers, text: _ }) => {
if modifiers.command() {
if key == iced::keyboard::Key::Character(SmolStr::new("z")) {
Some(Message::Undo)
} else if key == iced::keyboard::Key::Character(SmolStr::new("Z")) {
Some(Message::Redo)
if modifiers.shift() {
Some(Message::Redo)
} else {
Some(Message::Undo)
}
} else {
None
}
Expand Down
1 change: 1 addition & 0 deletions src/ui/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ impl canvas::Program<Message> for NuhxBoard {
Message::PushChange(Change::MoveElement {
index,
delta: state.delta_accumulator,
move_text: self.update_text_position,
})
});
state.delta_accumulator = Coord::default();
Expand Down

0 comments on commit 7362fbf

Please sign in to comment.