diet-guard/diet_guard/_gatelock_calendar_ui.py
Krzysztof kuhy Rudnicki c310861855 slots: minute-of-day consumers (phase 2 of 3: Python side)
Every Python consumer now speaks slot minutes:

- satisfaction is nearest-slot snapping (_slot_wire.satisfied_by_entries)
  at every "is this slot logged?" site: gate, CLI status, MCP, catering
  occupancy and dedup. Nothing on disk is rewritten; 07:00/10:00 entries
  keep covering 07:15/10:15 after a schedule move.
- log_meal writes slot_fields(minute) before signing (slot + slot_min).
- schedule store and sync encode via schedule_to_wire/from_wire; a
  whole-hour schedule stays byte-identical (f/l/n only).
- CLI ate --hour takes HH or HH:MM; MCP log_meal takes "HH:MM" or a
  legacy int hour, validated against today's slots; get_slots returns
  {minute, label}.
- gate History tab: first/last are HH:MM ttk.Comboboxes (quarter-hour
  list, typed times, Up/Down step without posting the popdown), locked
  as disabled; validation mirrors normalized().
- gate timer fires every 15 min; docs rewritten for minutes.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015npsYVB2PdtWkG7MPAi4tn
2026-10-10 19:53:39 +02:00

101 lines
3.2 KiB
Python

"""``ttk`` theming for the gate's History tab, and its public surface.
Split across three modules to keep each under the repo's 250-line limit:
:mod:`._gatelock_calendar_types` holds the data bundles, constants and
palette, :mod:`._gatelock_calendar_widgets` builds the widgets, and this
module owns the ``ttk.Notebook`` theming.
Everything the tab's controller (:mod:`._gatelock_calendar`) needs is
re-exported here, because that module already imports all seven names from
this one. The ``ttk`` import is also load-bearing beyond theming: the test
suite patches ``_gatelock_calendar_ui.ttk`` with a fake, so this module must
keep binding ``ttk`` at module level.
"""
from __future__ import annotations
from tkinter import ttk
from typing import TYPE_CHECKING
from diet_guard._gatelock_calendar_types import (
_ACCENT,
_COLORS,
_DECEMBER,
_DEFAULT_BUDGET_KCAL,
_FIELD_BG,
_JANUARY,
_MONTH_AFTER_DECEMBER,
CalendarCallbacks,
CalendarVars,
CalendarWidgets,
make_calendar_vars,
)
from diet_guard._gatelock_calendar_widgets import build_calendar_frame
from diet_guard._gatelock_spacing import MD, SM
from diet_guard._gatelock_ui import BG, FG
if TYPE_CHECKING:
import tkinter as tk
__all__ = [
"_DECEMBER",
"_DEFAULT_BUDGET_KCAL",
"_JANUARY",
"_MONTH_AFTER_DECEMBER",
"CalendarCallbacks",
"CalendarVars",
"CalendarWidgets",
"_style_notebook",
"build_calendar_frame",
"make_calendar_vars",
]
def _style_notebook(root: tk.Misc) -> None:
"""Theme the History tab's ttk widgets to match the gate's dark palette.
Covers the ``ttk.Notebook`` tab strip and the schedule row's
``ttk.Combobox`` fields.
``ttk`` widgets ignore plain ``bg=``/``fg=`` -- unlike every other widget
here, they only take color from a named ``ttk.Style``. Without this the
tab strip renders in the OS's default (light) ttk theme regardless of how
dark the rest of the gate is. ``clam`` is used because it is the ttk
theme that reliably honors ``.configure()`` overrides cross-platform; the
built-in ``default``/``alt`` themes on some platforms ignore custom tab
colors.
"""
style = ttk.Style(root)
style.theme_use("clam")
style.configure("TNotebook", background=BG, borderwidth=0)
style.configure(
"TNotebook.Tab",
background=_FIELD_BG,
foreground=FG,
padding=(MD, SM),
borderwidth=0,
)
style.map(
"TNotebook.Tab",
background=[("selected", _ACCENT)],
foreground=[("selected", _COLORS.palette.on_fill)],
)
# The meal-schedule row's HH:MM comboboxes. Locked they are ``disabled``,
# which clam greys out; mapping the disabled state back to the field
# colours keeps the stored schedule legible, matching the read-only
# ``tk.Entry`` beside them.
style.configure(
"TCombobox",
fieldbackground=_FIELD_BG,
background=_FIELD_BG,
foreground=FG,
arrowcolor=FG,
insertcolor=FG,
)
style.map(
"TCombobox",
fieldbackground=[("disabled", _FIELD_BG), ("readonly", _FIELD_BG)],
foreground=[("disabled", FG)],
arrowcolor=[("disabled", _COLORS.palette.muted)],
)