From 9371fc8ae987a8941e7dada3743be0edc001d11a Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Thu, 2 Feb 2023 22:55:42 +0200 Subject: [PATCH] scripting: Prefer VirtualDesktop objects --- src/effects/desktopgrid/qml/main.qml | 22 +++++++++------ src/effects/overview/qml/DesktopBar.qml | 2 +- src/effects/overview/qml/ScreenView.qml | 8 +++--- .../private/qml/WindowHeapDelegate.qml | 4 +-- src/effects/tileseditor/qml/main.qml | 2 +- src/effects/windowview/qml/main.qml | 6 ++-- src/scripting/scripting.cpp | 1 + src/scripting/workspace_wrapper.cpp | 27 ++++++------------ src/scripting/workspace_wrapper.h | 28 +++++++------------ src/scripts/minimizeall/contents/code/main.js | 2 +- 10 files changed, 45 insertions(+), 57 deletions(-) diff --git a/src/effects/desktopgrid/qml/main.qml b/src/effects/desktopgrid/qml/main.qml index e1d8ac5da8..c3972530cb 100644 --- a/src/effects/desktopgrid/qml/main.qml +++ b/src/effects/desktopgrid/qml/main.qml @@ -38,8 +38,8 @@ Rectangle { organized = false; } - function switchTo(desktopId) { - KWinComponents.Workspace.currentDesktop = desktopId; + function switchTo(desktop) { + KWinComponents.Workspace.currentDesktop = desktop; effect.deactivate(effect.animationDuration); } @@ -98,11 +98,15 @@ Rectangle { } else if (event.key === Qt.Key_Minus) { removeButton.clicked(); } else if (event.key >= Qt.Key_F1 && event.key <= Qt.Key_F12) { - const desktopId = (event.key - Qt.Key_F1) + 1; - switchTo(desktopId); + const desktopId = event.key - Qt.Key_F1; + if (desktopId < gridRepeater.count) { + switchTo(gridRepeater.itemAt(desktopId).desktop); + } } else if (event.key >= Qt.Key_0 && event.key <= Qt.Key_9) { - const desktopId = event.key === Qt.Key_0 ? 10 : (event.key - Qt.Key_0); - switchTo(desktopId); + const desktopId = event.key === Qt.Key_0 ? 9 : (event.key - Qt.Key_1); + if (desktopId < gridRepeater.count) { + switchTo(gridRepeater.itemAt(desktopId).desktop); + } } else if (event.key === Qt.Key_Up) { event.accepted = selectNext(WindowHeap.Direction.Up); if (!event.accepted) { @@ -138,7 +142,7 @@ Rectangle { } else if (event.key === Qt.Key_Return || event.key === Qt.Key_Space) { for (let i = 0; i < gridRepeater.count; i++) { if (gridRepeater.itemAt(i).focus) { - switchTo(gridRepeater.itemAt(i).desktop.x11DesktopNumber) + switchTo(gridRepeater.itemAt(i).desktop) break; } } @@ -249,7 +253,7 @@ Rectangle { id: thumbnail panelOpacity: grid.panelOpacity - readonly property bool current: KWinComponents.Workspace.currentVirtualDesktop === desktop + readonly property bool current: KWinComponents.Workspace.currentDesktop === desktop z: dragActive ? 1 : 0 onCurrentChanged: { if (current) { @@ -278,7 +282,7 @@ Rectangle { TapHandler { acceptedButtons: Qt.LeftButton onTapped: { - KWinComponents.Workspace.currentVirtualDesktop = thumbnail.desktop; + KWinComponents.Workspace.currentDesktop = thumbnail.desktop; container.effect.deactivate(container.effect.animationDuration); } } diff --git a/src/effects/overview/qml/DesktopBar.qml b/src/effects/overview/qml/DesktopBar.qml index 88616f79a1..ff357ec3a9 100644 --- a/src/effects/overview/qml/DesktopBar.qml +++ b/src/effects/overview/qml/DesktopBar.qml @@ -131,7 +131,7 @@ Item { } ScriptAction { script: { - KWinComponents.Workspace.currentVirtualDesktop = delegate.desktop; + KWinComponents.Workspace.currentDesktop = delegate.desktop; effect.quickDeactivate(); } } diff --git a/src/effects/overview/qml/ScreenView.qml b/src/effects/overview/qml/ScreenView.qml index 9d7e86a5d1..8f55e02816 100644 --- a/src/effects/overview/qml/ScreenView.qml +++ b/src/effects/overview/qml/ScreenView.qml @@ -77,7 +77,7 @@ FocusScope { KWinComponents.DesktopBackgroundItem { id: backgroundItem activity: KWinComponents.Workspace.currentActivity - desktop: KWinComponents.Workspace.currentVirtualDesktop + desktop: KWinComponents.Workspace.currentDesktop outputName: targetScreen.name property real blurRadius: 0 @@ -185,7 +185,7 @@ FocusScope { anchors.fill: parent clientModel: stackModel desktopModel: desktopModel - selectedDesktop: KWinComponents.Workspace.currentVirtualDesktop + selectedDesktop: KWinComponents.Workspace.currentDesktop heap: heap } } @@ -240,7 +240,7 @@ FocusScope { Keys.forwardTo: searchResults model: KWinComponents.ClientFilterModel { activity: KWinComponents.Workspace.currentActivity - desktop: KWinComponents.Workspace.currentVirtualDesktop + desktop: KWinComponents.Workspace.currentDesktop screenName: targetScreen.name clientModel: stackModel filter: effect.searchText @@ -295,7 +295,7 @@ FocusScope { Repeater { model: KWinComponents.ClientFilterModel { - desktop: KWinComponents.Workspace.currentVirtualDesktop + desktop: KWinComponents.Workspace.currentDesktop screenName: targetScreen.name clientModel: stackModel windowType: KWinComponents.ClientFilterModel.Dock diff --git a/src/effects/private/qml/WindowHeapDelegate.qml b/src/effects/private/qml/WindowHeapDelegate.qml index 18b3630691..0063eed973 100644 --- a/src/effects/private/qml/WindowHeapDelegate.qml +++ b/src/effects/private/qml/WindowHeapDelegate.qml @@ -22,8 +22,8 @@ Item { readonly property bool selected: windowHeap.selectedIndex === index - // -1 is a special value which means "All Desktops" - readonly property bool presentOnCurrentDesktop: client.desktop === KWinComponents.Workspace.currentDesktop || client.desktop === -1 + // no desktops is a special value which means "All Desktops" + readonly property bool presentOnCurrentDesktop: !client.desktops.length || client.desktops.indexOf(KWinComponents.Workspace.currentDesktop) !== -1 readonly property bool initialHidden: client.minimized || !presentOnCurrentDesktop readonly property bool activeHidden: { if (windowHeap.showOnly === "activeClass") { diff --git a/src/effects/tileseditor/qml/main.qml b/src/effects/tileseditor/qml/main.qml index 61c1866966..a94f67f10e 100644 --- a/src/effects/tileseditor/qml/main.qml +++ b/src/effects/tileseditor/qml/main.qml @@ -53,7 +53,7 @@ FocusScope { Repeater { model: KWinComponents.ClientFilterModel { activity: KWinComponents.Workspace.currentActivity - desktop: KWinComponents.Workspace.currentVirtualDesktop + desktop: KWinComponents.Workspace.currentDesktop screenName: targetScreen.name clientModel: KWinComponents.ClientModel {} } diff --git a/src/effects/windowview/qml/main.qml b/src/effects/windowview/qml/main.qml index ea5165ecb3..cebc8fa936 100644 --- a/src/effects/windowview/qml/main.qml +++ b/src/effects/windowview/qml/main.qml @@ -64,7 +64,7 @@ Item { KWinComponents.DesktopBackgroundItem { activity: KWinComponents.Workspace.currentActivity - desktop: KWinComponents.Workspace.currentVirtualDesktop + desktop: KWinComponents.Workspace.currentDesktop outputName: targetScreen.name layer.enabled: true @@ -192,7 +192,7 @@ Item { switch (container.effect.mode) { case WindowView.ModeCurrentDesktop: case WindowView.ModeWindowClassCurrentDesktop: - return KWinComponents.Workspace.currentVirtualDesktop; + return KWinComponents.Workspace.currentDesktop; default: return undefined; } @@ -219,7 +219,7 @@ Item { asynchronous: true model: KWinComponents.ClientFilterModel { - desktop: KWinComponents.Workspace.currentVirtualDesktop + desktop: KWinComponents.Workspace.currentDesktop screenName: targetScreen.name clientModel: stackModel windowType: KWinComponents.ClientFilterModel.Dock diff --git a/src/scripting/scripting.cpp b/src/scripting/scripting.cpp index e627dfa9fa..d3ee979907 100644 --- a/src/scripting/scripting.cpp +++ b/src/scripting/scripting.cpp @@ -151,6 +151,7 @@ KWin::Script::Script(int id, QString scriptName, QString pluginName, QObject *pa } qRegisterMetaType>(); + qRegisterMetaType>(); } KWin::Script::~Script() diff --git a/src/scripting/workspace_wrapper.cpp b/src/scripting/workspace_wrapper.cpp index ec17c9c3e6..f1009cc61c 100644 --- a/src/scripting/workspace_wrapper.cpp +++ b/src/scripting/workspace_wrapper.cpp @@ -34,9 +34,10 @@ WorkspaceWrapper::WorkspaceWrapper(QObject *parent) connect(ws, &Workspace::windowAdded, this, &WorkspaceWrapper::setupClientConnections); connect(ws, &Workspace::windowRemoved, this, &WorkspaceWrapper::clientRemoved); connect(ws, &Workspace::windowActivated, this, &WorkspaceWrapper::clientActivated); - connect(vds, &VirtualDesktopManager::countChanged, this, &WorkspaceWrapper::numberDesktopsChanged); + connect(vds, &VirtualDesktopManager::desktopCreated, this, &WorkspaceWrapper::desktopsChanged); + connect(vds, &VirtualDesktopManager::desktopRemoved, this, &WorkspaceWrapper::desktopsChanged); connect(vds, &VirtualDesktopManager::layoutChanged, this, &WorkspaceWrapper::desktopLayoutChanged); - connect(vds, &VirtualDesktopManager::currentChanged, this, &WorkspaceWrapper::currentVirtualDesktopChanged); + connect(vds, &VirtualDesktopManager::currentChanged, this, &WorkspaceWrapper::currentDesktopChanged); connect(ws, &Workspace::windowDemandsAttentionChanged, this, &WorkspaceWrapper::clientDemandsAttentionChanged); #if KWIN_BUILD_ACTIVITIES if (KWin::Activities *activities = ws->activities()) { @@ -63,26 +64,21 @@ WorkspaceWrapper::WorkspaceWrapper(QObject *parent) } } -VirtualDesktop *WorkspaceWrapper::currentVirtualDesktop() const +VirtualDesktop *WorkspaceWrapper::currentDesktop() const { return VirtualDesktopManager::self()->currentDesktop(); } -int WorkspaceWrapper::numberOfDesktops() const +QVector WorkspaceWrapper::desktops() const { - return VirtualDesktopManager::self()->count(); + return VirtualDesktopManager::self()->desktops(); } -void WorkspaceWrapper::setCurrentVirtualDesktop(VirtualDesktop *desktop) +void WorkspaceWrapper::setCurrentDesktop(VirtualDesktop *desktop) { VirtualDesktopManager::self()->setCurrent(desktop); } -void WorkspaceWrapper::setNumberOfDesktops(int count) -{ - VirtualDesktopManager::self()->setCount(count); -} - Window *WorkspaceWrapper::activeClient() const { return workspace()->activeWindow(); @@ -274,14 +270,9 @@ void WorkspaceWrapper::createDesktop(int position, const QString &name) const VirtualDesktopManager::self()->createVirtualDesktop(position, name); } -void WorkspaceWrapper::removeDesktop(int position) const +void WorkspaceWrapper::removeDesktop(VirtualDesktop *desktop) const { - VirtualDesktop *vd = VirtualDesktopManager::self()->desktopForX11Id(position + 1); - if (!vd) { - return; - } - - VirtualDesktopManager::self()->removeVirtualDesktop(vd->id()); + VirtualDesktopManager::self()->removeVirtualDesktop(desktop->id()); } QString WorkspaceWrapper::supportInformation() const diff --git a/src/scripting/workspace_wrapper.h b/src/scripting/workspace_wrapper.h index 35dd3710c6..d9b68516ff 100644 --- a/src/scripting/workspace_wrapper.h +++ b/src/scripting/workspace_wrapper.h @@ -29,7 +29,8 @@ class X11Window; class WorkspaceWrapper : public QObject { Q_OBJECT - Q_PROPERTY(KWin::VirtualDesktop *currentVirtualDesktop READ currentVirtualDesktop WRITE setCurrentVirtualDesktop NOTIFY currentVirtualDesktopChanged) + Q_PROPERTY(QVector desktops READ desktops NOTIFY desktopsChanged) + Q_PROPERTY(KWin::VirtualDesktop *currentDesktop READ currentDesktop WRITE setCurrentDesktop NOTIFY currentDesktopChanged) Q_PROPERTY(KWin::Window *activeClient READ activeClient WRITE setActiveClient NOTIFY clientActivated) // TODO: write and notify? Q_PROPERTY(QSize desktopGridSize READ desktopGridSize NOTIFY desktopLayoutChanged) @@ -38,10 +39,6 @@ class WorkspaceWrapper : public QObject Q_PROPERTY(int workspaceWidth READ workspaceWidth) Q_PROPERTY(int workspaceHeight READ workspaceHeight) Q_PROPERTY(QSize workspaceSize READ workspaceSize) - /** - * The number of desktops currently used. Minimum number of desktops is 1, maximum 20. - */ - Q_PROPERTY(int desktops READ numberOfDesktops WRITE setNumberOfDesktops NOTIFY numberDesktopsChanged) Q_PROPERTY(int activeScreen READ activeScreen) Q_PROPERTY(int numScreens READ numScreens NOTIFY numberScreensChanged) Q_PROPERTY(QString currentActivity READ currentActivity WRITE setCurrentActivity NOTIFY currentActivityChanged) @@ -80,11 +77,9 @@ Q_SIGNALS: void clientFullScreenSet(KWin::X11Window *client, bool fullScreen, bool user); void clientSetKeepAbove(KWin::X11Window *client, bool keepAbove); /** - * Signal emitted whenever the number of desktops changed. - * To get the current number of desktops use the property desktops. - * @param oldNumberOfDesktops The previous number of desktops. + * This signal is emitted when a virtual desktop is added or removed. */ - void numberDesktopsChanged(uint oldNumberOfDesktops); + void desktopsChanged(); /** * Signal emitted whenever the layout of virtual desktops changed. * That is desktopGrid(Size/Width/Height) will have new values. @@ -137,10 +132,8 @@ Q_SIGNALS: void virtualScreenGeometryChanged(); /** * This signal is emitted when the current virtual desktop changes. - * - * @since 5.23 */ - void currentVirtualDesktopChanged(); + void currentDesktopChanged(); /** * This signal is emitted when the cursor position changes. * @see cursorPos() @@ -191,7 +184,6 @@ public: #define GETTERSETTERDEF(rettype, getter, setter) \ rettype getter() const; \ void setter(rettype val); - GETTERSETTERDEF(int, numberOfDesktops, setNumberOfDesktops) GETTERSETTERDEF(QString, currentActivity, setCurrentActivity) GETTERSETTERDEF(KWin::Window *, activeClient, setActiveClient) #undef GETTERSETTERDEF @@ -208,8 +200,9 @@ public: QRect virtualScreenGeometry() const; QPoint cursorPos() const; - VirtualDesktop *currentVirtualDesktop() const; - void setCurrentVirtualDesktop(VirtualDesktop *desktop); + QVector desktops() const; + VirtualDesktop *currentDesktop() const; + void setCurrentDesktop(VirtualDesktop *desktop); Q_INVOKABLE int screenAt(const QPointF &pos) const; @@ -249,10 +242,9 @@ public: */ Q_SCRIPTABLE void createDesktop(int position, const QString &name) const; /** - * Remove the virtual desktop at the requested position - * @param position The position of the desktop to be removed. It should be in range [0, count - 1]. + * Removes the specified virtual desktop. */ - Q_SCRIPTABLE void removeDesktop(int position) const; + Q_SCRIPTABLE void removeDesktop(KWin::VirtualDesktop *desktop) const; /** * Provides support information about the currently running KWin instance. */ diff --git a/src/scripts/minimizeall/contents/code/main.js b/src/scripts/minimizeall/contents/code/main.js index fd4aa11588..d3da871378 100644 --- a/src/scripts/minimizeall/contents/code/main.js +++ b/src/scripts/minimizeall/contents/code/main.js @@ -11,7 +11,7 @@ var registeredBorders = []; function isRelevant(client) { return client.minimizable && - (client.onAllDesktops || client.desktop === workspace.currentDesktop); + (!client.desktops.length || client.desktops.indexOf(workspace.currentDesktop) != -1); (!client.activities.length || client.activities.indexOf(workspace.currentActivity.toString()) > -1); }