From a5ac7cf0ae1848da015bec10465cbdf04b03bccb Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Fri, 26 Aug 2022 09:34:19 +0300 Subject: [PATCH] scripting: Port ClientModel V2 from Screens --- autotests/integration/plasma_surface_test.cpp | 5 -- autotests/integration/pointer_input.cpp | 3 - autotests/integration/screen_changes_test.cpp | 8 --- autotests/integration/screens_test.cpp | 23 -------- src/screens.cpp | 28 +--------- src/screens.h | 8 --- src/scripting/v2/clientmodel.cpp | 55 ++++++++++--------- src/scripting/v2/clientmodel.h | 3 +- 8 files changed, 32 insertions(+), 101 deletions(-) diff --git a/autotests/integration/plasma_surface_test.cpp b/autotests/integration/plasma_surface_test.cpp index 11ce014ae7..be8bea1ff5 100644 --- a/autotests/integration/plasma_surface_test.cpp +++ b/autotests/integration/plasma_surface_test.cpp @@ -11,7 +11,6 @@ #include "cursor.h" #include "output.h" #include "platform.h" -#include "screens.h" #include "virtualdesktops.h" #include "wayland_server.h" #include "window.h" @@ -195,15 +194,11 @@ void PlasmaSurfaceTest::testOSDPlacement() QCOMPARE(window->frameGeometry(), QRect(1280 / 2 - 100 / 2, 2 * 1024 / 3 - 50 / 2, 100, 50)); // change the screen size - QSignalSpy screensChangedSpy(workspace()->screens(), &Screens::changed); - QVERIFY(screensChangedSpy.isValid()); const QVector geometries{QRect(0, 0, 1280, 1024), QRect(1280, 0, 1280, 1024)}; QMetaObject::invokeMethod(kwinApp()->platform(), "setVirtualOutputs", Qt::DirectConnection, Q_ARG(int, 2), Q_ARG(QVector, geometries)); - QVERIFY(screensChangedSpy.wait()); - QCOMPARE(screensChangedSpy.count(), 2); const auto outputs = workspace()->outputs(); QCOMPARE(outputs.count(), 2); QCOMPARE(outputs[0]->geometry(), geometries[0]); diff --git a/autotests/integration/pointer_input.cpp b/autotests/integration/pointer_input.cpp index 71a69e52fa..88398ff488 100644 --- a/autotests/integration/pointer_input.cpp +++ b/autotests/integration/pointer_input.cpp @@ -390,14 +390,11 @@ void PointerInputTest::testUpdateFocusAfterScreenChange() QVERIFY(!window->frameGeometry().contains(Cursors::self()->mouse()->pos())); QVERIFY(leftSpy.wait()); - QSignalSpy screensChangedSpy(workspace()->screens(), &Screens::changed); - QVERIFY(screensChangedSpy.isValid()); // now let's remove the screen containing the cursor QMetaObject::invokeMethod(kwinApp()->platform(), "setVirtualOutputs", Qt::DirectConnection, Q_ARG(int, 1), Q_ARG(QVector, QVector{QRect(0, 0, 1280, 1024)})); - QVERIFY(screensChangedSpy.wait()); QCOMPARE(workspace()->outputs().count(), 1); // this should have warped the cursor diff --git a/autotests/integration/screen_changes_test.cpp b/autotests/integration/screen_changes_test.cpp index 1e24fe5946..f8233d8e00 100644 --- a/autotests/integration/screen_changes_test.cpp +++ b/autotests/integration/screen_changes_test.cpp @@ -11,7 +11,6 @@ #include "cursor.h" #include "output.h" #include "platform.h" -#include "screens.h" #include "wayland_server.h" #include "workspace.h" @@ -87,15 +86,11 @@ void ScreenChangesTest::testScreenAddRemove() outputAnnouncedSpy.clear(); // let's announce a new output - QSignalSpy screensChangedSpy(workspace()->screens(), &Screens::changed); - QVERIFY(screensChangedSpy.isValid()); const QVector geometries{QRect(0, 0, 1280, 1024), QRect(1280, 0, 1280, 1024)}; QMetaObject::invokeMethod(kwinApp()->platform(), "setVirtualOutputs", Qt::DirectConnection, Q_ARG(int, 2), Q_ARG(QVector, geometries)); - QVERIFY(screensChangedSpy.wait()); - QCOMPARE(screensChangedSpy.count(), 2); auto outputs = workspace()->outputs(); QCOMPARE(outputs.count(), 2); QCOMPARE(outputs[0]->geometry(), geometries[0]); @@ -154,7 +149,6 @@ void ScreenChangesTest::testScreenAddRemove() // now let's try to remove one output again outputAnnouncedSpy.clear(); outputRemovedSpy.clear(); - screensChangedSpy.clear(); QSignalSpy o1RemovedSpy(o1.get(), &KWayland::Client::Output::removed); QVERIFY(o1RemovedSpy.isValid()); @@ -166,8 +160,6 @@ void ScreenChangesTest::testScreenAddRemove() Qt::DirectConnection, Q_ARG(int, 1), Q_ARG(QVector, geometries2)); - QVERIFY(screensChangedSpy.wait()); - QCOMPARE(screensChangedSpy.count(), 2); outputs = workspace()->outputs(); QCOMPARE(outputs.count(), 1); QCOMPARE(outputs[0]->geometry(), geometries2.at(0)); diff --git a/autotests/integration/screens_test.cpp b/autotests/integration/screens_test.cpp index f577f3f3e6..16ffbd7543 100644 --- a/autotests/integration/screens_test.cpp +++ b/autotests/integration/screens_test.cpp @@ -34,7 +34,6 @@ private Q_SLOTS: void cleanup(); void testSize_data(); void testSize(); - void testCount(); void testCurrent_data(); void testCurrent(); void testCurrentWithFollowsMouse_data(); @@ -118,28 +117,6 @@ void ScreensTest::testSize() QTEST(workspace()->screens()->size(), "expectedSize"); } -void ScreensTest::testCount() -{ - QSignalSpy countChangedSpy(workspace()->screens(), &Screens::countChanged); - QVERIFY(countChangedSpy.isValid()); - - // the test environments has two outputs - QCOMPARE(workspace()->screens()->count(), 2); - - // change to one screen - QMetaObject::invokeMethod(kwinApp()->platform(), "setVirtualOutputs", Qt::QueuedConnection, Q_ARG(int, 1)); - QVERIFY(countChangedSpy.wait()); - QCOMPARE(countChangedSpy.count(), 1); - QCOMPARE(workspace()->screens()->count(), 1); - - // setting the same geometries shouldn't emit the signal, but we should get a changed signal - QSignalSpy changedSpy(workspace()->screens(), &Screens::changed); - QVERIFY(changedSpy.isValid()); - QMetaObject::invokeMethod(kwinApp()->platform(), "setVirtualOutputs", Qt::QueuedConnection, Q_ARG(int, 1)); - QVERIFY(changedSpy.wait()); - QCOMPARE(countChangedSpy.count(), 1); -} - void ScreensTest::testCurrent_data() { QTest::addColumn("currentId"); diff --git a/src/screens.cpp b/src/screens.cpp index 8b478253a5..f91ff97d8e 100644 --- a/src/screens.cpp +++ b/src/screens.cpp @@ -22,17 +22,13 @@ namespace KWin { Screens::Screens() - : m_count(0) - , m_maxScale(1.0) + : m_maxScale(1.0) { - connect(kwinApp()->platform(), &Platform::screensQueried, this, &Screens::updateCount); connect(kwinApp()->platform(), &Platform::screensQueried, this, &Screens::changed); } void Screens::init() { - updateCount(); - connect(this, &Screens::countChanged, this, &Screens::changed, Qt::QueuedConnection); connect(this, &Screens::changed, this, &Screens::updateSize); connect(this, &Screens::sizeChanged, this, &Screens::geometryChanged); @@ -64,7 +60,7 @@ void Screens::updateSize() { QRect bounding; qreal maxScale = 1.0; - for (int i = 0; i < count(); ++i) { + for (int i = 0; i < workspace()->outputs().count(); ++i) { bounding = bounding.united(geometry(i)); maxScale = qMax(maxScale, scale(i)); } @@ -78,31 +74,11 @@ void Screens::updateSize() } } -void Screens::updateCount() -{ - setCount(workspace()->outputs().size()); -} - -void Screens::setCount(int count) -{ - if (m_count == count) { - return; - } - const int previous = m_count; - m_count = count; - Q_EMIT countChanged(previous, count); -} - Output *Screens::findOutput(int screen) const { return workspace()->outputs().value(screen); } -int Screens::count() const -{ - return m_count; -} - QSize Screens::size() const { return m_boundingSize; diff --git a/src/screens.h b/src/screens.h index 024789b0d4..93a3817b4b 100644 --- a/src/screens.h +++ b/src/screens.h @@ -29,14 +29,12 @@ class Platform; class KWIN_EXPORT Screens : public QObject { Q_OBJECT - Q_PROPERTY(int count READ count WRITE setCount NOTIFY countChanged) public: explicit Screens(); void init(); - int count() const; QRect geometry(int screen) const; /** * The bounding geometry of all screens combined. Overlapping areas @@ -67,7 +65,6 @@ public: QSize size() const; Q_SIGNALS: - void countChanged(int previousCount, int newCount); /** * Emitted whenever the screens are changed either count or geometry. */ @@ -90,17 +87,12 @@ Q_SIGNALS: */ void maxScaleChanged(); -protected Q_SLOTS: - void setCount(int count); - void updateCount(); - private Q_SLOTS: void updateSize(); private: Output *findOutput(int screenId) const; - int m_count; QSize m_boundingSize; qreal m_maxScale; }; diff --git a/src/scripting/v2/clientmodel.cpp b/src/scripting/v2/clientmodel.cpp index ac0f1af000..88902e0fec 100644 --- a/src/scripting/v2/clientmodel.cpp +++ b/src/scripting/v2/clientmodel.cpp @@ -10,12 +10,12 @@ #include -#include "window.h" #if KWIN_BUILD_ACTIVITIES #include "activities.h" #endif -#include "screens.h" +#include "output.h" #include "virtualdesktops.h" +#include "window.h" #include "workspace.h" namespace KWin::ScriptingModels::V2 @@ -324,7 +324,7 @@ AbstractLevel *AbstractLevel::create(const QList #endif } case ClientModel::ScreenRestriction: - for (int i = 0; i < workspace()->screens()->count(); ++i) { + for (int i = 0; i < workspace()->outputs().count(); ++i) { AbstractLevel *childLevel = create(childRestrictions, childrenRestrictions, model, currentLevel); if (!childLevel) { continue; @@ -398,7 +398,8 @@ ForkLevel::ForkLevel(const QList &childRestrictio , m_childRestrictions(childRestrictions) { connect(VirtualDesktopManager::self(), &VirtualDesktopManager::countChanged, this, &ForkLevel::desktopCountChanged); - connect(workspace()->screens(), &Screens::countChanged, this, &ForkLevel::screenCountChanged); + connect(workspace(), &Workspace::outputAdded, this, &ForkLevel::outputAdded); + connect(workspace(), &Workspace::outputRemoved, this, &ForkLevel::outputRemoved); #if KWIN_BUILD_ACTIVITIES if (Activities *activities = Workspace::self()->activities()) { connect(activities, &Activities::added, this, &ForkLevel::activityAdded); @@ -442,36 +443,36 @@ void ForkLevel::desktopCountChanged(uint previousCount, uint newCount) } } -void ForkLevel::screenCountChanged(int previousCount, int newCount) +void ForkLevel::outputAdded() { if (restriction() != ClientModel::ClientModel::ClientModel::ScreenRestriction) { return; } - if (newCount == previousCount || previousCount != count()) { + + const int row = count(); + + AbstractLevel *childLevel = AbstractLevel::create(m_childRestrictions, restrictions(), model(), this); + if (!childLevel) { + return; + } + Q_EMIT beginInsert(row, row, id()); + childLevel->setScreen(row); + childLevel->init(); + addChild(childLevel); + Q_EMIT endInsert(); +} + +void ForkLevel::outputRemoved() +{ + if (restriction() != ClientModel::ClientModel::ClientModel::ScreenRestriction) { return; } - if (previousCount > newCount) { - // screens got removed - Q_EMIT beginRemove(newCount, previousCount - 1, id()); - while (m_children.count() > newCount) { - delete m_children.takeLast(); - } - Q_EMIT endRemove(); - } else { - // screens got added - Q_EMIT beginInsert(previousCount, newCount - 1, id()); - for (int i = previousCount; i < newCount; ++i) { - AbstractLevel *childLevel = AbstractLevel::create(m_childRestrictions, restrictions(), model(), this); - if (!childLevel) { - continue; - } - childLevel->setScreen(i); - childLevel->init(); - addChild(childLevel); - } - Q_EMIT endInsert(); - } + const int row = count() - 1; + + Q_EMIT beginRemove(row, row, id()); + delete m_children.takeLast(); + Q_EMIT endRemove(); } void ForkLevel::activityAdded(const QString &activityId) diff --git a/src/scripting/v2/clientmodel.h b/src/scripting/v2/clientmodel.h index 1c0ccc5a8a..85182fafa8 100644 --- a/src/scripting/v2/clientmodel.h +++ b/src/scripting/v2/clientmodel.h @@ -187,9 +187,10 @@ public: Window *clientForId(quint32 child) const override; private Q_SLOTS: void desktopCountChanged(uint previousCount, uint newCount); - void screenCountChanged(int previousCount, int newCount); void activityAdded(const QString &id); void activityRemoved(const QString &id); + void outputAdded(); + void outputRemoved(); private: QList m_children;