diff --git a/nix/tests/vm/lib/komorebi_vm/core/scenarios/padding.py b/nix/tests/vm/lib/komorebi_vm/core/scenarios/padding.py new file mode 100644 index 00000000..94f249bb --- /dev/null +++ b/nix/tests/vm/lib/komorebi_vm/core/scenarios/padding.py @@ -0,0 +1,81 @@ +"""The padding of a workspace and of its containers moves the windows in from the edges and from +each other.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.geometry import Rect +from komorebi_vm.core.platform import Client, Platform +from komorebi_vm.core.scenarios.layouts import tiled + + +def run( + platform: Platform, + checks: Checks, + clients: list[Client], + configured: tuple[int, int] = (0, 0), +) -> None: + """Raises the workspace padding and then the container padding of the first workspace, which + has `clients` tiled in a container each, and sets them back to what the daemon says they were + (`configured` is the workspace and container padding for a daemon that does not say).""" + state = platform.state().workspace() + workspace = configured[0] if state.workspace_padding is None else state.workspace_padding + container = configured[1] if state.container_padding is None else state.container_padding + print(f" padding of the workspace {workspace}, of its containers {container}") + area = platform.work_area() + + def frames() -> list[Rect]: + return [w.frame for c in clients if (w := platform.os_window(c)) is not None] + + def margin() -> int: + """How far the windows are from the left edge of the work area: the padding of the + workspace and that of the first container.""" + return min(f.x for f in frames()) - area.x + + def room() -> int: + return sum(f.w * f.h for f in frames()) + + def near(value: int, want: int) -> bool: + return abs(value - want) <= 10 + + # what the windows are measured against is what these commands give, not what the config said + platform.komorebic("focused-workspace-padding", str(workspace)) + platform.komorebic("focused-workspace-container-padding", str(container)) + checks.check( + "the windows are inside the work area, tiled where the layout says", + platform.wait_for(lambda: tiled(platform, clients), timeout=20), + ) + start = margin() + print(f" margin {start} with the padding as it is") + base = room() + + platform.komorebic("focused-workspace-padding", str(workspace + 40)) + checks.check( + "a workspace padding 40 px more moves the windows 40 px in from the edge", + platform.wait_for(lambda: near(margin(), start + 40), timeout=20), + ) + checks.check( + "... and they stay where the layout says", + platform.wait_for(lambda: tiled(platform, clients), timeout=20), + ) + + platform.komorebic("focused-workspace-padding", str(workspace)) + platform.komorebic("focused-workspace-container-padding", str(container + 30)) + checks.check( + "a container padding 30 px more leaves the windows less room", + platform.wait_for(lambda: room() < base - 5000, timeout=20), + ) + checks.check( + "... and none is over another, or outside the work area", + all(f.inside(area) for f in frames()) + and not any(a.overlaps(b) for i, a in enumerate(frames()) for b in frames()[i + 1 :]), + ) + + platform.komorebic("focused-workspace-container-padding", str(container)) + checks.check( + "with the padding as it was, the windows are as far from the edge as they were", + platform.wait_for(lambda: near(margin(), start), timeout=20), + ) + print(f" margin {margin()} (was {start}), room {room()} (was {base})") + checks.check( + "... and have the room they had", + platform.wait_for(lambda: abs(room() - base) <= 2000, timeout=20), + ) diff --git a/nix/tests/vm/lib/komorebi_vm/core/snapshot.py b/nix/tests/vm/lib/komorebi_vm/core/snapshot.py index 0c104445..fa3512e0 100644 --- a/nix/tests/vm/lib/komorebi_vm/core/snapshot.py +++ b/nix/tests/vm/lib/komorebi_vm/core/snapshot.py @@ -49,6 +49,10 @@ class Workspace: floating: list[Window] latest_layout: list[Rect] """The rectangles the layout last computed, one for each container.""" + workspace_padding: int | None = None + """The padding of the workspace, when the daemon says.""" + container_padding: int | None = None + """The padding of each of its containers, when the daemon says.""" def windows(self) -> list[Window]: """Every window of the workspace: the tiled ones, the monocle ones, the maximized one and @@ -162,6 +166,8 @@ def _workspace(value: dict[str, Any]) -> Workspace: maximized=_window(maximized) if maximized else None, floating=[_window(w) for w in _ring(value["floating_windows"])], latest_layout=[r for r in map(_rect, value.get("latest_layout", [])) if r is not None], + workspace_padding=value.get("workspace_padding"), + container_padding=value.get("container_padding"), ) diff --git a/nix/tests/vm/lib/tests/fake_platform.py b/nix/tests/vm/lib/tests/fake_platform.py index a3707189..19bd5a47 100644 --- a/nix/tests/vm/lib/tests/fake_platform.py +++ b/nix/tests/vm/lib/tests/fake_platform.py @@ -80,6 +80,8 @@ class FakePlatform: self.display_of: dict[str, int] = {} self.forgets_displays = False self.floats: list[str] = [] + self.workspace_padding = 0 + self.container_padding = 0 self.minimized: list[str] = [] self.running = True self.placement: Any = None @@ -244,8 +246,12 @@ class FakePlatform: self.tiles = list(TILES) n = len(self.containers) if n: - width = 1920 // n - self.tiles = [Rect(i * width, 0, width, 1080) for i in range(n)] + list(TILES[n:]) + w, c = self.workspace_padding, self.container_padding + width = (1920 - 2 * w) // n + self.tiles = [ + Rect(w + i * width + c, w + c, width - 2 * c, 1080 - 2 * w - 2 * c) + for i in range(n) + ] + list(TILES[n:]) def _container(self, title: str) -> int: return self._find(title)[1] @@ -382,6 +388,12 @@ class FakePlatform: break case ("focus-workspace", number): self.current = int(number) + case ("focused-workspace-padding", size): + self.workspace_padding = int(size) + self._relayout() + case ("focused-workspace-container-padding", size): + self.container_padding = int(size) + self._relayout() case ("toggle-float",): if self.floats: self.containers.insert(self.focus, [self.floats.pop()]) @@ -450,6 +462,8 @@ class FakePlatform: }, "layout": {"Default": "Columns"}, "tile": True, + "workspace_padding": self.workspace_padding, + "container_padding": self.container_padding, "latest_layout": [rect(r) for r in self.tiles[: len(containers)]], } diff --git a/nix/tests/vm/lib/tests/test_focus_moving.py b/nix/tests/vm/lib/tests/test_focus_moving.py index 9bb8de9d..acd24254 100644 --- a/nix/tests/vm/lib/tests/test_focus_moving.py +++ b/nix/tests/vm/lib/tests/test_focus_moving.py @@ -14,6 +14,7 @@ from komorebi_vm.core.scenarios import ( minimizing, monocle, moving, + padding, placement, resize, rules, @@ -205,6 +206,27 @@ def test_a_daemon_that_keeps_the_room_of_a_minimized_window_fails_minimizing() - assert checks.failed > 0 +def test_a_daemon_that_is_right_passes_padding() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + padding.run(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed > 5 + + +def test_a_daemon_that_ignores_the_padding_fails_padding() -> None: + class Ignoring(FakePlatform): + def komorebic(self, *args: str) -> str: + return "" if args[0].endswith("padding") else super().komorebic(*args) + + platform = Ignoring() + checks = Checks() + clients = opened(platform, checks) + padding.run(platform, checks, clients) + assert checks.failed > 0 + + def test_a_command_the_port_lacks_is_skipped_by_name() -> None: platform = FakePlatform() checks = Checks() diff --git a/nix/tests/vm/suites/core-e2e/test.py b/nix/tests/vm/suites/core-e2e/test.py index 71a91553..5f7c8ee5 100644 --- a/nix/tests/vm/suites/core-e2e/test.py +++ b/nix/tests/vm/suites/core-e2e/test.py @@ -24,6 +24,7 @@ from komorebi_vm.core.scenarios import ( minimizing, monocle, moving, + padding, placement, rules, stacks, @@ -454,6 +455,7 @@ checks.check( layouts.run(platform, checks, neutral) floating.run(platform, checks, neutral) minimizing.run(platform, checks, neutral) +padding.run(platform, checks, neutral) monocle.run(platform, checks, neutral) workspaces.run(platform, checks, neutral) # the third window is on workspace 1 now: the first is closed, and the second takes its room