EditorState::LeaveAction MovingSelectionState::onLeaveState(Editor* editor, EditorState* newState)
{
  Document* doc = editor->document();
  Mask* mask = doc->mask();
  gfx::Point newOrigin = mask->bounds().origin();

  // Restore the mask to the original state so we can transform it
  // with the a undoable transaction.
  mask->setOrigin(m_selOrigin.x,
                  m_selOrigin.y);
  mask->unfreeze();

  {
    ContextWriter writer(UIContext::instance(), 1000);
    Transaction transaction(writer.context(), "Move Selection Edges", DoesntModifyDocument);
    transaction.execute(new cmd::SetMaskPosition(doc, newOrigin));
    transaction.commit();
  }

  doc->resetTransformation();
  doc->notifyGeneralUpdate();
  return StandbyState::onLeaveState(editor, newState);
}