Fix crash when pressing ESC while moving a selection (fix #2829)

This commit is contained in:
Martín Capello 2021-07-16 10:08:40 -03:00
parent 0145458686
commit fc79146c56
2 changed files with 29 additions and 7 deletions

View File

@ -12,6 +12,8 @@
#include "app/ui/editor/moving_selection_state.h"
#include "app/cmd/set_mask_position.h"
#include "app/commands/command.h"
#include "app/commands/commands.h"
#include "app/context_access.h"
#include "app/tx.h"
#include "app/ui/editor/editor.h"
@ -31,6 +33,7 @@ MovingSelectionState::MovingSelectionState(Editor* editor, MouseMessage* msg)
: m_editor(editor)
, m_cursorStart(editor->screenToEditor(msg->position()))
, m_selOrigin(editor->document()->mask()->bounds().origin())
, m_selectionCanceled(false)
{
editor->captureMouse();
@ -42,6 +45,12 @@ MovingSelectionState::MovingSelectionState(Editor* editor, MouseMessage* msg)
void MovingSelectionState::onBeforeCommandExecution(CommandExecutionEvent& ev)
{
if (ev.command()->id() == CommandId::Cancel()) {
// We cancel the cancel command to avoid calling the inputChain()->cancel(), which
// deselects the selection.
ev.cancel();
m_selectionCanceled = true;
}
m_editor->backToPreviousState();
m_editor->releaseMouse();
}
@ -66,18 +75,29 @@ EditorState::LeaveAction MovingSelectionState::onLeaveState(Editor* editor, Edit
m_selOrigin.y);
mask->unfreeze();
{
ContextWriter writer(UIContext::instance(), 1000);
Tx tx(writer.context(), "Move Selection Edges", DoesntModifyDocument);
tx(new cmd::SetMaskPosition(doc, newOrigin));
tx.commit();
if (m_selectionCanceled) {
doc->resetTransformation();
doc->generateMaskBoundaries();
}
else {
{
ContextWriter writer(UIContext::instance(), 1000);
Tx tx(writer.context(), "Move Selection Edges", DoesntModifyDocument);
tx(new cmd::SetMaskPosition(doc, newOrigin));
tx.commit();
}
doc->resetTransformation();
}
doc->resetTransformation();
doc->notifyGeneralUpdate();
return StandbyState::onLeaveState(editor, newState);
}
void MovingSelectionState::onBeforePopState(Editor* editor)
{
m_ctxConn.disconnect();
StandbyState::onBeforePopState(editor);
}
bool MovingSelectionState::onMouseDown(Editor* editor, MouseMessage* msg)
{
// Do nothing

View File

@ -26,6 +26,7 @@ namespace app {
virtual bool onMouseMove(Editor* editor, ui::MouseMessage* msg) override;
virtual bool onSetCursor(Editor* editor, const gfx::Point& mouseScreenPos) override;
virtual bool onUpdateStatusBar(Editor* editor) override;
virtual void onBeforePopState(Editor* editor) override;
virtual bool requireBrushPreview() override { return false; }
private:
@ -37,6 +38,7 @@ namespace app {
gfx::Point m_selOrigin;
gfx::Point m_delta;
obs::scoped_connection m_ctxConn;
bool m_selectionCanceled;
};
} // namespace app