from __future__ import annotations import tkinter as tk from . import theme from .page_context import PageContext from .widgets import card, field, select_field, spin_field, switch_row FieldSpec = tuple[str, str, object] | dict[str, object] def pack_fields( parent: tk.Misc, ctx: PageContext, items: list[FieldSpec], columns: int = 2, ) -> None: grid = tk.Frame(parent, bg=theme.SURFACE) grid.pack(fill="x", padx=12, pady=(3, 12)) for col in range(columns): grid.grid_columnconfigure(col, weight=1, uniform="field") for idx, item in enumerate(items): cell = _build_field_cell(grid, ctx, item) cell.grid(row=idx // columns, column=idx % columns, sticky="ew", padx=5, pady=4) def select_spec(label: str, var_name: str, fallback: object, values: list[str] | tuple[str, ...]) -> dict[str, object]: return {"kind": "select", "label": label, "var": var_name, "fallback": fallback, "values": values} def spin_spec( label: str, var_name: str, fallback: object, *, from_: float | int, to: float | int, increment: float | int = 1, ) -> dict[str, object]: return { "kind": "spin", "label": label, "var": var_name, "fallback": fallback, "from": from_, "to": to, "increment": increment, } def _build_field_cell(parent: tk.Misc, ctx: PageContext, item: FieldSpec) -> tk.Frame: if isinstance(item, dict): label = str(item.get("label", "")) var_name = str(item.get("var", "")) fallback = item.get("fallback", "") variable = ctx.var(var_name) kind = str(item.get("kind", "text")) if kind == "select": return select_field(parent, label, variable, value=fallback, values=tuple(item.get("values", ()))) if kind == "spin": return spin_field( parent, label, variable, value=fallback, from_=item.get("from", 0), to=item.get("to", 99999), increment=item.get("increment", 1), ) return field(parent, label, variable, value=fallback) label, var_name, fallback = item return field(parent, label, ctx.var(var_name), value=fallback) def pack_switches( parent: tk.Misc, ctx: PageContext, items: list[tuple[str, str, bool]], columns: int = 2, ) -> None: grid = tk.Frame(parent, bg=theme.SURFACE) grid.pack(fill="x", padx=12, pady=(3, 12)) for col in range(columns): grid.grid_columnconfigure(col, weight=1, uniform="switch") for idx, (label, var_name, fallback) in enumerate(items): cell = switch_row(grid, label, ctx.var(var_name), fallback) cell.grid(row=idx // columns, column=idx % columns, sticky="ew", padx=5, pady=4) def text_box( parent: tk.Misc, ctx: PageContext, key: str, title: str, initial: list[str], height: int = 7, ) -> tk.Text: box = card(parent, title) box.pack(fill="both", expand=True, pady=(0, 12)) text = tk.Text( box, height=height, bd=0, bg=theme.SURFACE_ALT, fg=theme.TEXT, insertbackground=theme.TEAL, font=theme.FONT_UI, padx=9, pady=8, ) text.pack(fill="both", expand=True, padx=11, pady=(4, 11)) text.insert("1.0", "\n".join(str(x) for x in initial)) ctx.texts[key] = text return text def reservation_rows(data) -> list[dict]: rows = [] for idx, item in enumerate(data.reservations, 1): rows.append({ "idx": idx, "pos": item.get("pos_seen") or item.get("room_pos") or item.get("pos") or "-", "title": item.get("room_title") or item.get("title") or item.get("live_title") or "-", "raw_title": item.get("room_title_raw") or item.get("title_raw") or "-", "content": item.get("content") or item.get("prize_content") or item.get("gift") or "-", "value": item.get("prize_value_yuan") or item.get("value") or "-", "countdown": item.get("time_text") or item.get("countdown") or item.get("future_str") or "-", "participants": item.get("participants") or "-", "copies": item.get("copies") or "-", "task_count": item.get("task_count") or "-", "status": item.get("status") or item.get("state") or "等待中", "target_time": item.get("future_ts") or item.get("target_ts") or "-", "created_time": item.get("created_ts") or "-", }) if rows: return rows state = getattr(data, "reservations_state", {}) or {} if isinstance(state, dict) and state: rows.append({ "idx": "-", "pos": state.get("room_pos") or "-", "title": state.get("current_room_title") or "-", "raw_title": state.get("current_room_title_raw") or "-", "content": state.get("reason") or "队列为空", "value": "-", "countdown": "-", "participants": "-", "copies": "-", "task_count": "-", "status": "队列为空", "target_time": "-", "created_time": state.get("updated_ts") or "-", }) return rows