mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-10 22:47:14 +01:00
Extracted from wake_alarm, screen-locker, and diet_guard, which each independently implemented fullscreen-lock, input-grab, VT-disable, and HMAC-signed-state mechanics at different levels of maturity (the HMAC module was already a hand-copied duplicate between two of them). LockConfig exposes overrideredirect/grab/disable_vt as independent axes so one LockWindow can reproduce all three projects' exact prior behavior, plus screen-locker's confirmed upgrade to retry-forever grab.
37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
"""Safe Tk root window that never lets a callback exception escape."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import tkinter as tk
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Callable
|
|
from types import TracebackType
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class GateRoot(tk.Tk):
|
|
"""Tk root that routes callback errors to a handler instead of crashing.
|
|
|
|
Overriding ``report_callback_exception`` is the idiomatic, blind-except-free
|
|
way to guarantee that no exception raised inside a Tk callback escapes the
|
|
event loop -- essential while a global input grab is held, since a crashed
|
|
mainloop would leave the screen grabbed with no way to release it.
|
|
"""
|
|
|
|
on_callback_error: Callable[[], None] | None = None
|
|
|
|
def report_callback_exception(
|
|
self,
|
|
exc: type[BaseException],
|
|
val: BaseException,
|
|
tb: TracebackType | None,
|
|
) -> None:
|
|
"""Log a callback error and notify the handler; never re-raise."""
|
|
_logger.error("gatelock callback error", exc_info=(exc, val, tb))
|
|
if self.on_callback_error is not None:
|
|
self.on_callback_error()
|