diff --git a/gui/dashboard_app/page_data.py b/gui/dashboard_app/page_data.py index d4cba29..bde9c54 100644 --- a/gui/dashboard_app/page_data.py +++ b/gui/dashboard_app/page_data.py @@ -5,7 +5,7 @@ import tkinter as tk from tkinter import ttk from . import theme -from .data import DashboardData, fmt_ts +from .data import DashboardData, fmt_ts, load_dashboard_data from .page_context import PageContext from .page_helpers import reservation_rows from .widgets import flow_grid, log_panel, preview_panel, section, stat_tile, table_panel, text_panel, toolbar @@ -192,19 +192,133 @@ def build_preview(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> Non preview_panel(parent, data.current_screenshot_path, "当前截图").pack(fill="both", expand=True) -def build_reservation_queue(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: - section(parent, "预约队列", "预约队列总览与等待状态") - toolbar(parent, [("刷新", ctx.action("refresh"), "soft")]) +RESERVATION_COLUMNS = ( + ("idx", "#", 40), ("pos", "位置", 70), ("title", "直播间", 220), ("raw_title", "原始标题", 240), + ("content", "内容", 220), ("value", "价值", 80), ("countdown", "倒计时/开奖", 120), + ("participants", "人数", 74), ("copies", "份数", 70), ("task_count", "任务数", 74), + ("status", "状态", 90), ("target_time", "开奖时间", 160), ("created_time", "记录时间", 160), +) + + +HOME_RESERVATION_COLUMNS = ( + ("idx", "#", 32), ("pos", "位置", 54), ("title", "直播间", 120), ("content", "内容", 160), + ("value", "价值", 58), ("countdown", "倒计时", 76), ("status", "状态", 68), ("target_time", "开奖时间", 132), +) + + +def _reservation_rows_for_display(data: DashboardData, *, include_created: bool) -> list[dict]: rows = reservation_rows(data) for row in rows: row["target_time"] = fmt_ts(row["target_time"]) - row["created_time"] = fmt_ts(row["created_time"]) - table_panel(parent, "预约队列", ( - ("idx", "#", 40), ("pos", "位置", 70), ("title", "直播间", 220), ("raw_title", "原始标题", 240), - ("content", "内容", 220), ("value", "价值", 80), ("countdown", "倒计时/开奖", 120), - ("participants", "人数", 74), ("copies", "份数", 70), ("task_count", "任务数", 74), - ("status", "状态", 90), ("target_time", "开奖时间", 160), ("created_time", "记录时间", 160), - ), rows, height=18).pack(fill="both", expand=True) + if include_created: + row["created_time"] = fmt_ts(row["created_time"]) + return rows + + +def _reservation_auto_refresh_var(ctx: PageContext) -> tk.BooleanVar: + var = ctx.vars.get("reservation_auto_refresh") + if isinstance(var, tk.BooleanVar): + return var + var = tk.BooleanVar(value=True) + ctx.vars["reservation_auto_refresh"] = var + return var + + +def _replace_table_rows(tree: ttk.Treeview, columns: tuple[tuple[str, str, int], ...], rows: list[dict]) -> None: + try: + yview = tree.yview() + top_pos = yview[0] + except Exception: + top_pos = 0.0 + for item_id in tree.get_children(""): + tree.delete(item_id) + if rows: + for row in rows: + tree.insert("", "end", values=[str(row.get(key, "-")) for key, _, _ in columns]) + else: + tree.insert("", "end", values=["暂无数据" if idx == 0 else "" for idx, _c in enumerate(columns)]) + try: + tree.yview_moveto(top_pos) + except Exception: + pass + + +def _schedule_reservation_table_refresh( + table_box: tk.Misc, + ctx: PageContext, + columns: tuple[tuple[str, str, int], ...], + *, + include_created: bool, + interval_ms: int = 5000, +) -> None: + enabled_var = _reservation_auto_refresh_var(ctx) + if getattr(table_box, "_reservation_refresh_scheduled", False): + return + setattr(table_box, "_reservation_refresh_scheduled", True) + + def tick() -> None: + setattr(table_box, "_reservation_refresh_scheduled", False) + try: + if not table_box.winfo_exists(): + return + except Exception: + return + if bool(enabled_var.get()): + tree = getattr(table_box, "tree", None) + if tree is not None: + latest = load_dashboard_data() + rows = _reservation_rows_for_display(latest, include_created=include_created) + _replace_table_rows(tree, columns, rows) + _schedule_reservation_table_refresh( + table_box, + ctx, + columns, + include_created=include_created, + interval_ms=interval_ms, + ) + + try: + table_box.after(interval_ms, tick) + except Exception: + pass + + +def reservation_auto_refresh_control(parent: tk.Misc, ctx: PageContext, table_box: tk.Misc, columns, *, include_created: bool) -> tk.Checkbutton: + auto_refresh_var = _reservation_auto_refresh_var(ctx) + check = tk.Checkbutton( + parent, + text="自动刷新(5s)", + variable=auto_refresh_var, + bg=theme.BG, + fg=theme.TEXT, + activebackground=theme.BG, + activeforeground=theme.TEXT, + selectcolor=theme.SURFACE, + relief="flat", + bd=0, + font=theme.FONT_UI, + cursor="hand2", + ) + check.configure( + command=lambda: _schedule_reservation_table_refresh( + table_box, + ctx, + columns, + include_created=include_created, + ) + ) + if bool(auto_refresh_var.get()): + _schedule_reservation_table_refresh(table_box, ctx, columns, include_created=include_created) + return check + + +def build_reservation_queue(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: + section(parent, "预约队列", "预约队列总览与等待状态") + tools = toolbar(parent, [("刷新", ctx.action("refresh"), "soft")]) + rows = _reservation_rows_for_display(data, include_created=True) + table = table_panel(parent, "预约队列", RESERVATION_COLUMNS, rows, height=18) + table.pack(fill="both", expand=True) + reservation_auto_refresh_control(tools, ctx, table, RESERVATION_COLUMNS, include_created=True).pack(side="left", padx=(4, 0)) def build_high_value(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: diff --git a/gui/dashboard_app/page_home.py b/gui/dashboard_app/page_home.py index 7103eff..98f6d2c 100644 --- a/gui/dashboard_app/page_home.py +++ b/gui/dashboard_app/page_home.py @@ -9,8 +9,8 @@ from typing import Any from . import theme from .data import DashboardData, fmt_ts, load_dashboard_data +from .page_data import HOME_RESERVATION_COLUMNS, _reservation_rows_for_display, reservation_auto_refresh_control from .page_context import PageContext -from .page_helpers import reservation_rows from .stats_view import StatsDashboardView from .widgets import flow_grid, preview_panel, section, stat_tile, table_panel @@ -202,13 +202,18 @@ def build_home(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: left.grid(row=0, column=0, sticky="nsew", padx=(0, 10)) right.grid(row=0, column=1, sticky="nsew", padx=(10, 0)) - rows = reservation_rows(data) - for row in rows: - row["target_time"] = fmt_ts(row["target_time"]) - table_panel(left, "预约队列", ( - ("idx", "#", 32), ("pos", "位置", 54), ("title", "直播间", 120), ("content", "内容", 160), - ("value", "价值", 58), ("countdown", "倒计时", 76), ("status", "状态", 68), ("target_time", "开奖时间", 132), - ), rows, height=18).pack(fill="both", expand=True) + rows = _reservation_rows_for_display(data, include_created=False) + reservation_table = table_panel(left, "预约队列", HOME_RESERVATION_COLUMNS, rows, height=18) + reservation_table.pack(fill="both", expand=True) + control_row = tk.Frame(left, bg=theme.BG) + control_row.pack(fill="x", pady=(6, 0)) + reservation_auto_refresh_control( + control_row, + ctx, + reservation_table, + HOME_RESERVATION_COLUMNS, + include_created=False, + ).pack(side="left") preview_panel(right, data.current_screenshot_path, "实时截图").pack(fill="both", expand=True) diff --git a/gui/dashboard_app/widgets.py b/gui/dashboard_app/widgets.py index 08a2db7..a9b79e7 100644 --- a/gui/dashboard_app/widgets.py +++ b/gui/dashboard_app/widgets.py @@ -394,6 +394,8 @@ def table_panel(parent, title: str, columns: Sequence[tuple[str, str, int]], row tree.grid(row=0, column=0, sticky="nsew") ybar.grid(row=0, column=1, sticky="ns") xbar.grid(row=1, column=0, sticky="ew") + box.tree = tree # type: ignore[attr-defined] + box.columns_spec = list(columns) # type: ignore[attr-defined] return box