From 7937aa78efbcca8c6e78e420ecbb3e02a98a3995 Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Tue, 17 Aug 2021 09:42:09 +0300 Subject: [PATCH] Port X11 desktop user actions menu to VirtualDesktop It's mostly identical to the virtual desktop user actions menu on Wayland. --- src/useractions.cpp | 66 +++++++++++++++++++++------------------------ src/useractions.h | 6 ----- 2 files changed, 31 insertions(+), 41 deletions(-) diff --git a/src/useractions.cpp b/src/useractions.cpp index 6785831085..e084a1134e 100644 --- a/src/useractions.cpp +++ b/src/useractions.cpp @@ -455,7 +455,6 @@ void UserActionsMenu::initDesktopPopup() return; m_desktopMenu = new QMenu(m_menu); - connect(m_desktopMenu, &QMenu::triggered, this, &UserActionsMenu::slotSendToDesktop); connect(m_desktopMenu, &QMenu::aboutToShow, this, &UserActionsMenu::desktopPopupAboutToShow); QAction *action = m_desktopMenu->menuAction(); @@ -511,8 +510,15 @@ void UserActionsMenu::desktopPopupAboutToShow() } QActionGroup *group = new QActionGroup(m_desktopMenu); QAction *action = m_desktopMenu->addAction(i18n("&All Desktops")); - action->setData(0); + connect(action, &QAction::triggered, this, [this]() { + if (m_client) { + m_client->setOnAllDesktops(!m_client->isOnAllDesktops()); + } + }); action->setCheckable(true); + if (m_client && m_client->isOnAllDesktops()) { + action->setChecked(true); + } group->addAction(action); if (m_client && m_client->isOnAllDesktops()) { @@ -522,27 +528,41 @@ void UserActionsMenu::desktopPopupAboutToShow() const uint BASE = 10; - for (uint i = 1; i <= vds->count(); ++i) { + const auto desktops = vds->desktops(); + for (VirtualDesktop *desktop : desktops) { + const uint legacyId = desktop->x11DesktopNumber(); + QString basic_name(QStringLiteral("%1 %2")); - if (i < BASE) { + if (legacyId < BASE) { basic_name.prepend(QLatin1Char('&')); } - action = m_desktopMenu->addAction(basic_name.arg(i).arg(vds->name(i).replace(QLatin1Char('&'), QStringLiteral("&&")))); - action->setData(i); + action = m_desktopMenu->addAction(basic_name.arg(legacyId).arg(desktop->name().replace(QLatin1Char('&'), QStringLiteral("&&")))); + connect(action, &QAction::triggered, this, [this, desktop]() { + if (m_client) { + workspace()->sendClientToDesktop(m_client, desktop->x11DesktopNumber(), false); + } + }); action->setCheckable(true); group->addAction(action); - if (m_client && !m_client->isOnAllDesktops() && m_client->isOnDesktop(i)) { + if (m_client && !m_client->isOnAllDesktops() && m_client->isOnDesktop(desktop)) { action->setChecked(true); } } m_desktopMenu->addSeparator(); action = m_desktopMenu->addAction(i18nc("Create a new desktop and move the window there", "&New Desktop")); - action->setData(vds->count() + 1); - - if (vds->count() >= vds->maximum()) - action->setEnabled(false); + connect(action, &QAction::triggered, this, [this]() { + if (!m_client) { + return; + } + VirtualDesktopManager *vds = VirtualDesktopManager::self(); + VirtualDesktop *desktop = vds->createVirtualDesktop(vds->count()); + if (desktop) { + workspace()->sendClientToDesktop(m_client, desktop->x11DesktopNumber(), false); + } + }); + action->setEnabled(vds->count() < vds->maximum()); } void UserActionsMenu::multipleDesktopsPopupAboutToShow() @@ -748,30 +768,6 @@ void UserActionsMenu::slotWindowOperation(QAction *action) Q_ARG(Options::WindowOperation, op)); } -void UserActionsMenu::slotSendToDesktop(QAction *action) -{ - bool ok = false; - uint desk = action->data().toUInt(&ok); - if (!ok) { - return; - } - if (m_client.isNull()) - return; - Workspace *ws = Workspace::self(); - VirtualDesktopManager *vds = VirtualDesktopManager::self(); - if (desk == 0) { - // the 'on_all_desktops' menu entry - if (m_client) { - m_client->setOnAllDesktops(!m_client->isOnAllDesktops()); - } - return; - } else if (desk > vds->count()) { - vds->setCount(desk); - } - - ws->sendClientToDesktop(m_client.data(), desk, false); -} - void UserActionsMenu::slotSendToScreen(QAction *action) { const int screen = action->data().toInt(); diff --git a/src/useractions.h b/src/useractions.h index 2351354e82..f5c5f29950 100644 --- a/src/useractions.h +++ b/src/useractions.h @@ -128,12 +128,6 @@ private Q_SLOTS: * the Client. */ void activityPopupAboutToShow(); - /** - * Sends the client to desktop \a desk - * - * @param action Invoked Action containing the Desktop as data element - */ - void slotSendToDesktop(QAction *action); /** * Sends the Client to screen \a screen *