diff --git a/autotests/integration/xdgshellwindow_test.cpp b/autotests/integration/xdgshellwindow_test.cpp index 10b7d6c5a6..5697bf0615 100644 --- a/autotests/integration/xdgshellwindow_test.cpp +++ b/autotests/integration/xdgshellwindow_test.cpp @@ -402,7 +402,14 @@ private Q_SLOTS: void testTransientTilingFacadeFractionalScale(); void testTransientTilingFacadeClosedSibling(); void testTransientTilingFacadeMasterClosed(); + void testTransientTilingFacadeScales_data(); + void testTransientTilingFacadeScales(); + void testTransientTilingFacadeBordersChangeWhenTiled(); void testTransientTilingFacadeKeyConflict(); + void testTransientTilingFacadeReleasedMaster_data(); + void testTransientTilingFacadeReleasedMaster(); + void testTransientFloatingFrameOnAnotherScale_data(); + void testTransientFloatingFrameOnAnotherScale(); void testTransientTilingFacadeProviderKeepsRecording_data(); void testTransientTilingFacadeProviderKeepsRecording(); void testControllerProviderRecordDuringApplication(); @@ -10393,14 +10400,38 @@ void TestXdgShellWindow::testTransientRequestConstraints() void TestXdgShellWindow::testTransientRequestFractional_data() { - QTest::addColumn("fits"); - QTest::newRow("snapped") << true; - QTest::newRow("nonrepresentable") << false; + QTest::addColumn("tileSize"); + QTest::addColumn("minimum"); + QTest::addColumn("maximum"); + QTest::addColumn("frameSize"); + // The output's pixel is 0.8 logical pixels: a 200x100 tile is on it. + QTest::newRow("snapped") << QSizeF(200, 100) << QSize() << QSize() << QSizeF(200, 100); + // 201x101 is not (251.25 x 126.25 device pixels). A client asked for 201x101 is 251x126 + // device pixels, a 200.8 x 100.8 frame: the largest the window reaches inside the tile. + QTest::newRow("off-the-pixel-grid") << QSizeF(201, 101) << QSize() << QSize() << QSizeF(200.8, 100.8); + // One axis on the grid, the other not. + QTest::newRow("height-off-the-pixel-grid") << QSizeF(200, 101) << QSize() << QSize() << QSizeF(200, 100.8); + // The client's own grid is a whole logical pixel. A 202-wide client is 252.5 device pixels, + // which is 253: a 202.4 frame, wider than the tile. The one that fits is 201 wide. + QTest::newRow("client-size-over-the-tile") << QSizeF(202, 100) << QSize() << QSize() << QSizeF(200.8, 100); + // Nothing inside the tile is a size the client accepts: refused, as before. + QTest::newRow("below-the-minimum") << QSizeF(201, 101) << QSize(201, 101) << QSize() << QSizeF(); + // A maximum below the tile: the window could stand in a corner of it, but a fit is the + // tile's size on the window's pixels. Refused, as before. + QTest::newRow("above-the-maximum") << QSizeF(201, 101) << QSize() << QSize(190, 90) << QSizeF(); } void TestXdgShellWindow::testTransientRequestFractional() { - QFETCH(bool, fits); + // A tile's rectangle is whole logical pixels, and on a fractional-scale output most of + // them are off the pixels a frame can have. The window is not refused for that: it takes + // the tile's corner and the largest size it can reach inside the tile, and that frame is + // the request's target. A rectangle with no reachable size inside it is still refused. + QFETCH(QSizeF, tileSize); + QFETCH(QSize, minimum); + QFETCH(QSize, maximum); + QFETCH(QSizeF, frameSize); + const bool reachable = frameSize.isValid(); Test::OutputInfo output; output.geometry = Rect(0, 0, 1280, 1024); output.scale = 1.25; @@ -10408,22 +10439,58 @@ void TestXdgShellWindow::testTransientRequestFractional() workspace()->setActiveOutput(QPoint(100, 100)); std::unique_ptr surface(Test::createSurface()); std::unique_ptr shell(Test::createXdgToplevelSurface(surface.get())); + if (minimum.isValid()) { + shell->set_min_size(minimum.width(), minimum.height()); + } + if (maximum.isValid()) { + shell->set_max_size(maximum.width(), maximum.height()); + } + // A window larger than the tile, or at its maximum where it has one. + const QSize initial = maximum.isValid() ? maximum : QSize(220, 120); QSignalSpy configure(shell->xdgSurface(), &Test::XdgSurface::configureRequested); - Window *window = Test::renderAndWaitForShown(surface.get(), QSize(200, 100), Qt::red); + QSignalSpy toplevelConfigure(shell.get(), &Test::XdgToplevel::configureRequested); + Window *window = Test::renderAndWaitForShown(surface.get(), initial, Qt::red); QVERIFY(window); QVERIFY(!configure.isEmpty() || configure.wait()); shell->xdgSurface()->ack_configure(configure.last().at(0).value()); - Test::render(surface.get(), QSize(200, 100), Qt::red); + configure.clear(); + Test::render(surface.get(), initial, Qt::red); QVERIFY(Test::waylandSync()); QCOMPARE(window->nextTargetScale(), 1.25); RequestTestTile tile(window); - tile.setTargetForTest(RectF(100, 100, fits ? 200 : 201, fits ? 100 : 101)); - QCOMPARE(window->isTransientTileTargetFeasible(tile.windowGeometry()), fits); + const RectF rectangle(QPointF(100, 100), tileSize); + tile.setTargetForTest(rectangle); + const RectF frame = window->transientTileFrame(tile.windowGeometry()); + const bool onGrid = frameSize == tileSize; + QCOMPARE(window->isTransientTileTargetFeasible(tile.windowGeometry()), onGrid); + QCOMPARE(window->isTransientTileTargetFeasible(frame), reachable); + // RectF compares exactly. Without a fit it is the rectangle itself, which is refused. + QCOMPARE(frame, reachable ? RectF(QPointF(100, 100), frameSize) : rectangle); + // The fit is the tile's frame. So is a fit under other borders, a size of the client or + // two below it (a 200x100 client is 250x125 device pixels, inside every tile here and + // within a step of the client's grid and two device pixels of it); nothing at another + // corner, larger than the tile or further inside. + QVERIFY(window->isTransientTileFrame(frame, rectangle)); + QVERIFY(window->isTransientTileFrame(RectF(100, 100, 200, 100), rectangle)); + QVERIFY(!window->isTransientTileFrame(RectF(100.8, 100, 200, 100), rectangle)); + QVERIFY(!window->isTransientTileFrame(RectF(100, 100, 203.2, 100), rectangle)); + QVERIFY(!window->isTransientTileFrame(RectF(100, 100, 196.8, 100), rectangle)); + QVERIFY(!window->isTransientTileFrame(RectF(100, 100, 200, 96.8), rectangle)); QObject owner; auto authority = std::make_shared(); authority->owner = &owner; const auto receipt = window->requestTransientTile(std::make_unique(window, &tile, authority)); - QCOMPARE(receipt.outcome, fits ? Window::TransientRequestOutcome::Applied : Window::TransientRequestOutcome::Rejected); + QCOMPARE(receipt.outcome, reachable ? Window::TransientRequestOutcome::Applied : Window::TransientRequestOutcome::Rejected); + if (!reachable) { + return; + } + // The request is for the fitted frame, and the client's commit lands on it exactly. + QCOMPARE(window->moveResizeGeometry(), frame); + QVERIFY(!configure.isEmpty() || configure.wait()); + shell->xdgSurface()->ack_configure(configure.last().at(0).value()); + Test::render(surface.get(), toplevelConfigure.last().at(0).value(), Qt::blue); + QVERIFY(Test::waylandSync()); + QCOMPARE(window->frameGeometry(), frame); } void TestXdgShellWindow::testTransientRequestDecorated() @@ -10527,6 +10594,20 @@ struct TilingFacade return QMetaObject::invokeMethod(api, "restoreWindow", Q_ARG(Window *, window)); } + int settleWindow(Window *window, const QString &token) const + { + int result = notCallable; + QMetaObject::invokeMethod(api, "settleWindow", Q_RETURN_ARG(int, result), Q_ARG(Window *, window), Q_ARG(QString, token)); + return result; + } + + QRectF floatingFrame(Window *window) const + { + QRectF frame; + QMetaObject::invokeMethod(api, "floatingFrame", Q_RETURN_ARG(QRectF, frame), Q_ARG(Window *, window)); + return frame; + } + QRectF masterWindowRect() const { QRectF rect; @@ -10577,6 +10658,13 @@ bool exactFrame(const QRectF &left, const QRectF &right) return left.x() == right.x() && left.y() == right.y() && left.width() == right.width() && left.height() == right.height(); } +// The same size up to the arithmetic's last bits: a rectangle stores its edges, and a size read +// back from one that was moved differs there from the size it was given. +bool sameSize(const QSizeF &left, const QSizeF &right) +{ + return std::abs(left.width() - right.width()) < 1.0 / 1024 && std::abs(left.height() - right.height()) < 1.0 / 1024; +} + QByteArray frameText(const QRectF &frame) { return QByteArray::number(frame.x(), 'g', 17) + ',' + QByteArray::number(frame.y(), 'g', 17) + ' ' @@ -10588,18 +10676,20 @@ QByteArray frameText(const QRectF &frame) void TestXdgShellWindow::testConfigurelessRequestSynchronizesRequestedGeometry_data() { QTest::addColumn("outputs"); + QTest::addColumn("secondScale"); QTest::addColumn("request"); QTest::addColumn("anchored"); - QTest::addColumn("synchronized"); + QTest::addColumn("crosses"); // A 361-wide client on a 4/3 output is 481 device pixels, a 360.75-wide frame: a request // for 361 asks the client for the size it has. - QTest::newRow("in-place") << 1 << QRectF(102, 102, 361, 240) << false << true; - QTest::newRow("moved") << 1 << QRectF(132, 123, 361, 240) << false << true; + QTest::newRow("in-place") << 1 << 4.0 / 3.0 << QRectF(102, 102, 361, 240) << false << false; + QTest::newRow("moved") << 1 << 4.0 / 3.0 << QRectF(132, 123, 361, 240) << false << false; // As during a resize from the top-left corner: the bottom-right corner stays, so the // published frame's top-left is not the request's. - QTest::newRow("bottom-right-kept") << 1 << QRectF(132, 123, 361, 240) << true << true; + QTest::newRow("bottom-right-kept") << 1 << 4.0 / 3.0 << QRectF(132, 123, 361, 240) << true << false; // The request's center belongs to the second output, its image's to the first. - QTest::newRow("center-on-an-output-boundary") << 2 << QRectF(1019.0625, 123, 361, 240) << false << false; + QTest::newRow("center-on-an-output-boundary") << 2 << 4.0 / 3.0 << QRectF(1019.0625, 123, 361, 240) << false << true; + QTest::newRow("center-on-a-boundary-to-another-scale") << 2 << 1.25 << QRectF(1019.0625, 123, 361, 240) << false << true; } void TestXdgShellWindow::testConfigurelessRequestSynchronizesRequestedGeometry() @@ -10607,15 +10697,16 @@ void TestXdgShellWindow::testConfigurelessRequestSynchronizesRequestedGeometry() // An ordinary request for the client size the window already has is resolved without a // configure: its image on the output's pixels is published at once, and from then on that // image is the requested geometry, as after a configure's commit. Where the image's center - // would be on another output than the request's, the requested geometry stays as it was - // asked and no output changes under the request. + // is on another output than the request's, the request follows its image there, the + // output's properties with it, as a commit's synchronization would take it. QFETCH(int, outputs); + QFETCH(qreal, secondScale); QFETCH(QRectF, request); QFETCH(bool, anchored); - QFETCH(bool, synchronized); + QFETCH(bool, crosses); QList config{Test::OutputInfo{.geometry = Rect(0, 0, 1200, 900), .scale = 4.0 / 3.0}}; if (outputs == 2) { - config.append(Test::OutputInfo{.geometry = Rect(1200, 0, 1200, 900), .scale = 4.0 / 3.0}); + config.append(Test::OutputInfo{.geometry = Rect(1200, 0, 1200, 900), .scale = secondScale}); } Test::setOutputConfig(config); workspace()->setActiveOutput(QPoint(100, 100)); @@ -10626,6 +10717,7 @@ void TestXdgShellWindow::testConfigurelessRequestSynchronizesRequestedGeometry() QVERIFY(window); QVERIFY(!configure.isEmpty() || configure.wait()); shell->xdgSurface()->ack_configure(configure.last().at(0).toUInt()); + configure.clear(); Test::render(surface.get(), QSize(361, 240), Qt::blue); QVERIFY(Test::waylandSync()); auto toplevel = static_cast(window); @@ -10638,27 +10730,35 @@ void TestXdgShellWindow::testConfigurelessRequestSynchronizesRequestedGeometry() if (anchored) { toplevel->m_configureGravity = Gravity::TopLeft; } - const QRectF image(request.topLeft(), before.size()); - QCOMPARE(workspace()->outputAt(image.center()) == workspace()->outputAt(request.center()), synchronized); + const auto first = workspace()->outputAt(before.center()); + QCOMPARE(workspace()->outputAt(request.center()) != first, crosses); window->moveResize(request); + // An output of another scale asks the client again; the client answers at the size it has. + for (int i = 0; i < 3 && toplevel->hasPendingNativeGeometry(); ++i) { + QVERIFY(!configure.isEmpty() || configure.wait()); + shell->xdgSurface()->ack_configure(configure.last().at(0).toUInt()); + configure.clear(); + Test::render(surface.get(), QSize(361, 240), Qt::blue); + QVERIFY(Test::waylandSync()); + } QVERIFY(!toplevel->hasPendingNativeGeometry()); const QRectF frame = window->frameGeometry(); - QCOMPARE(frame.size(), before.size()); if (anchored) { + QCOMPARE(frame.size(), before.size()); QCOMPARE(frame.x() + frame.width(), request.x() + request.width()); QCOMPARE(frame.y() + frame.height(), request.y() + request.height()); QVERIFY(frame.x() != request.x()); } else { - QVERIFY2(exactFrame(frame, image), frameText(frame).constData()); + QCOMPARE(frame.topLeft(), request.topLeft()); + QVERIFY(frame != request); } - if (synchronized) { - QVERIFY2(exactFrame(window->moveResizeGeometry(), frame), frameText(window->moveResizeGeometry()).constData()); - QCOMPARE(window->moveResizeOutput(), workspace()->outputAt(frame.center())); - } else { - QVERIFY2(exactFrame(window->moveResizeGeometry(), request), frameText(window->moveResizeGeometry()).constData()); - QCOMPARE(window->moveResizeOutput(), workspace()->outputAt(request.center())); - QVERIFY(window->moveResizeOutput() != workspace()->outputAt(frame.center())); + // Settled: what is published is what is requested, on the output the frame is on. + QVERIFY2(exactFrame(window->moveResizeGeometry(), frame), frameText(window->moveResizeGeometry()).constData()); + QCOMPARE(window->moveResizeOutput(), workspace()->outputAt(frame.center())); + QCOMPARE(window->nextTargetScale(), window->moveResizeOutput()->scale()); + if (crosses) { + QCOMPARE(window->moveResizeOutput(), first); } } @@ -10668,17 +10768,30 @@ void TestXdgShellWindow::testTransientTilingFacadeFractionalScale_data() QTest::addColumn("client"); QTest::addColumn("floating"); QTest::addColumn("request"); + QTest::addColumn("relative"); + QTest::addColumn("tile"); + QTest::addColumn("managed"); + // A quarter of the output: 600x450 less the tile padding, both on the output's pixel grid. + const QRectF quarter(0, 0, 0.5, 0.5); + const QRectF onGrid(4, 4, 594, 444); // The output's pixel is 0.75 logical pixels. A title bar a decoration snapped to it (50.25, // 67 device pixels) over a 241-high client is a whole 291-high frame over a fractional client // (240.75); a 361-wide client is 481 device pixels, a 360.75-wide frame. - QTest::newRow("title-bar") << true << QSize(360, 241) << QRectF(102, 102, 360, 291) << QRectF(); - QTest::newRow("fractional-frame") << false << QSize(361, 240) << QRectF(102, 102, 360.75, 240) << QRectF(); - QTest::newRow("title-bar-fractional-frame") << true << QSize(361, 240) << QRectF(102, 102, 360.75, 290.25) << QRectF(); + QTest::newRow("title-bar") << true << QSize(360, 241) << QRectF(102, 102, 360, 291) << QRectF() << quarter << onGrid << onGrid; + QTest::newRow("fractional-frame") << false << QSize(361, 240) << QRectF(102, 102, 360.75, 240) << QRectF() << quarter << onGrid << onGrid; + QTest::newRow("title-bar-fractional-frame") << true << QSize(361, 240) << QRectF(102, 102, 360.75, 290.25) << QRectF() << quarter << onGrid << onGrid; // A script places the window first, in whole pixels: the image of the frame it has, where // it is or elsewhere. The client's size is already that one, so no configure follows. - QTest::newRow("whole-pixel-request") << true << QSize(361, 240) << QRectF(102, 102, 360.75, 290.25) << QRectF(102, 102, 361, 290); - QTest::newRow("whole-pixel-move") << true << QSize(361, 240) << QRectF(102, 102, 360.75, 290.25) << QRectF(132, 123, 361, 290); - QTest::newRow("whole-pixel-move-without-a-title-bar") << false << QSize(361, 240) << QRectF(102, 102, 360.75, 240) << QRectF(132, 123, 361, 240); + QTest::newRow("whole-pixel-request") << true << QSize(361, 240) << QRectF(102, 102, 360.75, 290.25) << QRectF(102, 102, 361, 290) << quarter << onGrid << onGrid; + QTest::newRow("whole-pixel-move") << true << QSize(361, 240) << QRectF(102, 102, 360.75, 290.25) << QRectF(132, 123, 361, 290) << quarter << onGrid << onGrid; + QTest::newRow("whole-pixel-move-without-a-title-bar") << false << QSize(361, 240) << QRectF(102, 102, 360.75, 240) << QRectF(132, 123, 361, 240) << quarter << onGrid << onGrid; + // A master column of 55% over the whole height: 654 wide is on the output's pixels (872), + // 892 high is not (1189.33). An 892-high client is 1189 device pixels, an 891.75-high + // frame: the window's in that tile. (Decorated windows in such a tile, at this and the + // other scales: testTransientTilingFacadeScales.) + const QRectF column(0, 0, 0.55, 1); + const QRectF offGrid(4, 4, 654, 892); + QTest::newRow("tile-off-the-pixel-grid") << false << QSize(361, 240) << QRectF(102, 102, 360.75, 240) << QRectF() << column << offGrid << QRectF(4, 4, 654, 891.75); } void TestXdgShellWindow::testTransientTilingFacadeFractionalScale() @@ -10690,6 +10803,9 @@ void TestXdgShellWindow::testTransientTilingFacadeFractionalScale() QFETCH(QSize, client); QFETCH(QRectF, floating); QFETCH(QRectF, request); + QFETCH(QRectF, relative); + QFETCH(QRectF, tile); + QFETCH(QRectF, managed); Test::setOutputConfig({Test::OutputInfo{.geometry = Rect(0, 0, 1200, 900), .scale = 4.0 / 3.0}}); workspace()->setActiveOutput(QPoint(100, 100)); const TilingFacade facade; @@ -10767,19 +10883,19 @@ void TestXdgShellWindow::testTransientTilingFacadeFractionalScale() auto output = window->moveResizeOutput(); auto desktop = VirtualDesktopManager::self()->currentDesktop(output); - // A quarter of the output: 600x450 less the tile padding, both on the output's pixel grid. - const QRectF quarter(0, 0, 0.5, 0.5); - const QRectF managed(4, 4, 594, 444); - const QVariantMap tree = facade.createTile(output, desktop, quarter); + // The tile's rectangle is the layout's, in whole pixels; the window's frame in it is the + // largest it reaches there: the rectangle itself where that is on the window's pixel grid. + const QVariantMap tree = facade.createTile(output, desktop, relative); QCOMPARE(tree.value(QStringLiteral("result")).toInt(), int(TransientTileController::Result::Accepted)); QVERIFY(tree.value(QStringLiteral("generation")).toInt() > 0); - QVERIFY2(exactFrame(facade.masterWindowRect(), managed), frameText(facade.masterWindowRect()).constData()); + QVERIFY2(exactFrame(facade.masterWindowRect(), tile), frameText(facade.masterWindowRect()).constData()); QCOMPARE(facade.admitTile(tree.value(QStringLiteral("generation")).toInt(), window, token), int(TransientTileController::Result::Accepted)); QVERIFY(window->m_transientOwnership); QVERIFY(follow()); QVERIFY2(exactFrame(window->frameGeometry(), managed), frameText(window->frameGeometry()).constData()); QVERIFY(window->tile()); QVERIFY(window->m_transientOwnership); + QVERIFY2(exactFrame(facade.masterWindowRect(), tile), frameText(facade.masterWindowRect()).constData()); // The explicit restore for this window: the Window Restore action, addressed to the window. QSignalSpy treeChanged(facade.api, SIGNAL(treeChanged())); @@ -10794,7 +10910,7 @@ void TestXdgShellWindow::testTransientTilingFacadeFractionalScale() QVERIFY(exactFrame(facade.entry(key), scriptRecord(floating))); // Nothing of the first session is left on the window: it is admitted and restored again. - const QVariantMap again = facade.createTile(output, desktop, quarter); + const QVariantMap again = facade.createTile(output, desktop, relative); QCOMPARE(again.value(QStringLiteral("result")).toInt(), int(TransientTileController::Result::Accepted)); QCOMPARE(facade.admitTile(again.value(QStringLiteral("generation")).toInt(), window, token), int(TransientTileController::Result::Accepted)); QVERIFY(follow()); @@ -10806,6 +10922,293 @@ void TestXdgShellWindow::testTransientTilingFacadeFractionalScale() QTRY_VERIFY(!facade.hasTree()); } +void TestXdgShellWindow::testTransientTilingFacadeScales_data() +{ + QTest::addColumn("scale"); + QTest::addColumn("decorated"); + QTest::addColumn("client"); + QTest::addColumn("relative"); + // A master column of 55% over the whole height, 654x892 less nothing: off the pixels of + // every fractional scale here on at least one axis. + const QRectF column(0, 0, 0.55, 1); + // 600x400: on the pixels of 125%, 150% and 175%, so only the floating frame is fractional. + const QRectF onGrid(0, 0, 0.505, 406.0 / 900); + // The bottom-right 55% by 60%: 654x534 at 542,362. A frame that lands there is put + // together at the place the window came from and then moved, so its corners are other + // numbers than a frame's at the output's origin. + const QRectF corner(0.45, 0.4, 0.55, 0.6); + // The client sizes are the ones whose frame, stored as a rectangle's corners at 102,102, + // reads back a last bit away from the size on the pixel grid (a 101-high client at 125% + // is 126 device pixels, 100.8; 102 + 100.8 - 102 is not 100.8). + QTest::newRow("75%") << 0.75 << false << QSize(201, 101) << column; + QTest::newRow("75%-tile-off-the-origin") << 0.75 << false << QSize(201, 101) << corner; + QTest::newRow("100%") << 1.0 << false << QSize(201, 101) << column; + QTest::newRow("110%-tile-off-the-origin") << 1.1 << false << QSize(201, 101) << corner; + QTest::newRow("125%-tile-off-the-origin") << 1.25 << false << QSize(201, 101) << corner; + QTest::newRow("125%-title-bar-tile-off-the-origin") << 1.25 << true << QSize(361, 241) << corner; + QTest::newRow("133%-tile-off-the-origin") << 4.0 / 3.0 << false << QSize(201, 101) << corner; + QTest::newRow("150%-tile-off-the-origin") << 1.5 << false << QSize(201, 101) << corner; + QTest::newRow("150%-title-bar-tile-off-the-origin") << 1.5 << true << QSize(361, 241) << corner; + QTest::newRow("175%-tile-off-the-origin") << 1.75 << false << QSize(201, 101) << corner; + QTest::newRow("175%-title-bar-tile-off-the-origin") << 1.75 << true << QSize(361, 241) << corner; + QTest::newRow("110%") << 1.1 << false << QSize(201, 101) << column; + QTest::newRow("125%") << 1.25 << false << QSize(201, 101) << column; + QTest::newRow("125%-tile-on-the-pixel-grid") << 1.25 << false << QSize(201, 101) << onGrid; + QTest::newRow("125%-title-bar") << 1.25 << true << QSize(361, 241) << column; + QTest::newRow("133%") << 4.0 / 3.0 << false << QSize(201, 101) << column; + QTest::newRow("150%") << 1.5 << false << QSize(201, 101) << column; + QTest::newRow("150%-tile-on-the-pixel-grid") << 1.5 << false << QSize(201, 101) << onGrid; + QTest::newRow("150%-title-bar") << 1.5 << true << QSize(361, 241) << column; + QTest::newRow("150%-title-bar-tile-on-the-pixel-grid") << 1.5 << true << QSize(361, 241) << onGrid; + QTest::newRow("175%") << 1.75 << false << QSize(201, 101) << column; + QTest::newRow("175%-title-bar") << 1.75 << true << QSize(361, 241) << column; + QTest::newRow("200%") << 2.0 << false << QSize(201, 101) << column; +} + +void TestXdgShellWindow::testTransientTilingFacadeScales() +{ + // A whole session at each scale: admitted to the tile, maximized and back, restored to + // the exact floating frame, admitted again. At 75%, 133% and 200% the output's pixel is a + // binary fraction and every size on its grid is an exact number; at 110%, 125%, 150% and + // 175% it is not, and a size read back from a rectangle or put together from a client + // size and borders differs from the grid's in its last bit. Nothing may be refused or + // left unfinished for that. + QFETCH(qreal, scale); + QFETCH(bool, decorated); + QFETCH(QSize, client); + QFETCH(QRectF, relative); + Test::setOutputConfig({Test::OutputInfo{.geometry = Rect(0, 0, 1200, 900), .scale = scale}}); + workspace()->setActiveOutput(QPoint(100, 100)); + const TilingFacade facade; + QVERIFY(facade.api); + QVERIFY(!facade.hasTree()); + const auto releaseTree = qScopeGuard([&]() { + if (facade.hasTree()) { + facade.releaseTile(facade.generation()); + } + }); + const QString key = QStringLiteral("org.example.facade.scale.") + QString::fromLatin1(QTest::currentDataTag()).remove(QLatin1Char('%')); + std::unique_ptr surface(Test::createSurface()); + std::unique_ptr shell(Test::createXdgToplevelSurface(surface.get(), Test::CreationSetup::CreateOnly)); + shell->set_app_id(key); + std::unique_ptr decoration; + if (decorated) { + decoration = Test::createXdgToplevelDecorationV1(shell.get()); + decoration->set_mode(Test::XdgToplevelDecorationV1::mode_server_side); + } + QSignalSpy configure(shell->xdgSurface(), &Test::XdgSurface::configureRequested); + QSignalSpy toplevelConfigure(shell.get(), &Test::XdgToplevel::configureRequested); + QSize committed = client; + // The client's side of a configure: acknowledge the newest one and commit its size (the + // size it has where the configure leaves the choice to it). + const auto follow = [&]() { + if (configure.isEmpty() && !configure.wait()) { + return false; + } + shell->xdgSurface()->ack_configure(configure.last().at(0).toUInt()); + configure.clear(); + const QSize asked = toplevelConfigure.last().at(0).value(); + if (!asked.isEmpty()) { + committed = asked; + } + Test::render(surface.get(), committed, Qt::blue); + return Test::waylandSync(); + }; + surface->commit(KWayland::Client::Surface::CommitFlag::None); + QVERIFY(configure.wait()); + shell->xdgSurface()->ack_configure(configure.last().at(0).toUInt()); + configure.clear(); + Window *window = Test::renderAndWaitForShown(surface.get(), client, Qt::blue); + QVERIFY(window); + QCOMPARE(window->isDecorated(), decorated); + QVERIFY(follow()); + // The decoration's own borders, which it keeps when its window is tiled. + const QMarginsF borders = decorated ? window->nextDecoration()->nextState()->borders() : QMarginsF(); + QCOMPARE(borders.top() > 0, decorated); + auto toplevel = static_cast(window); + const auto answerAll = [&]() { + for (int i = 0; i < 6 && toplevel->hasPendingNativeGeometry(); ++i) { + if (!follow()) { + return false; + } + } + return !toplevel->hasPendingNativeGeometry(); + }; + window->move(QPointF(102, 102)); + QVERIFY(answerAll()); + QCOMPARE(window->nextTargetScale(), scale); + const QRectF floating = window->frameGeometry(); + QCOMPARE(floating.topLeft(), QPointF(102, 102)); + QVERIFY2(exactFrame(window->moveResizeGeometry(), floating), frameText(window->moveResizeGeometry()).constData()); + + QObject provider; + const QString token = facade.registerProvider(&provider); + QVERIFY(!token.isEmpty()); + QVERIFY(facade.trackWindow(window)); + QCOMPARE(facade.record(token, 1, window, scriptRecord(floating), 1), int(TransientPlacementWriter::Result::Accepted)); + + auto output = window->moveResizeOutput(); + auto desktop = VirtualDesktopManager::self()->currentDesktop(output); + for (int session = 0; session < 2; ++session) { + const QVariantMap tree = facade.createTile(output, desktop, relative); + QCOMPARE(tree.value(QStringLiteral("result")).toInt(), int(TransientTileController::Result::Accepted)); + const QRectF tile = facade.masterWindowRect(); + QCOMPARE(facade.admitTile(tree.value(QStringLiteral("generation")).toInt(), window, token), int(TransientTileController::Result::Accepted)); + QVERIFY(window->m_transientOwnership); + QVERIFY(follow()); + QVERIFY(answerAll()); + // In the tile: its corner, and the largest size the client reaches inside it. One more + // step of the client's grid (a whole logical pixel here) would be over the tile. + const QRectF frame = window->frameGeometry(); + QVERIFY(window->tile()); + QVERIFY(window->m_transientOwnership); + QCOMPARE(frame.topLeft(), tile.topLeft()); + QVERIFY2(frame.width() <= tile.width() && frame.height() <= tile.height(), frameText(frame).constData()); + QCOMPARE(decorated ? window->nextDecoration()->nextState()->borders() : QMarginsF(), borders); + QVERIFY2(borders.left() + borders.right() + std::round((committed.width() + 1) * scale) / scale > tile.width(), frameText(frame).constData()); + QVERIFY2(borders.top() + borders.bottom() + std::round((committed.height() + 1) * scale) / scale > tile.height(), frameText(frame).constData()); + QVERIFY2(exactFrame(frame, QRectF(window->transientTileFrame(RectF(tile)))), frameText(frame).constData()); + QVERIFY2(exactFrame(window->moveResizeGeometry(), frame), frameText(window->moveResizeGeometry()).constData()); + + if (session == 0) { + // An ordinary maximize keeps the ownership, and its end returns the window to + // its frame in the tile, not to the tile's rectangle. + window->maximize(MaximizeFull); + QVERIFY(answerAll()); + QCOMPARE(window->maximizeMode(), MaximizeFull); + QVERIFY(window->m_transientOwnership); + window->maximize(MaximizeRestore); + QVERIFY(answerAll()); + QCOMPARE(window->maximizeMode(), MaximizeRestore); + QVERIFY(window->m_transientOwnership); + QVERIFY(window->tile()); + QVERIFY2(exactFrame(window->frameGeometry(), frame), frameText(window->frameGeometry()).constData()); + } + + // The explicit restore: the exact floating frame, the record's end and the tree's. + QVERIFY(facade.restoreWindow(window)); + QVERIFY(follow()); + QVERIFY(answerAll()); + QVERIFY2(exactFrame(window->frameGeometry(), floating), frameText(window->frameGeometry()).constData()); + QVERIFY(!window->tile()); + QVERIFY(!window->m_transientOwnership); + QTRY_VERIFY(!facade.hasTree()); + QVERIFY(exactFrame(facade.entry(key), scriptRecord(floating))); + } +} + +void TestXdgShellWindow::testTransientTilingFacadeBordersChangeWhenTiled() +{ + // A decoration can change its borders when its window is tiled, in the middle of the + // admission: the sizes the client reaches change with them, and with them the largest one + // inside a tile that is off the pixel grid. The request's frame was that fit when the + // request was made; it stays the request's while it is at the tile's corner, inside the + // tile by no more than a fit is, and still reachable. Here the title bar is set by hand + // before the admission and the decoration puts its own back as the window is tiled. + const qreal scale = 4.0 / 3.0; + Test::setOutputConfig({Test::OutputInfo{.geometry = Rect(0, 0, 1200, 900), .scale = scale}}); + workspace()->setActiveOutput(QPoint(100, 100)); + const TilingFacade facade; + QVERIFY(facade.api); + QVERIFY(!facade.hasTree()); + const auto releaseTree = qScopeGuard([&]() { + if (facade.hasTree()) { + facade.releaseTile(facade.generation()); + } + }); + const QString key = QStringLiteral("org.example.facade.borders"); + std::unique_ptr surface(Test::createSurface()); + std::unique_ptr shell(Test::createXdgToplevelSurface(surface.get(), Test::CreationSetup::CreateOnly)); + shell->set_app_id(key); + std::unique_ptr decoration(Test::createXdgToplevelDecorationV1(shell.get())); + decoration->set_mode(Test::XdgToplevelDecorationV1::mode_server_side); + QSignalSpy configure(shell->xdgSurface(), &Test::XdgSurface::configureRequested); + QSignalSpy toplevelConfigure(shell.get(), &Test::XdgToplevel::configureRequested); + QSize committed(360, 241); + const auto follow = [&]() { + if (configure.isEmpty() && !configure.wait()) { + return false; + } + shell->xdgSurface()->ack_configure(configure.last().at(0).toUInt()); + configure.clear(); + const QSize asked = toplevelConfigure.last().at(0).value(); + if (!asked.isEmpty()) { + committed = asked; + } + Test::render(surface.get(), committed, Qt::blue); + return Test::waylandSync(); + }; + surface->commit(KWayland::Client::Surface::CommitFlag::None); + QVERIFY(configure.wait()); + shell->xdgSurface()->ack_configure(configure.last().at(0).toUInt()); + configure.clear(); + Window *window = Test::renderAndWaitForShown(surface.get(), committed, Qt::blue); + QVERIFY(window); + QVERIFY(window->isDecorated()); + QVERIFY(follow()); + auto toplevel = static_cast(window); + const auto answerAll = [&]() { + for (int i = 0; i < 6 && toplevel->hasPendingNativeGeometry(); ++i) { + if (!follow()) { + return false; + } + } + return !toplevel->hasPendingNativeGeometry(); + }; + window->move(QPointF(102, 102)); + QVERIFY(answerAll()); + + // The decoration's own borders, and the window's fit under them in a tile whose height + // (892, 1189.33 device pixels) is off the grid. + const QMarginsF own = window->nextDecoration()->nextState()->borders(); + const RectF tile(4, 4, 654, 892); + const RectF ownFit = window->transientTileFrame(tile); + const int ownPixels = qRound((own.top() + own.bottom()) * scale); + // A title bar a few device pixels taller, under which the fit is another one that the + // client still reaches under the decoration's own borders: a height whose client part is + // the image of a whole client size. + RectF fit; + for (int taller = 1; taller <= 12 && fit.isNull(); ++taller) { + auto state = window->nextDecoration()->nextState(); + state->setBorders(QMarginsF(own.left(), own.top() + taller / scale, own.right(), own.bottom())); + Q_EMIT window->nextDecoration()->nextStateChanged(state); + QVERIFY(follow()); + QVERIFY(answerAll()); + const RectF candidate = window->transientTileFrame(tile); + const int clientPixels = qRound(candidate.height() * scale) - ownPixels; + const bool reachable = qRound(qRound(clientPixels / scale) * scale) == clientPixels; + if (qRound(candidate.height() * scale) != qRound(ownFit.height() * scale) && reachable) { + fit = candidate; + } + } + QVERIFY2(!fit.isNull(), "no title bar within a dozen device pixels gives another fit"); + const QRectF floating = window->frameGeometry(); + QVERIFY2(exactFrame(window->moveResizeGeometry(), floating), frameText(window->moveResizeGeometry()).constData()); + + QObject provider; + const QString token = facade.registerProvider(&provider); + QVERIFY(!token.isEmpty()); + QVERIFY(facade.trackWindow(window)); + QCOMPARE(facade.record(token, 1, window, scriptRecord(floating), 1), int(TransientPlacementWriter::Result::Accepted)); + auto output = window->moveResizeOutput(); + auto desktop = VirtualDesktopManager::self()->currentDesktop(output); + const QVariantMap tree = facade.createTile(output, desktop, QRectF(0, 0, 0.55, 1)); + QCOMPARE(tree.value(QStringLiteral("result")).toInt(), int(TransientTileController::Result::Accepted)); + QVERIFY2(exactFrame(facade.masterWindowRect(), QRectF(tile)), frameText(facade.masterWindowRect()).constData()); + QCOMPARE(facade.admitTile(tree.value(QStringLiteral("generation")).toInt(), window, token), int(TransientTileController::Result::Accepted)); + QVERIFY(follow()); + QVERIFY(answerAll()); + // The decoration has its own borders again, and under them the fit is another frame. + QCOMPARE(window->nextDecoration()->nextState()->borders(), own); + QVERIFY(window->transientTileFrame(tile) != fit); + // The window is in the tile on the frame the request was made for. + QVERIFY(window->tile()); + QVERIFY(window->m_transientOwnership); + QVERIFY2(exactFrame(window->frameGeometry(), QRectF(fit)), frameText(window->frameGeometry()).constData()); + QVERIFY(window->isTransientTileFrame(fit, tile)); + QVERIFY(window->isTransientTileTargetFeasible(fit)); +} + void TestXdgShellWindow::testTransientTilingFacadeClosedSibling() { // The provider tracks every window it watches, and a key has one window at a reservation. @@ -10989,6 +11392,533 @@ void TestXdgShellWindow::testTransientTilingFacadeKeyConflict() QCOMPARE(facade.releaseTile(again.value(QStringLiteral("generation")).toInt()), int(TransientTileController::Result::Accepted)); } +namespace +{ + +// How the native layer comes to release the master without restoring it. +enum ReleasedBy { + DesktopRemoved, + KeyConflict, + MovedByHand, + MovedByHandThenByARequest, + MovedByARequest, + MovedByARequestThenHeldStill, + RecheckedByTheWorkspace, + SentToAnotherOutput, + SentToAnOutputOfAnotherScale, + OutputRemoved, + MaximizedAfterwards, +}; + +} + +void TestXdgShellWindow::testTransientTilingFacadeReleasedMaster_data() +{ + QTest::addColumn("release"); + QTest::addColumn("scale"); + QTest::addColumn("client"); + QTest::newRow("desktop-removed") << int(DesktopRemoved) << 1.0 << QSize(200, 100); + QTest::newRow("key-conflict") << int(KeyConflict) << 1.0 << QSize(200, 100); + QTest::newRow("moved-by-hand") << int(MovedByHand) << 1.0 << QSize(200, 100); + // The user's move, then a request that is not the user's: the evidence is stale. + QTest::newRow("moved-by-hand-then-by-a-request") << int(MovedByHandThenByARequest) << 1.0 << QSize(200, 100); + // A script's request, or the compositor's own: not the user's hand. + QTest::newRow("moved-by-a-request") << int(MovedByARequest) << 1.0 << QSize(200, 100); + // The same, and then the user takes the window and lets it go where it is (a press on + // the title bar held and released, the keyboard move ended at once): an interactive + // operation that moved nothing chose no place. + QTest::newRow("moved-by-a-request-then-held-still") << int(MovedByARequestThenHeldStill) << 1.0 << QSize(200, 100); + // The workspace re-checks every window's position when the work area changes (a panel, a + // strut, an output's size), and asks a tiled one for its tile's rectangle. + QTest::newRow("re-checked-by-the-workspace") << int(RecheckedByTheWorkspace) << 1.0 << QSize(200, 100); + QTest::newRow("sent-to-another-output") << int(SentToAnotherOutput) << 1.0 << QSize(200, 100); + // The session's output is unplugged: the window is put on the other one, and its + // floating frame was on coordinates that are gone. + QTest::newRow("output-removed") << int(OutputRemoved) << 1.0 << QSize(200, 100); + QTest::newRow("maximized-afterwards") << int(MaximizedAfterwards) << 1.0 << QSize(200, 100); + // The output's pixel is 0.8: the floating frame is 200.8x100.8 and the tile's rectangle + // (634 wide, 792.5 device pixels) is off the grid, so the automatic rectangle is the + // window's fit in it and none of the frames is a binary fraction. + QTest::newRow("desktop-removed-at-125%") << int(DesktopRemoved) << 1.25 << QSize(201, 101); + QTest::newRow("moved-by-hand-at-125%") << int(MovedByHand) << 1.25 << QSize(201, 101); + // There the rectangle the re-check asks for is the tile's own, off the pixel grid: the + // window lands on its image, which is neither the fit it was admitted to nor inside the + // tile. + QTest::newRow("re-checked-by-the-workspace-at-125%") << int(RecheckedByTheWorkspace) << 1.25 << QSize(201, 101); + // From an output whose pixel is 0.8 to one whose pixel is 1: the floating frame is + // 200.8x100.8 and goes along as it is, but no window is 200.8 wide on whole pixels. A + // request for it lands on 201x101, frame and requested geometry both, which is not the + // floating frame: the record answered AtManagedRectangle however often the window was + // asked back (a 133% laptop panel and a 100% second screen: the master sent across could + // not be tiled again until it was moved by hand). + QTest::newRow("sent-to-an-output-of-another-scale") << int(SentToAnOutputOfAnotherScale) << 1.25 << QSize(201, 101); +} + +void TestXdgShellWindow::testTransientTilingFacadeReleasedMaster() +{ + // A release that is not the explicit restore is detach-only: the master stays where it + // stands and its record is held, which keeps the automatic rectangle out of the memory. + // Nothing native ever ended that record, so the window kept the tile's size and could not + // be admitted again until it closed (the ThinkPad after a desktop removal, a second window + // of the app, a move of the master). The policy ends it: it gives the window its floating + // frame back with an ordinary request, or leaves it where the user put it, and asks the + // facade to settle the record. The native layer does that only when the window's geometry + // is settled and the window stands on its floating frame or where the user's own move + // left it: every other frame can be an automatic rectangle, and not only the one the + // window was admitted to. + QFETCH(int, release); + QFETCH(qreal, scale); + QFETCH(QSize, client); + using Settle = Window::TransientSettleResult; + if (release == SentToAnOutputOfAnotherScale) { + Test::setOutputConfig({Test::OutputInfo{.geometry = Rect(0, 0, 1280, 1024), .scale = scale}, + Test::OutputInfo{.geometry = Rect(1280, 0, 1280, 1024), .scale = 1.0}}); + workspace()->setActiveOutput(QPoint(100, 100)); + } else if (scale != 1.0) { + Test::setOutputConfig({Test::OutputInfo{.geometry = Rect(0, 0, 1280, 1024), .scale = scale}}); + workspace()->setActiveOutput(QPoint(100, 100)); + } + if (release == OutputRemoved) { + // The window opens on the second output, the one that goes. + workspace()->setActiveOutput(QPoint(1280 + 640, 512)); + } + const TilingFacade facade; + QVERIFY(facade.api); + QVERIFY(!facade.hasTree()); + const auto releaseTree = qScopeGuard([&]() { + if (facade.hasTree()) { + facade.releaseTile(facade.generation()); + } + }); + auto desktops = VirtualDesktopManager::self(); + const auto restoreDesktops = qScopeGuard([desktops]() { + desktops->setCount(1); + }); + const QString key = QStringLiteral("org.example.facade.released.") + QString::fromLatin1(QTest::currentDataTag()); + std::unique_ptr surface(Test::createSurface()); + std::unique_ptr shell(Test::createXdgToplevelSurface(surface.get())); + shell->set_app_id(key); + QSignalSpy configure(shell->xdgSurface(), &Test::XdgSurface::configureRequested); + QSignalSpy toplevelConfigure(shell.get(), &Test::XdgToplevel::configureRequested); + Window *master = Test::renderAndWaitForShown(surface.get(), client, Qt::red); + QVERIFY(master); + QVERIFY(!configure.isEmpty() || configure.wait()); + shell->xdgSurface()->ack_configure(configure.last().at(0).toUInt()); + configure.clear(); + Test::render(surface.get(), client, Qt::red); + QVERIFY(Test::waylandSync()); + QCOMPARE(master->nextTargetScale(), scale); + // The client's side of a configure: acknowledge the newest one and commit its size (the + // size it has where the configure leaves the choice to it). + QSize committed = client; + const auto follow = [&]() { + if (configure.isEmpty() && !configure.wait()) { + return false; + } + shell->xdgSurface()->ack_configure(configure.last().at(0).toUInt()); + configure.clear(); + const QSize asked = toplevelConfigure.last().at(0).value(); + if (!asked.isEmpty()) { + committed = asked; + } + Test::render(surface.get(), committed, Qt::blue); + return Test::waylandSync(); + }; + auto toplevel = static_cast(master); + // Until nothing of the native geometry is in flight: a release, an activation or a + // deactivation each bring a configure of their own. + const auto answerAll = [&]() { + for (int i = 0; i < 6 && toplevel->hasPendingNativeGeometry(); ++i) { + if (!follow()) { + return false; + } + } + return !toplevel->hasPendingNativeGeometry(); + }; + const QRectF floating = master->frameGeometry(); + QObject provider; + const QString token = facade.registerProvider(&provider); + QVERIFY(!token.isEmpty()); + QVERIFY(facade.trackWindow(master)); + QCOMPARE(facade.record(token, 1, master, scriptRecord(floating), 1), int(TransientPlacementWriter::Result::Accepted)); + // Nothing to settle: no record, then an owned one. + QCOMPARE(facade.settleWindow(master, token), int(Settle::NotReleased)); + auto output = master->moveResizeOutput(); + desktops->setCount(2); + const auto first = desktops->currentDesktop(output); + const auto second = desktops->desktops().constLast(); + QVERIFY(first != second); + const QVariantMap tree = facade.createTile(output, first, QRectF(0, 0, 0.5, 1)); + QCOMPARE(tree.value(QStringLiteral("result")).toInt(), int(TransientTileController::Result::Accepted)); + QCOMPARE(facade.admitTile(tree.value(QStringLiteral("generation")).toInt(), master, token), int(TransientTileController::Result::Accepted)); + // The automatic rectangle: the window's frame in the tile. + const QRectF managed(master->transientTileFrame(RectF(facade.masterWindowRect()))); + QCOMPARE(exactFrame(managed, facade.masterWindowRect()), scale == 1.0); + QVERIFY(follow()); + QVERIFY(master->tile()); + QVERIFY2(exactFrame(master->frameGeometry(), managed), frameText(master->frameGeometry()).constData()); + QCOMPARE(facade.settleWindow(master, token), int(Settle::NotReleased)); + QVERIFY(master->m_transientOwnership); + + std::unique_ptr siblingSurface; + std::unique_ptr siblingShell; + int revision = 1; + switch (release) { + case DesktopRemoved: + case MaximizedAfterwards: + desktops->setCurrent(second); + desktops->removeVirtualDesktop(first); + break; + case KeyConflict: { + siblingSurface = Test::createSurface(); + siblingShell = Test::createXdgToplevelSurface(siblingSurface.get()); + siblingShell->set_app_id(key); + Window *sibling = Test::renderAndWaitForShown(siblingSurface.get(), QSize(220, 120), Qt::green); + QVERIFY(sibling); + QVERIFY(facade.trackWindow(sibling)); + break; + } + case MovedByHand: + case MovedByHandThenByARequest: { + // The user's own move: its first step releases the master. While it lasts the + // geometry is not settled; where it ends is the user's placement. + QSignalSpy finished(master, &Window::interactiveMoveResizeFinished); + workspace()->slotWindowMove(); + QCOMPARE(workspace()->moveResizeWindow(), master); + for (const auto key : {Qt::Key_Right, Qt::Key_Right, Qt::Key_Down}) { + master->keyPressEvent(key); + master->updateInteractiveMoveResize(KWin::Cursors::self()->mouse()->pos(), Qt::KeyboardModifiers()); + } + QTRY_VERIFY(!facade.hasTree()); + QVERIFY(master->isInteractiveMove()); + QCOMPARE(facade.settleWindow(master, token), int(Settle::Unsettled)); + master->keyPressEvent(Qt::Key_Enter); + QCOMPARE(finished.count(), 1); + QVERIFY(!master->isInteractiveMove()); + if (release == MovedByHandThenByARequest) { + // The workspace's re-check, a script: a request after the user's hand let go. + master->moveResize(QRectF(300, 200, 260, 180)); + } + break; + } + case MovedByARequest: + master->moveResize(QRectF(300, 200, 260, 180)); + break; + case MovedByARequestThenHeldStill: { + master->moveResize(QRectF(300, 200, 260, 180)); + QTRY_VERIFY(!facade.hasTree()); + QVERIFY(answerAll()); + const RectF standing = master->frameGeometry(); + QSignalSpy stepped(master, &Window::interactiveMoveResizeStepped); + QSignalSpy finished(master, &Window::interactiveMoveResizeFinished); + workspace()->slotWindowMove(); + QCOMPARE(workspace()->moveResizeWindow(), master); + QVERIFY(master->isInteractiveMove()); + master->keyPressEvent(Qt::Key_Enter); + QCOMPARE(finished.count(), 1); + QCOMPARE(stepped.count(), 0); + QVERIFY(!master->isInteractiveMove()); + QCOMPARE(master->frameGeometry(), standing); + break; + } + case RecheckedByTheWorkspace: + // What Workspace::rearrange() calls for every window: a tiled one is asked for its + // tile's rectangle with a public request, which releases the master. + master->checkWorkspacePosition(); + break; + case SentToAnotherOutput: + case SentToAnOutputOfAnotherScale: { + const auto outputs = workspace()->outputs(); + QCOMPARE(outputs.count(), 2); + master->sendToOutput(outputs.at(0) == output ? outputs.at(1) : outputs.at(0)); + QVERIFY(master->moveResizeOutput() != output); + QCOMPARE(master->moveResizeOutput()->scale() != scale, release == SentToAnOutputOfAnotherScale); + break; + } + case OutputRemoved: { + QCOMPARE(output->geometry(), Rect(1280, 0, 1280, 1024)); + Test::setOutputConfig({Rect(0, 0, 1280, 1024)}); + QCOMPARE(workspace()->outputs().count(), 1); + QCOMPARE(master->moveResizeOutput(), workspace()->outputs().constFirst()); + break; + } + } + QTRY_VERIFY(!facade.hasTree()); + QVERIFY(master->m_transientOwnership); + QCOMPARE(master->m_transientOwnership->phase, Window::TransientOwnershipPhase::Restoring); + QVERIFY(!master->tile()); + + // Only the provider that admitted the window settles its record. + QCOMPARE(facade.settleWindow(master, QUuid::createUuid().toString()), int(Settle::StaleProvider)); + // The release changed the window's states, and some releases its geometry; until the + // client has answered those configures the geometry is not settled. + if (toplevel->hasPendingNativeGeometry()) { + QCOMPARE(facade.settleWindow(master, token), int(Settle::Unsettled)); + QVERIFY(answerAll()); + } + QVERIFY(!toplevel->hasPendingNativeGeometry()); + if (release == MaximizedAfterwards) { + // A mode owns the frame and gives it back to the rectangle the window had before: + // the record waits for the ordinary state. + master->maximize(MaximizeFull); + QVERIFY(answerAll()); + QCOMPARE(master->maximizeMode(), MaximizeFull); + QCOMPARE(facade.settleWindow(master, token), int(Settle::Unsettled)); + master->maximize(MaximizeRestore); + QVERIFY(answerAll()); + QCOMPARE(master->maximizeMode(), MaximizeRestore); + QVERIFY2(exactFrame(master->frameGeometry(), managed), frameText(master->frameGeometry()).constData()); + } + QRectF expected = master->frameGeometry(); + if (release == MovedByHand) { + QVERIFY2(!exactFrame(expected, managed) && !exactFrame(expected, floating), frameText(expected).constData()); + // The provider records the place the user chose; the key is still reserved. + QCOMPARE(facade.record(token, ++revision, master, scriptRecord(expected), revision), int(TransientPlacementWriter::Result::Deferred)); + } else { + // Settled, and on a rectangle nothing says the user chose: the tile's frame the + // window was admitted to, or another one (the tile's new rectangle, the tile's size + // on another output, a script's request). The record stays, the memory's barrier + // with it. + const bool admittedFrame = release == DesktopRemoved || release == KeyConflict || release == MaximizedAfterwards + || (release == RecheckedByTheWorkspace && scale == 1.0); + QCOMPARE(exactFrame(expected, managed), admittedFrame); + QVERIFY2(!exactFrame(expected, floating), frameText(expected).constData()); + QCOMPARE(facade.settleWindow(master, token), int(Settle::AtManagedRectangle)); + QVERIFY(master->m_transientOwnership); + // The policy gives the window its floating frame back: an ordinary request for the + // frame the record holds, which followed the window to another output. + expected = facade.floatingFrame(master); + QVERIFY(expected.isValid()); + QCOMPARE(exactFrame(expected, floating), + release != SentToAnotherOutput && release != SentToAnOutputOfAnotherScale && release != OutputRemoved); + QCOMPARE(workspace()->outputAt(expected.center()), master->moveResizeOutput()); + if (release == SentToAnOutputOfAnotherScale) { + // The frame the window takes there: the record's corner, the floating size on + // that output's pixels. The record keeps the size it was taken with, so a + // transfer back gives the window its own frame again. + const QRectF carried(master->m_transientOwnership->floating); + QVERIFY2(exactFrame(QRectF(carried.topLeft(), QSizeF(201, 101)), expected), frameText(expected).constData()); + QVERIFY2(sameSize(carried.size(), floating.size()), frameText(carried).constData()); + QVERIFY(!exactFrame(carried, expected)); + } + master->moveResize(expected); + // The client has not answered the request yet. + QCOMPARE(facade.settleWindow(master, token), int(Settle::Unsettled)); + QVERIFY(master->m_transientOwnership); + QVERIFY(answerAll()); + } + QVERIFY2(exactFrame(master->frameGeometry(), expected), frameText(master->frameGeometry()).constData()); + QSignalSpy settled(master, &Window::transientReleaseSettled); + QCOMPARE(facade.settleWindow(master, token), int(Settle::Settled)); + QCOMPARE(settled.count(), 1); + QVERIFY(!master->m_transientOwnership); + QVERIFY(!facade.floatingFrame(master).isValid()); + QVERIFY2(exactFrame(master->frameGeometry(), expected), frameText(master->frameGeometry()).constData()); + // The memory: the user's place, recorded while the key was reserved, or the entry as it + // was (the tiler returns the window without recording anything). + QVERIFY(exactFrame(facade.entry(key), scriptRecord(release == MovedByHand ? expected : floating))); + QCOMPARE(facade.settleWindow(master, token), int(Settle::NotReleased)); + if (release != MovedByHand) { + // The policy records the frame the window settled at. It is the entry already, except + // on another output, where the next admission's reservation needs it. + QCOMPARE(facade.record(token, ++revision, master, scriptRecord(expected), revision), int(TransientPlacementWriter::Result::Accepted)); + QVERIFY(exactFrame(facade.entry(key), scriptRecord(expected))); + } + + // The window is free again: a new session admits it and the explicit restore returns it. + if (siblingSurface) { + siblingShell.reset(); + siblingSurface.reset(); + QVERIFY(Test::waylandSync()); + QTRY_COMPARE(workspace()->windows().count(), 1); + // The master is the active window again; the client answers that configure. + QTRY_COMPARE(workspace()->activeWindow(), master); + QVERIFY(answerAll()); + } + output = master->moveResizeOutput(); + const auto desktop = desktops->currentDesktop(output); + const QVariantMap again = facade.createTile(output, desktop, QRectF(0, 0, 0.5, 1)); + QCOMPARE(again.value(QStringLiteral("result")).toInt(), int(TransientTileController::Result::Accepted)); + QCOMPARE(facade.admitTile(again.value(QStringLiteral("generation")).toInt(), master, token), int(TransientTileController::Result::Accepted)); + QVERIFY(follow()); + QVERIFY(master->tile()); + QVERIFY(exactFrame(facade.floatingFrame(master), expected)); + QVERIFY(facade.restoreWindow(master)); + // A live restoration completes at its own boundary; there is nothing to settle. + QCOMPARE(facade.settleWindow(master, token), int(Settle::NotReleased)); + QVERIFY(master->m_transientOwnership); + QVERIFY(follow()); + QVERIFY2(exactFrame(master->frameGeometry(), expected), frameText(master->frameGeometry()).constData()); + QVERIFY(!master->m_transientOwnership); + QTRY_VERIFY(!facade.hasTree()); +} + +void TestXdgShellWindow::testTransientFloatingFrameOnAnotherScale_data() +{ + QTest::addColumn("from"); + QTest::addColumn("to"); + QTest::addColumn("decorated"); + QTest::addColumn("client"); + // 200.8x100.8 at 125%: no size of a window on whole pixels. + QTest::newRow("125%-to-100%") << 1.25 << 1.0 << false << QSize(201, 101); + QTest::newRow("125%-to-100%-title-bar") << 1.25 << 1.0 << true << QSize(361, 241); + // The pair it was seen with: a 133% panel and a 100% second screen, a decorated window + // (560.25x521.25 under a 50.25 title bar there). + QTest::newRow("133%-to-100%-title-bar") << 4.0 / 3.0 << 1.0 << true << QSize(361, 241); + // To a fractional scale, and from one to another. + QTest::newRow("100%-to-150%") << 1.0 << 1.5 << false << QSize(201, 101); + QTest::newRow("125%-to-150%-title-bar") << 1.25 << 1.5 << true << QSize(361, 241); + QTest::newRow("150%-to-175%") << 1.5 << 1.75 << false << QSize(201, 101); + // The same scale on both outputs: the frame goes along as it is. + QTest::newRow("125%-to-125%") << 1.25 << 1.25 << false << QSize(201, 101); + QTest::newRow("125%-to-125%-title-bar") << 1.25 << 1.25 << true << QSize(361, 241); +} + +void TestXdgShellWindow::testTransientFloatingFrameOnAnotherScale() +{ + // A released record's floating frame goes to another output with the size it was taken + // with. Where that size is not on the other output's pixels the window cannot stand on + // it, and the policy's request for it used to end on a rectangle the record did not + // know: the settle answered AtManagedRectangle for as long as the window lived. The + // policy is given the frame as the window takes it there, the record settles on it, and + // the record itself keeps the size it had. + QFETCH(qreal, from); + QFETCH(qreal, to); + QFETCH(bool, decorated); + QFETCH(QSize, client); + using Settle = Window::TransientSettleResult; + Test::setOutputConfig({Test::OutputInfo{.geometry = Rect(0, 0, 1280, 1024), .scale = from}, + Test::OutputInfo{.geometry = Rect(1280, 0, 1280, 1024), .scale = to}}); + workspace()->setActiveOutput(QPoint(100, 100)); + const TilingFacade facade; + QVERIFY(facade.api); + QVERIFY(!facade.hasTree()); + const auto releaseTree = qScopeGuard([&]() { + if (facade.hasTree()) { + facade.releaseTile(facade.generation()); + } + }); + const QString key = QStringLiteral("org.example.facade.image.") + QString::fromLatin1(QTest::currentDataTag()).remove(QLatin1Char('%')); + std::unique_ptr surface(Test::createSurface()); + std::unique_ptr shell(Test::createXdgToplevelSurface(surface.get(), Test::CreationSetup::CreateOnly)); + shell->set_app_id(key); + std::unique_ptr decoration; + if (decorated) { + decoration = Test::createXdgToplevelDecorationV1(shell.get()); + decoration->set_mode(Test::XdgToplevelDecorationV1::mode_server_side); + } + QSignalSpy configure(shell->xdgSurface(), &Test::XdgSurface::configureRequested); + QSignalSpy toplevelConfigure(shell.get(), &Test::XdgToplevel::configureRequested); + QSize committed = client; + // The client's side of a configure: acknowledge the newest one and commit its size (the + // size it has where the configure leaves the choice to it). + const auto follow = [&]() { + if (configure.isEmpty() && !configure.wait()) { + return false; + } + shell->xdgSurface()->ack_configure(configure.last().at(0).toUInt()); + configure.clear(); + const QSize asked = toplevelConfigure.last().at(0).value(); + if (!asked.isEmpty()) { + committed = asked; + } + Test::render(surface.get(), committed, Qt::blue); + return Test::waylandSync(); + }; + surface->commit(KWayland::Client::Surface::CommitFlag::None); + QVERIFY(configure.wait()); + shell->xdgSurface()->ack_configure(configure.last().at(0).toUInt()); + configure.clear(); + Window *master = Test::renderAndWaitForShown(surface.get(), client, Qt::blue); + QVERIFY(master); + QCOMPARE(master->isDecorated(), decorated); + QVERIFY(follow()); + QCOMPARE(master->nextTargetScale(), from); + auto toplevel = static_cast(master); + const auto answerAll = [&]() { + for (int i = 0; i < 6 && toplevel->hasPendingNativeGeometry(); ++i) { + if (!follow()) { + return false; + } + } + return !toplevel->hasPendingNativeGeometry(); + }; + QVERIFY(answerAll()); + const QRectF floating = master->frameGeometry(); + QObject provider; + const QString token = facade.registerProvider(&provider); + QVERIFY(!token.isEmpty()); + QVERIFY(facade.trackWindow(master)); + QCOMPARE(facade.record(token, 1, master, scriptRecord(floating), 1), int(TransientPlacementWriter::Result::Accepted)); + const auto first = master->moveResizeOutput(); + const QVariantMap tree = facade.createTile(first, VirtualDesktopManager::self()->currentDesktop(first), QRectF(0, 0, 0.5, 1)); + QCOMPARE(tree.value(QStringLiteral("result")).toInt(), int(TransientTileController::Result::Accepted)); + QCOMPARE(facade.admitTile(tree.value(QStringLiteral("generation")).toInt(), master, token), int(TransientTileController::Result::Accepted)); + QVERIFY(answerAll()); + QVERIFY(master->tile()); + // An owned record's frame is the restoration's target, exactly. + QVERIFY2(exactFrame(facade.floatingFrame(master), floating), frameText(facade.floatingFrame(master)).constData()); + + const auto outputs = workspace()->outputs(); + QCOMPARE(outputs.count(), 2); + master->sendToOutput(outputs.at(0) == first ? outputs.at(1) : outputs.at(0)); + QVERIFY(master->moveResizeOutput() != first); + QTRY_VERIFY(!facade.hasTree()); + QVERIFY(master->m_transientOwnership); + QCOMPARE(master->m_transientOwnership->phase, Window::TransientOwnershipPhase::Restoring); + QVERIFY(answerAll()); + QCOMPARE(master->nextTargetScale(), to); + // Where the transfer left the window is the tile's size, carried along. + QVERIFY2(!exactFrame(master->frameGeometry(), floating), frameText(master->frameGeometry()).constData()); + QCOMPARE(facade.settleWindow(master, token), int(Settle::AtManagedRectangle)); + + // The record: the corner moved to the other output, the size as it was taken. + const QRectF carried(master->m_transientOwnership->floating); + QCOMPARE(workspace()->outputAt(carried.center()), master->moveResizeOutput()); + QVERIFY2(sameSize(carried.size(), floating.size()), frameText(carried).constData()); + // What the policy is given: that frame where the window reaches it, otherwise the same + // corner with a size the window takes there. + const QRectF expected = facade.floatingFrame(master); + QVERIFY2(exactFrame(expected, QRectF(master->transientFloatingImage(RectF(carried)))), frameText(expected).constData()); + QCOMPARE(exactFrame(expected, carried), from == to); + QVERIFY2(expected.topLeft() == carried.topLeft(), frameText(expected).constData()); + // A frame the window takes: asked for again, it is itself. + QVERIFY2(exactFrame(QRectF(master->transientFloatingImage(RectF(expected))), expected), frameText(expected).constData()); + // Within a device pixel and a step of the client's grid of the size it was. + QVERIFY2(std::abs(expected.width() - carried.width()) < 1 + 1 / to && std::abs(expected.height() - carried.height()) < 1 + 1 / to, + frameText(expected).constData()); + + master->moveResize(expected); + if (toplevel->hasPendingNativeGeometry()) { + QCOMPARE(facade.settleWindow(master, token), int(Settle::Unsettled)); + QVERIFY(answerAll()); + } + // The window stands on what it was asked for: frame and requested geometry. + QVERIFY2(exactFrame(master->frameGeometry(), expected), frameText(master->frameGeometry()).constData()); + QVERIFY2(exactFrame(QRectF(master->moveResizeGeometry()), expected), frameText(QRectF(master->moveResizeGeometry())).constData()); + QSignalSpy settled(master, &Window::transientReleaseSettled); + QCOMPARE(facade.settleWindow(master, token), int(Settle::Settled)); + QCOMPARE(settled.count(), 1); + QVERIFY(!master->m_transientOwnership); + QVERIFY(!facade.floatingFrame(master).isValid()); + + // The window is free again: the policy records where it settled, a new session admits it + // there and the explicit restore returns it to that frame. + QCOMPARE(facade.record(token, 2, master, scriptRecord(expected), 2), int(TransientPlacementWriter::Result::Accepted)); + const auto second = master->moveResizeOutput(); + const QVariantMap again = facade.createTile(second, VirtualDesktopManager::self()->currentDesktop(second), QRectF(0, 0, 0.5, 1)); + QCOMPARE(again.value(QStringLiteral("result")).toInt(), int(TransientTileController::Result::Accepted)); + QCOMPARE(facade.admitTile(again.value(QStringLiteral("generation")).toInt(), master, token), int(TransientTileController::Result::Accepted)); + QVERIFY(answerAll()); + QVERIFY(master->tile()); + QVERIFY(exactFrame(facade.floatingFrame(master), expected)); + QVERIFY(facade.restoreWindow(master)); + QVERIFY(answerAll()); + QVERIFY2(exactFrame(master->frameGeometry(), expected), frameText(master->frameGeometry()).constData()); + QVERIFY(!master->m_transientOwnership); + QTRY_VERIFY(!facade.hasTree()); +} + void TestXdgShellWindow::testTransientTilingFacadeProviderKeepsRecording_data() { QTest::addColumn("moment"); diff --git a/autotests/test_transientplacementwriter.cpp b/autotests/test_transientplacementwriter.cpp index 818a2ce1c5..792d80fd37 100644 --- a/autotests/test_transientplacementwriter.cpp +++ b/autotests/test_transientplacementwriter.cpp @@ -40,6 +40,8 @@ private Q_SLOTS: void reservationRequiresExactPersistedFrame(); void memoryFrameIsTheProvidersWholePixelImage(); void deferredManualWinnerSurvivesCompletion(); + void restoredFrameDoesNotReplaceASiblingsUpdate_data(); + void restoredFrameDoesNotReplaceASiblingsUpdate(); void failedFlushIsNotRetriedByDestructor(); void failedReservationDoesNotHoldKey(); void duplicateGlobalKeyRefusesReservation(); @@ -292,6 +294,66 @@ void TransientPlacementWriterTest::deferredManualWinnerSurvivesCompletion() QCOMPARE(writer.snapshot().entries.value(QStringLiteral("app")), later); } +void TransientPlacementWriterTest::restoredFrameDoesNotReplaceASiblingsUpdate_data() +{ + QTest::addColumn("order"); + QTest::addColumn("destroyed"); + QTest::newRow("restored-frame-after-the-sibling") << 0 << false; + QTest::newRow("restored-frame-after-the-sibling, owner destroyed") << 0 << true; + QTest::newRow("sibling-after-the-restored-frame") << 1 << false; + QTest::newRow("owner-moved-by-hand-after-the-sibling") << 2 << false; +} + +void TransientPlacementWriterTest::restoredFrameDoesNotReplaceASiblingsUpdate() +{ + // While the owner restores, the key has one deferred slot. A sibling's manual update in it + // is the user's newer placement; the owner's own record of the frame it is restored to (the + // reservation's frame, which the provider re-records when it lands) is not, and must not + // take the slot from it whatever its sequence. A frame the released owner was moved to by + // hand is a manual update like any other: the latest wins. + QFETCH(int, order); + QFETCH(bool, destroyed); + using Result = TransientPlacementWriter::Result; + QTemporaryDir directory; + QVERIFY(directory.isValid()); + TransientPlacementWriter writer(directory.filePath(QStringLiteral("placementrc"))); + QObject provider; + const auto token = writer.registerProvider(&provider); + const auto owner = QUuid::createUuid(); + const auto sibling = QUuid::createUuid(); + const QRectF original(73, 91, 430, 310); + const QRectF manual(200, 100, 600, 400); + const QRectF moved(260, 140, 500, 380); + QVERIFY(writer.trackWindow(owner, QStringLiteral("app"))); + QCOMPARE(writer.record(token, 1, {owner, original, 1, TransientPlacementWriter::Origin::Manual}), Result::Accepted); + QCOMPARE(writer.reserve(token, reservation(writer, owner, original)), Result::Accepted); + QCOMPARE(writer.activateReservation(token, owner, QStringLiteral("generation-1")), Result::Accepted); + QCOMPARE(writer.beginRestore(owner, QStringLiteral("generation-1")), Result::Accepted); + QVERIFY(writer.trackWindow(sibling, QStringLiteral("app"))); + QRectF expected; + if (order == 0) { + QCOMPARE(writer.record(token, 2, {sibling, manual, 20, TransientPlacementWriter::Origin::Manual}), Result::Deferred); + QCOMPARE(writer.record(token, 3, {owner, original, 30, TransientPlacementWriter::Origin::Manual}), Result::Suppressed); + expected = manual; + } else if (order == 1) { + QCOMPARE(writer.record(token, 2, {owner, original, 20, TransientPlacementWriter::Origin::Manual}), Result::Deferred); + QCOMPARE(writer.record(token, 3, {sibling, manual, 30, TransientPlacementWriter::Origin::Manual}), Result::Deferred); + expected = manual; + } else { + QCOMPARE(writer.record(token, 2, {sibling, manual, 20, TransientPlacementWriter::Origin::Manual}), Result::Deferred); + QCOMPARE(writer.record(token, 3, {owner, moved, 30, TransientPlacementWriter::Origin::Manual}), Result::Deferred); + expected = moved; + } + QCOMPARE(writer.snapshot().entries.value(QStringLiteral("app")), original); + if (destroyed) { + QVERIFY(writer.forgetWindow(owner)); + } else { + QCOMPARE(writer.completeRestore(owner, QStringLiteral("generation-1")), Result::Accepted); + } + QVERIFY(!writer.isReserved(owner)); + QCOMPARE(writer.snapshot().entries.value(QStringLiteral("app")), expected); +} + void TransientPlacementWriterTest::failedFlushIsNotRetriedByDestructor() { QTemporaryDir directory; diff --git a/src/scripting/transienttilingapi.cpp b/src/scripting/transienttilingapi.cpp index 68a3ebe815..052f8c1a1f 100644 --- a/src/scripting/transienttilingapi.cpp +++ b/src/scripting/transienttilingapi.cpp @@ -228,6 +228,22 @@ void TransientTilingApi::restoreWindow(Window *window) } } +int TransientTilingApi::settleWindow(Window *window, const QString &providerToken) +{ + if (!m_workspace || !window || window->isDeleted()) { + return -1; + } + return static_cast(window->settleReleasedTransientOwnership(QUuid(providerToken))); +} + +QRectF TransientTilingApi::floatingFrame(Window *window) const +{ + if (!m_workspace || !window || window->isDeleted()) { + return {}; + } + return QRectF(window->transientFloatingFrame()); +} + QRectF TransientTilingApi::masterWindowRect() const { auto controller = m_workspace ? m_workspace->transientTileController() : nullptr; diff --git a/src/scripting/transienttilingapi.h b/src/scripting/transienttilingapi.h index 53297d86c7..a3d082ea64 100644 --- a/src/scripting/transienttilingapi.h +++ b/src/scripting/transienttilingapi.h @@ -111,6 +111,31 @@ class TransientTilingApi : public QObject */ Q_INVOKABLE void restoreWindow(Window *window); + /** + * End the record of a window the native layer released without restoring it (a removed + * desktop or output, a second window of its app, a move of the window, a change of the + * work area): the master the tiler has since given its floating frame back with an + * ordinary request for floatingFrame(), or one the user moved or resized. The native + * layer settles it only for the provider that admitted the window, once the window's + * geometry is settled and the window stands on its floating frame or where the user's + * own interactive move or resize left it; any other frame may be an automatic rectangle + * and never reaches the memory. Returns Window::TransientSettleResult: 0 settled (the + * window can be admitted again), 1 nothing to settle, 2 another provider's, 3 not settled + * yet (a configure the client has not answered, or the window is maximized, full screen + * or tiled: ask again), 4 neither on its floating frame nor placed by the user, 5 refused + * by the writer; -1 without a window. + */ + Q_INVOKABLE int settleWindow(Window *window, const QString &providerToken); + + /** + * The floating frame of @p window's transient record: of an owned window the frame an + * explicit restoration returns it to; of a released one that frame as the window takes + * it on the output it is on now (it follows the window to another output, and on an + * output of another scale its size is on that output's pixels) - the rectangle to ask + * for before settleWindow(). Null when the window has no record. + */ + Q_INVOKABLE QRectF floatingFrame(Window *window) const; + /** * The transient leaf's absolute window geometry (the master's placed rectangle, * including the tile padding and strut adjustments). diff --git a/src/tiles/transientplacementwriter.cpp b/src/tiles/transientplacementwriter.cpp index 33576b5c46..4e01dd015b 100644 --- a/src/tiles/transientplacementwriter.cpp +++ b/src/tiles/transientplacementwriter.cpp @@ -254,6 +254,19 @@ TransientPlacementWriter::Result TransientPlacementWriter::record(const QUuid &p if (reservation->deferred && update.eventSequence <= reservation->deferred->eventSequence) { return Result::OutdatedEvent; } + if (reservation->phase == Phase::Restoring && reservation->owner == update.window + && reservation->deferred && reservation->deferred->window != update.window + && sameFrame(update.frame, reservation->original)) { + // The owner's record of the frame it is restored to (the reservation's own frame, + // re-recorded when it lands) is not a newer placement: a sibling's manual update + // keeps the one deferred slot whatever the sequences say. A frame the released + // owner was moved to by hand differs from it and takes the slot as the latest. + // An owner moved by hand back onto exactly that frame is not told apart from the + // landing and is suppressed too: the sibling's update is the newer placement + // either way. After a transfer to another output the frame the owner is restored + // to is no longer the reservation's, and the latest record wins as before. + return Result::Suppressed; + } reservation->deferred = update; return Result::Deferred; } diff --git a/src/tiles/transienttilecontroller.cpp b/src/tiles/transienttilecontroller.cpp index 482abcde41..545c75e5fe 100644 --- a/src/tiles/transienttilecontroller.cpp +++ b/src/tiles/transienttilecontroller.cpp @@ -582,7 +582,8 @@ bool TransientTileController::admissionCurrent(Window *window, quint64 generatio if (m_ownershipGeneration) { const auto &ownership = *window->m_transientOwnership; if (!window->transientProviderIsCurrent(ownership) - || !sameFrame(ownership.target, m_tile->windowGeometry()) || !window->isTransientTileTargetFeasible(ownership.target)) { + || !window->isTransientTileFrame(ownership.target, m_tile->windowGeometry()) + || !window->isTransientTileTargetFeasible(ownership.target)) { return false; } if (ownership.progress == Window::TransientRequestProgress::NoRequest @@ -616,7 +617,7 @@ TransientTileController::Result TransientTileController::admit(quint64 generatio // then the headroom for the whole synchronous path and its failure cleanup, a known shortage // refused rather than superseded after a write. if (!window || !writer || !activity || key.isEmpty() || placementKey(window) != key || !freeOfNativeMembership(window) - || !window->transientAdmissionEligible(m_tile->windowGeometry())) { + || !window->transientAdmissionEligible(window->transientTileFrame(m_tile->windowGeometry()))) { return Result::Ineligible; } if (!window->transientAdmissionHeadroom()) { diff --git a/src/window.cpp b/src/window.cpp index 55090e7622..a5ea542cce 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -68,6 +68,18 @@ bool exactFrameGeometry(const RectF &left, const RectF &right) return left.x() == right.x() && left.y() == right.y() && left.width() == right.width() && left.height() == right.height(); } +// The same frame up to the arithmetic's last bits. Where an output's pixel is not a binary +// fraction (125%, 150%), the frame a commit publishes and the rectangle that was asked for can +// differ there; frames that are different on any output differ by far more. The tolerance is +// in logical pixels (the comparison knows no output), below a thousandth of one. +bool sameFrameGeometry(const RectF &left, const RectF &right) +{ + const auto same = [](qreal a, qreal b) { + return std::abs(a - b) < 1.0 / 1024; + }; + return same(left.x(), right.x()) && same(left.y(), right.y()) && same(left.width(), right.width()) && same(left.height(), right.height()); +} + quint64 nextAssociationChange(quint64 current) { // Saturate rather than allowing an old pending notification to match a wrapped identity. @@ -1078,6 +1090,7 @@ bool Window::startInteractiveMoveResize() m_interactiveMoveResize.initialMaximizeMode = requestedMaximizeMode(); m_interactiveMoveResize.initialQuickTileMode = requestedQuickTileMode(); m_interactiveMoveResize.initialGeometryRestore = geometryRestore(); + m_interactiveMoveResize.stepped = false; updateElectricGeometryRestore(); performDelayedRaise(); @@ -1129,6 +1142,14 @@ void Window::finishInteractiveMoveResize(bool cancel) workspace()->outline()->hide(); m_interactiveMoveResize.counter++; + if (!cancel && m_interactiveMoveResize.stepped && m_transientOwnership + && m_transientOwnership->phase == TransientOwnershipPhase::Restoring) { + // The operation's own first request released the record; where it ended is the place + // the user chose, for as long as no later request moves the window. A cancelled one + // put the window back, and one that moved nothing (a press held and released on a + // window an earlier release left on an automatic rectangle) chose no place. + m_transientOwnership->placedByUser = m_geometryRequestGeneration; + } Q_EMIT interactiveMoveResizeFinished(); } @@ -1223,6 +1244,7 @@ void Window::updateInteractiveMoveResize(const QPointF &global, Qt::KeyboardModi } } + m_interactiveMoveResize.stepped = true; doInteractiveResizeSync(nextMoveResizeGeom); Q_EMIT interactiveMoveResizeStepped(nextMoveResizeGeom); } @@ -1254,6 +1276,7 @@ void Window::updateInteractiveMoveResize(const QPointF &global, Qt::KeyboardModi } } + m_interactiveMoveResize.stepped = true; move(nextMoveResizeGeom.topLeft()); Q_EMIT interactiveMoveResizeStepped(nextMoveResizeGeom); } @@ -4959,7 +4982,8 @@ Window::TransientTileRequest::TransientTileRequest(Window *window, Tile *tile, q , m_initialGeometryGeneration(window ? window->geometryRequestGeneration() : 0) { if (m_tile && m_manager && m_output && m_desktop) { - m_target = m_tile->windowGeometry(); + // The window's frame in the tile: the tile's rectangle where the window reaches it. + m_target = window ? window->transientTileFrame(m_tile->windowGeometry()) : m_tile->windowGeometry(); } } @@ -5247,6 +5271,78 @@ Window::TransientRestoreConsumption Window::consumeExplicitUntileOrRestore() return TransientRestoreConsumption::RestoreStopped; } +Window::TransientSettleResult Window::settleReleasedTransientOwnership(const QUuid &provider) +{ + if (isDeleted() || !m_transientOwnership) { + return TransientSettleResult::NotReleased; + } + const auto &ownership = *m_transientOwnership; + const auto generation = ownership.generation; + // A released record whose tile request was made; a live restoration completes at its own + // boundary (completeTransientRestoration), a superseded or cancelled one never does. + if (ownership.phase != TransientOwnershipPhase::Restoring || ownership.releasing || ownership.executing + || ownership.request || m_activeTransientRequest || m_requestedTransientTileGeneration + || ownership.progress != TransientRequestProgress::Requested + || (ownership.restoreAttempt && transientRestoreAttemptIsCurrent(generation, ownership.restoreAttempt->sequence))) { + return TransientSettleResult::NotReleased; + } + if (provider.isNull() || provider != ownership.provider || !ownership.writer || !ownership.writer->isProviderRegistered(provider) + || !ownership.writer->ownsRestoringReservation(internalId(), QString::number(generation))) { + return TransientSettleResult::StaleProvider; + } + // A maximized, full-screen or tiled window returns to the frame it had before, which for a + // released window is the automatic rectangle: its record waits for the ordinary state. + if (m_requestedTile || m_tile || !isTransientGeometrySettled()) { + return TransientSettleResult::Unsettled; + } + // Positive evidence only. The rectangle recorded at the admission is not the only + // automatic one a released window can stand on: a work-area re-check releases the window + // with a public request for the tile's rectangle as it is then, and a transfer to another + // output carries the tile's size along. The record ends on the floating frame, or where + // the user's own move or resize left the window. + // On an output of another scale than the one the floating frame was taken on, the + // window stands on that frame where it stands on its image there. + const RectF frame = frameGeometry(); + const bool onFloating = sameFrameGeometry(frame, ownership.floating) || sameFrameGeometry(frame, transientFloatingImage(ownership.floating)); + // The user's placement is evidence only while it is the window's last request: a later + // one (the workspace's re-check, a script's) has put the window somewhere nobody chose. + const bool placedByUser = ownership.placedByUser && *ownership.placedByUser == m_geometryRequestGeneration + && !sameFrameGeometry(frame, ownership.target); + if (!onFloating && !placedByUser) { + return TransientSettleResult::AtManagedRectangle; + } + if (ownership.writer->completeRestore(internalId(), QString::number(generation)) != TransientPlacementWriter::Result::Accepted) { + return TransientSettleResult::Refused; + } + ref(); + const auto lifetime = qScopeGuard([this]() { + unref(); + }); + // As after a completed restoration, the close cleanup stays connected: the writer forgets + // the tracked window at its destruction. + m_transientOwnership.reset(); + Q_EMIT transientReleaseSettled(QRectF(frame)); + return TransientSettleResult::Settled; +} + +RectF Window::transientFloatingFrame() const +{ + if (!m_transientOwnership) { + return RectF(); + } + // An owned record's frame is the restoration's exact target, and so is a live + // restoration's. Only a released record is returned by the policy's own request. + const auto &ownership = *m_transientOwnership; + const bool released = ownership.phase == TransientOwnershipPhase::Restoring + && !(ownership.restoreAttempt && transientRestoreAttemptIsCurrent(ownership.generation, ownership.restoreAttempt->sequence)); + return released ? transientFloatingImage(ownership.floating) : ownership.floating; +} + +RectF Window::transientFloatingImage(const RectF &floating) const +{ + return floating; +} + Window::TransientRestoreClaim Window::claimTransientRestoreWithMaximizeNormalization(quint64 generation) { return {}; @@ -5722,11 +5818,26 @@ bool Window::canStartTransientRestoreRequest(const RectF &target) const return false; } +bool Window::isTransientGeometrySettled() const +{ + return false; +} + bool Window::isTransientTileTargetFeasible(const RectF &target) const { return false; } +RectF Window::transientTileFrame(const RectF &tile) const +{ + return tile; +} + +bool Window::isTransientTileFrame(const RectF &frame, const RectF &tile) const +{ + return exactFrameGeometry(frame, transientTileFrame(tile)); +} + bool Window::transientRequestTargetIsCurrent(const TransientTileRequest &request) const { if (request.m_window != this || !request.m_tile || !request.m_manager || !request.m_output || !request.m_desktop @@ -5738,7 +5849,7 @@ bool Window::transientRequestTargetIsCurrent(const TransientTileRequest &request || moveResizeOutput() != request.m_output || !request.m_tile->isActive() || !isOnDesktop(request.m_desktop)) { return false; } - return exactFrameGeometry(request.m_target, request.m_tile->windowGeometry()) + return isTransientTileFrame(request.m_target, request.m_tile->windowGeometry()) && request.m_output->geometryF().contains(request.m_target) && isTransientTileTargetFeasible(request.m_target); } @@ -6092,10 +6203,17 @@ void Window::checkWorkspacePosition(RectF oldGeometry, LogicalOutput *oldOutput) moveResize(ensureSpecialStateGeometry(decision.geometry)); setFullscreenGeometryRestore(moveToArea(m_fullscreenGeometryRestore, decision.oldScreenArea, decision.screenArea)); setGeometryRestore(moveToArea(m_maximizeGeometryRestore, decision.oldScreenArea, decision.screenArea)); - return; + break; case WorkspacePositionDecision::Kind::Geometry: moveResize(decision.geometry); - return; + break; + } + // A released record's floating frame follows the window's screen as the restore fields + // above do, and as it does on a transfer (sendToOutput): an output that was removed or + // resized takes its coordinates with it, and a floating frame left on them is one the + // policy could never return the window to. Untouched while the screen is the same. + if (!isDeleted() && decision.oldScreenArea != decision.screenArea && decision.oldScreenArea.isValid() && decision.screenArea.isValid()) { + rebaseTransientFloatingOnOutputTransfer(decision.oldScreenArea, decision.screenArea); } } diff --git a/src/window.h b/src/window.h index caae1e39e3..e0b8145b2c 100644 --- a/src/window.h +++ b/src/window.h @@ -1414,6 +1414,43 @@ class KWIN_EXPORT Window : public QObject NoStart, }; TransientRestoreConsumption consumeExplicitUntileOrRestore(); + /** + * The end of a released record that no explicit restoration will complete. A release that + * is not the explicit restore is detach-only: the window stays where it stands and its + * record (Restoring) keeps the automatic rectangle out of the placement memory. The policy + * that admitted the window ends that record once the window stands on a frame that is + * known not to be automatic: its floating frame (the policy's own ordinary request for + * transientFloatingFrame(), or the compositor's replay of it), or where the user's own + * interactive move or resize left it and no later request has moved it. Any other frame + * can be an automatic rectangle (the + * tile's frame the window was admitted to, the tile's rectangle a work-area re-check asks + * for as it releases the window, either of them carried to another output) and is + * answered AtManagedRectangle. Settled only for the provider that admitted the window + * (StaleProvider otherwise, also when the writer no longer holds the record's + * reservation), with no live restoration (NotReleased), in an ordinary mode outside + * every tile and with the native geometry settled: nothing scheduled, sent or + * acknowledged, the frame the requested geometry (Unsettled otherwise). Every answer but + * Settled changes nothing. The writer finalizes as it does for a completed restoration: + * a deferred update for the key is applied, else the entry stays. + */ + enum class TransientSettleResult { + Settled, + NotReleased, + StaleProvider, + Unsettled, + AtManagedRectangle, + Refused, + }; + TransientSettleResult settleReleasedTransientOwnership(const QUuid &provider); + /** + * The floating frame of this window's transient record. Of an owned record: where an + * explicit restoration returns the window, exactly. Of a released one: that frame, + * carried along when the window was sent to another output or its output went, as the + * window takes it on the output it is on now (transientFloatingImage()) - the rectangle + * the policy asks for to return the window, and on which the record settles. Null + * without a record. + */ + RectF transientFloatingFrame() const; /** * NT04's read-only state/completion contract. The state query reflects the native record * only - no protocol logging and no timeout participates - for the placement policy that @@ -1807,6 +1844,7 @@ protected Q_SLOTS: void readyForPaintingChanged(); void maximizeGeometryRestoreChanged(); void transientRestorationCompleted(const QRectF &restoredFrame); + void transientReleaseSettled(const QRectF &frame); void fullscreenGeometryRestoreChanged(); void offscreenRenderingChanged(); void targetScaleChanged(); @@ -2182,6 +2220,14 @@ protected Q_SLOTS: bool releasing = false; bool releaseWriterReady = false; bool committed = false; + // A released record's window was moved or resized by the user's own interactive + // operation, which ended without being cancelled: the window's geometry request + // generation at that end. An operation that moved nothing leaves no mark. While the + // generation is still the window's, where the window stands is + // the user's placement, not an automatic rectangle; any later request (the + // workspace's re-check, a script's) makes the evidence stale + // (settleReleasedTransientOwnership). + std::optional placedByUser; QMetaObject::Connection closeCleanup; std::shared_ptr noRequestProof; std::unique_ptr request; @@ -2405,8 +2451,40 @@ protected Q_SLOTS: quint64 requestedTransientTileGeneration() const; void commitTileFromConfigure(Tile *tile, quint64 transientGeneration); virtual bool canStartTransientTileRequest(const RectF &target) const; + // Nothing of the native geometry is in flight and the frame is the requested geometry. + virtual bool isTransientGeometrySettled() const; virtual bool canStartTransientRestoreRequest(const RectF &target) const; virtual bool isTransientTileTargetFeasible(const RectF &target) const; + /** + * The frame this window takes in a transient tile. A tile's rectangle is whole logical + * pixels; a frame is on the output's device pixels over a client size on the client's own + * grid, and at a fractional scale most whole rectangles are neither. The window takes the + * tile's top-left corner and the largest size it reaches inside the tile, which is the + * rectangle itself where that is reachable. A rectangle nothing inside which is reachable + * is returned as it is, and refused where its feasibility is checked. + */ + virtual RectF transientTileFrame(const RectF &tile) const; + /** + * Whether @p frame is a frame this window takes in a tile of rectangle @p tile: the one + * transientTileFrame() gives now, or one it gave under other borders. A decoration that + * changes its borders when its window is tiled changes, in the middle of the admission, + * which sizes the client reaches; the request's frame is then still at the tile's corner + * and inside the tile, short of it by no more than a fit ever is. Reachability is the + * caller's separate check (isTransientTileTargetFeasible). + */ + virtual bool isTransientTileFrame(const RectF &frame, const RectF &tile) const; + /** + * The frame a request for a transient record's floating frame gives this window on the + * output it is on now. The floating frame was a frame of the window where it was taken. + * Carried to an output of another scale (sendToOutput, a removed output) its size is not + * on the pixels there and the window cannot take it: a request for it ends with the + * frame and the requested geometry on its image, which is not the floating frame, so a + * released window asked for it stands on a rectangle nothing vouches for, however often + * it is asked. This is @p floating where the window reaches it, and otherwise its corner + * with its size taken to this output's pixels and to the client's grid and back, as a + * request and its commit take it. + */ + virtual RectF transientFloatingImage(const RectF &floating) const; // Backend hooks of requestMoveResizeForAssignment(). Support is consulted before any mutation; // a dispatcher reports what it staged or published through the stack-bound request. virtual bool supportsAssignmentGeometryRequest() const; @@ -2720,6 +2798,9 @@ protected Q_SLOTS: RectF initialScreenArea; QTimer *delayedTimer = nullptr; uint32_t counter = 0; + // Whether the operation has moved or resized the window: one that started and ended + // where the window stood (a press on the title bar held and released) chose no place. + bool stepped = false; MaximizeMode initialMaximizeMode; QuickTileMode initialQuickTileMode; } m_interactiveMoveResize; diff --git a/src/xdgshellwindow.cpp b/src/xdgshellwindow.cpp index 9395bd267e..ba212ea345 100644 --- a/src/xdgshellwindow.cpp +++ b/src/xdgshellwindow.cpp @@ -53,6 +53,35 @@ namespace KWin static bool changeMaximizeRecursion = false; static bool isFiniteExtent(const RectF &rect); +// Whether two logical lengths are the same number of an output's pixels. A size read back from +// a rectangle is the difference of two corners, and a frame's size is a client size plus its +// borders: wherever the output's pixel is not a binary fraction (0.8 at 125%, two thirds at +// 150%) either differs in its last bits from the number the pixel grid itself gives, and an +// exact comparison of the two refuses a frame the window has had all along. Sizes that are +// different on the grid differ by a whole pixel; the tolerance is far below that and far above +// the arithmetic's noise. +static bool samePixels(qreal left, qreal right, qreal scale) +{ + return std::abs(left - right) * scale < 1.0 / 1024; +} + +static bool samePixels(const QSizeF &left, const QSizeF &right, qreal scale) +{ + return samePixels(left.width(), right.width(), scale) && samePixels(left.height(), right.height(), scale); +} + +// Smaller by a pixel's worth, not by the last bits of a read-back size. +static bool belowPixels(qreal length, qreal limit, qreal scale) +{ + return length < limit && !samePixels(length, limit, scale); +} + +// The same rectangle on an output's pixels. +static bool samePixels(const RectF &left, const RectF &right, qreal scale) +{ + return samePixels(left.x(), right.x(), scale) && samePixels(left.y(), right.y(), scale) && samePixels(left.size(), right.size(), scale); +} + XdgSurfaceWindow::XdgSurfaceWindow(XdgSurfaceInterface *shellSurface) : WaylandWindow(shellSurface->surface()) , m_shellSurface(shellSurface) @@ -299,15 +328,24 @@ void XdgSurfaceWindow::dispatchMoveResize(const RectF &rect, MoveResizeMode mode } else { // The request is resolved here, with no configure and nothing for the client to // commit: what is published is the request's image on the output's pixels, and - // the requested geometry follows it, as it does when a configure's commit lands - // (synchronizeCommittedGeometry). Left at the rectangle that was asked for, it - // would differ from the frame until the client's next commit, and the window - // would read as unsettled for that long. Half a pixel must not carry the - // center to another output: there the request stays as it was asked. A guarded + // the requested geometry follows it through the synchronization a configure's + // commit uses (synchronizeCommittedGeometry). Left at the rectangle that was + // asked for, it would differ from the frame until the client's next commit, and + // the window would read as unsettled for that long. Where half a pixel carries + // the image's center to another output, the synchronization takes the output + // and its properties with it, as it does for a committed frame. A guarded // request keeps its rectangle on purpose: its caller compares the requested // geometry with the rectangle it asked for (a tile's, a restoration's). - if (target != rect && workspace()->outputAt(target.center()) == moveResizeOutput()) { - setMoveResizeGeometry(target); + if (target != rect) { + const auto requestIsCurrent = [this, generation]() { + return geometryRequestIsCurrent(generation) && transientGeometryRequestIsCurrent(generation); + }; + // A refused synchronization leaves the request as it was asked; a newer + // request from one of its callbacks ends this one. + if (synchronizeConfigureGeometry(target, m_geometryRequestOrigin, requestIsCurrent).outcome != AssignmentStepOutcome::AppliedCurrent + && !current()) { + return; + } } updateGeometry(target); } @@ -887,6 +925,18 @@ void XdgToplevelWindow::handleRoleCommit() } } + // A frame that answers a request is that request. It is put together where the window + // stands and then moved to the configure's place, so where the output's pixel is not a + // binary fraction its corners can be a last bit away from the rectangle the configure was + // sent for, though it is that rectangle on the output's pixels: a restoration from a tile + // at 542,362 came back 200.79999999999995 wide for a floating frame of 200.8 at 125%. The + // configure's own rectangle is published then, so a restoration returns the floating + // frame it saved bit for bit, and a tiled window stands on the frame it was admitted to. + if (placement && !fullscreen && !isInteractiveMove() && frameGeometry != placement->bounds + && samePixels(frameGeometry, placement->bounds, targetScale())) { + frameGeometry = placement->bounds; + } + const bool synchronized = synchronizeCommittedGeometry(frameGeometry, origin, generation); if (!configureCommitIsCurrent(generation)) { return; @@ -936,6 +986,13 @@ bool XdgToplevelWindow::canStartTransientTileRequest(const RectF &target) const return !hasPendingNativeGeometry() && !requestedTile() && !tile() && isTransientTileTargetFeasible(target); } +bool XdgToplevelWindow::isTransientGeometrySettled() const +{ + return m_isInitialized && readyForPainting() && !isInteractiveMoveResize() && !hasPendingNativeGeometry() + && !isFullScreen() && !isRequestedFullScreen() && maximizeMode() == MaximizeRestore && requestedMaximizeMode() == MaximizeRestore + && frameGeometry() == moveResizeGeometry(); // RectF: an exact comparison +} + bool XdgToplevelWindow::canStartTransientRestoreRequest(const RectF &target) const { // Pending admission geometry is allowed. Do not clear a pending configure gravity (one an @@ -964,6 +1021,104 @@ bool XdgToplevelWindow::isTransientTileTargetFeasible(const RectF &target) const return isTransientRestoreFrameFeasible(target); } +RectF XdgToplevelWindow::transientTileFrame(const RectF &tile) const +{ + if (isDeleted() || !surface() || isTransientRestoreFrameFeasible(tile)) { + return tile; + } + const qreal scale = nextTargetScale(); + const qreal clientScale = surface()->serverScale() * surface()->compositorToClientScale(); + for (qreal value : {tile.x(), tile.y(), tile.width(), tile.height(), scale, clientScale}) { + if (!std::isfinite(value)) { + return tile; + } + } + if (tile.width() <= 0 || tile.height() <= 0 || scale <= 0 || clientScale <= 0) { + return tile; + } + // The size a request for the tile would publish is the tile's size taken to the output's + // pixels, to the client's grid and back (dispatchMoveResize, then the commit), and each of + // those roundings can go up: a 892-high tile at 133% under a 50.25 title bar asks the + // client for 842, which commits a 892.5-high frame. So the size is asked one device pixel + // smaller on an axis for as long as its image is larger than the tile there. An image is + // at most half a step of the client's grid and one device pixel over what was asked, which + // bounds the steps and keeps the fit within a step of that grid and two device pixels of + // the tile. + // Counted in whole device pixels, so that every step is one of the grid's own numbers. + // Bounded whatever the two scales are. + const int steps = int(std::min(std::ceil(scale / clientScale), 64.0)) + 2; + qreal width = std::round(tile.width() * scale); + qreal height = std::round(tile.height() * scale); + for (int step = 0; step <= steps && width > 0 && height > 0; ++step) { + const QSizeF client = surface()->snappedSize(nextFrameSizeToClientSize(QSizeF(width / scale, height / scale))); + const QSizeF image = nextClientSizeToFrameSize(snapToPixels(client, scale)); + const bool wide = image.width() > tile.width() && !samePixels(image.width(), tile.width(), scale); + const bool tall = image.height() > tile.height() && !samePixels(image.height(), tile.height(), scale); + if (!wide && !tall) { + // Inside the tile. Not reachable only where the client's size limits say so: a + // minimum above the tile, or a maximum below it. The fit is the tile's size on + // the window's pixels, never a smaller window in a corner of the tile (the + // request's frame is checked against the tile by that nearness, + // isTransientTileFrame), so such a tile is refused, as it was. + const RectF frame(tile.topLeft(), image); + return isTransientRestoreFrameFeasible(frame) ? frame : tile; + } + if (wide) { + --width; + } + if (tall) { + --height; + } + } + return tile; +} + +RectF XdgToplevelWindow::transientFloatingImage(const RectF &floating) const +{ + // The frame itself wherever the window reaches it: on the output it was taken on, and on + // any other whose pixels it is on. + if (isDeleted() || !surface() || isTransientRestoreFrameFeasible(floating)) { + return floating; + } + const qreal scale = nextTargetScale(); + for (qreal value : {floating.x(), floating.y(), floating.width(), floating.height(), scale}) { + if (!std::isfinite(value)) { + return floating; + } + } + if (floating.width() <= 0 || floating.height() <= 0 || scale <= 0) { + return floating; + } + // What a request for the frame publishes (dispatchMoveResize, then the commit): the size + // to the output's pixels, to the client's grid and back. The corner stays. Where even + // that is not reachable (the client's size limits) the frame is left as it is. + const QSizeF client = surface()->snappedSize(nextFrameSizeToClientSize(snapToPixels(floating.size(), scale))); + const RectF image(floating.topLeft(), nextClientSizeToFrameSize(snapToPixels(client, scale))); + return isTransientRestoreFrameFeasible(image) ? image : floating; +} + +bool XdgToplevelWindow::isTransientTileFrame(const RectF &frame, const RectF &tile) const +{ + if (Window::isTransientTileFrame(frame, tile)) { + return true; + } + if (isDeleted() || !surface() || frame.x() != tile.x() || frame.y() != tile.y()) { + return false; + } + const qreal scale = nextTargetScale(); + const qreal clientScale = surface()->serverScale() * surface()->compositorToClientScale(); + if (!std::isfinite(scale) || !std::isfinite(clientScale) || scale <= 0 || clientScale <= 0) { + return false; + } + // A fit is short of its tile by less than a step of the client's grid and two device + // pixels on an axis (transientTileFrame). + const qreal slack = 1 / clientScale + 2 / scale; + const auto fits = [scale, slack](qreal size, qreal extent) { + return (size <= extent || samePixels(size, extent, scale)) && extent - size < slack; + }; + return fits(frame.width(), tile.width()) && fits(frame.height(), tile.height()); +} + bool XdgToplevelWindow::isTransientRestoreFrameFeasible(const RectF &target) const { for (qreal value : {target.x(), target.y(), target.width(), target.height()}) { @@ -974,12 +1129,14 @@ bool XdgToplevelWindow::isTransientRestoreFrameFeasible(const RectF &target) con if (target.width() <= 0 || target.height() <= 0) { return false; } - const auto constrained = constrainFrameSize(target.size()); - if (constrained.width() != target.width() || constrained.height() != target.height()) { + // Every comparison here is of sizes on the output's pixels (samePixels): the target's is + // read back from a rectangle, the others are computed on the grid. + const qreal scale = nextTargetScale(); + if (!samePixels(constrainFrameSize(target.size()), target.size(), scale)) { return false; } - const auto snappedFrame = snapToPixels(target.size(), nextTargetScale()); - if (snappedFrame.width() != target.width() || snappedFrame.height() != target.height()) { + const auto snappedFrame = snapToPixels(target.size(), scale); + if (!samePixels(snappedFrame, target.size(), scale)) { return false; } // The size a configure can carry is on the client's own grid (whole logical pixels without @@ -989,8 +1146,8 @@ bool XdgToplevelWindow::isTransientRestoreFrameFeasible(const RectF &target) con // snapped back to the output's pixels and decorated again - which is the path a geometry // request takes (moveResizeInternal, then the commit's clientGeometry). const auto client = surface()->snappedSize(nextFrameSizeToClientSize(snappedFrame)); - const auto roundTrip = nextClientSizeToFrameSize(snapToPixels(client, nextTargetScale())); - return roundTrip.width() == target.width() && roundTrip.height() == target.height(); + const auto roundTrip = nextClientSizeToFrameSize(snapToPixels(client, scale)); + return samePixels(roundTrip, target.size(), scale); } std::function XdgToplevelWindow::transientRestoreStateConstraint(const RectF &target, bool normalizeMaximize, bool geometryReady) const @@ -1099,8 +1256,9 @@ Window::TransientRestoreClaim XdgToplevelWindow::claimTransientRestoreWithMaximi if (!isTransientRestoreFrameFeasible(target)) { return {}; } - } else if (!isFiniteExtent(target) || target.width() < minSize().width() || target.height() < minSize().height() - || snapToPixels(target.size(), nextTargetScale()) != target.size()) { + } else if (!isFiniteExtent(target) || belowPixels(target.width(), minSize().width(), nextTargetScale()) + || belowPixels(target.height(), minSize().height(), nextTargetScale()) + || !samePixels(snapToPixels(target.size(), nextTargetScale()), target.size(), nextTargetScale())) { // Maximized or fullscreen borders are not the borders that A will use. These are only necessary // checks; exact frame/client feasibility follows the same-decoration response. return {}; @@ -2374,7 +2532,7 @@ std::optional XdgToplevelWindow::ownedFullSc if (!transientOwnershipIsCurrent(generation)) { return std::nullopt; } - const RectF managed = m_transientOwnership->tile->windowGeometry(); + const RectF managed = transientTileFrame(m_transientOwnership->tile->windowGeometry()); if (!isFiniteExtent(managed)) { return std::nullopt; } @@ -2686,9 +2844,12 @@ bool XdgToplevelWindow::ownedMaximizeFeasible(const RectF &managed, MaximizeMode if (released == MaximizeRestore) { return true; } + // Compared on the output's pixels: the constrained size is put together from a client size + // and its borders, the managed one is read back from a rectangle (samePixels). const QSizeF constrained = constrainFrameSize(managed.size()); - return (!(released & MaximizeHorizontal) || constrained.width() == managed.width()) - && (!(released & MaximizeVertical) || constrained.height() == managed.height()); + const qreal scale = nextTargetScale(); + return (!(released & MaximizeHorizontal) || samePixels(constrained.width(), managed.width(), scale)) + && (!(released & MaximizeVertical) || samePixels(constrained.height(), managed.height(), scale)); } std::optional XdgToplevelWindow::ownedMaximizePlan(MaximizeMode mode, MaximizeMode oldMode, const RectF &restore) const @@ -2706,7 +2867,7 @@ std::optional XdgToplevelWindow::ownedMaximize if (!transientOwnershipIsCurrent(generation)) { return std::nullopt; } - const RectF managed = m_transientOwnership->tile->windowGeometry(); + const RectF managed = transientTileFrame(m_transientOwnership->tile->windowGeometry()); if (!ownedMaximizeFeasible(managed, mode, oldMode)) { return std::nullopt; } diff --git a/src/xdgshellwindow.h b/src/xdgshellwindow.h index 4e07002fe0..8508f4ecaa 100644 --- a/src/xdgshellwindow.h +++ b/src/xdgshellwindow.h @@ -239,8 +239,12 @@ class XdgToplevelWindow final : public XdgSurfaceWindow void doSetPreferredBufferTransform() override; void doSetPreferredColorDescription() override; bool canStartTransientTileRequest(const RectF &target) const override; + bool isTransientGeometrySettled() const override; bool canStartTransientRestoreRequest(const RectF &target) const override; bool isTransientTileTargetFeasible(const RectF &target) const override; + RectF transientTileFrame(const RectF &tile) const override; + bool isTransientTileFrame(const RectF &frame, const RectF &tile) const override; + RectF transientFloatingImage(const RectF &floating) const override; bool supportsAssignmentGeometryRequest() const override; private: