Skip to content

Commit

Permalink
Improve the default canvas position on first opening a score
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-spa committed Sep 6, 2024
1 parent 7e09e2c commit 2bd9198
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/notation/view/abstractnotationpaintview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ void AbstractNotationPaintView::onLoadNotation(INotationPtr)
{
if (viewport().isValid() && !m_notation->viewState()->isMatrixInited()) {
m_inputController->initZoom();
m_inputController->initCanvasPos();
}

if (publishMode()) {
Expand Down Expand Up @@ -378,6 +379,7 @@ void AbstractNotationPaintView::onViewSizeChanged()
if (viewport().isValid()) {
if (!notation()->viewState()->isMatrixInited()) {
m_inputController->initZoom();
m_inputController->initCanvasPos();
} else {
m_inputController->updateZoomAfterSizeChange();
}
Expand Down
30 changes: 30 additions & 0 deletions src/notation/view/notationviewinputcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,36 @@ void NotationViewInputController::initZoom()
currentNotation()->viewState()->setMatrixInited(true);
}

void NotationViewInputController::initCanvasPos()
{
if (currentNotation()->viewState()->zoomType().val != ZoomType::Percentage) {
return;
}

const std::vector<Page*>& pages = currentNotation()->elements()->msScore()->pages();
IF_ASSERT_FAILED(!pages.empty()) {
return;
}

const Page* lastPage = pages.back();
double curScaling = m_view->currentScaling();

double totalScoreWidth = lastPage->x() + lastPage->width();
double totalScoreHeight = lastPage->y() + lastPage->height();
double viewWidth = m_view->width() / curScaling;
double viewHeight = m_view->height() / curScaling;

bool centerHorizontally = totalScoreWidth < viewWidth;
bool centerVertically = totalScoreHeight < viewHeight;

const double canvasMargin = MScore::horizontalPageGapOdd;

double xMove = centerHorizontally ? 0.5 * (viewWidth - totalScoreWidth) : canvasMargin;
double yMove = centerVertically ? 0.5 * (viewHeight - totalScoreHeight) : canvasMargin;

m_view->moveCanvas(xMove, yMove);
}

void NotationViewInputController::updateZoomAfterSizeChange()
{
IF_ASSERT_FAILED(currentNotation()) {
Expand Down
1 change: 1 addition & 0 deletions src/notation/view/notationviewinputcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class NotationViewInputController : public muse::actions::Actionable, public mus
void init();

void initZoom();
void initCanvasPos();
void updateZoomAfterSizeChange();
void zoomIn();
void zoomOut();
Expand Down

0 comments on commit 2bd9198

Please sign in to comment.