mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-11 18:13:55 +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>
132 lines
4.7 KiB
Python
132 lines
4.7 KiB
Python
"""Walk the gate's real focus ring and report keyboard reachability.
|
|
|
|
Reading the code is not enough to know what is reachable: Tk decides via
|
|
``::tk::FocusOK``, which consults ``takefocus``, mapped-ness, *and* whether the
|
|
widget's class has any key bindings at all. A ``tk.Canvas`` fails that last test
|
|
even though it looks like a normal widget, and a ``Menubutton`` fails the first.
|
|
So this walks ``tk_focusNext`` the way the Tab key does.
|
|
|
|
There is deliberately no ``print``-based CLI: ruff runs with
|
|
``--unsafe-fixes``, whose ``T201`` fix *deletes* print statements, silently
|
|
turning a diagnostic script into a no-op. :func:`probe` is consumed by
|
|
``tests/test_gatelock_keyboard.py``, which fails rather than reports.
|
|
|
|
Test support, not a test: it lives under ``tests/`` and is excluded from the
|
|
``test_*.py`` naming hook.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
import tkinter as tk
|
|
from tkinter import ttk
|
|
|
|
from diet_guard._gatelock_ui import GateCallbacks, build_layout
|
|
from diet_guard.tests._gate_layout_probe import _make_vars, _noop
|
|
|
|
# How far to walk before declaring the ring non-terminating.
|
|
_MAX_RING_STEPS = 60
|
|
|
|
|
|
@dataclass
|
|
class RingReport:
|
|
"""What a focus-ring walk found."""
|
|
|
|
classes: list[str] = field(default_factory=list)
|
|
focus_rings_visible: dict[str, str] = field(default_factory=dict)
|
|
text_escapes_tab: bool = False
|
|
buttons_take_return: bool = False
|
|
|
|
def has(self, widget_class: str) -> bool:
|
|
"""Whether the ring includes at least one widget of this class."""
|
|
return widget_class in self.classes
|
|
|
|
def describe(self) -> str:
|
|
"""Return a multi-line human-readable summary."""
|
|
lines = [f"focus ring ({len(self.classes)} stops):"]
|
|
lines.append(" " + " -> ".join(self.classes) + " -> (wrap)")
|
|
lines.append("focus-ring colors by class:")
|
|
for name, color in sorted(self.focus_rings_visible.items()):
|
|
verdict = (
|
|
"visible"
|
|
if color.lower() not in {"", "#000000", "black"}
|
|
else "INVISIBLE"
|
|
)
|
|
lines.append(f" {name:<12} highlightcolor={color or '<unset>'} {verdict}")
|
|
lines.append(f"Text escapes Tab : {self.text_escapes_tab}")
|
|
lines.append(f"Buttons accept Return : {self.buttons_take_return}")
|
|
return "\n".join(lines)
|
|
|
|
|
|
def _walk_ring(start: tk.Misc, limit: int = _MAX_RING_STEPS) -> list[tk.Misc]:
|
|
"""Return the widgets Tab visits, starting from ``start``, until it wraps."""
|
|
seen: list[tk.Misc] = []
|
|
current = start
|
|
for _ in range(limit):
|
|
if current in seen:
|
|
break
|
|
seen.append(current)
|
|
current = current.tk_focusNext()
|
|
if current is None:
|
|
break
|
|
return seen
|
|
|
|
|
|
def probe(screen_w: int = 1366, screen_h: int = 768) -> RingReport:
|
|
"""Build the gate and probe its keyboard behaviour."""
|
|
root = tk.Tk()
|
|
try:
|
|
root.geometry(f"{screen_w}x{screen_h}+0+0")
|
|
notebook = ttk.Notebook(root)
|
|
notebook.place(relx=0, rely=0, relwidth=1, relheight=1)
|
|
callbacks = GateCallbacks(
|
|
on_unit_change=_noop,
|
|
on_submit=_noop,
|
|
on_close=_noop,
|
|
on_fetch_sync=_noop,
|
|
)
|
|
widgets = build_layout(
|
|
notebook, _make_vars(populated=True), callbacks, demo_mode=False
|
|
)
|
|
notebook.add(widgets.frame, text="Log Meal")
|
|
notebook.enable_traversal()
|
|
root.focus_force()
|
|
root.update()
|
|
root.update_idletasks()
|
|
|
|
report = RingReport()
|
|
ring = _walk_ring(widgets.desc_text)
|
|
for widget in ring:
|
|
report.classes.append(widget.winfo_class())
|
|
try:
|
|
color = str(widget.cget("highlightcolor"))
|
|
except tk.TclError:
|
|
continue
|
|
report.focus_rings_visible.setdefault(widget.winfo_class(), color)
|
|
|
|
# Tab out of the description box: it must not stay focused.
|
|
widgets.desc_text.focus_set()
|
|
root.update()
|
|
widgets.desc_text.event_generate("<Tab>")
|
|
root.update()
|
|
root.update_idletasks()
|
|
report.text_escapes_tab = root.focus_get() is not widgets.desc_text
|
|
|
|
# Return must invoke a button (Tk binds only <space> on X11).
|
|
fired: list[str] = []
|
|
probe_button = next(
|
|
(w for w in ring if isinstance(w, tk.Button)),
|
|
None,
|
|
)
|
|
if probe_button is not None:
|
|
probe_button.configure(command=lambda: fired.append("hit"))
|
|
probe_button.focus_set()
|
|
root.update()
|
|
probe_button.event_generate("<Return>")
|
|
root.update()
|
|
root.update_idletasks()
|
|
report.buttons_take_return = bool(fired)
|
|
return report
|
|
finally:
|
|
root.destroy()
|