screen-locker/screen_locker/_window_setup.py
Krzysztof kuhy Rudnicki 600d0fd084 Migrate screen_locker to gatelock v0.2.0 per-output locking
The lock now paints one container per live output instead of one window on
the primary, and claims the screen through an Arbiter at RANK_SCREEN_LOCKER
so the wake alarm can outrank it.

The fan-out happens at the widget factory, not the render loop. Painting a
screen here is not a pure function -- the ~20 clear_container() flows record
workouts, credit weeks and branch on state while they paint -- so re-running
a flow per surface would double those side effects. Instead `_label`,
`_button_row` and friends build N copies and return a group that mirrors
later configure calls to all of them, and a flow body still runs once. Frame
groups nest, so `_button(button_row, ...)` reads exactly as it did.

The degenerate case carries as much weight as the general one: the status
window, the post-sick-day verify window and the relaxed-day prompt have no
lock and no surfaces, and share this same factory. They get a group of
exactly one frame and behave as they always did.

Text boxes are the one thing that cannot be shared. Entries fan out over a
single StringVar, but tk.Text has no such variable, so the sick-day
description genuinely diverges per monitor; TextGroup.get() returns whichever
copy was actually typed into, preserving "answer on any monitor".

Tests: the suite hung rather than failed on first run, which is the trap
gatelock's own conftest documents -- screen_locker patches its own `tk`, but
LockWindow builds Toplevels through gatelock's, and real tkinter over a
MagicMock root recurses without bound. `_gatelock_fixtures` closes that and
redirects the runtime dir so no test can stand a production locker down.
Every other test runs single-output, where fan-out is unobservable, so
test_surface_fanout runs on two outputs; each assertion was checked by
cropping the group loops to the primary and confirming it goes red.

874 tests, 100% branch coverage, pre-commit clean, no lint suppressions.
2026-07-25 16:26:39 +02:00

112 lines
4.2 KiB
Python

"""Auxiliary (non-lock) window setup for ScreenLocker.
The fullscreen lock-window mechanics (overrideredirect, input grab,
VT-disable) now live in the shared ``gatelock`` package. This module keeps
only the screen-locker-specific windows that are never the lock itself: the
post-sick-day verification window, the demo close button, and the optional
relaxed-day prompt.
"""
from __future__ import annotations
import tkinter as tk
from typing import TYPE_CHECKING
from gatelock import RANK_SCREEN_LOCKER, Arbiter, LockWindow
from screen_locker._surface_group import FrameGroup
if TYPE_CHECKING:
from gatelock import SurfaceInfo
class WindowSetupMixin:
"""Mixin providing the screen-locker-specific auxiliary windows."""
# Declared because this mixin both reads and *reassigns* it; without the
# annotation mypy cannot infer the attribute's type through the mixin.
container: FrameGroup
def _build_lock_window(self) -> LockWindow:
"""Claim the screen at this app's rank and build the lock.
The arbiter is published here and then handed to ``LockWindow``,
which owns releasing it on close -- so it is deliberately not kept
as an attribute of the locker.
"""
arbiter = Arbiter(
"screen_locker",
RANK_SCREEN_LOCKER,
grab=self._colors.resolved_grab(),
disable_vt=self._colors.resolved_disable_vt(),
)
arbiter.publish()
arbiter.acquire_holder()
lock = LockWindow(self.root, self._colors, hooks=self, arbiter=arbiter)
lock.setup()
return lock
def _ensure_container(self) -> None:
"""Give the non-lock windows a container group of exactly one.
The verify and relaxed-day windows never build a lock, so nothing
called ``build_surface`` and the group is still empty. They are
ordinary single windows, and one frame is all they need.
"""
if not self.container.surfaces:
self.container = FrameGroup.single(self.root, bg=self._colors.bg)
def build_surface(self, parent: tk.Misc, surface: SurfaceInfo) -> None:
"""Add one centred container for a newly-live output.
Only the container is built here, not the screen inside it: which
screen is showing depends on flow state the lock knows nothing
about, so the caller repaints through the usual flow entry point
once the surface exists.
"""
frame = tk.Frame(parent, bg=self._colors.bg)
frame.place(relx=0.5, rely=0.5, anchor="center")
self.container.add(frame, surface.output_name)
def teardown_surface(self, surface: SurfaceInfo) -> None:
"""Forget the container for an output that went dark."""
self.container.discard(surface.output_name)
def on_focus_ready(self, surface: SurfaceInfo | None) -> None:
"""No typed-input field in the lock window; nothing to focus.
``surface`` is None when no output is live at all -- the lock is
held with nothing to show, which is still a valid state.
"""
def on_callback_error(self) -> None:
"""Surfaced via GateRoot's logging already; no extra action yet."""
def on_close(self) -> None:
"""No extra hardware/state beyond what close() already handles."""
def _setup_verify_window(self) -> None:
"""Configure window for post-sick-day workout verification."""
self.root.geometry("600x400")
self.root.configure(bg=self._colors.bg, cursor="arrow")
self.root.protocol("WM_DELETE_WINDOW", self.close)
def _setup_demo_close_button(self) -> None:
"""Add close button for demo mode."""
close_btn = tk.Button(
self.root,
text="✕ Close Demo",
font=(self._colors.font_family, 12),
bg=self._colors.danger,
fg=self._colors.on_fill,
command=self.close,
cursor="hand2",
)
close_btn.place(x=10, y=10)
def _setup_relaxed_day_window(self) -> None:
"""Configure a small non-locking window for the optional Tue-Thu prompt."""
self.root.geometry("700x450")
self.root.configure(bg=self._colors.bg, cursor="arrow")
self.root.protocol("WM_DELETE_WINDOW", self.close)