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]>
This commit is contained in:
ekklesia
2026-09-06 20:24:51 -05:00
co-authored by Claude Opus 5
parent a1d8150d7c
commit f65c814eff
18 changed files with 1144 additions and 100 deletions
+206
View File
@@ -0,0 +1,206 @@
#!/usr/bin/env python3
"""Keep Chrome's first window tiled and make later windows usable popups.
Chrome gives the main browser and its secondary top-level windows the same
app_id, the same process and often nothing else to tell them apart. The only
stable fact is creation order, so this remembers the first Chrome container
and treats every later one as secondary. Page titles are deliberately ignored.
The close affordance for a secondary window lives in the dock. An earlier
attempt anchored a layer-shell button to the popup's own corner and moved it
whenever the window moved; on real hardware it chased the window and
flickered, because every position change meant tearing a surface down and
building another one. The dock is already on screen, already owns the
classroom's visual language, and does not move. This publishes which window
the dock's Close button should act on, and the dock does the rest.
Driven by Sway's own window events rather than a poll, so a window that is
being dragged costs nothing.
"""
import fcntl
import json
import os
import subprocess
import sys
import time
from pathlib import Path
sys.path.insert(0, "/usr/local/lib/ekklesia-classroom")
sys.path.insert(
0,
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "lib"),
)
from classroom_chrome import ChromeRoles, chrome_windows # noqa: E402
from classroom_sway import command, windows # noqa: E402
SECONDARY_SIZE_PERCENT = 76
# Chrome renegotiates the size of a popup that was opened with explicit
# dimensions, and does it after the resize returns. Centring before that
# settles leaves the window visibly off to one side, so it is placed
# once more a moment later. Only ever at creation: after that the window
# is the teacher's to move, and nothing here touches it again.
SETTLE_SECONDS = 0.35
# Everything else a window event can report — a title change, an urgency
# hint, a mark — cannot change which window is which, and Chrome retitles
# constantly.
INTERESTING = frozenset(
("new", "close", "focus", "move", "floating", "fullscreen_mode")
)
RESUBSCRIBE_SECONDS = 2
STATE_NAME = "chrome-close-target"
def state_directory():
runtime = Path(os.environ.get("XDG_RUNTIME_DIR") or "/tmp")
directory = runtime / "ekklesia-classroom"
directory.mkdir(parents=True, exist_ok=True)
return directory
def criterion(identifier):
return f"[con_id={identifier}]"
def configure_primary(identifier):
"""A promoted remaining window becomes the ordinary classroom browser."""
command(f"{criterion(identifier)} floating disable")
command(f"{criterion(identifier)} border none")
command(f"{criterion(identifier)} focus")
def place(target):
command(
f"{target} resize set width {SECONDARY_SIZE_PERCENT} ppt "
f"height {SECONDARY_SIZE_PERCENT} ppt"
)
command(f"{target} move position center")
def configure_secondary(identifier):
"""Float one exact container; never apply a broad Chrome float rule."""
target = criterion(identifier)
command(f"{target} floating enable")
command(f"{target} border none")
place(target)
command(f"{target} focus")
# Let Chrome have its say about the size, then place it again.
time.sleep(SETTLE_SECONDS)
place(target)
def focused_secondary(secondary, chrome):
"""The secondary window the dock's Close button should act on.
Only ever a focused one. If the teacher is in the main browser there is
nothing to close, and the dock shows no Close button at all.
"""
for identifier in secondary:
window = chrome.get(identifier)
if window and window["focused"] and not window["hidden"]:
return identifier
return None
def publish(target, path):
"""Tell the dock which window to offer to close, atomically."""
temporary = path.with_suffix(".tmp")
try:
temporary.write_text("" if target is None else f"{target}\n")
os.replace(temporary, path)
except OSError as error:
print(f"[classroom-chrome-windows] could not publish state: {error}",
flush=True)
def reconcile(roles, path):
current = windows()
chrome = {window["id"]: window for window in chrome_windows(current)}
changes = roles.update(current)
if changes["promoted"] is not None:
configure_primary(changes["promoted"])
for identifier in changes["new_secondary"]:
configure_secondary(identifier)
# Reassert the role if something put a secondary back into the tiling
# layout — a restore, or an application-initiated geometry change.
# Hidden windows are left parked quietly.
for identifier in roles.secondary:
window = chrome.get(identifier)
if window and not window["hidden"] and not window["floating"]:
configure_secondary(identifier)
publish(focused_secondary(roles.secondary, chrome), path)
def subscribe():
try:
return subprocess.Popen(
["swaymsg", "-t", "subscribe", "-m", "-r", '["window"]'],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
)
except (OSError, subprocess.SubprocessError):
return None
def watch():
directory = state_directory()
path = directory / STATE_NAME
with (directory / "chrome-windows.lock").open("w") as lock:
try:
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError:
return 0
roles = ChromeRoles()
try:
while True:
# Whatever happened while there was no subscription.
reconcile(roles, path)
stream = subscribe()
if stream is None or stream.stdout is None:
time.sleep(RESUBSCRIBE_SECONDS)
continue
for line in stream.stdout:
try:
event = json.loads(line)
except ValueError:
continue
if event.get("change") in INTERESTING:
reconcile(roles, path)
# Sway went away or restarted; wait, then pick it back up.
stream.wait()
time.sleep(RESUBSCRIBE_SECONDS)
finally:
publish(None, path)
def main():
return watch()
if __name__ == "__main__":
sys.exit(main())