diff --git a/src/utils/power-button.cpp b/src/utils/power-button.cpp index 4dfe7f7..8cdc1c0 100644 --- a/src/utils/power-button.cpp +++ b/src/utils/power-button.cpp @@ -54,7 +54,7 @@ QString PowerButton::getToolTip() void PowerButton::clicked(bool leftButtonClicked, int mouseX, int mouseY, bool isFullScreen) { if (leftButtonClicked) { - QProcess::startDetached("ukui-session-tools"); + QProcess::startDetached("ukui-session-tools", {}); } else { openMenu(mouseX, mouseY, isFullScreen); } @@ -62,87 +62,93 @@ void PowerButton::clicked(bool leftButtonClicked, int mouseX, int mouseY, bool i void PowerButton::openMenu(int menuX, int menuY, bool isFullScreen) { - QMenu powerMenu; + if (m_contextMenu) { + m_contextMenu.data()->close(); + return; + } + + auto powerMenu = new QMenu; QDBusReply reply; QDBusInterface qDBusInterface("org.gnome.SessionManager", "/org/gnome/SessionManager", "org.gnome.SessionManager", QDBusConnection::sessionBus()); if (canSwitch() && hasMultipleUsers()) { - QAction *action = new QAction(tr("Switch user"), &powerMenu); - connect(action, &QAction::triggered, &powerMenu, [] { + QAction *action = new QAction(tr("Switch user"), powerMenu); + connect(action, &QAction::triggered, powerMenu, [] { AppManager::instance()->launchBinaryApp("ukui-session-tools", "--switchuser"); }); - powerMenu.addAction(action); + powerMenu->addAction(action); } reply = qDBusInterface.call("canHibernate"); if (reply.isValid() && reply.value()) { - QAction *action = new QAction(tr("Hibernate"), &powerMenu); + QAction *action = new QAction(tr("Hibernate"), powerMenu); action->setToolTip(tr("

Turn off the computer, but the applications " "will remain open. When you turn on the computer again, " "you can return to the state you were in before

")); - connect(action, &QAction::triggered, &powerMenu, [] { + connect(action, &QAction::triggered, powerMenu, [] { AppManager::instance()->launchBinaryApp("ukui-session-tools", "--hibernate"); }); - powerMenu.addAction(action); + powerMenu->addAction(action); } reply = qDBusInterface.call("canSuspend"); if (reply.isValid() && reply.value()) { - QAction *action = new QAction(tr("Suspend"), &powerMenu); + QAction *action = new QAction(tr("Suspend"), powerMenu); action->setToolTip(tr("

The computer remains on but consumes less power, " "and the applications will remain open. You can quickly wake " "up the computer and return to the state you left

")); - connect(action, &QAction::triggered, &powerMenu, [] { + connect(action, &QAction::triggered, powerMenu, [] { AppManager::instance()->launchBinaryApp("ukui-session-tools", "--suspend"); }); - powerMenu.addAction(action); + powerMenu->addAction(action); } - QAction *lockAction = new QAction(tr("Lock Screen"), &powerMenu); - connect(lockAction, &QAction::triggered, &powerMenu, [] { + QAction *lockAction = new QAction(tr("Lock Screen"), powerMenu); + connect(lockAction, &QAction::triggered, powerMenu, [] { AppManager::instance()->launchBinaryApp("ukui-screensaver-command", "-l"); }); - powerMenu.addAction(lockAction); + powerMenu->addAction(lockAction); reply = qDBusInterface.call("canLogout"); if (reply.isValid() && reply.value()) { - QAction *action = new QAction(tr("Log Out"), &powerMenu); + QAction *action = new QAction(tr("Log Out"), powerMenu); action->setToolTip(tr("

The current user logs out of the system, ending " "their session and returning to the login screen

")); - connect(action, &QAction::triggered, &powerMenu, [] { + connect(action, &QAction::triggered, powerMenu, [] { AppManager::instance()->launchBinaryApp("ukui-session-tools", "--logout"); }); - powerMenu.addAction(action); + powerMenu->addAction(action); } reply = qDBusInterface.call("canReboot"); if (reply.isValid() && reply.value()) { - QAction *action = new QAction(tr("Reboot"), &powerMenu); + QAction *action = new QAction(tr("Reboot"), powerMenu); action->setToolTip(tr("

Close all applications, turn off the computer, and then turn it back on

")); - connect(action, &QAction::triggered, &powerMenu, [] { + connect(action, &QAction::triggered, powerMenu, [] { AppManager::instance()->launchBinaryApp("ukui-session-tools", "--reboot"); }); - powerMenu.addAction(action); + powerMenu->addAction(action); } reply = qDBusInterface.call("canPowerOff"); if (reply.isValid() && reply.value()) { - QAction *action = new QAction(tr("Power Off"), &powerMenu); + QAction *action = new QAction(tr("Power Off"), powerMenu); action->setToolTip(tr("

Close all applications, and then turn off the computer

")); - connect(action, &QAction::triggered, &powerMenu, [] { + connect(action, &QAction::triggered, powerMenu, [] { AppManager::instance()->launchBinaryApp("ukui-session-tools", "--shutdown"); }); - powerMenu.addAction(action); + powerMenu->addAction(action); } - powerMenu.setToolTipsVisible(true); - powerMenu.setAttribute(Qt::WA_DeleteOnClose); + m_contextMenu = powerMenu; + powerMenu->setToolTipsVisible(true); + powerMenu->setAttribute(Qt::WA_DeleteOnClose); if (isFullScreen) { - powerMenu.exec(QPoint(menuX - powerMenu.sizeHint().width(), menuY - powerMenu.sizeHint().height())); + m_contextMenu.data()->popup(QPoint(menuX - powerMenu->sizeHint().width(), menuY - powerMenu->sizeHint().height())); } else { - powerMenu.exec(QPoint(menuX, menuY - powerMenu.sizeHint().height())); + m_contextMenu.data()->popup(QPoint(menuX, menuY - powerMenu->sizeHint().height())); } } diff --git a/src/utils/power-button.h b/src/utils/power-button.h index 0fe6c36..19178f8 100644 --- a/src/utils/power-button.h +++ b/src/utils/power-button.h @@ -20,6 +20,8 @@ #include #include +#include +#include namespace UkuiMenu { @@ -43,6 +45,8 @@ private: bool hasMultipleUsers(); bool canSwitch(); + QPointer m_contextMenu; + Q_SIGNALS: void iconChanged(); void toolTipChanged();