scripting: Prefer VirtualDesktop objects

This commit is contained in:
Vlad Zahorodnii 2023-02-02 22:55:42 +02:00
parent 0218a8f3ba
commit 9371fc8ae9
10 changed files with 45 additions and 57 deletions

View file

@ -38,8 +38,8 @@ Rectangle {
organized = false; organized = false;
} }
function switchTo(desktopId) { function switchTo(desktop) {
KWinComponents.Workspace.currentDesktop = desktopId; KWinComponents.Workspace.currentDesktop = desktop;
effect.deactivate(effect.animationDuration); effect.deactivate(effect.animationDuration);
} }
@ -98,11 +98,15 @@ Rectangle {
} else if (event.key === Qt.Key_Minus) { } else if (event.key === Qt.Key_Minus) {
removeButton.clicked(); removeButton.clicked();
} else if (event.key >= Qt.Key_F1 && event.key <= Qt.Key_F12) { } else if (event.key >= Qt.Key_F1 && event.key <= Qt.Key_F12) {
const desktopId = (event.key - Qt.Key_F1) + 1; const desktopId = event.key - Qt.Key_F1;
switchTo(desktopId); if (desktopId < gridRepeater.count) {
switchTo(gridRepeater.itemAt(desktopId).desktop);
}
} else if (event.key >= Qt.Key_0 && event.key <= Qt.Key_9) { } 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); const desktopId = event.key === Qt.Key_0 ? 9 : (event.key - Qt.Key_1);
switchTo(desktopId); if (desktopId < gridRepeater.count) {
switchTo(gridRepeater.itemAt(desktopId).desktop);
}
} else if (event.key === Qt.Key_Up) { } else if (event.key === Qt.Key_Up) {
event.accepted = selectNext(WindowHeap.Direction.Up); event.accepted = selectNext(WindowHeap.Direction.Up);
if (!event.accepted) { if (!event.accepted) {
@ -138,7 +142,7 @@ Rectangle {
} else if (event.key === Qt.Key_Return || event.key === Qt.Key_Space) { } else if (event.key === Qt.Key_Return || event.key === Qt.Key_Space) {
for (let i = 0; i < gridRepeater.count; i++) { for (let i = 0; i < gridRepeater.count; i++) {
if (gridRepeater.itemAt(i).focus) { if (gridRepeater.itemAt(i).focus) {
switchTo(gridRepeater.itemAt(i).desktop.x11DesktopNumber) switchTo(gridRepeater.itemAt(i).desktop)
break; break;
} }
} }
@ -249,7 +253,7 @@ Rectangle {
id: thumbnail id: thumbnail
panelOpacity: grid.panelOpacity panelOpacity: grid.panelOpacity
readonly property bool current: KWinComponents.Workspace.currentVirtualDesktop === desktop readonly property bool current: KWinComponents.Workspace.currentDesktop === desktop
z: dragActive ? 1 : 0 z: dragActive ? 1 : 0
onCurrentChanged: { onCurrentChanged: {
if (current) { if (current) {
@ -278,7 +282,7 @@ Rectangle {
TapHandler { TapHandler {
acceptedButtons: Qt.LeftButton acceptedButtons: Qt.LeftButton
onTapped: { onTapped: {
KWinComponents.Workspace.currentVirtualDesktop = thumbnail.desktop; KWinComponents.Workspace.currentDesktop = thumbnail.desktop;
container.effect.deactivate(container.effect.animationDuration); container.effect.deactivate(container.effect.animationDuration);
} }
} }

View file

@ -131,7 +131,7 @@ Item {
} }
ScriptAction { ScriptAction {
script: { script: {
KWinComponents.Workspace.currentVirtualDesktop = delegate.desktop; KWinComponents.Workspace.currentDesktop = delegate.desktop;
effect.quickDeactivate(); effect.quickDeactivate();
} }
} }

View file

@ -77,7 +77,7 @@ FocusScope {
KWinComponents.DesktopBackgroundItem { KWinComponents.DesktopBackgroundItem {
id: backgroundItem id: backgroundItem
activity: KWinComponents.Workspace.currentActivity activity: KWinComponents.Workspace.currentActivity
desktop: KWinComponents.Workspace.currentVirtualDesktop desktop: KWinComponents.Workspace.currentDesktop
outputName: targetScreen.name outputName: targetScreen.name
property real blurRadius: 0 property real blurRadius: 0
@ -185,7 +185,7 @@ FocusScope {
anchors.fill: parent anchors.fill: parent
clientModel: stackModel clientModel: stackModel
desktopModel: desktopModel desktopModel: desktopModel
selectedDesktop: KWinComponents.Workspace.currentVirtualDesktop selectedDesktop: KWinComponents.Workspace.currentDesktop
heap: heap heap: heap
} }
} }
@ -240,7 +240,7 @@ FocusScope {
Keys.forwardTo: searchResults Keys.forwardTo: searchResults
model: KWinComponents.ClientFilterModel { model: KWinComponents.ClientFilterModel {
activity: KWinComponents.Workspace.currentActivity activity: KWinComponents.Workspace.currentActivity
desktop: KWinComponents.Workspace.currentVirtualDesktop desktop: KWinComponents.Workspace.currentDesktop
screenName: targetScreen.name screenName: targetScreen.name
clientModel: stackModel clientModel: stackModel
filter: effect.searchText filter: effect.searchText
@ -295,7 +295,7 @@ FocusScope {
Repeater { Repeater {
model: KWinComponents.ClientFilterModel { model: KWinComponents.ClientFilterModel {
desktop: KWinComponents.Workspace.currentVirtualDesktop desktop: KWinComponents.Workspace.currentDesktop
screenName: targetScreen.name screenName: targetScreen.name
clientModel: stackModel clientModel: stackModel
windowType: KWinComponents.ClientFilterModel.Dock windowType: KWinComponents.ClientFilterModel.Dock

View file

@ -22,8 +22,8 @@ Item {
readonly property bool selected: windowHeap.selectedIndex === index readonly property bool selected: windowHeap.selectedIndex === index
// -1 is a special value which means "All Desktops" // no desktops is a special value which means "All Desktops"
readonly property bool presentOnCurrentDesktop: client.desktop === KWinComponents.Workspace.currentDesktop || client.desktop === -1 readonly property bool presentOnCurrentDesktop: !client.desktops.length || client.desktops.indexOf(KWinComponents.Workspace.currentDesktop) !== -1
readonly property bool initialHidden: client.minimized || !presentOnCurrentDesktop readonly property bool initialHidden: client.minimized || !presentOnCurrentDesktop
readonly property bool activeHidden: { readonly property bool activeHidden: {
if (windowHeap.showOnly === "activeClass") { if (windowHeap.showOnly === "activeClass") {

View file

@ -53,7 +53,7 @@ FocusScope {
Repeater { Repeater {
model: KWinComponents.ClientFilterModel { model: KWinComponents.ClientFilterModel {
activity: KWinComponents.Workspace.currentActivity activity: KWinComponents.Workspace.currentActivity
desktop: KWinComponents.Workspace.currentVirtualDesktop desktop: KWinComponents.Workspace.currentDesktop
screenName: targetScreen.name screenName: targetScreen.name
clientModel: KWinComponents.ClientModel {} clientModel: KWinComponents.ClientModel {}
} }

View file

@ -64,7 +64,7 @@ Item {
KWinComponents.DesktopBackgroundItem { KWinComponents.DesktopBackgroundItem {
activity: KWinComponents.Workspace.currentActivity activity: KWinComponents.Workspace.currentActivity
desktop: KWinComponents.Workspace.currentVirtualDesktop desktop: KWinComponents.Workspace.currentDesktop
outputName: targetScreen.name outputName: targetScreen.name
layer.enabled: true layer.enabled: true
@ -192,7 +192,7 @@ Item {
switch (container.effect.mode) { switch (container.effect.mode) {
case WindowView.ModeCurrentDesktop: case WindowView.ModeCurrentDesktop:
case WindowView.ModeWindowClassCurrentDesktop: case WindowView.ModeWindowClassCurrentDesktop:
return KWinComponents.Workspace.currentVirtualDesktop; return KWinComponents.Workspace.currentDesktop;
default: default:
return undefined; return undefined;
} }
@ -219,7 +219,7 @@ Item {
asynchronous: true asynchronous: true
model: KWinComponents.ClientFilterModel { model: KWinComponents.ClientFilterModel {
desktop: KWinComponents.Workspace.currentVirtualDesktop desktop: KWinComponents.Workspace.currentDesktop
screenName: targetScreen.name screenName: targetScreen.name
clientModel: stackModel clientModel: stackModel
windowType: KWinComponents.ClientFilterModel.Dock windowType: KWinComponents.ClientFilterModel.Dock

View file

@ -151,6 +151,7 @@ KWin::Script::Script(int id, QString scriptName, QString pluginName, QObject *pa
} }
qRegisterMetaType<QList<KWin::Window *>>(); qRegisterMetaType<QList<KWin::Window *>>();
qRegisterMetaType<QVector<KWin::VirtualDesktop *>>();
} }
KWin::Script::~Script() KWin::Script::~Script()

View file

@ -34,9 +34,10 @@ WorkspaceWrapper::WorkspaceWrapper(QObject *parent)
connect(ws, &Workspace::windowAdded, this, &WorkspaceWrapper::setupClientConnections); connect(ws, &Workspace::windowAdded, this, &WorkspaceWrapper::setupClientConnections);
connect(ws, &Workspace::windowRemoved, this, &WorkspaceWrapper::clientRemoved); connect(ws, &Workspace::windowRemoved, this, &WorkspaceWrapper::clientRemoved);
connect(ws, &Workspace::windowActivated, this, &WorkspaceWrapper::clientActivated); 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::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); connect(ws, &Workspace::windowDemandsAttentionChanged, this, &WorkspaceWrapper::clientDemandsAttentionChanged);
#if KWIN_BUILD_ACTIVITIES #if KWIN_BUILD_ACTIVITIES
if (KWin::Activities *activities = ws->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(); return VirtualDesktopManager::self()->currentDesktop();
} }
int WorkspaceWrapper::numberOfDesktops() const QVector<VirtualDesktop *> 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); VirtualDesktopManager::self()->setCurrent(desktop);
} }
void WorkspaceWrapper::setNumberOfDesktops(int count)
{
VirtualDesktopManager::self()->setCount(count);
}
Window *WorkspaceWrapper::activeClient() const Window *WorkspaceWrapper::activeClient() const
{ {
return workspace()->activeWindow(); return workspace()->activeWindow();
@ -274,14 +270,9 @@ void WorkspaceWrapper::createDesktop(int position, const QString &name) const
VirtualDesktopManager::self()->createVirtualDesktop(position, name); VirtualDesktopManager::self()->createVirtualDesktop(position, name);
} }
void WorkspaceWrapper::removeDesktop(int position) const void WorkspaceWrapper::removeDesktop(VirtualDesktop *desktop) const
{ {
VirtualDesktop *vd = VirtualDesktopManager::self()->desktopForX11Id(position + 1); VirtualDesktopManager::self()->removeVirtualDesktop(desktop->id());
if (!vd) {
return;
}
VirtualDesktopManager::self()->removeVirtualDesktop(vd->id());
} }
QString WorkspaceWrapper::supportInformation() const QString WorkspaceWrapper::supportInformation() const

View file

@ -29,7 +29,8 @@ class X11Window;
class WorkspaceWrapper : public QObject class WorkspaceWrapper : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(KWin::VirtualDesktop *currentVirtualDesktop READ currentVirtualDesktop WRITE setCurrentVirtualDesktop NOTIFY currentVirtualDesktopChanged) Q_PROPERTY(QVector<KWin::VirtualDesktop *> 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) Q_PROPERTY(KWin::Window *activeClient READ activeClient WRITE setActiveClient NOTIFY clientActivated)
// TODO: write and notify? // TODO: write and notify?
Q_PROPERTY(QSize desktopGridSize READ desktopGridSize NOTIFY desktopLayoutChanged) 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 workspaceWidth READ workspaceWidth)
Q_PROPERTY(int workspaceHeight READ workspaceHeight) Q_PROPERTY(int workspaceHeight READ workspaceHeight)
Q_PROPERTY(QSize workspaceSize READ workspaceSize) 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 activeScreen READ activeScreen)
Q_PROPERTY(int numScreens READ numScreens NOTIFY numberScreensChanged) Q_PROPERTY(int numScreens READ numScreens NOTIFY numberScreensChanged)
Q_PROPERTY(QString currentActivity READ currentActivity WRITE setCurrentActivity NOTIFY currentActivityChanged) 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 clientFullScreenSet(KWin::X11Window *client, bool fullScreen, bool user);
void clientSetKeepAbove(KWin::X11Window *client, bool keepAbove); void clientSetKeepAbove(KWin::X11Window *client, bool keepAbove);
/** /**
* Signal emitted whenever the number of desktops changed. * This signal is emitted when a virtual desktop is added or removed.
* To get the current number of desktops use the property desktops.
* @param oldNumberOfDesktops The previous number of desktops.
*/ */
void numberDesktopsChanged(uint oldNumberOfDesktops); void desktopsChanged();
/** /**
* Signal emitted whenever the layout of virtual desktops changed. * Signal emitted whenever the layout of virtual desktops changed.
* That is desktopGrid(Size/Width/Height) will have new values. * That is desktopGrid(Size/Width/Height) will have new values.
@ -137,10 +132,8 @@ Q_SIGNALS:
void virtualScreenGeometryChanged(); void virtualScreenGeometryChanged();
/** /**
* This signal is emitted when the current virtual desktop changes. * 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. * This signal is emitted when the cursor position changes.
* @see cursorPos() * @see cursorPos()
@ -191,7 +184,6 @@ public:
#define GETTERSETTERDEF(rettype, getter, setter) \ #define GETTERSETTERDEF(rettype, getter, setter) \
rettype getter() const; \ rettype getter() const; \
void setter(rettype val); void setter(rettype val);
GETTERSETTERDEF(int, numberOfDesktops, setNumberOfDesktops)
GETTERSETTERDEF(QString, currentActivity, setCurrentActivity) GETTERSETTERDEF(QString, currentActivity, setCurrentActivity)
GETTERSETTERDEF(KWin::Window *, activeClient, setActiveClient) GETTERSETTERDEF(KWin::Window *, activeClient, setActiveClient)
#undef GETTERSETTERDEF #undef GETTERSETTERDEF
@ -208,8 +200,9 @@ public:
QRect virtualScreenGeometry() const; QRect virtualScreenGeometry() const;
QPoint cursorPos() const; QPoint cursorPos() const;
VirtualDesktop *currentVirtualDesktop() const; QVector<VirtualDesktop *> desktops() const;
void setCurrentVirtualDesktop(VirtualDesktop *desktop); VirtualDesktop *currentDesktop() const;
void setCurrentDesktop(VirtualDesktop *desktop);
Q_INVOKABLE int screenAt(const QPointF &pos) const; Q_INVOKABLE int screenAt(const QPointF &pos) const;
@ -249,10 +242,9 @@ public:
*/ */
Q_SCRIPTABLE void createDesktop(int position, const QString &name) const; Q_SCRIPTABLE void createDesktop(int position, const QString &name) const;
/** /**
* Remove the virtual desktop at the requested position * Removes the specified virtual desktop.
* @param position The position of the desktop to be removed. It should be in range [0, count - 1].
*/ */
Q_SCRIPTABLE void removeDesktop(int position) const; Q_SCRIPTABLE void removeDesktop(KWin::VirtualDesktop *desktop) const;
/** /**
* Provides support information about the currently running KWin instance. * Provides support information about the currently running KWin instance.
*/ */

View file

@ -11,7 +11,7 @@ var registeredBorders = [];
function isRelevant(client) { function isRelevant(client) {
return client.minimizable && 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); (!client.activities.length || client.activities.indexOf(workspace.currentActivity.toString()) > -1);
} }