From a6bbbd3ccedd126c589d970b65a5b27bc36a256e Mon Sep 17 00:00:00 2001 From: hewenfei Date: Wed, 13 Mar 2024 15:55:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=82=B9=E5=87=BB?= =?UTF-8?q?=E7=94=B5=E6=BA=90=E6=8C=89=E9=92=AE=E5=8F=B3=E9=94=AE=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E6=97=B6=EF=BC=8C=E7=84=A6=E7=82=B9=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/power-button.cpp | 60 +++++++++++++++++++++----------------- src/utils/power-button.h | 4 +++ 2 files changed, 37 insertions(+), 27 deletions(-) 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();