mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-20 10:32:28 +01:00
The gate is laid out once per live output instead of once on the primary, and claims the screen through an Arbiter at RANK_DIET_GUARD so the wake alarm and the workout locker can both outrank it. Three kinds of state needed three different answers. The display fields already fanned out for free -- GateVars holds root-mastered StringVars bound to labels. The typed-in fields did not: amount, per, meal name and the four macro cells were bare tk.Entry widgets, so N copies would have been N independent forms. They are variable-backed now, which is what makes logging work from whichever screen the user is actually looking at. The description box and the suggestion list cannot be shared at all -- tk.Text and tk.Listbox have no variable -- so those genuinely diverge and reads answer from the copy the user touched. The History tab is per-surface too: its grid, budget field and edit button are driven across every monitor, with the budget likewise variable-backed so the value cannot differ between screens mid-edit. Laying the gate out on the primary monitor only was considered and rejected: not having to hunt for the screen the form landed on is the entire point of per-output locking. Tests: the suite hung rather than failed on first run -- diet_guard patches its own `tk`, but LockWindow builds Toplevels through gatelock's, and real tkinter over a MagicMock root recurses without bound. `_hermetic_gatelock` 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_gatelock_fanout runs on two; each assertion was checked by cropping the fan-out loops to the first surface and confirming it goes red. _gatelock_ui and _gatelock_calendar were each split in two to stay under the 500-line limit, and GateVars' typed-in fields moved into a nested GateEntryVars rather than raising pylint's attribute cap. 470 tests, 100% branch coverage, pre-commit clean, no lint suppressions.
80 lines
2.6 KiB
Python
80 lines
2.6 KiB
Python
"""Shared primary/secondary/danger button builder for the diet_guard gate.
|
|
|
|
Split out of :mod:`._gatelock_ui` to keep every gate module under the repo's
|
|
500-line limit. Centralizes what used to be 7 independent ``tk.Button(...)``
|
|
calls (across :mod:`._gatelock_ui` and :mod:`._gatelock_calendar`) each
|
|
picking their own bg/fg/activebackground -- see ``DESIGN_AUDIT_TODO.md``.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import tkinter as tk
|
|
from typing import TYPE_CHECKING, Literal
|
|
|
|
from gatelock import LockConfig
|
|
|
|
from diet_guard._gatelock_typography import BODY, LABEL
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Callable
|
|
|
|
_COLORS = LockConfig()
|
|
|
|
|
|
def _lighten(hex_color: str, amount: float = 0.12) -> str:
|
|
"""Blend ``hex_color`` toward white by ``amount``, for a button hover state."""
|
|
red, green, blue = (int(hex_color[i : i + 2], 16) for i in (1, 3, 5))
|
|
return "#" + "".join(
|
|
f"{round(channel + (255 - channel) * amount):02x}"
|
|
for channel in (red, green, blue)
|
|
)
|
|
|
|
|
|
# Fill/text pairs per button role, read from the shared LockConfig palette
|
|
# instead of each call site inventing its own hex pair. "primary" is the
|
|
# one high-emphasis action per screen (rule 3); "secondary" and "danger"
|
|
# stay low-contrast/alarm respectively. All three use `on_fill` (never
|
|
# `fg`) for text-on-a-filled-surface, per tokens.md.
|
|
_BUTTON_FILLS: dict[str, tuple[str, str]] = {
|
|
"primary": (_COLORS.accent, _COLORS.on_fill),
|
|
"secondary": (_COLORS.field_bg, _COLORS.fg),
|
|
"danger": (_COLORS.danger, _COLORS.on_fill),
|
|
}
|
|
# The one high-emphasis button per screen reads larger than everything else
|
|
# (rule 3: size is another axis of prominence, alongside color) -- every
|
|
# other variant shares the same, smaller chrome size (tokens.md's type scale).
|
|
_BUTTON_SIZE: dict[str, int] = {
|
|
"primary": BODY,
|
|
"secondary": LABEL,
|
|
"danger": LABEL,
|
|
}
|
|
# Horizontal = 2x vertical (tokens.md "Buttons"; both on the 4px spacing scale).
|
|
_BUTTON_PADY = 12
|
|
_BUTTON_PADX = 24
|
|
|
|
|
|
def make_button(
|
|
parent: tk.Misc,
|
|
*,
|
|
text: str,
|
|
variant: Literal["primary", "secondary", "danger"],
|
|
command: Callable[[], None],
|
|
bold: bool = True,
|
|
) -> tk.Button:
|
|
"""Build a button from the shared primary/secondary/danger palette."""
|
|
fill, text_color = _BUTTON_FILLS[variant]
|
|
size = _BUTTON_SIZE[variant]
|
|
font = (_COLORS.font_family, size, "bold") if bold else (_COLORS.font_family, size)
|
|
return tk.Button(
|
|
parent,
|
|
text=text,
|
|
font=font,
|
|
bg=fill,
|
|
fg=text_color,
|
|
activebackground=_lighten(fill),
|
|
cursor="hand2",
|
|
padx=_BUTTON_PADX,
|
|
pady=_BUTTON_PADY,
|
|
command=command,
|
|
)
|