Files
Classroom_Desktop/lib/classroom_sway.py
T
ekklesiaandClaude Opus 5 f65c814eff fix: put the popup close button in the dock, and clear the printer location
Three things, none of them redesigns.

The close affordance for a secondary Chrome window was a separate
layer-shell surface anchored to that window's corner, repositioned by a
five-per-second poll. Any position change tore the surface down and
built another, so on real hardware it chased the window and flickered.
Removed entirely: the GTK button process, the geometry maths that placed
it, the --close-button mode that spawned it, and the poll loop that
drove it. The window helper no longer imports GTK at all.

The behaviour underneath it was right and is kept. The first Chrome
top-level is the classroom browser, tiled and borderless; later ones
float centred at about three quarters of the output. Creation order is
the only thing that can tell them apart — confirmed on a real session,
where every Chrome top-level shares app_id, shell, PID and a null
transient_for, and sway exposes no window properties for Wayland
toplevels at all. Titles are still not used. It now follows sway's own
window events instead of polling, so dragging a popup costs nothing.

Close moved into the dock, which was already on screen and does not
move, so it cannot chase anything. It appears only while a secondary
window is focused and runs kill on that one container. The helper
publishes which window that is; the dock watches the file rather than
polling. A stock sway title bar has no close button, so restoring the
border would not have solved this either.

Two bugs found by looking rather than by testing state. Chrome
renegotiates the size of a popup opened with explicit dimensions after
the resize returns, which left windows centred for a width they no
longer had; placement now settles and repeats once, at creation only, so
dragging is still free. And the dock's Close button came up empty
because show_all does not recurse into a no-show-all widget.

The printer's Location is now blank. CUPS prints it under the queue name
in every dialog, so "Ekklesia Classroom" was a second line the teacher
had to read past; the identity is one word. The already-configured check
includes Location, so a machine set up before this is corrected rather
than left as close enough.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-09-06 20:24:51 -05:00

127 lines
3.3 KiB
Python

"""Talking to sway.
Shared by classroom-window, which moves windows around, and by
classroom-dvd-session, which needs to know when VLC has actually taken
the screen.
"""
import json
import re
import subprocess
HIDDEN_WORKSPACE = "__ekklesia_hidden"
# sway parks scratchpad windows on a workspace of its own. Nothing here
# puts them there, but treating it as hidden means a window left in the
# scratchpad by an older classroom build can still be restored.
HIDDEN_WORKSPACES = {HIDDEN_WORKSPACE, "__i3_scratch"}
def swaymsg(*arguments):
"""Run swaymsg, returning stdout or None when sway is unavailable."""
try:
result = subprocess.run(
["swaymsg", *arguments],
capture_output=True,
text=True,
)
except (OSError, subprocess.SubprocessError):
return None
if result.returncode != 0:
return None
return result.stdout
def command(argument):
"""Run a sway command, ignoring criteria that match nothing."""
swaymsg("--", argument)
def query(kind):
output = swaymsg("-t", kind, "-r")
if not output:
return None
try:
return json.loads(output)
except ValueError:
return None
def collect(node, workspace, found, floating=False):
"""Flatten the sway tree into the windows the classroom cares about."""
if node.get("type") == "workspace":
workspace = node.get("name")
is_window = node.get("pid") is not None and not node.get("nodes")
if is_window:
properties = node.get("window_properties") or {}
found.append(
{
"id": node.get("id"),
"app_id": node.get("app_id") or "",
"class": properties.get("class") or "",
"instance": properties.get("instance") or "",
"focused": bool(node.get("focused")),
"fullscreen": bool(node.get("fullscreen_mode")),
"workspace": workspace,
"hidden": workspace in HIDDEN_WORKSPACES,
"floating": floating,
}
)
for child in node.get("nodes", []):
collect(child, workspace, found, floating)
for child in node.get("floating_nodes", []):
collect(child, workspace, found, True)
return found
def windows():
tree = query("get_tree")
if tree is None:
return []
return collect(tree, None, [])
def matches(window, patterns):
"""Match a window against app_id / X11 class / X11 instance."""
identities = [
window["app_id"],
window["class"],
window["instance"],
]
for pattern in patterns:
expression = re.compile(pattern, re.IGNORECASE)
for identity in identities:
if identity and expression.fullmatch(identity):
return True
return False
def visible_workspace():
"""The workspace a restored window should come back to."""
workspaces = query("get_workspaces") or []
for workspace in workspaces:
if workspace.get("focused") and workspace.get("name") not in HIDDEN_WORKSPACES:
return workspace.get("name")
for workspace in workspaces:
if workspace.get("visible") and workspace.get("name") not in HIDDEN_WORKSPACES:
return workspace.get("name")
return "1"