Skip to content

Commit

Permalink
fixed musescore#26976: close context menu before reseting to default …
Browse files Browse the repository at this point in the history
…and before closing panel
  • Loading branch information
Eism committed Mar 11, 2025
1 parent f32615b commit 83e8b65
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/framework/dockwindow/internal/dockbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class DockBase : public QQuickItem

Q_INVOKABLE bool isOpen() const;
Q_INVOKABLE void open();
Q_INVOKABLE void close();
Q_INVOKABLE virtual void close();
Q_INVOKABLE void resize(int width, int height);

ui::NavigationSection* navigationSection() const;
Expand Down
14 changes: 14 additions & 0 deletions src/framework/dockwindow/qml/Muse/Dock/DockPanelTab.qml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ StyledTabButton {

clip: true

function closeMenu() {
contextMenuButton.closeMenu()
}

contentItem: Row {
spacing: root.buttonPadding

Expand All @@ -68,6 +72,16 @@ StyledTabButton {
anchors.verticalCenter: parent.verticalCenter
visible: root.isCurrent

onMenuModelChanged: {
if (Boolean(menuModel)) {
menuModel.onMenuCloseRequested.connect(function(){
if (Boolean(root)) {
root.closeMenu()
}
})
}
}

Connections {
target: root

Expand Down
174 changes: 92 additions & 82 deletions src/framework/dockwindow/view/dockpanelview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,116 +41,108 @@ using namespace muse::actions;
static const QString SET_DOCK_OPEN_ACTION_CODE = "dock-set-open";
static const QString TOGGLE_FLOATING_ACTION_CODE = "dock-toggle-floating";

class DockPanelView::DockPanelMenuModel : public muse::uicomponents::AbstractMenuModel
DockPanelMenuModel::DockPanelMenuModel(DockPanelView* panel)
: AbstractMenuModel(panel), m_panel(panel)
{
public:
DockPanelMenuModel(DockPanelView* panel)
: AbstractMenuModel(panel), m_panel(panel)
{
listenFloatingChanged();
}

void load() override
{
TRACEFUNC;

MenuItemList items;

if (m_customMenuModel && m_customMenuModel->rowCount() > 0) {
items << m_customMenuModel->items();
items << makeSeparator();
}

MenuItem* closeDockItem = makeMenuItem(SET_DOCK_OPEN_ACTION_CODE, TranslatableString("appshell/dock", "Close"));
closeDockItem->setArgs(ActionData::make_arg2<QString, bool>(m_panel->objectName(), false));
items << closeDockItem;
listenFloatingChanged();
}

MenuItem* toggleFloatingItem = makeMenuItem(TOGGLE_FLOATING_ACTION_CODE, toggleFloatingActionTitle());
toggleFloatingItem->setArgs(ActionData::make_arg1<QString>(m_panel->objectName()));
items << toggleFloatingItem;
void DockPanelMenuModel::load()
{
TRACEFUNC;

setItems(items);
}
MenuItemList items;

AbstractMenuModel* customMenuModel() const
{
return m_customMenuModel;
if (m_customMenuModel && m_customMenuModel->rowCount() > 0) {
items << m_customMenuModel->items();
items << makeSeparator();
}

void setCustomMenuModel(AbstractMenuModel* model)
{
m_customMenuModel = model;
MenuItem* closeDockItem = makeMenuItem(SET_DOCK_OPEN_ACTION_CODE, TranslatableString("appshell/dock", "Close"));
closeDockItem->setArgs(ActionData::make_arg2<QString, bool>(m_panel->objectName(), false));
items << closeDockItem;

if (!model) {
return;
}
MenuItem* toggleFloatingItem = makeMenuItem(TOGGLE_FLOATING_ACTION_CODE, toggleFloatingActionTitle());
toggleFloatingItem->setArgs(ActionData::make_arg1<QString>(m_panel->objectName()));
items << toggleFloatingItem;

connect(model, &AbstractMenuModel::itemsChanged, this, [this]() {
load();
});
setItems(items);
}

connect(model, &AbstractMenuModel::itemChanged, this, [this](MenuItem* item) {
updateItem(item);
});
}
AbstractMenuModel* DockPanelMenuModel::customMenuModel() const
{
return m_customMenuModel;
}

private:
uicomponents::MenuItem* makeMenuItem(const QString& actionCode, const TranslatableString& title)
{
MenuItem* item = new MenuItem(this);
item->setId(actionCode);
void DockPanelMenuModel::setCustomMenuModel(AbstractMenuModel* model)
{
m_customMenuModel = model;

UiAction action;
action.code = codeFromQString(actionCode);
action.title = title;
item->setAction(action);
if (!model) {
return;
}

UiActionState state;
state.enabled = true;
item->setState(state);
connect(model, &AbstractMenuModel::itemsChanged, this, [this]() {
load();
});

return item;
}
connect(model, &AbstractMenuModel::itemChanged, this, [this](MenuItem* item) {
updateItem(item);
});
}

TranslatableString toggleFloatingActionTitle() const
{
return m_panel->floating() ? TranslatableString("appshell/dock", "Dock") : TranslatableString("appshell/dock", "Undock");
}
MenuItem* DockPanelMenuModel::makeMenuItem(const QString& actionCode, const TranslatableString& title)
{
MenuItem* item = new MenuItem(this);
item->setId(actionCode);

void listenFloatingChanged()
{
connect(m_panel, &DockPanelView::floatingChanged, this, [this]() {
int index = itemIndex(TOGGLE_FLOATING_ACTION_CODE);
UiAction action;
action.code = codeFromQString(actionCode);
action.title = title;
item->setAction(action);

if (index == INVALID_ITEM_INDEX) {
return;
}
UiActionState state;
state.enabled = true;
item->setState(state);

MenuItem& item = this->item(index);
return item;
}

UiAction action = item.action();
action.title = toggleFloatingActionTitle();
item.setAction(action);
});
}
TranslatableString DockPanelMenuModel::toggleFloatingActionTitle() const
{
return m_panel->floating() ? TranslatableString("appshell/dock", "Dock") : TranslatableString("appshell/dock", "Undock");
}

void updateItem(MenuItem* newItem)
{
int index = itemIndex(newItem->id());
void DockPanelMenuModel::listenFloatingChanged()
{
connect(m_panel, &DockPanelView::floatingChanged, this, [this]() {
int index = itemIndex(TOGGLE_FLOATING_ACTION_CODE);

if (index == INVALID_ITEM_INDEX) {
return;
}

setItem(index, newItem);
MenuItem& item = this->item(index);

UiAction action = item.action();
action.title = toggleFloatingActionTitle();
item.setAction(action);
});
}

void DockPanelMenuModel::updateItem(MenuItem* newItem)
{
int index = itemIndex(newItem->id());

if (index == INVALID_ITEM_INDEX) {
return;
}

AbstractMenuModel* m_customMenuModel = nullptr;
DockPanelView* m_panel = nullptr;
};
setItem(index, newItem);
}

DockPanelView::DockPanelView(QQuickItem* parent)
: DockBase(DockType::Panel, parent), m_menuModel(new DockPanelMenuModel(this))
: DockBase(DockType::Panel, parent), m_menuModel(new dock::DockPanelMenuModel(this))
{
setLocation(Location::Left);
}
Expand All @@ -168,6 +160,24 @@ DockPanelView::~DockPanelView()
dockWidget->setProperty(TOOLBAR_COMPONENT_PROPERTY, QVariant::fromValue(nullptr));
}

void DockPanelView::close()
{
if (m_menuModel) {
emit m_menuModel->menuCloseRequested();
}

DockBase::close();
}

void DockPanelView::resetToDefault()
{
if (m_menuModel) {
emit m_menuModel->menuCloseRequested();
}

DockBase::resetToDefault();
}

QString DockPanelView::groupName() const
{
return m_groupName;
Expand Down
34 changes: 33 additions & 1 deletion src/framework/dockwindow/view/dockpanelview.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class AbstractMenuModel;
}

namespace muse::dock {
class DockPanelMenuModel;
class DockPanelView : public DockBase
{
Q_OBJECT
Expand All @@ -49,6 +50,9 @@ class DockPanelView : public DockBase
explicit DockPanelView(QQuickItem* parent = nullptr);
~DockPanelView() override;

Q_INVOKABLE void close() override;
void resetToDefault() override;

QString groupName() const;
uicomponents::AbstractMenuModel* contextMenuModel() const;
QQmlComponent* titleBar() const;
Expand Down Expand Up @@ -81,10 +85,38 @@ public slots:
QString m_groupName;

class DockPanelMenuModel;
DockPanelMenuModel* m_menuModel = nullptr;
dock::DockPanelMenuModel* m_menuModel = nullptr;
QQmlComponent* m_titleBar = nullptr;
QQmlComponent* m_toolbarComponent = nullptr;
};

class DockPanelMenuModel : public muse::uicomponents::AbstractMenuModel
{
Q_OBJECT

public:
DockPanelMenuModel(DockPanelView* panel);

void load() override;

AbstractMenuModel* customMenuModel() const;
void setCustomMenuModel(AbstractMenuModel* model);

signals:
void menuCloseRequested();

private:
uicomponents::MenuItem* makeMenuItem(const QString& actionCode, const TranslatableString& title);

TranslatableString toggleFloatingActionTitle() const;

void listenFloatingChanged();

void updateItem(muse::uicomponents::MenuItem* newItem);

AbstractMenuModel* m_customMenuModel = nullptr;
DockPanelView* m_panel = nullptr;
};
}

#endif // MUSE_DOCK_DOCKPANELVIEW_H
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ FlatButton {
menuLoader.toggleOpened(menuModel, x, y)
}

function closeMenu() {
menuLoader.close()
}

enabled: visible

icon: IconCode.MENU_THREE_DOTS
Expand Down

0 comments on commit 83e8b65

Please sign in to comment.