Skip to content

Commit

Permalink
Add undo action for selection moved with keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandlo committed Apr 27, 2023
1 parent 9c8538b commit 37117b4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/core/control/tools/EditSelection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,14 +753,18 @@ void EditSelection::updateMatrix() {
cairo_matrix_translate(&this->cmatrix, -rx, -ry);
}

void EditSelection::moveSelection(double dx, double dy) {
void EditSelection::moveSelection(double dx, double dy, bool addMoveUndo) {
this->x += dx;
this->y += dy;
this->snappedBounds.x += dx;
this->snappedBounds.y += dy;

updateMatrix();

if (addMoveUndo) {
this->contents->addMoveUndo(this->undo, dx, dy);
}

this->view->getXournal()->repaintSelection();
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/control/tools/EditSelection.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class EditSelection: public ElementContainer, public Serializable {
/**
* Move the selection
*/
void moveSelection(double dx, double dy);
void moveSelection(double dx, double dy, bool addMoveUndo = false);

/**
* Get the cursor type for the current position (if 0 then the default cursor should be used)
Expand Down
9 changes: 9 additions & 0 deletions src/core/control/tools/EditSelectionContents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ void EditSelectionContents::replaceInsertOrder(std::deque<std::pair<Element*, El
this->insertOrder = std::move(newInsertOrder);
}

void EditSelectionContents::addMoveUndo(UndoRedoHandler* undo, double dx, double dy) {
undo->addUndoAction(std::make_unique<MoveUndoAction>(this->sourceLayer, this->sourcePage, &this->selected, dx, dy,
this->sourceLayer, this->sourcePage));
this->lastBounds.x += dx;
this->lastBounds.y += dy;
this->lastSnappedBounds.x += dx;
this->lastSnappedBounds.y += dy;
}

/**
* Returns all containing elements of this selection
*/
Expand Down
2 changes: 2 additions & 0 deletions src/core/control/tools/EditSelectionContents.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class EditSelectionContents: public ElementContainer, public Serializable {
* */
void replaceInsertOrder(std::deque<std::pair<Element*, Element::Index>> newInsertOrder);

void addMoveUndo(UndoRedoHandler* undo, double dx, double dy);

public:
/**
* paints the selection
Expand Down
2 changes: 1 addition & 1 deletion src/core/gui/XournalView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ auto XournalView::onKeyPressEvent(GdkEventKey* event) -> bool {
ydir = 1;
}
if (xdir != 0 || ydir != 0) {
selection->moveSelection(d * xdir, d * ydir);
selection->moveSelection(d * xdir, d * ydir, true);
selection->ensureWithinVisibleArea();
return true;
}
Expand Down

0 comments on commit 37117b4

Please sign in to comment.