Files
komorebi/nix/tests/vm/suites/placement/test.py
T
LGUG2Z 21017b573f feat(wm): initial window placement rules
A window that opens goes after the focused container. A workspace can now say
another place for its windows: the primary or the secondary tile, before or
after the focused container, the end of the list, or a place by number, counting
from 1. The place is for every window, or for the windows that match a rule on
their program, app id, title or path; the rules are the conditions the ignore
and manage rules are made of, and the first that matches in key order decides. A
place that does not exist, such as the secondary tile of a workspace with one
container, is the default place.

A place is read from a name, a number or text that is a number. The
documentation of the setting shows a number and a map's keys can only be text,
and komorebi for Windows (83c4e11d) reads only text, so the form its own page
shows is refused there.

A place that was reserved with preselect-direction is used before the rules are
asked, and a locked container keeps its index. A workspace whose
window_container_behaviour is Append puts a window in the focused container, and
the rules are not used.

A workspace takes the rules from the key initial_window_placement_rules of its
entry in komorebi.json. The key is in schema.json and so in the typed config
option of the module, and the page docs/initial-window-placement-rules.md says
how to write it. vm-placement opens windows in a headless KWin and checks where
they go, by program, app id and title.

The schema that schemars derives for a place is an object with a Placement or an
Index key, which is not what is read or written, so a check against it refuses a
file that the daemon reads. The schema of PlacementTarget is written by hand
instead: a name, a number, or text that is a number. A file with a place of each
form is checked against schema.json, and the module's config option takes them.
2026-10-28 07:20:57 -07:00

132 lines
4.6 KiB
Python

# `initial_window_placement_rules` in a real (headless) KWin: where in the layout a window that
# opens goes. The daemon reads the rules from a `komorebi.json` it is started with, and the rules
# match a window by what it is called: its program (`Exe`), its Wayland app id (`Class`) and its
# title. `komorebi/tests/placement.rs` checks the same against the fake; this checks that the names
# the rules match are the ones a real window has, and that the places of a file are the places the
# windows get.
import json
import shlex
import time
from typing import Any
from komorebi_vm import Checks, Kde
CONFIG = "/tmp/placement.json"
start_all()
machine.wait_for_unit("multi-user.target")
machine.wait_for_unit("user@1000.service")
checks = Checks()
kde = Kde.headless(machine, "main", size=(1920, 1080), scale=SCALE)
opened: list[str] = []
def order() -> list[str]:
"""The title of the window of each container of the first workspace, in the layout's order."""
return [titles[0] for titles in kde.containers()]
def open_window(title: str, app_id: str = "foot") -> None:
"""Opens a window, and waits until the daemon has put it in a container."""
kde.spawn(f"foot -a {shlex.quote(app_id)} -T {shlex.quote(title)} sleep 3000")
opened.append(title)
kde.wait_for(lambda: kde.wid(title) != "", timeout=30)
kde.wait_for(lambda: kde.managed() == len(opened), timeout=20)
# the window that has just opened has the focus once the daemon has told KWin so
kde.wait_for(lambda: kde.focused_title() == title, timeout=20)
time.sleep(0.5 * SCALE)
def fresh(rules: Any) -> None:
"""Starts the daemon again, with `rules` as the placement rules of the workspace's windows.
The windows of the run before are closed. `None` leaves the key out of the file.
"""
if kde.komorebi_running():
kde.komorebic("stop")
kde.wait_for(lambda: not kde.komorebi_running(), timeout=30)
for title in opened:
wid = kde.wid(title)
if wid != "":
kde.close(wid)
kde.wait_for(lambda: all(kde.wid(title) == "" for title in opened), timeout=30)
opened.clear()
workspace: dict[str, Any] = {"name": "a", "layout": "BSP"}
if rules is not None:
workspace["initial_window_placement_rules"] = rules
config = {
"default_workspace_padding": 10,
"default_container_padding": 10,
"monitors": [{"workspaces": [workspace]}],
}
kde.sh(f"printf %s {shlex.quote(json.dumps(config))} > {CONFIG}")
kde.start_komorebi("--config", CONFIG)
checks.check(
"the daemon answers komorebic",
kde.wait_for(lambda: kde.run("komorebic state")[0] == 0, timeout=30),
)
print("== 1. no rules: after the one in focus")
fresh(None)
for title in ("a", "b", "c"):
open_window(title)
checks.check(f"a, b, c ({order()})", order() == ["a", "b", "c"])
print("== 2. every window to the primary tile")
fresh("Primary")
for title in ("a", "b", "c"):
open_window(title)
checks.check(f"c, b, a ({order()})", order() == ["c", "b", "a"])
print("== 3. by the number of the place, counted from 1, as a number and as text")
fresh(2)
for title in ("a", "b", "c"):
open_window(title)
checks.check(f"a, c, b ({order()})", order() == ["a", "c", "b"])
fresh("2")
for title in ("a", "b", "c"):
open_window(title)
checks.check(
f"text that is a number is the same place: a, c, b ({order()})", order() == ["a", "c", "b"]
)
print("== 4. by the app id and the title")
fresh(
{
"Primary": {"kind": "Class", "id": "special", "matching_strategy": "Equals"},
"Last": [{"kind": "Title", "id": "chat", "matching_strategy": "Contains"}],
}
)
for title in ("one", "two"):
open_window(title)
open_window("three", "special")
checks.check(
f"the window with the app id special is first ({order()})", order() == ["three", "one", "two"]
)
open_window("a chat room")
checks.check(
f"the chat room is last ({order()})", order() == ["three", "one", "two", "a chat room"]
)
open_window("four")
checks.check(
f"a window no rule matches goes after the one in focus, the chat room ({order()})",
order() == ["three", "one", "two", "a chat room", "four"],
)
print("== 5. by the program, with the name of a Windows executable")
fresh({"Primary": {"kind": "Exe", "id": "foot.exe", "matching_strategy": "Equals"}})
for title in ("a", "b", "c"):
open_window(title)
checks.check(
f"foot.exe is foot, so every window is a primary one: c, b, a ({order()})",
order() == ["c", "b", "a"],
)
kde.komorebic("stop")
checks.check("a clean stop", kde.wait_for(lambda: not kde.komorebi_running(), timeout=30))
kde.stop()
checks.finish()