diet-guard/diet_guard/_gatelock_calendar.py
Krzysztof Rudnicki 1702685afd Make the meal gate usable without a pointer on a 1366x768 screen
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>
2026-07-30 16:21:27 +02:00

286 lines
11 KiB
Python

"""History tab: budget-adherence calendar, streaks, YTD tally, budget edit.
Split out of :mod:`._gatelock` (and kept out of :mod:`._gatelock_ui`) to keep
every gate module under the repo's 500-line limit. ``_GateCalendar`` extends
:class:`~diet_guard._gatelock_mealflow._GateMealFlow` with a second
``ttk.Notebook`` tab: the calendar/streak/tally view built from
:mod:`diet_guard._calendar_view`'s pure grid math, plus a budget-edit field
writing through :func:`diet_guard._budget.write_budget` -- the one place
either device edits the now freely-editable, synced daily budget.
The month-grid math itself lives in :mod:`diet_guard._calendar_view` (no Tk
dependency), so the boundary cases (varying month length/first weekday,
every :class:`~diet_guard._daystatus.DayStatus`, future days, no budget set)
are unit-tested directly without a fake Tk in the loop.
"""
from __future__ import annotations
import calendar
import contextlib
import tkinter as tk
from tkinter import ttk
from diet_guard._budget import (
BudgetError,
BudgetFileCorruptError,
BudgetNotInitializedError,
budget_weight,
current_schedule,
daily_budget,
write_budget,
)
from diet_guard._calendar_view import (
CalendarCell,
build_month_cells,
cell_style,
streaks_text,
ytd_text,
)
from diet_guard._daystatus import DayStatus, status_map
from diet_guard._gatelock_calendar_ui import (
_DECEMBER,
_DEFAULT_BUDGET_KCAL,
_JANUARY,
_MONTH_AFTER_DECEMBER,
CalendarCallbacks,
CalendarVars,
CalendarWidgets,
_style_notebook,
build_calendar_frame,
make_calendar_vars,
)
from diet_guard._gatelock_mealflow import _GateMealFlow
from diet_guard._gatelock_ui import (
ERR,
FG,
GateCallbacks,
GateWidgets,
build_layout,
)
from diet_guard._state import load_log, now_local
__all__ = [
"CalendarCallbacks",
"CalendarVars",
"CalendarWidgets",
"_GateCalendar",
"build_calendar_frame",
"make_calendar_vars",
]
class _GateCalendar(_GateMealFlow):
"""History tab: calendar, streaks, YTD tally, and budget editing."""
_cal_vars: CalendarVars
_cal_widgets: CalendarWidgets
_cal_surfaces: list[CalendarWidgets]
_cal_year: int
_cal_month: int
_cal_editing_budget: bool
_notebook: ttk.Notebook
def _build_tabs(self, parent: tk.Misc, callbacks: GateCallbacks) -> GateWidgets:
"""Build one monitor's notebook, wire both tabs, return its meal widgets.
Called once per live output since gatelock v0.2.0. Every copy shares
one set of ``GateVars``, so the meal form stays in step across
monitors; the calendar tab is read-only apart from the budget field,
which is likewise variable-backed.
The History tab's own widgets are stashed per surface so the refresh
pass can repaint every monitor's grid.
"""
today = now_local().date()
self._cal_year = today.year
self._cal_month = today.month
self._cal_editing_budget = False
_style_notebook(parent)
notebook = ttk.Notebook(parent)
notebook.place(relx=0, rely=0, relwidth=1, relheight=1)
self._notebook = notebook
widgets = build_layout(
notebook, self._vars, callbacks, demo_mode=self.demo_mode
)
notebook.add(widgets.frame, text="Log Meal")
cal_callbacks = CalendarCallbacks(
on_prev_month=self._on_prev_month,
on_next_month=self._on_next_month,
on_edit_or_save_budget=self._on_edit_or_save_budget,
)
cal_widgets = build_calendar_frame(notebook, self._cal_vars, cal_callbacks)
notebook.add(cal_widgets.frame, text="History")
# Ctrl+Tab / Ctrl+PageUp / Ctrl+PageDown and Alt+mnemonic tab switching
# all live in toplevel bindings that ttk only installs on request. The
# notebook is in the focus ring and Left/Right work once it holds focus,
# but without this the idioms a user actually reaches for are dead.
notebook.enable_traversal()
self._cal_surfaces.append(cal_widgets)
self._cal_widgets = cal_widgets
return widgets
# -- refresh --------------------------------------------------------------
def _refresh_calendar(self) -> None:
"""Recompute the calendar grid, streaks, YTD tally, and budget field.
Each past day is judged against the budget that applied *then*
(:mod:`diet_guard._budget_history`), so changing the budget today
leaves the grid, the streaks, and the tally for earlier days alone.
A budget that was never set defaults to
:data:`_DEFAULT_BUDGET_KCAL`, matching the phone app, so the tab is
meaningful from a fresh install with no setup ritual. A genuinely
corrupt budget file is a real problem, not just "unset" -- that
still degrades to a neutral grid and an error message, instead of
raising through the tab and crashing (or failing open) the lock.
Degradation is deliberately asymmetric: an unreadable *history*
silently falls back to today's scalar budget (the pre-history
behaviour), because that is a display nicety, whereas an unreadable
budget is the number itself.
"""
log = load_log()
try:
budget: int | None = daily_budget()
except BudgetNotInitializedError:
budget = _DEFAULT_BUDGET_KCAL
except BudgetFileCorruptError:
budget = None
status_map_ = (
status_map(log, schedule=current_schedule(default=budget))
if budget is not None
else None
)
self._render_month(status_map_)
if status_map_ is None:
self._cal_vars.streaks.set("Budget file is corrupt -- fix it below.")
self._cal_vars.ytd.set("")
else:
self._cal_vars.streaks.set(streaks_text(status_map_))
self._cal_vars.ytd.set(ytd_text(status_map_))
if not self._cal_editing_budget:
self._refresh_budget_field(budget)
def _render_month(self, status_map_: dict[str, DayStatus] | None) -> None:
"""Redraw the day-cell grid and month label for the displayed month."""
weeks = build_month_cells(self._cal_year, self._cal_month, status_map_)
self._cal_vars.month_label.set(
f"{calendar.month_name[self._cal_month]} {self._cal_year}",
)
for surface in self._cal_surfaces:
for index, cell_widget in enumerate(surface.day_cells):
row, col = divmod(index, 7)
spec = weeks[row][col] if row < len(weeks) else CalendarCell(None, None)
bg, fg, outline = cell_style(spec.status)
with contextlib.suppress(tk.TclError):
cell_widget.config(
text=str(spec.day) if spec.day else "",
bg=bg,
fg=fg,
highlightbackground=outline,
)
def _refresh_budget_field(self, budget: int | None) -> None:
"""Show ``budget`` in the (read-only) entry, or leave it blank if unset.
The text goes through the shared variable rather than the widget,
which sidesteps the old dance of flipping to "normal" to mutate it:
a ``readonly`` Entry rejects ``.insert``/``.delete`` but still
follows its ``textvariable``.
"""
self._cal_vars.budget.set("" if budget is None else str(budget))
self._set_budget_entry_state("readonly")
# -- month navigation -------------------------------------------------------
def _on_prev_month(self) -> None:
"""Step the displayed month back one, wrapping the year at January."""
self._cal_month -= 1
if self._cal_month < _JANUARY:
self._cal_month = _DECEMBER
self._cal_year -= 1
self._refresh_calendar()
def _on_next_month(self) -> None:
"""Step the displayed month forward one, wrapping the year at December."""
self._cal_month += 1
if self._cal_month == _MONTH_AFTER_DECEMBER:
self._cal_month = _JANUARY
self._cal_year += 1
self._refresh_calendar()
# -- budget editing -----------------------------------------------------
def _set_budget_entry_state(self, state: str) -> None:
"""Lock or unlock the budget entry on every monitor."""
for surface in self._cal_surfaces:
with contextlib.suppress(tk.TclError):
surface.budget_entry.config(state=state)
def _set_budget_button_text(self, text: str) -> None:
"""Relabel the budget edit/save button on every monitor."""
for surface in self._cal_surfaces:
with contextlib.suppress(tk.TclError):
surface.budget_edit_button.config(text=text)
def _set_budget_status(self, text: str, *, error: bool) -> None:
"""Update the budget-edit status line, red for errors."""
self._cal_vars.budget_status.set(text)
colour = ERR if error else FG
for surface in self._cal_surfaces:
with contextlib.suppress(tk.TclError):
surface.budget_status_label.config(fg=colour)
def _on_edit_or_save_budget(self) -> None:
"""Toggle the budget row between read-only display and editing.
First click: unlock the entry for typing and relabel the button
"Save" -- nothing is persisted yet. Second click: validate and
persist; on success, lock the entry back to read-only and relabel
the button "Edit". A validation failure leaves editing open so the
user can correct the value instead of silently discarding it.
"""
if not self._cal_editing_budget:
self._cal_editing_budget = True
self._set_budget_entry_state("normal")
self._set_budget_button_text("Save")
self._set_budget_status("", error=False)
return
if not self._save_budget_entry():
return
self._cal_editing_budget = False
self._set_budget_button_text("Edit")
self._refresh_calendar()
self._refresh_dashboard()
self._refresh_projection()
def _save_budget_entry(self) -> bool:
"""Validate and persist the entry's current text.
Preserves any body weight already stored alongside the budget (used
to derive the protein target) -- a bare ``write_budget(value)`` would
silently drop it, since the file holds one record, not a diff.
Returns:
Whether the value was valid and persisted.
"""
raw = self._cal_vars.budget.get().strip()
try:
value = int(raw)
except ValueError:
self._set_budget_status("Enter a whole number of kcal.", error=True)
return False
if value <= 0:
self._set_budget_status("Budget must be a positive number.", error=True)
return False
try:
weight = budget_weight()
except BudgetError:
weight = None
write_budget(value, weight_kg=weight)
self._set_budget_status("Saved.", error=False)
return True