feat(nix): workspace-switch as a KDE extra

This commit is contained in:
LGUG2Z committed 2026-10-30 12:03:01 -07:00
1 parent 92aaaeb65f
commit b72edf4bcf
2 files changed
+164 -128

No files matched your search

@@ -0,0 +1,150 @@
"""What only KDE can tell about workspaces being KWin's own virtual desktops, per output.
The shared `workspaces` scenario (run by `core-e2e`) checks that `send-to-workspace` and
`focus-workspace` put a window away and bring it back, with the focus agreed. What is here is what
a real KWin adds: the daemon turns on KWin's per-output virtual desktops and puts the user's
setting back when it stops; a window sent to another workspace is on another desktop while the one
in front keeps showing; `focus-workspace` has KWin show the other desktop and focus the window
there, which the daemon agrees with and without parking anything; a switch by anything else (a
pager, a shortcut, an app raising a window of another desktop) is followed once, with nothing
flip-flopping; and a window that somebody moves to another desktop (the window menu's "Move to
Desktop") moves to that workspace.
"""
import time
from komorebi_vm import daemon
from komorebi_vm.core.checks import Checks
from komorebi_vm.core.platform import Client
from komorebi_vm.kde_platform import KdePlatform
TITLES = ("w1", "w2", "w3")
def _focus_changes(platform: KdePlatform) -> int:
return sum(1 for e in daemon.events(platform.kde.komorebi_log()) if e.kind == "FocusChange")
def _ws(platform: KdePlatform) -> int:
"""The workspace the daemon has in front on the (only) monitor."""
return platform.state().monitors[0].focused_workspace
def _active_title(platform: KdePlatform) -> str:
active = platform.kde.active()
return platform.kde.title(active) if active else ""
def _per_output(platform: KdePlatform) -> bool:
return platform.kde.kwinrc("Windows", "PerOutputVirtualDesktops") == "true"
def run(platform: KdePlatform, checks: Checks, clients: list[Client]) -> None:
"""Starts the daemon with three windows, and walks through the desktops. The daemon is stopped
at the end, cleanly. It must not be running when this starts, with no window open."""
kde = platform.kde
scale = kde.scale
checks.check(
"per-output virtual desktops are off before the daemon starts", not _per_output(platform)
)
platform.start_komorebi()
for title in TITLES:
platform.spawn(title)
checks.check("the daemon manages the three", kde.wait_for(lambda: kde.managed(0) == 3, 20))
checks.check(
"the daemon turned on per-output virtual desktops in kwinrc", _per_output(platform)
)
checks.check(
"all three windows are on desktop 1, which is showing",
all(kde.desk(kde.wid(t)) == 1 for t in TITLES) and kde.shown() == 1,
)
# one window goes to workspace 1 and stays there
moved = _active_title(platform)
mid = kde.wid(moved)
kde.komorebic("send-to-workspace", "1")
checks.check(
f"{moved} is on desktop 2, and desktop 1 is still showing",
kde.wait_for(lambda: kde.desk(mid) == 2 and kde.shown() == 1 and _ws(platform) == 0),
)
checks.check(
"the other two are on workspace 0, and the daemon has the moved one on workspace 1",
kde.wait_for(lambda: kde.managed(0) == 2 and kde.managed(1) == 1),
)
stayed = [t for t in TITLES if t != moved]
# komorebic switches workspace: KWin shows the desktop, the daemon hides nothing
kde.komorebic("focus-workspace", "1")
checks.check(
"desktop 2 is showing and the daemon agrees",
kde.wait_for(lambda: kde.shown() == 2 and _ws(platform) == 1),
)
checks.check(
"KWin focused the window of that desktop and the daemon knows it",
kde.wait_for(lambda: _active_title(platform) == moved and kde.focused_title(1) == moved),
)
checks.check(
"the windows of desktop 1 never left it", all(kde.desk(kde.wid(t)) == 1 for t in stayed)
)
checks.check("the daemon made one desktop, for workspace 1, and no other", kde.desktops() == 2)
kde.komorebic("focus-workspace", "0")
checks.check("back on desktop 1", kde.wait_for(lambda: kde.shown() == 1 and _ws(platform) == 0))
# something else switches: a pager or a shortcut
kde.sh("busctl --user call org.kde.KWin /KWin org.kde.KWin setCurrentDesktop i 2 > /dev/null")
checks.check("the daemon followed to workspace 1", kde.wait_for(lambda: _ws(platform) == 1))
checks.check(
"and agrees with KWin on the focused window",
kde.wait_for(
lambda: (
_active_title(platform) != "" and _active_title(platform) == kde.focused_title(1)
)
),
)
# an app raises a window of another desktop
kde.sh("busctl --user call org.kde.KWin /KWin org.kde.KWin setCurrentDesktop i 1 > /dev/null")
kde.wait_for(lambda: _ws(platform) == 0)
time.sleep(1 * scale)
before = _focus_changes(platform)
kde.activate(mid)
checks.check(
"KWin switched to the window's desktop and the daemon followed",
kde.wait_for(lambda: kde.shown() == 2 and _ws(platform) == 1),
)
checks.check(
f"KWin and the daemon agree on the focused window: {moved}",
kde.wait_for(lambda: _active_title(platform) == moved and kde.focused_title(1) == moved),
)
time.sleep(3 * scale)
after = _focus_changes(platform)
print(f" focus changes the daemon handled meanwhile: {after - before}")
checks.check(
f"nothing is flip-flopping (at most 6 focus events, got {after - before})",
after - before <= 6,
)
time.sleep(3 * scale)
final = _focus_changes(platform)
checks.check(f"and it stays quiet afterwards ({final - after} more)", final - after <= 1)
# somebody moves a window to another desktop (the window menu's Move to Desktop)
other = kde.wid(stayed[0])
kde.sh(f"kdotool set_desktop_for_window {other} 2")
checks.check(
"the daemon moved it to workspace 1: one window left on workspace 0, two on workspace 1",
kde.wait_for(lambda: kde.managed(0) == 1 and kde.managed(1) == 2),
)
checks.check("and left it where KWin put it", kde.desk(other) == 2)
checks.check(
"KWin carried out every command", "the bridge could not do" not in kde.komorebi_log()
)
# a clean stop
kde.komorebic("stop")
checks.check("the daemon exited", kde.wait_for(lambda: not kde.komorebi_running()))
checks.check(
"per-output virtual desktops are back as they were (off)", not _per_output(platform)
)
checks.check("the desktops the daemon made stay: they are the user's now", kde.desktops() == 2)
+14 -128
View File
@@ -1,15 +1,16 @@
# komorebi's workspaces are KWin's own virtual desktops, per output. What only a real KWin can
# tell: that komorebi turns on KWin's per-output virtual desktops and puts the user's setting back
# when it stops; that `komorebic send-to-workspace` puts a window on another desktop and leaves the
# one in front showing; that `komorebic focus-workspace` has KWin show the other desktop, and KWin
# focuses the window there, which komorebi agrees with, without komorebi hiding or parking any
# window; that a switch by anything else (a shortcut, an app raising a window of another desktop)
# is followed once, with KWin and komorebi agreeing on the focused window and nothing flip-flopping;
# and that a window moved to another desktop by someone else (the window menu's "Move to Desktop")
# moves to that workspace. `komorebi/tests/workspaces.rs` checks the same against the fake.
import time
from komorebi_vm import Checks, Kde, daemon
# tell is in `komorebi_vm.platforms.kde.extras.workspace_switch`: that komorebi turns on KWin's
# per-output virtual desktops and puts the user's setting back when it stops, that a window sent to
# another workspace is on another desktop while the one in front keeps showing, that
# `focus-workspace` has KWin show the desktop and focus the window there, that a switch by anything
# else (a pager, a shortcut, an app raising a window) is followed once without flip-flopping, and
# that a window moved to another desktop by someone else moves to that workspace. The shared
# `workspaces` scenario, which `core-e2e` runs, checks the commands put windows away and back.
# `komorebi/tests/workspaces.rs` checks the same against the fake.
from komorebi_vm import Checks, Kde
from komorebi_vm.core.suite import platform_only, run_suite
from komorebi_vm.kde_platform import KdePlatform
from komorebi_vm.platforms.kde.extras import workspace_switch as kde_switch
start_all()
machine.wait_for_unit("multi-user.target")
@@ -17,124 +18,9 @@ machine.wait_for_unit("user@1000.service")
checks = Checks()
kde = Kde.headless(machine, "main", size=(1920, 1080), scale=SCALE)
platform = KdePlatform(kde, (1920, 1080))
def focus_changes() -> int:
return sum(1 for e in daemon.events(kde.komorebi_log()) if e.kind == "FocusChange")
def ws() -> int:
"""The workspace komorebi has in front on the (only) monitor."""
return kde.state()["monitors"]["elements"][0]["workspaces"]["focused"]
def active_title() -> str:
active = kde.active()
return kde.title(active) if active else ""
def per_output() -> bool:
return kde.kwinrc("Windows", "PerOutputVirtualDesktops") == "true"
print("== 1. start, and three windows")
checks.check("per-output virtual desktops are off before komorebi starts", not per_output())
kde.start_komorebi()
checks.check(
"the daemon answers komorebic",
kde.wait_for(lambda: kde.run("komorebic state")[0] == 0, timeout=30),
)
for title in ["w1", "w2", "w3"]:
kde.spawn(f"foot -T {title} sleep 3000")
kde.wait_for(lambda title=title: kde.wid(title) != "", timeout=30)
checks.check("komorebi manages the three", kde.wait_for(lambda: kde.managed(0) == 3, timeout=20))
print("== 2. workspaces are desktops")
checks.check("komorebi turned on per-output virtual desktops in kwinrc", per_output())
checks.check(
"all three windows are on desktop 1, which is showing",
all(kde.desk(kde.wid(t)) == 1 for t in ["w1", "w2", "w3"]) and kde.shown() == 1,
)
print("== 3. one window goes to workspace 1 and stays there")
moved = active_title()
mid = kde.wid(moved)
kde.komorebic("send-to-workspace", "1")
checks.check(
f"{moved} is on desktop 2, and desktop 1 is still showing",
kde.wait_for(lambda: kde.desk(mid) == 2 and kde.shown() == 1 and ws() == 0),
)
checks.check(
"the other two are on workspace 0, and komorebi has the moved one on workspace 1",
kde.wait_for(lambda: kde.managed(0) == 2 and kde.managed(1) == 1),
)
stayed = [t for t in ["w1", "w2", "w3"] if t != moved]
print("== 4. komorebic switches workspace: KWin shows the desktop, komorebi hides nothing")
kde.komorebic("focus-workspace", "1")
checks.check(
"desktop 2 is showing and komorebi agrees",
kde.wait_for(lambda: kde.shown() == 2 and ws() == 1),
)
checks.check(
"KWin focused the window of that desktop and komorebi knows it",
kde.wait_for(lambda: active_title() == moved and kde.focused_title(1) == moved),
)
checks.check(
"the windows of desktop 1 never left it", all(kde.desk(kde.wid(t)) == 1 for t in stayed)
)
checks.check("komorebi made one desktop, for workspace 1, and no other", kde.desktops() == 2)
kde.komorebic("focus-workspace", "0")
checks.check("back on desktop 1", kde.wait_for(lambda: kde.shown() == 1 and ws() == 0))
print("== 5. something else switches: a pager or a shortcut")
kde.sh("busctl --user call org.kde.KWin /KWin org.kde.KWin setCurrentDesktop i 2 > /dev/null")
checks.check("komorebi followed to workspace 1", kde.wait_for(lambda: ws() == 1))
checks.check(
"and agrees with KWin on the focused window",
kde.wait_for(lambda: active_title() != "" and active_title() == kde.focused_title(1)),
)
print("== 6. an app raises a window of another desktop")
kde.sh("busctl --user call org.kde.KWin /KWin org.kde.KWin setCurrentDesktop i 1 > /dev/null")
kde.wait_for(lambda: ws() == 0)
time.sleep(1 * SCALE)
before = focus_changes()
kde.activate(mid)
checks.check(
"KWin switched to the window's desktop and komorebi followed",
kde.wait_for(lambda: kde.shown() == 2 and ws() == 1),
)
checks.check(
f"KWin and komorebi agree on the focused window: {moved}",
kde.wait_for(lambda: active_title() == moved and kde.focused_title(1) == moved),
)
time.sleep(3 * SCALE)
after = focus_changes()
print(f" focus changes komorebi handled meanwhile: {after - before}")
checks.check(
f"nothing is flip-flopping (at most 6 focus events, got {after - before})",
after - before <= 6,
)
time.sleep(3 * SCALE)
final = focus_changes()
checks.check(f"and it stays quiet afterwards ({final - after} more)", final - after <= 1)
print("== 7. somebody moves a window to another desktop (the window menu's Move to Desktop)")
other = kde.wid(stayed[0])
kde.sh(f"kdotool set_desktop_for_window {other} 2")
checks.check(
"komorebi moved it to workspace 1: one window left on workspace 0, two on workspace 1",
kde.wait_for(lambda: kde.managed(0) == 1 and kde.managed(1) == 2),
)
checks.check("and left it where KWin put it", kde.desk(other) == 2)
checks.check("KWin carried out every command", "the bridge could not do" not in kde.komorebi_log())
print("== 8. a clean stop")
kde.komorebic("stop")
checks.check("the daemon exited", kde.wait_for(lambda: not kde.komorebi_running()))
checks.check("per-output virtual desktops are back as they were (off)", not per_output())
checks.check("the desktops komorebi made stay: they are the user's now", kde.desktops() == 2)
run_suite(checks, platform_only("kde", kde_switch.run, platform, checks, []))
kde.stop()
checks.finish()