fix: 修复点击电源按钮右键菜单时,焦点异常问题

This commit is contained in:
hewenfei 2024-03-13 15:55:37 +08:00 committed by He Sir
parent 32fb0ab303
commit a6bbbd3cce
2 changed files with 37 additions and 27 deletions

View File

@ -54,7 +54,7 @@ QString PowerButton::getToolTip()
void PowerButton::clicked(bool leftButtonClicked, int mouseX, int mouseY, bool isFullScreen) void PowerButton::clicked(bool leftButtonClicked, int mouseX, int mouseY, bool isFullScreen)
{ {
if (leftButtonClicked) { if (leftButtonClicked) {
QProcess::startDetached("ukui-session-tools"); QProcess::startDetached("ukui-session-tools", {});
} else { } else {
openMenu(mouseX, mouseY, isFullScreen); 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) void PowerButton::openMenu(int menuX, int menuY, bool isFullScreen)
{ {
QMenu powerMenu; if (m_contextMenu) {
m_contextMenu.data()->close();
return;
}
auto powerMenu = new QMenu;
QDBusReply<bool> reply; QDBusReply<bool> reply;
QDBusInterface qDBusInterface("org.gnome.SessionManager", "/org/gnome/SessionManager", QDBusInterface qDBusInterface("org.gnome.SessionManager", "/org/gnome/SessionManager",
"org.gnome.SessionManager", QDBusConnection::sessionBus()); "org.gnome.SessionManager", QDBusConnection::sessionBus());
if (canSwitch() && hasMultipleUsers()) { if (canSwitch() && hasMultipleUsers()) {
QAction *action = new QAction(tr("Switch user"), &powerMenu); QAction *action = new QAction(tr("Switch user"), powerMenu);
connect(action, &QAction::triggered, &powerMenu, [] { connect(action, &QAction::triggered, powerMenu, [] {
AppManager::instance()->launchBinaryApp("ukui-session-tools", "--switchuser"); AppManager::instance()->launchBinaryApp("ukui-session-tools", "--switchuser");
}); });
powerMenu.addAction(action); powerMenu->addAction(action);
} }
reply = qDBusInterface.call("canHibernate"); reply = qDBusInterface.call("canHibernate");
if (reply.isValid() && reply.value()) { if (reply.isValid() && reply.value()) {
QAction *action = new QAction(tr("Hibernate"), &powerMenu); QAction *action = new QAction(tr("Hibernate"), powerMenu);
action->setToolTip(tr("<p>Turn off the computer, but the applications " action->setToolTip(tr("<p>Turn off the computer, but the applications "
"will remain open. When you turn on the computer again, " "will remain open. When you turn on the computer again, "
"you can return to the state you were in before</p>")); "you can return to the state you were in before</p>"));
connect(action, &QAction::triggered, &powerMenu, [] { connect(action, &QAction::triggered, powerMenu, [] {
AppManager::instance()->launchBinaryApp("ukui-session-tools", "--hibernate"); AppManager::instance()->launchBinaryApp("ukui-session-tools", "--hibernate");
}); });
powerMenu.addAction(action); powerMenu->addAction(action);
} }
reply = qDBusInterface.call("canSuspend"); reply = qDBusInterface.call("canSuspend");
if (reply.isValid() && reply.value()) { if (reply.isValid() && reply.value()) {
QAction *action = new QAction(tr("Suspend"), &powerMenu); QAction *action = new QAction(tr("Suspend"), powerMenu);
action->setToolTip(tr("<p>The computer remains on but consumes less power, " action->setToolTip(tr("<p>The computer remains on but consumes less power, "
"and the applications will remain open. You can quickly wake " "and the applications will remain open. You can quickly wake "
"up the computer and return to the state you left</p>")); "up the computer and return to the state you left</p>"));
connect(action, &QAction::triggered, &powerMenu, [] { connect(action, &QAction::triggered, powerMenu, [] {
AppManager::instance()->launchBinaryApp("ukui-session-tools", "--suspend"); AppManager::instance()->launchBinaryApp("ukui-session-tools", "--suspend");
}); });
powerMenu.addAction(action); powerMenu->addAction(action);
} }
QAction *lockAction = new QAction(tr("Lock Screen"), &powerMenu); QAction *lockAction = new QAction(tr("Lock Screen"), powerMenu);
connect(lockAction, &QAction::triggered, &powerMenu, [] { connect(lockAction, &QAction::triggered, powerMenu, [] {
AppManager::instance()->launchBinaryApp("ukui-screensaver-command", "-l"); AppManager::instance()->launchBinaryApp("ukui-screensaver-command", "-l");
}); });
powerMenu.addAction(lockAction); powerMenu->addAction(lockAction);
reply = qDBusInterface.call("canLogout"); reply = qDBusInterface.call("canLogout");
if (reply.isValid() && reply.value()) { if (reply.isValid() && reply.value()) {
QAction *action = new QAction(tr("Log Out"), &powerMenu); QAction *action = new QAction(tr("Log Out"), powerMenu);
action->setToolTip(tr("<p>The current user logs out of the system, ending " action->setToolTip(tr("<p>The current user logs out of the system, ending "
"their session and returning to the login screen</p>")); "their session and returning to the login screen</p>"));
connect(action, &QAction::triggered, &powerMenu, [] { connect(action, &QAction::triggered, powerMenu, [] {
AppManager::instance()->launchBinaryApp("ukui-session-tools", "--logout"); AppManager::instance()->launchBinaryApp("ukui-session-tools", "--logout");
}); });
powerMenu.addAction(action); powerMenu->addAction(action);
} }
reply = qDBusInterface.call("canReboot"); reply = qDBusInterface.call("canReboot");
if (reply.isValid() && reply.value()) { if (reply.isValid() && reply.value()) {
QAction *action = new QAction(tr("Reboot"), &powerMenu); QAction *action = new QAction(tr("Reboot"), powerMenu);
action->setToolTip(tr("<p>Close all applications, turn off the computer, and then turn it back on</p>")); action->setToolTip(tr("<p>Close all applications, turn off the computer, and then turn it back on</p>"));
connect(action, &QAction::triggered, &powerMenu, [] { connect(action, &QAction::triggered, powerMenu, [] {
AppManager::instance()->launchBinaryApp("ukui-session-tools", "--reboot"); AppManager::instance()->launchBinaryApp("ukui-session-tools", "--reboot");
}); });
powerMenu.addAction(action); powerMenu->addAction(action);
} }
reply = qDBusInterface.call("canPowerOff"); reply = qDBusInterface.call("canPowerOff");
if (reply.isValid() && reply.value()) { if (reply.isValid() && reply.value()) {
QAction *action = new QAction(tr("Power Off"), &powerMenu); QAction *action = new QAction(tr("Power Off"), powerMenu);
action->setToolTip(tr("<p>Close all applications, and then turn off the computer</p>")); action->setToolTip(tr("<p>Close all applications, and then turn off the computer</p>"));
connect(action, &QAction::triggered, &powerMenu, [] { connect(action, &QAction::triggered, powerMenu, [] {
AppManager::instance()->launchBinaryApp("ukui-session-tools", "--shutdown"); AppManager::instance()->launchBinaryApp("ukui-session-tools", "--shutdown");
}); });
powerMenu.addAction(action); powerMenu->addAction(action);
} }
powerMenu.setToolTipsVisible(true); m_contextMenu = powerMenu;
powerMenu.setAttribute(Qt::WA_DeleteOnClose); powerMenu->setToolTipsVisible(true);
powerMenu->setAttribute(Qt::WA_DeleteOnClose);
if (isFullScreen) { 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 { } else {
powerMenu.exec(QPoint(menuX, menuY - powerMenu.sizeHint().height())); m_contextMenu.data()->popup(QPoint(menuX, menuY - powerMenu->sizeHint().height()));
} }
} }

View File

@ -20,6 +20,8 @@
#include <QObject> #include <QObject>
#include <QDBusInterface> #include <QDBusInterface>
#include <QMenu>
#include <QPointer>
namespace UkuiMenu { namespace UkuiMenu {
@ -43,6 +45,8 @@ private:
bool hasMultipleUsers(); bool hasMultipleUsers();
bool canSwitch(); bool canSwitch();
QPointer<QMenu> m_contextMenu;
Q_SIGNALS: Q_SIGNALS:
void iconChanged(); void iconChanged();
void toolTipChanged(); void toolTipChanged();