mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-11 14:48:45 +01:00
The Log Meal tab did not fit this laptop, and had not for some time. Measured
under a real 1366x768 X server: it required 866px inside a 739px content pane
with every field EMPTY, and 938px with a day of meals logged.
Because the content frame was place()-centred it was sheared at BOTH edges at
once -- the "Diet Gate" title and the slot header (which meal you are being
asked to log) off the top, the calorie headline and the whole dashboard off the
bottom -- with no scrollbar anywhere in the package and nothing to indicate
content was missing. This is a hard lock: overrideredirect, a global grab, VT
switching disabled. Unreachable content is unrecoverable.
Two independent causes, both needed fixing:
- Every font size was handed to Tk as *points*. Tk encodes the unit in the
SIGN of the size, and _gatelock_typography carried a comment asserting the
opposite ("Tk has no separate px/pt distinction"). At ~100 DPI that made
every string ~30% oversized. Negative sizes now; the docstring records why.
- There was no scroll container. build_layout now uses gatelock's
ScrollableSurface, which top-anchors, scrolls by keyboard, and scrolls to
follow focus.
Font sizes alone were not enough: they take the layout to 702px, still over the
571px pane at 1024x600. Both fixes are load-bearing.
The grams/items selector was mouse-only. tk.OptionMenu's Menubutton defaults to
takefocus=0, so ::tk::FocusOK rejects it and it never entered the tab ring --
verified by walking the real ring, which ran Entry -> Text -> Listbox -> Button
-> TNotebook and skipped it entirely. Switching units rescales the whole entry,
so a pointerless user could not correct "250 grams" to "3 items" inside a lock
they cannot leave without logging a meal. It is now two Radiobuttons, which are
in the ring by default and take both Space and Return -- and which post nothing,
unlike a menu, whose grab-stealing is why screen-locker bans OptionMenu on lock
surfaces outright.
Also: the focus ring was invisible (Tk's default is 1px BLACK on #211D1B) and
_build_desc actively inverted it by setting highlightbackground, the UNfocused
ring, so the box outlined while idle and went black the moment it took focus.
Enter now activates buttons (X11 Tk binds only <space>). Text widgets no longer
trap Tab. notebook.enable_traversal() is called, so Ctrl+Tab and Ctrl+PageDown
work. wraplength drops 900 -> 640, the line-length cap, which also stops a
label forcing a 900px-wide layout onto a 1024px screen.
The Flutter app had two controls with no keyboard route at all. Its desktop
target is the web build in a Chrome --app window, so a touch gesture is not a
fallback there, it is the only interaction:
- Deleting a history entry was bound solely to onLongPress. Long-press has no
keyboard equivalent whatsoever. It now has a focusable IconButton; the
long-press stays as a redundant pointer shortcut.
- Calendar day cells were a bare GestureDetector, so navigating to
date-filtered history was pointer-only and invisible to a screen reader.
Now Semantics(button: true) over an InkWell.
Plus: the filter sheet built a TextEditingController inside build() with the
caret forced to end-of-text, so every keystroke discarded it and silently undid
mid-string Home/End/arrow editing. And nothing capped width, so on the 1366px
desktop window prose ran ~180 characters per line (twice the readable limit)
and a four-digit kcal field spanned ~1334px. AppWidth adds those caps.
Tests: the render check is a failing test, not a script -- ruff runs with
--unsafe-fixes, whose T201 fix DELETES print statements, which would silently
turn a diagnostic into a no-op. test_gate_layout_fits.py was verified red on
the pre-fix code ("content 866 in viewport 768 -> NO SCROLL VIEWPORT ->
UNREACHABLE CONTENT") and green after. The Flutter suite gains its first
keyboard-reachability assertions; it previously had ~109 tester.tap calls and
zero checks that anything was focusable.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
95 lines
3.2 KiB
Python
95 lines
3.2 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.
|
|
|
|
Binds ``<Return>`` as well as relying on ``command``. Tk's X11 class
|
|
bindings give ``tk.Button`` only ``<space>``; there is no ``<Return>``
|
|
binding, so without this every button in the gate is Space-only and Enter
|
|
silently does nothing -- which is not the key a user reaches for.
|
|
"""
|
|
fill, text_color = _BUTTON_FILLS[variant]
|
|
size = _BUTTON_SIZE[variant]
|
|
font = (_COLORS.font_family, size, "bold") if bold else (_COLORS.font_family, size)
|
|
button = 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,
|
|
**_COLORS.focus_kwargs(),
|
|
)
|
|
|
|
def _activate(_event: tk.Event) -> str:
|
|
button.invoke()
|
|
return "break"
|
|
|
|
button.bind("<Return>", _activate, add="+")
|
|
button.bind("<KP_Enter>", _activate, add="+")
|
|
return button
|