Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions framework/uicomponents/qml/Muse/UiComponents/menuview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ void MenuView::updateGeometry()
setCascadeAlign(Qt::AlignmentFlag::AlignLeft);
newPopupPos = PopupPosition::Right;
} else {
// move to the left to an area that doesn't fit
movePos(m_globalPos.x() - (viewRect.right() - paddedAnchorRect.right()) + padding() * 2, m_globalPos.y());
// Place the menu to the left of the cursor instead of pinning it to the screen edge
movePos(parentTopLeft.x() - viewRect.width() + padding() * 2, m_globalPos.y());
}
}

Expand Down
4 changes: 4 additions & 0 deletions framework/uicomponents/qml/Muse/UiComponents/menuview.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "popupview.h"

namespace muse::uicomponents {
class MenuViewTestAccess;

class MenuView : public PopupView
{
Q_OBJECT
Expand Down Expand Up @@ -84,6 +86,8 @@ public slots:
void cascadeAlignChanged(Qt::AlignmentFlag cascadeAlign);

private:
friend class MenuViewTestAccess;

void componentComplete() override;

void updateGeometry() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ set(MODULE_TEST muse_uicomponents_qml_tests)
set(MODULE_TEST_SRC
${CMAKE_CURRENT_LIST_DIR}/doubleinputvalidator_tests.cpp
${CMAKE_CURRENT_LIST_DIR}/intinputvalidator_tests.cpp
${CMAKE_CURRENT_LIST_DIR}/menuview_tests.cpp
${CMAKE_CURRENT_LIST_DIR}/sectionproxymodel_tests.cpp
${CMAKE_CURRENT_LIST_DIR}/sortfilterproxymodel_tests.cpp

Expand All @@ -31,6 +32,7 @@ set(MODULE_TEST_SRC

set(MODULE_TEST_LINK
Qt::Qml
Qt::Quick
muse_uicomponents_qml
)

Expand Down
143 changes: 143 additions & 0 deletions framework/uicomponents/qml/Muse/UiComponents/tests/menuview_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* SPDX-License-Identifier: GPL-3.0-only
* MuseScore-CLA-applies
*
* MuseScore Studio
* Music Composition & Notation
*
* Copyright (C) 2026 MuseScore Limited and others
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include <memory>

#include <gtest/gtest.h>

#include <QGuiApplication>
#include <QQuickItem>
#include <QQuickWindow>
#include <QScreen>

#include "../menuview.h"

using namespace muse::uicomponents;

namespace muse::uicomponents {
class MenuViewTestAccess
{
public:
static void updateGeometry(MenuView& view)
{
view.updateGeometry();
}
};
}

namespace {
class MainWindowStub final : public muse::ui::IMainWindow
{
public:
void init(muse::ui::MainWindowBridge*) override {}
void deinit() override {}

QWindow* qWindow() const override { return nullptr; }

void requestShowOnBack() override {}
void requestShowOnFront() override {}

bool isFullScreen() const override { return false; }
muse::async::Notification isFullScreenChanged() const override { return {}; }
void toggleFullScreen() override {}

QScreen* screen() const override { return QGuiApplication::primaryScreen(); }
};

class TestMenuView final : public MenuView
{
public:
using MenuView::MenuView;

QPointF globalPosition() const { return m_globalPos; }

void setMainWindowForTest(const std::shared_ptr<muse::ui::IMainWindow>& window)
{
mainWindow.set(window);
}
};
}

TEST(MenuViewTests, OverflowingTopLevelMenuOpensLeftOfCursor)
{
QScreen* screen = QGuiApplication::primaryScreen();
ASSERT_NE(screen, nullptr);

const QRect availableGeometry = screen->availableGeometry();
ASSERT_GT(availableGeometry.width(), 400);

QQuickWindow parentWindow;
parentWindow.setGeometry(availableGeometry);

QQuickItem anchor(parentWindow.contentItem());
anchor.setX(availableGeometry.width() - 100);
anchor.setY(100);
anchor.setSize(QSizeF(1, 1));

QQuickItem content;
TestMenuView menu(&anchor);
menu.setMainWindowForTest(std::make_shared<MainWindowStub>());
menu.setContentItem(&content);
menu.setDesiredWidth(200);
menu.setDesiredHeight(100);

MenuViewTestAccess::updateGeometry(menu);

const qreal cursorX = anchor.mapToGlobal(QPointF(0, 0)).x();
const qreal contentRight = menu.globalPosition().x() + menu.padding() + menu.desiredWidth();

EXPECT_LT(menu.globalPosition().x(), cursorX);
EXPECT_DOUBLE_EQ(contentRight, cursorX);
EXPECT_NE(contentRight, availableGeometry.right());
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

TEST(MenuViewTests, TopLevelMenuUsesScreenBoundaryInsteadOfParentWindow)
{
QScreen* screen = QGuiApplication::primaryScreen();
ASSERT_NE(screen, nullptr);

const QRect availableGeometry = screen->availableGeometry();
ASSERT_GT(availableGeometry.width(), 400);

QQuickWindow parentWindow;
parentWindow.setGeometry(availableGeometry.x(), availableGeometry.y(), availableGeometry.width() / 2,
availableGeometry.height());

QQuickItem anchor(parentWindow.contentItem());
anchor.setX(parentWindow.width() - 50);
anchor.setY(100);
anchor.setSize(QSizeF(1, 1));

QQuickItem content;
TestMenuView menu(&anchor);
menu.setMainWindowForTest(std::make_shared<MainWindowStub>());
menu.setContentItem(&content);
menu.setDesiredWidth(200);
menu.setDesiredHeight(100);

MenuViewTestAccess::updateGeometry(menu);

const qreal contentRight = menu.globalPosition().x() + menu.padding() + menu.desiredWidth();

EXPECT_GT(contentRight, parentWindow.geometry().right());
EXPECT_LE(contentRight, availableGeometry.right());
}