From 3beb1b82d9ceb015d877f2656dd18de0d51991b2 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 30 Oct 2026 12:42:03 -0700 Subject: [PATCH] feat(nix): a window the user moves to another display is a shared scenario hotplug.user_move moves a window with Platform.move_window to the second display and checks that the daemon has it on that monitor and every window is tiled on its display. The KDE suite runs it before the unplug and after the replug, and the extra sent_by_the_user is gone. --- docs/dev/suite-ledger.md | 4 +- .../lib/komorebi_vm/core/scenarios/hotplug.py | 72 ++++++++++++++++ .../platforms/kde/extras/hotplug.py | 86 ++----------------- nix/tests/vm/lib/tests/fake_platform.py | 30 ++++++- nix/tests/vm/lib/tests/test_moved.py | 19 ++++ nix/tests/vm/suites/hotplug/test.py | 9 +- 6 files changed, 133 insertions(+), 87 deletions(-) diff --git a/docs/dev/suite-ledger.md b/docs/dev/suite-ledger.md index 60dba88e..4d77d8ac 100644 --- a/docs/dev/suite-ledger.md +++ b/docs/dev/suite-ledger.md @@ -15,7 +15,7 @@ platform". The sections the shared scenario repeats are gone; the rest are `kde_ | 1, the daemon answers with both monitors; three windows on the left; the monitor's size; tiled | extra `setup` | | 2, the scale of the left output changes and returns | extra `scale` | | 3, `move-to-monitor 1`: containers, KWin's `OutputChange`, tiled on the right | extra `sent_by_the_daemon` | -| 4, the user moves a window to the right output | extra `sent_by_the_user` | +| 4, the user moves a window to the right output | shared `hotplug.user_move` (the suite runs it before the unplug) | | 5, one monitor after the unplug | shared "after a display is unplugged the daemon has one monitor fewer" | | 5, all three windows on the left monitor | shared "... the daemon still manages every window", and "every window is on the displays that are left" | | 5, tiled inside the screen that is left, not on each other | shared "... tiled and clear of each other" | @@ -23,7 +23,7 @@ platform". The sections the shared scenario repeats are gone; the rest are `kde_ | 6, two monitors again | shared "after a display is plugged in the daemon has a monitor for it again" | | 6, still three windows | shared "... still manages every window" | | 6, each window somewhere it can be seen | shared "... each where it can be seen" | -| 7, a window moved to the other output again is followed | extra `after` | +| 7, a window moved to the other output again is followed | shared `hotplug.user_move`, run again after the replug | | 8, the daemon runs and answers | extra `after` | Not kept: that the daemon's container count on the left monitor is 3 after the unplug. The shared diff --git a/nix/tests/vm/lib/komorebi_vm/core/scenarios/hotplug.py b/nix/tests/vm/lib/komorebi_vm/core/scenarios/hotplug.py index 15fadc47..2031e19f 100644 --- a/nix/tests/vm/lib/komorebi_vm/core/scenarios/hotplug.py +++ b/nix/tests/vm/lib/komorebi_vm/core/scenarios/hotplug.py @@ -104,3 +104,75 @@ def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: timeout=30, ), ) + + +def _display_of(frame: Rect, displays: list[Rect]) -> int | None: + """The display the middle of the frame is on.""" + x, y = frame.x + frame.w // 2, frame.y + frame.h // 2 + for i, d in enumerate(displays): + if d.x <= x < d.x + d.w and d.y <= y < d.y + d.h: + return i + return None + + +def _split(platform: Platform, clients: list[Client]) -> dict[int, list[Client]]: + """The windows on each display, by the middle of their frames.""" + displays = platform.displays() + found: dict[int, list[Client]] = {i: [] for i in range(len(displays))} + for client in clients: + window = platform.os_window(client) + if window is not None and (i := _display_of(window.frame, displays)) is not None: + found[i].append(client) + return found + + +def _containers(platform: Platform, monitor: int) -> int: + return len(platform.state().workspace(monitor=monitor).containers) + + +def _each_tiled_on_its_display(platform: Platform, clients: list[Client]) -> bool: + displays = platform.displays() + return all(_tiled_in(platform, on, displays[i]) for i, on in _split(platform, clients).items()) + + +def user_move(platform: Platform, checks: Checks, clients: list[Client]) -> None: + """A window that something other than the daemon moves to the other display is tiled on that + monitor, and the windows that are left are tiled on the first. + + `clients` are windows the daemon manages, each in a container of its own, on the first display, + two of them or more. A second display is plugged in first if the platform has only one. The + window that moves is the last of `clients` on the first display, and it stays on the second. + """ + if len(platform.displays()) < 2: + platform.add_display() + platform.wait_for(lambda: _monitors(platform) == len(platform.displays()), timeout=30) + displays = platform.displays() + on_first = _split(platform, clients)[0] + if len(on_first) < 2: + checks.skip("a window moved to the other display", "fewer than two windows on the first") + return + window = on_first[-1] + first, second = _containers(platform, 0), _containers(platform, 1) + # the daemon asks again for the frames of the windows it moved, for a little while after a + # command: a user moves a window later than that + platform.wait_for(lambda: False, timeout=2) + platform.move_window(window, displays[1].x + 180, displays[1].y + 300) + checks.check( + "the daemon has the window on the other monitor", + platform.wait_for( + lambda: ( + _containers(platform, 0) == first - 1 and _containers(platform, 1) == second + 1 + ), + timeout=30, + ), + ) + checks.check( + "every window is tiled inside the display it is on, and the moved one is on the second", + platform.wait_for( + lambda: ( + window in _split(platform, clients)[1] + and _each_tiled_on_its_display(platform, clients) + ), + timeout=30, + ), + ) diff --git a/nix/tests/vm/lib/komorebi_vm/platforms/kde/extras/hotplug.py b/nix/tests/vm/lib/komorebi_vm/platforms/kde/extras/hotplug.py index df424211..683e9e0b 100644 --- a/nix/tests/vm/lib/komorebi_vm/platforms/kde/extras/hotplug.py +++ b/nix/tests/vm/lib/komorebi_vm/platforms/kde/extras/hotplug.py @@ -1,16 +1,15 @@ """What only KDE can tell about monitors that change, round the shared `hotplug` scenario. A scale that `kscreen-doctor` changes reaches the daemon as a change of the monitor's size; KWin -tells the daemon that a window is on another output, whether the daemon or the user sent it -there; and what the daemon believes of its monitors is what the compositor has. +tells the daemon that a window is on another output when the daemon sent it there; and what the +daemon believes of its monitors is what the compositor has. -The suite runs `setup`, `scale`, `sent_by_the_daemon` and `sent_by_the_user`, then the shared -scenario, which unplugs a display and plugs it in again, then `after`. The daemon runs on a -session with two outputs, and `clients` are three windows on the left one. +The suite runs `setup`, `scale` and `sent_by_the_daemon`, then the shared `user_move`, then the +shared `run`, which unplugs a display and plugs it in again, then `user_move` once more, then +`after`. The daemon runs on a session with two outputs, and `clients` are three windows on the +left one. """ -import time - from komorebi_vm import daemon from komorebi_vm.core.checks import Checks from komorebi_vm.core.geometry import Rect @@ -60,29 +59,6 @@ def _output_changes(platform: KdePlatform) -> int: return sum(1 for e in events if e.kind == "OutputChange") -def _let_komorebi_finish(platform: KdePlatform) -> None: - """Waits until the bridge has stopped asking again for the position of the windows the daemon - just moved. - - The bridge asks again for a second after each command (`WANT_MS` in `bridge.js`), which puts - back a window that somebody moves in that time. A user moves a window later than that, so a - test that plays the user does too. - """ - time.sleep(2 * platform.kde.scale) - - -def _show(platform: KdePlatform, clients: list[Client], what: str) -> None: - """Prints where the daemon has the windows and where KWin has them, for a failure to be read.""" - state = f"{_containers_on(platform, 0)} on the left monitor, {_containers_on(platform, 1)} on" - frames = ", ".join( - f"{c.title} {r}" for c, r in zip(clients, _rects(platform, clients), strict=True) - ) - print(f" {what}: komorebi has {state} the right; KWin has {frames}") - log = platform.kde.komorebi_log().splitlines() - lines = [x[-160:] for x in log if "OutputChange" in x or "following them" in x][-8:] - print(" " + "\n ".join(lines)) - - def setup(platform: KdePlatform, checks: Checks, clients: list[Client]) -> None: """The daemon has a monitor for each of the two outputs, with the size of its output, and the windows are on the left one.""" @@ -164,45 +140,10 @@ def sent_by_the_daemon(platform: KdePlatform, checks: Checks, clients: list[Clie ) -def _send_by_the_user(platform: KdePlatform, clients: list[Client]) -> Client: - """Moves a window that is on the left output to the right one, as a shortcut that sends a - window to the next screen does: not by the daemon.""" - right = _on_the_right(platform, clients) - window = next(c for c in clients if c not in right) - _let_komorebi_finish(platform) - platform.move_window(window, platform.size[0] + 180, 300) - return window - - -def sent_by_the_user(platform: KdePlatform, checks: Checks, clients: list[Client]) -> None: - """A window the user moves to the other output is tiled on that monitor.""" - _send_by_the_user(platform, clients) - checks.check( - "the daemon has one container on the left monitor and two on the right", - platform.wait_for( - lambda: _containers_on(platform, 0) == 1 and _containers_on(platform, 1) == 2 - ), - ) - checks.check( - "the two are tiled on the right output, and the third fills the left one", - platform.wait_for(lambda: _split_tiled(platform, clients, 2)), - ) - _show(platform, clients, "after the user's move") - - -def _split_tiled(platform: KdePlatform, clients: list[Client], on_right: int) -> bool: - right = _on_the_right(platform, clients) - left = [c for c in clients if c not in right] - return ( - len(right) == on_right - and _tiled_in(platform, right, _monitor(platform, 1)) - and (not left or _tiled_in(platform, left, _monitor(platform, 0))) - ) - - def after(platform: KdePlatform, checks: Checks, clients: list[Client]) -> None: """After the display was unplugged and plugged in again the daemon is alive and agrees with - KWin on the focus, and a window the user moves to the other output is followed.""" + KWin on the focus. The shared `user_move` runs once more before it, for a window the user moves + to the other output.""" kde = platform.kde def focus_agrees() -> bool: @@ -217,17 +158,6 @@ def after(platform: KdePlatform, checks: Checks, clients: list[Client]) -> None: checks.check( "the daemon is alive and its idea of the focus is KWin's", platform.wait_for(focus_agrees) ) - on_right = len(_on_the_right(platform, clients)) - _send_by_the_user(platform, clients) - checks.check( - "the daemon follows a window moved to the other output again", - platform.wait_for(lambda: _containers_on(platform, 1) == on_right + 1), - ) - checks.check( - "it is tiled on the right output, and the others on the left", - platform.wait_for(lambda: _split_tiled(platform, clients, on_right + 1)), - ) - _show(platform, clients, "after the second move") checks.check( "the daemon is running and answers", kde.komorebi_running() and kde.run("komorebic state")[0] == 0, diff --git a/nix/tests/vm/lib/tests/fake_platform.py b/nix/tests/vm/lib/tests/fake_platform.py index d2ef2d53..68e6acc9 100644 --- a/nix/tests/vm/lib/tests/fake_platform.py +++ b/nix/tests/vm/lib/tests/fake_platform.py @@ -75,6 +75,7 @@ class FakePlatform: clears_rules: bool = True, cycles_in_monocle: bool = True, no_op_in_monocle: bool = True, + follows_user_moves: bool = True, ) -> None: self.panel = panel """The height of a panel along the bottom of the first display, which the work area leaves @@ -83,6 +84,7 @@ class FakePlatform: """A daemon that tiles on the whole display, under the panel.""" self.second = second """The rectangle of the second display, when it is not one of the first's size.""" + self.follows_user_moves = follows_user_moves self.cycles_in_monocle = cycles_in_monocle self.no_op_in_monocle = no_op_in_monocle self.monocle_cycles = False @@ -179,6 +181,10 @@ class FakePlatform: def move_window(self, client: Client, x: int, y: int) -> None: here = self.os_window(client) assert here is not None + target = min(max(x, 0) // 1920, self.display_count - 1) + if self.follows_user_moves and target != self.display_of.get(client.title, 0): + self.display_of[client.title] = target + return self.moved[client.title] = Rect(x, y, here.frame.w, here.frame.h) @property @@ -484,8 +490,13 @@ class FakePlatform: focused = ( s == self.current and i == self.focuses[s] and self.spaces[s][i][0] == client.title ) - tile = self.tiles[i] elsewhere = self.display_of.get(client.title, 0) + rank = sum( + 1 + for stack in self.spaces[s][:i] + if stack and self.display_of.get(stack[0], 0) == elsewhere + ) + tile = self.tiles[rank] if client.title in self.moved: return OsWindow(client.title, self.moved[client.title], focused=focused) if elsewhere: @@ -758,13 +769,23 @@ class FakePlatform: "latest_layout": [rect(r) for r in self.tiles[: len(containers)]], } + def on_display(stacks: list[list[str]], display: int) -> list[list[str]]: + # a stack with no window is the first display's + return [ + stack + for stack in stacks + if (self.display_of.get(stack[0], 0) if stack else 0) == display + ] + monitor = { "id": 1, "size": rect(SCREEN), "work_area_size": rect(self.area), "workspaces": { "elements": [ - workspace(i, c, f, i == self.current) + workspace( + i, on_display(c, 0), min(f, len(on_display(c, 0)) - 1), i == self.current + ) for i, (c, f) in enumerate(zip(self.spaces, self.focuses, strict=True)) ], "focused": self.current, @@ -774,7 +795,10 @@ class FakePlatform: { "id": n + 1, "size": rect(self._others()[n - 1]), - "workspaces": {"elements": [workspace(0, [], 0, False)], "focused": 0}, + "workspaces": { + "elements": [workspace(0, on_display(self.containers, n), 0, False)], + "focused": 0, + }, } for n in range(1, 1 if self.forgets_displays else self.display_count) ] diff --git a/nix/tests/vm/lib/tests/test_moved.py b/nix/tests/vm/lib/tests/test_moved.py index 732eac8c..56be9740 100644 --- a/nix/tests/vm/lib/tests/test_moved.py +++ b/nix/tests/vm/lib/tests/test_moved.py @@ -6,6 +6,7 @@ from komorebi_vm.core.geometry import Rect from komorebi_vm.core.scenarios import ( commands, handover, + hotplug, monocle, placement, queries, @@ -182,3 +183,21 @@ def test_a_daemon_that_cycles_a_monocle_with_no_op_fails_monocle_behaviour() -> clients = opened(platform, checks) monocle.focus_behaviour(platform, checks, clients) assert any("with the no-op behaviour" in f for f in checks.failures), checks.failures + + +def test_a_daemon_that_follows_a_window_moved_to_the_other_display_passes() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + base = checks.passed + hotplug.user_move(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed - base == 2 + + +def test_a_daemon_that_ignores_a_window_moved_to_the_other_display_fails() -> None: + platform = FakePlatform(follows_user_moves=False) + checks = Checks() + clients = opened(platform, checks) + hotplug.user_move(platform, checks, clients) + assert any("the daemon has the window on the other monitor" in f for f in checks.failures) diff --git a/nix/tests/vm/suites/hotplug/test.py b/nix/tests/vm/suites/hotplug/test.py index a3f46e9e..c1c446f1 100644 --- a/nix/tests/vm/suites/hotplug/test.py +++ b/nix/tests/vm/suites/hotplug/test.py @@ -2,9 +2,9 @@ # `komorebi_vm.core.scenarios.hotplug`, which runs on Windows too: it unplugs a display, plugs one in # and sends a window to it, and asks the operating system where the windows are. What only a real # KWin can tell is in `komorebi_vm.platforms.kde.extras.hotplug`: that `kscreen-doctor` changing -# the scale of an output reaches komorebi as a change of that monitor's size, that KWin reports a -# window on another output whether komorebi or the user sent it there, and that a window the user -# moves to the other output is tiled on that monitor. `komorebi/tests/monitor_resize.rs`, +# the scale of an output reaches komorebi as a change of that monitor's size, and that KWin +# reports a window on another output when komorebi sent it there. A window the user moves to the +# other output is the shared `user_move`. `komorebi/tests/monitor_resize.rs`, # `komorebi/tests/hotplug.rs` and `komorebi/tests/follow_output.rs` check the same against the fake. from komorebi_vm import Checks, Kde from komorebi_vm.core.scenarios import hotplug @@ -31,8 +31,9 @@ run_suite( platform_only("kde", kde_hotplug.setup, platform, checks, clients), platform_only("kde", kde_hotplug.scale, platform, checks, clients), platform_only("kde", kde_hotplug.sent_by_the_daemon, platform, checks, clients), - platform_only("kde", kde_hotplug.sent_by_the_user, platform, checks, clients), + shared(hotplug.user_move, platform, checks, clients), shared(hotplug.run, platform, checks, clients), + shared(hotplug.user_move, platform, checks, clients), platform_only("kde", kde_hotplug.after, platform, checks, clients), )