fix: keep the system print dialog in front of the window that rendered it

The render helper sat on top of the Print dialog, so the dialog could
not be reached and nothing could be printed.

Two causes. The daemon could not see window titles at all — the tree
walk never collected them — and both windows arrive close enough
together to be configured in the same pass, in whatever order the tree
lists them. Configuring a window focuses it, so whichever was done last
ended up in front, and that was not reliably the dialog.

The dialog is now recognised by its exact title and configured last, and
if a Chrome popup takes focus while it exists, focus is handed straight
back. Focus is only ever reclaimed from another popup: clicking the main
browser or the dock is the teacher's business and is left alone.

A title match, narrowly, and only for this one known dialog. Creation
order still decides which Chrome window is the browser; nothing else is
classified by what it happens to be called.

Nothing auto-closes the helper. After Print is cancelled it stays, and
the sticky dock X still closes it.

Proven by looking rather than by state. The render window is given a
saturated background in the fixture so a screenshot answers "which
window is in front" unambiguously: Print opens in front, the helper is
then deliberately raised over it and Print comes back in front,
cancelling Print leaves the helper visible with the dock X still
offering it, and that X closes exactly the helper and leaves the main
browser untouched.

Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
ekklesia
2026-09-07 20:22:53 -05:00
co-authored by Claude Opus 5
parent 0e91b4cbd3
commit aadb1b8819
4 changed files with 124 additions and 2 deletions
+69 -1
View File
@@ -54,6 +54,15 @@ INTERESTING = frozenset(
RESUBSCRIBE_SECONDS = 2
# The system print dialog, matched by its exact title.
#
# This is the one place a title is used, and only to recognise a single
# known dialog. It is not classifying Chrome windows — creation order
# still does that. The dialog and the window that rendered the document
# for it are otherwise identical to sway, and if the render window ends
# up in front the teacher cannot print at all.
PRINT_TITLE = "Print"
STATE_NAME = "chrome-close-target"
@@ -105,6 +114,10 @@ def configure_secondary(identifier, arriving=True):
place(target)
def is_print_dialog(window):
return (window or {}).get("name") == PRINT_TITLE
class ActiveSecondary:
"""The one secondary window the dock offers to close.
@@ -171,7 +184,16 @@ def reconcile(roles, active, path):
if changes["promoted"] is not None:
configure_primary(changes["promoted"])
for identifier in changes["new_secondary"]:
# The print dialog is configured last, because configuring a window
# focuses it and whichever is done last ends up in front. Both can
# arrive between two events, and the tree does not promise to list
# them in the order they were created.
arriving = sorted(
changes["new_secondary"],
key=lambda identifier: is_print_dialog(chrome.get(identifier)),
)
for identifier in arriving:
configure_secondary(identifier)
# Reassert the role if something put a secondary back into the tiling
@@ -183,9 +205,55 @@ def reconcile(roles, active, path):
if window and not window["hidden"] and not window["floating"]:
configure_secondary(identifier, arriving=False)
keep_print_in_front(roles.secondary, chrome)
publish(active.update(roles.secondary, chrome), path)
def keep_print_in_front(secondary, chrome):
"""The print dialog stays the obvious foreground window.
The window that rendered the document is a secondary like any other
and will raise itself over the dialog given the chance, which leaves
the teacher looking at a document they cannot print.
Focus is only ever taken back from another Chrome popup. Clicking
the main browser, the dock or anything else is the teacher's
business and is left alone.
"""
dialogs = [
identifier
for identifier in secondary
if identifier in chrome
and is_print_dialog(chrome[identifier])
and not chrome[identifier]["hidden"]
]
if not dialogs:
return
dialog = dialogs[-1]
if chrome[dialog]["focused"]:
return
intruder = next(
(
identifier
for identifier in secondary
if identifier in chrome
and chrome[identifier]["focused"]
and identifier != dialog
),
None,
)
if intruder is None:
return
command(f"[con_id={dialog}] focus")
def subscribe():
try:
return subprocess.Popen(