diff --git a/gui/dashboard_app/app.py b/gui/dashboard_app/app.py index 8267d66..2fa29a9 100644 --- a/gui/dashboard_app/app.py +++ b/gui/dashboard_app/app.py @@ -18,7 +18,7 @@ from . import theme from .data import load_dashboard_data from .page_context import PageContext from .page_registry import PAGES -from .widgets import ScrollFrame, button, clear, nav_button, status_pill +from .widgets import ScrollFrame, button, clear, nav_button, rounded_surface, status_pill class DashboardApp: @@ -64,7 +64,7 @@ class DashboardApp: shell = ttk.Frame(self.root, style="Dashboard.TFrame") shell.pack(fill="both", expand=True) - top = tk.Frame(shell, bg=theme.SURFACE, highlightbackground=theme.BORDER, highlightthickness=1) + top = rounded_surface(shell, fill=theme.SURFACE, outline=theme.BORDER, radius=theme.RADIUS_MD) top.pack(fill="x", padx=18, pady=(14, 8)) brand = tk.Frame(top, bg=theme.SURFACE) brand.pack(fill="x", padx=18, pady=(12, 5)) @@ -79,7 +79,7 @@ class DashboardApp: tool_row = tk.Frame(top, bg=theme.SURFACE) tool_row.pack(fill="x", padx=18, pady=(0, 12)) ttk.Label(tool_row, text="设备:", background=theme.SURFACE, foreground=theme.MUTED).pack(side="left") - self.device_combo = ttk.Combobox(tool_row, textvariable=self.device_var, values=[], width=28) + self.device_combo = ttk.Combobox(tool_row, textvariable=self.device_var, values=[], width=28, state="readonly") self.device_combo.pack(side="left", padx=(4, 8)) button(tool_row, "刷新设备", self.refresh_devices, width=126).pack(side="left", padx=3) self.scan_button = button(tool_row, "扫描 WiFi", self.scan_wifi, width=128) @@ -95,20 +95,36 @@ class DashboardApp: main.grid_columnconfigure(1, weight=1) main.grid_rowconfigure(0, weight=1) - sidebar = tk.Frame(main, bg=theme.SURFACE, highlightbackground=theme.BORDER, highlightthickness=1) + sidebar = rounded_surface(main, fill=theme.SURFACE, outline=theme.BORDER, radius=theme.RADIUS_MD) sidebar.grid(row=0, column=0, sticky="nsw", padx=(0, 16)) sidebar.configure(width=204) sidebar.grid_propagate(False) sidebar.pack_propagate(False) + nav_canvas = tk.Canvas(sidebar, bg=theme.SURFACE, highlightthickness=0, bd=0) + nav_scroll = ttk.Scrollbar(sidebar, orient="vertical", command=nav_canvas.yview) + nav_inner = tk.Frame(nav_canvas, bg=theme.SURFACE) + nav_window = nav_canvas.create_window((0, 0), window=nav_inner, anchor="nw") + nav_canvas.configure(yscrollcommand=nav_scroll.set) + nav_canvas.pack(side="left", fill="both", expand=True, padx=(0, 2), pady=8) + nav_scroll.pack(side="right", fill="y", pady=10) + nav_inner.bind("", lambda _e: nav_canvas.configure(scrollregion=nav_canvas.bbox("all"))) + nav_canvas.bind("", lambda e: nav_canvas.itemconfigure(nav_window, width=e.width)) + + def _nav_wheel(event): + nav_canvas.yview_scroll(int(-1 * (event.delta / 120)), "units") + return "break" + + nav_canvas.bind("", _nav_wheel) + nav_inner.bind("", _nav_wheel) for group_title, names in [ ("总览", ["数据看板"]), ("配置", ["运行设置", "筛选设置", "高价值预约", "养号设置", "调试参数", "授权", "日志配置", "关键词"]), - ("数据", ["运行日志", "截图预览", "统计信息", "预约队列", "高价值房间", "行为日志", "实时状态", "OCR结果", "中奖记录", "映射表"]), + ("数据", ["运行日志", "行为日志", "实时状态", "截图预览", "统计信息", "预约队列", "高价值房间", "OCR结果", "中奖记录", "映射表"]), ("连接", ["WiFi ADB"]), ]: - ttk.Label(sidebar, text=group_title, background=theme.SURFACE, foreground=theme.MUTED, font=theme.FONT_SMALL).pack(anchor="w", padx=14, pady=(13, 5)) + ttk.Label(nav_inner, text=group_title, background=theme.SURFACE, foreground=theme.MUTED, font=theme.FONT_SMALL).pack(anchor="w", padx=14, pady=(13, 5)) for name in names: - btn = nav_button(sidebar, name, lambda n=name: self.show_page(n), active=name == self.current_page) + btn = nav_button(nav_inner, name, lambda n=name: self.show_page(n), active=name == self.current_page) btn.pack(fill="x", padx=10, pady=1) self.nav_buttons[name] = btn @@ -116,7 +132,7 @@ class DashboardApp: self.content_scroll.grid(row=0, column=1, sticky="nsew") self.content = self.content_scroll.inner - bottom = tk.Frame(shell, bg=theme.SURFACE, highlightbackground=theme.BORDER, highlightthickness=1) + bottom = rounded_surface(shell, fill=theme.SURFACE, outline=theme.BORDER, radius=theme.RADIUS_MD) bottom.pack(fill="x", padx=18, pady=(4, 14)) inner_bottom = tk.Frame(bottom, bg=theme.SURFACE) inner_bottom.pack(fill="x", padx=14, pady=11) @@ -155,6 +171,8 @@ class DashboardApp: "screenshot_ocr": self.screenshot_ocr, "scan_wifi": self.scan_wifi, "wifi_adb": self.open_wifi_adb, + "show_run_log": lambda: self.show_page("运行日志"), + "show_actions_log": lambda: self.show_page("行为日志"), } def _selected_device(self) -> str: @@ -329,7 +347,7 @@ class DashboardApp: self._ensure_text_widgets() clear(self.content) builder = PAGES.get(name, PAGES["数据看板"]) - builder(self.content, self.data, PageContext(self.config_vars, self.text_widgets, self._actions(), self.status_var)) + builder(self.content, self.data, PageContext(self.config_vars, self.text_widgets, self._actions(), self.status_var, self.runner)) try: self.content_scroll.canvas.yview_moveto(0) except Exception: diff --git a/gui/dashboard_app/page_config.py b/gui/dashboard_app/page_config.py index 2a3a802..1ea9c0b 100644 --- a/gui/dashboard_app/page_config.py +++ b/gui/dashboard_app/page_config.py @@ -5,7 +5,7 @@ import tkinter as tk from . import theme from .data import DashboardData, cfg_get from .page_context import PageContext -from .page_helpers import pack_fields, pack_switches, text_box +from .page_helpers import pack_fields, pack_switches, select_spec, spin_spec, text_box from .widgets import button, card, section @@ -21,13 +21,13 @@ def build_run_settings(parent: tk.Misc, data: DashboardData, ctx: PageContext) - ("启用下滑回退直播间", "swipe_down_back_enabled_var", False), ]) pack_fields(box, ctx, [ - ("运行时长(分钟)", "wait_minutes_var", cfg_get(data.cfg, "wait_minutes")), - ("倒计时大于(秒)时不参与", "skip_gt_seconds_var", cfg_get(data.cfg, "skip_if_countdown_gt_seconds")), - ("次数", "reset_after_swipes_var", cfg_get(data.cfg, "reset_after_swipes")), - ("滑动重置回退方式", "reset_after_swipes_action_var", cfg_get(data.cfg, "reset_after_swipes_action")), - ("进入直播间坐标模式", "reenter_live_entry_mode_var", cfg_get(data.cfg, "reenter_live_entry_mode")), - ("下滑回退次数", "swipe_down_back_count_var", cfg_get(data.cfg, "swipe_down_back_count")), - ("连续无福袋退出阈值", "fudai_exit_after_misses_var", cfg_get(data.cfg, "fudai_exit_after_misses")), + spin_spec("运行时长(分钟)", "wait_minutes_var", cfg_get(data.cfg, "wait_minutes"), from_=1, to=60), + spin_spec("倒计时大于(秒)时不参与", "skip_gt_seconds_var", cfg_get(data.cfg, "skip_if_countdown_gt_seconds"), from_=0, to=3600), + spin_spec("次数", "reset_after_swipes_var", cfg_get(data.cfg, "reset_after_swipes"), from_=1, to=9999), + select_spec("滑动重置回退方式", "reset_after_swipes_action_var", cfg_get(data.cfg, "reset_after_swipes_action"), ["swipe", "back"]), + select_spec("进入直播间坐标模式", "reenter_live_entry_mode_var", cfg_get(data.cfg, "reenter_live_entry_mode"), ["follow_list", "screen_center"]), + spin_spec("下滑回退次数", "swipe_down_back_count_var", cfg_get(data.cfg, "swipe_down_back_count"), from_=1, to=9999), + spin_spec("连续无福袋退出阈值", "fudai_exit_after_misses_var", cfg_get(data.cfg, "fudai_exit_after_misses"), from_=1, to=99999), ]) @@ -37,14 +37,14 @@ def build_filter_settings(parent: tk.Misc, data: DashboardData, ctx: PageContext box.pack(fill="x") pack_switches(box, ctx, [("自动加入粉丝团(谨慎:可能消耗钻石)", "auto_join_var", False)], columns=1) pack_fields(box, ctx, [ - ("最低价值(元)", "min_value_var", cfg_get(data.cfg, "min_prize_value_yuan")), - ("参与人数最小", "min_participants_var", cfg_get(data.cfg, "min_participants")), - ("参与人数最大", "max_participants_var", cfg_get(data.cfg, "max_participants")), - ("倒计时小于(秒)时不参与", "skip_lt_seconds_var", cfg_get(data.cfg, "skip_if_countdown_lt_seconds")), - ("粉丝团失败策略", "fansclub_action_var", cfg_get(data.cfg, "fansclub_fail_action")), - ("粉丝团点击延迟(s)", "fansclub_click_delay_var", cfg_get(data.cfg, "fansclub_click_delay")), - ("自动加入等待(s)", "fansclub_auto_wait_var", cfg_get(data.cfg, "fansclub_auto_join_wait_seconds")), - ("手动加入等待(s)", "fansclub_manual_wait_var", cfg_get(data.cfg, "fansclub_manual_wait_seconds")), + spin_spec("最低价值(元)", "min_value_var", cfg_get(data.cfg, "min_prize_value_yuan"), from_=0, to=99999, increment=0.01), + spin_spec("参与人数最小", "min_participants_var", cfg_get(data.cfg, "min_participants"), from_=0, to=99999999), + spin_spec("参与人数最大", "max_participants_var", cfg_get(data.cfg, "max_participants"), from_=0, to=99999999), + spin_spec("倒计时小于(秒)时不参与", "skip_lt_seconds_var", cfg_get(data.cfg, "skip_if_countdown_lt_seconds"), from_=0, to=300), + select_spec("粉丝团失败策略", "fansclub_action_var", cfg_get(data.cfg, "fansclub_fail_action"), ["stay", "switch"]), + spin_spec("粉丝团点击延迟(s)", "fansclub_click_delay_var", cfg_get(data.cfg, "fansclub_click_delay"), from_=0.1, to=5.0, increment=0.1), + spin_spec("自动加入等待(s)", "fansclub_auto_wait_var", cfg_get(data.cfg, "fansclub_auto_join_wait_seconds"), from_=0.5, to=10.0, increment=0.5), + spin_spec("手动加入等待(s)", "fansclub_manual_wait_var", cfg_get(data.cfg, "fansclub_manual_wait_seconds"), from_=0.5, to=10.0, increment=0.5), ]) @@ -59,18 +59,18 @@ def build_reservation_settings(parent: tk.Misc, data: DashboardData, ctx: PageCo ("预约回退校验", "reservation_back_verify_enabled_var", True), ]) pack_fields(box, ctx, [ - ("价值阈值(元)", "high_value_fudai_threshold_yuan_var", cfg_get(data.cfg, "high_value_fudai_threshold_yuan")), - ("提前参与(秒)", "high_value_fudai_join_ahead_seconds_var", cfg_get(data.cfg, "high_value_fudai_join_ahead_seconds")), - ("最多回退直播间次数", "high_value_fudai_max_swipe_back_var", cfg_get(data.cfg, "high_value_fudai_max_swipe_back")), - ("最多记录数量", "high_value_fudai_max_reservations_var", cfg_get(data.cfg, "high_value_fudai_max_reservations")), - ("完成后策略", "high_value_fudai_after_action_var", cfg_get(data.cfg, "high_value_fudai_after_action")), - ("重访最小间隔(秒)", "high_value_room_revisit_min_interval_seconds_var", cfg_get(data.cfg, "high_value_room_revisit_min_interval_seconds")), - ("每次运行最大次数", "high_value_room_revisit_max_per_run_var", cfg_get(data.cfg, "high_value_room_revisit_max_per_run")), - ("最低奖品价值(元)", "high_value_room_revisit_min_prize_value_var", cfg_get(data.cfg, "high_value_room_revisit_min_prize_value")), - ("最大年龄(天)", "high_value_room_revisit_max_age_days_var", cfg_get(data.cfg, "high_value_room_revisit_max_age_days")), - ("开奖前轮询时间(秒)", "high_value_fudai_poll_before_seconds_var", cfg_get(data.cfg, "high_value_fudai_poll_before_seconds")), - ("回退滑延迟(秒)", "reservation_back_swipe_delay_var", cfg_get(data.cfg, "reservation_back_swipe_delay")), - ("校验等待时间(秒)", "reservation_back_verify_wait_seconds_var", cfg_get(data.cfg, "reservation_back_verify_wait_seconds")), + spin_spec("价值阈值(元)", "high_value_fudai_threshold_yuan_var", cfg_get(data.cfg, "high_value_fudai_threshold_yuan"), from_=0, to=99999, increment=100), + spin_spec("提前参与(秒)", "high_value_fudai_join_ahead_seconds_var", cfg_get(data.cfg, "high_value_fudai_join_ahead_seconds"), from_=5, to=900, increment=10), + spin_spec("最多回退直播间次数", "high_value_fudai_max_swipe_back_var", cfg_get(data.cfg, "high_value_fudai_max_swipe_back"), from_=0, to=300, increment=5), + spin_spec("最多记录数量", "high_value_fudai_max_reservations_var", cfg_get(data.cfg, "high_value_fudai_max_reservations"), from_=1, to=50), + select_spec("完成后策略", "high_value_fudai_after_action_var", cfg_get(data.cfg, "high_value_fudai_after_action"), ["continue", "stay"]), + spin_spec("重访最小间隔(秒)", "high_value_room_revisit_min_interval_seconds_var", cfg_get(data.cfg, "high_value_room_revisit_min_interval_seconds"), from_=60, to=3600), + spin_spec("每次运行最大次数", "high_value_room_revisit_max_per_run_var", cfg_get(data.cfg, "high_value_room_revisit_max_per_run"), from_=1, to=20), + spin_spec("最低奖品价值(元)", "high_value_room_revisit_min_prize_value_var", cfg_get(data.cfg, "high_value_room_revisit_min_prize_value"), from_=0.0, to=10000.0), + spin_spec("最大年龄(天)", "high_value_room_revisit_max_age_days_var", cfg_get(data.cfg, "high_value_room_revisit_max_age_days"), from_=1, to=365), + spin_spec("开奖前轮询时间(秒)", "high_value_fudai_poll_before_seconds_var", cfg_get(data.cfg, "high_value_fudai_poll_before_seconds"), from_=0.0, to=60.0), + spin_spec("回退滑延迟(秒)", "reservation_back_swipe_delay_var", cfg_get(data.cfg, "reservation_back_swipe_delay"), from_=0.0, to=10.0, increment=0.1), + spin_spec("校验等待时间(秒)", "reservation_back_verify_wait_seconds_var", cfg_get(data.cfg, "reservation_back_verify_wait_seconds"), from_=0.1, to=10.0, increment=0.1), ]) @@ -86,17 +86,17 @@ def build_yanghao_settings(parent: tk.Misc, data: DashboardData, ctx: PageContex ("启用定时禁抢", "fudai_quiet_hours_enabled_var", True), ]) pack_fields(box, ctx, [ - ("养号时长(秒)", "yg_recommend_duration_var", cfg_get(data.cfg, "yanghao_recommend_duration_seconds")), - ("养号间隔(秒)", "yg_recommend_interval_var", cfg_get(data.cfg, "yanghao_recommend_interval_seconds")), - ("爱心概率", "yg_like_prob_var", cfg_get(data.cfg, "yanghao_like_prob")), - ("收藏概率", "yg_fav_prob_var", cfg_get(data.cfg, "yanghao_fav_prob")), - ("评论概率", "yg_comment_prob_var", cfg_get(data.cfg, "yanghao_comment_prob")), - ("检测动作后停留最小(秒)", "yg_dwell_on_action_min_var", cfg_get(data.cfg, "yanghao_dwell_on_action_min_seconds")), - ("检测动作后停留最大(秒)", "yg_dwell_on_action_max_var", cfg_get(data.cfg, "yanghao_dwell_on_action_max_seconds")), - ("图标ROI大小(px)", "yg_icon_roi_size_var", cfg_get(data.cfg, "yanghao_icon_roi_size")), - ("匹配阈值", "yg_icon_match_threshold_var", cfg_get(data.cfg, "yanghao_icon_match_threshold")), - ("开始小时", "fudai_quiet_start_hour_var", cfg_get(data.cfg, "fudai_quiet_start_hour")), - ("结束小时", "fudai_quiet_end_hour_var", cfg_get(data.cfg, "fudai_quiet_end_hour")), + spin_spec("养号时长(秒)", "yg_recommend_duration_var", cfg_get(data.cfg, "yanghao_recommend_duration_seconds"), from_=1, to=3600, increment=5), + spin_spec("养号间隔(秒)", "yg_recommend_interval_var", cfg_get(data.cfg, "yanghao_recommend_interval_seconds"), from_=0, to=3600, increment=10), + spin_spec("爱心概率", "yg_like_prob_var", cfg_get(data.cfg, "yanghao_like_prob"), from_=0.0, to=1.0, increment=0.05), + spin_spec("收藏概率", "yg_fav_prob_var", cfg_get(data.cfg, "yanghao_fav_prob"), from_=0.0, to=1.0, increment=0.05), + spin_spec("评论概率", "yg_comment_prob_var", cfg_get(data.cfg, "yanghao_comment_prob"), from_=0.0, to=1.0, increment=0.05), + spin_spec("检测动作后停留最小(秒)", "yg_dwell_on_action_min_var", cfg_get(data.cfg, "yanghao_dwell_on_action_min_seconds"), from_=0.0, to=120.0), + spin_spec("检测动作后停留最大(秒)", "yg_dwell_on_action_max_var", cfg_get(data.cfg, "yanghao_dwell_on_action_max_seconds"), from_=0.0, to=120.0), + spin_spec("图标ROI大小(px)", "yg_icon_roi_size_var", cfg_get(data.cfg, "yanghao_icon_roi_size"), from_=80, to=600, increment=10), + spin_spec("匹配阈值", "yg_icon_match_threshold_var", cfg_get(data.cfg, "yanghao_icon_match_threshold"), from_=0.0, to=1.0, increment=0.01), + spin_spec("开始小时", "fudai_quiet_start_hour_var", cfg_get(data.cfg, "fudai_quiet_start_hour"), from_=0, to=23), + spin_spec("结束小时", "fudai_quiet_end_hour_var", cfg_get(data.cfg, "fudai_quiet_end_hour"), from_=0, to=23), ]) texts = tk.Frame(parent, bg=theme.BG) texts.pack(fill="both", expand=True) @@ -129,19 +129,19 @@ def build_logging(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> Non ("详细阶段诊断日志", "stage_diag_verbose_var", False), ]) pack_fields(box, ctx, [ - ("日志格式模式", "logging_format_mode_var", cfg_get(data.cfg, "logging", "log_format_mode")), + select_spec("日志格式模式", "logging_format_mode_var", cfg_get(data.cfg, "logging", "log_format_mode"), ["simple", "detailed"]), ("统一日志路径", "log_unified_path_var", cfg_get(data.cfg, "log_unified_path")), - ("保留天数", "logging_keep_days_var", cfg_get(data.cfg, "logging", "keep_days")), - ("总量上限(GB)", "logging_max_total_size_gb_var", cfg_get(data.cfg, "logging", "max_total_size_gb")), - ("压缩阈值(天)", "logging_compress_after_days_var", cfg_get(data.cfg, "logging", "compress_after_days")), - ("日志总大小警告阈值(MB)", "logging_monitor_warning_mb_var", cfg_get(data.cfg, "logging", "monitor_warning_mb")), - ("严重阈值(MB)", "logging_monitor_critical_mb_var", cfg_get(data.cfg, "logging", "monitor_critical_mb")), + spin_spec("保留天数", "logging_keep_days_var", cfg_get(data.cfg, "logging", "keep_days"), from_=1, to=365), + spin_spec("总量上限(GB)", "logging_max_total_size_gb_var", cfg_get(data.cfg, "logging", "max_total_size_gb"), from_=0.1, to=100.0, increment=0.1), + spin_spec("压缩阈值(天)", "logging_compress_after_days_var", cfg_get(data.cfg, "logging", "compress_after_days"), from_=0, to=365), + spin_spec("日志总大小警告阈值(MB)", "logging_monitor_warning_mb_var", cfg_get(data.cfg, "logging", "monitor_warning_mb"), from_=50, to=20000, increment=50), + spin_spec("严重阈值(MB)", "logging_monitor_critical_mb_var", cfg_get(data.cfg, "logging", "monitor_critical_mb"), from_=100, to=100000, increment=100), ("日志调试详细度", "logging_debug_level_var", cfg_get(data.cfg, "logging", "debug_detail_level")), ("错误详细度", "logging_error_level_var", cfg_get(data.cfg, "logging", "error_detail_level")), ("行为日志格式", "logging_action_format_var", cfg_get(data.cfg, "logging", "action_log_format")), - ("界面主题", "gui_theme_var", cfg_get(data.cfg, "gui", "theme")), - ("实时状态刷新(ms)", "gui_realtime_refresh_interval_ms_var", cfg_get(data.cfg, "gui", "realtime_status", "refresh_interval_ms")), - ("实时状态条数", "gui_realtime_tail_items_var", cfg_get(data.cfg, "gui", "realtime_status", "tail_items")), + select_spec("界面主题", "gui_theme_var", cfg_get(data.cfg, "gui", "theme"), ["litera", "flatly", "journal", "minty", "cosmo", "darkly"]), + spin_spec("实时状态刷新(ms)", "gui_realtime_refresh_interval_ms_var", cfg_get(data.cfg, "gui", "realtime_status", "refresh_interval_ms"), from_=200, to=10000, increment=100), + spin_spec("实时状态条数", "gui_realtime_tail_items_var", cfg_get(data.cfg, "gui", "realtime_status", "tail_items"), from_=10, to=500, increment=10), ]) row = tk.Frame(box, bg=theme.SURFACE) row.pack(fill="x", padx=20, pady=(0, 16)) diff --git a/gui/dashboard_app/page_context.py b/gui/dashboard_app/page_context.py index 3d0a837..4e30b53 100644 --- a/gui/dashboard_app/page_context.py +++ b/gui/dashboard_app/page_context.py @@ -1,7 +1,7 @@ from __future__ import annotations import tkinter as tk -from typing import Callable +from typing import Any, Callable Action = Callable[[], None] @@ -13,11 +13,13 @@ class PageContext: texts: dict[str, tk.Text], actions: dict[str, Action], status_var: tk.StringVar | None = None, + runner: Any | None = None, ) -> None: self.vars = vars_ self.texts = texts self.actions = actions self.status_var = status_var + self.runner = runner def var(self, name: str) -> tk.Variable | None: return self.vars.get(name) diff --git a/gui/dashboard_app/page_data.py b/gui/dashboard_app/page_data.py index ebc773b..7552699 100644 --- a/gui/dashboard_app/page_data.py +++ b/gui/dashboard_app/page_data.py @@ -6,13 +6,54 @@ from . import theme from .data import DashboardData, fmt_ts from .page_context import PageContext from .page_helpers import reservation_rows -from .widgets import flow_grid, preview_panel, section, stat_tile, table_panel, text_panel, toolbar +from .widgets import flow_grid, log_panel, preview_panel, section, stat_tile, table_panel, text_panel, toolbar + + +def _set_log_text(text: tk.Text, lines: list[str]) -> None: + try: + at_bottom = text.yview()[1] >= 0.98 + except Exception: + at_bottom = True + text.configure(state="normal") + text.delete("1.0", "end") + text.insert("1.0", "\n".join(lines[-1000:]) or "暂无数据") + text.configure(state="disabled") + if at_bottom: + text.yview_moveto(1.0) + + +def _runner_lines(ctx: PageContext, fallback: list[str]) -> list[str]: + runner = ctx.runner + if runner is not None: + try: + with runner._lock: + lines = list(runner._log[-1000:]) + if lines: + return lines + except Exception: + pass + return fallback def build_run_log(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: - section(parent, "运行日志", "筛选 / 搜索 / 刷新") - toolbar(parent, [("刷新", ctx.action("refresh"), "soft"), "上一个", "下一个", "清除"]) - text_panel(parent, "最近运行日志", data.run_log, height=24).pack(fill="both", expand=True) + section(parent, "运行日志", "实时输出 / 独立滚动 / 刷新") + toolbar(parent, [("刷新", ctx.action("refresh"), "soft"), ("行为日志", ctx.action("show_actions_log"), "soft"), "上一个", "下一个", "清除"]) + box, text = log_panel(parent, "运行日志", _runner_lines(ctx, data.run_log), height=24) + box.pack(fill="both", expand=True) + + def refresh_live() -> None: + try: + if not text.winfo_exists(): + return + except Exception: + return + _set_log_text(text, _runner_lines(ctx, data.run_log)) + try: + text.after(1200, refresh_live) + except Exception: + pass + + refresh_live() def build_preview(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: @@ -52,9 +93,29 @@ def build_high_value(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> def build_actions_log(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: - section(parent, "行为日志", "关键字筛选 / 搜索 / 刷新") - toolbar(parent, [("刷新", ctx.action("refresh"), "soft"), "上一个", "下一个", "清除"]) - text_panel(parent, "最近行为日志", data.action_log, height=24).pack(fill="both", expand=True) + section(parent, "行为日志", "actions.log / 独立滚动 / 刷新") + toolbar(parent, [("刷新", ctx.action("refresh"), "soft"), ("运行日志", ctx.action("show_run_log"), "soft"), "上一个", "下一个", "清除"]) + box, text = log_panel(parent, "行为日志", data.action_log, height=24) + box.pack(fill="both", expand=True) + + def refresh_file() -> None: + try: + if not text.winfo_exists(): + return + except Exception: + return + try: + from .data import load_dashboard_data + latest = load_dashboard_data().action_log + except Exception: + latest = data.action_log + _set_log_text(text, latest) + try: + text.after(2000, refresh_file) + except Exception: + pass + + refresh_file() def build_realtime(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: diff --git a/gui/dashboard_app/page_debug.py b/gui/dashboard_app/page_debug.py index f86afac..a9ca4ed 100644 --- a/gui/dashboard_app/page_debug.py +++ b/gui/dashboard_app/page_debug.py @@ -4,7 +4,7 @@ import tkinter as tk from .data import DashboardData, cfg_get from .page_context import PageContext -from .page_helpers import pack_fields, pack_switches +from .page_helpers import pack_fields, pack_switches, select_spec, spin_spec from .widgets import card, section @@ -26,8 +26,8 @@ def _debug_images(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> Non ("保存完成后记录日志(显示路径)", "debug_captures_log_on_complete_var", False), ], columns=2) pack_fields(box, ctx, [ - ("保留天数", "debug_captures_keep_days_var", cfg_get(data.cfg, "debug_captures", "keep_days")), - ("最大文件数量", "debug_captures_max_files_var", cfg_get(data.cfg, "debug_captures", "max_files")), + spin_spec("保留天数", "debug_captures_keep_days_var", cfg_get(data.cfg, "debug_captures", "keep_days"), from_=1, to=30), + spin_spec("最大文件数量", "debug_captures_max_files_var", cfg_get(data.cfg, "debug_captures", "max_files"), from_=100, to=10000, increment=100), ]) @@ -41,13 +41,13 @@ def _fudai_find(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: ("启用坐标调整(默认关闭,避免滚轮误触)", "region_coords_edit_enabled_var", False), ], columns=2) pack_fields(box, ctx, [ - ("找福袋最大尝试次数", "fudai_find_max_attempts_var", cfg_get(data.cfg, "fudai_find_max_attempts")), - ("找福袋重试间隔(秒)", "fudai_icon_retry_delay_seconds_var", cfg_get(data.cfg, "fudai_icon_retry_delay_seconds")), - ("最大等待时间(秒)", "fudai_icon_max_wait_seconds_var", cfg_get(data.cfg, "fudai_icon_max_wait_seconds")), - ("点击福袋后等待时间(秒)", "fudai_click_wait_seconds_var", cfg_get(data.cfg, "fudai_click_wait_seconds")), - ("福袋详情页UI版本", "fudai_detail_ui_mode_var", cfg_get(data.cfg, "fudai_detail_ui_mode")), - ("切换直播间后等待时间(秒)", "switch_room_wait_after_stage_diag_seconds_var", cfg_get(data.cfg, "switch_room_wait_after_stage_diag_seconds")), - ("直播间检测阈值", "live_room_match_threshold_var", cfg_get(data.cfg, "live_room_match_threshold")), + spin_spec("找福袋最大尝试次数", "fudai_find_max_attempts_var", cfg_get(data.cfg, "fudai_find_max_attempts"), from_=0, to=50), + spin_spec("找福袋重试间隔(秒)", "fudai_icon_retry_delay_seconds_var", cfg_get(data.cfg, "fudai_icon_retry_delay_seconds"), from_=0.1, to=3.0, increment=0.1), + spin_spec("最大等待时间(秒)", "fudai_icon_max_wait_seconds_var", cfg_get(data.cfg, "fudai_icon_max_wait_seconds"), from_=0.0, to=30.0, increment=0.5), + spin_spec("点击福袋后等待时间(秒)", "fudai_click_wait_seconds_var", cfg_get(data.cfg, "fudai_click_wait_seconds"), from_=0.5, to=10.0, increment=0.1), + select_spec("福袋详情页UI版本", "fudai_detail_ui_mode_var", cfg_get(data.cfg, "fudai_detail_ui_mode"), ["auto", "legacy", "v2_0"]), + spin_spec("切换直播间后等待时间(秒)", "switch_room_wait_after_stage_diag_seconds_var", cfg_get(data.cfg, "switch_room_wait_after_stage_diag_seconds"), from_=0.0, to=30.0, increment=0.5), + spin_spec("直播间检测阈值", "live_room_match_threshold_var", cfg_get(data.cfg, "live_room_match_threshold"), from_=0.0, to=1.0, increment=0.01), ]) @@ -79,16 +79,16 @@ def _ocr_and_pixel(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> No ("页面OCR", "ocr_stage_page_enabled_var", True), ]) pack_fields(box, ctx, [ - ("福袋模板匹配阈值", "fudai_template_match_threshold_var", cfg_get(data.cfg, "fudai_template_match_threshold")), - ("详情页模板匹配阈值", "fudai_detail_template_threshold_var", cfg_get(data.cfg, "fudai_detail_template_threshold")), - ("开奖页红色占比阈值", "lucky_draw_red_ratio_threshold_var", cfg_get(data.cfg, "lucky_draw_red_ratio_threshold")), - ("优惠券模板匹配阈值", "coupon_template_match_threshold_var", cfg_get(data.cfg, "coupon_template_match_threshold")), - ("参与按钮红色占比阈值", "attend_button_red_ratio_threshold_var", cfg_get(data.cfg, "attend_button_red_ratio_threshold")), - ("粉丝团加入红色占比阈值", "fansclub_join_red_ratio_threshold_var", cfg_get(data.cfg, "fansclub_join_red_ratio_threshold")), - ("购物红包背景红色占比阈值", "shopping_redpacket_red_ratio_threshold_var", cfg_get(data.cfg, "shopping_redpacket_red_ratio_threshold")), - ("详情主体蓝紫比例阈值", "fudai_detail_body_bluepurple_threshold_var", cfg_get(data.cfg, "fudai_detail_body_bluepurple_threshold")), - ("暗色比例阈值", "fudai_detail_body_dark_threshold_var", cfg_get(data.cfg, "fudai_detail_body_dark_threshold")), - ("达成形状匹配阈值", "fudai_task_status_template_threshold_var", cfg_get(data.cfg, "fudai_task_status_template_threshold")), + spin_spec("福袋模板匹配阈值", "fudai_template_match_threshold_var", cfg_get(data.cfg, "fudai_template_match_threshold"), from_=0.0, to=1.0, increment=0.01), + spin_spec("详情页模板匹配阈值", "fudai_detail_template_threshold_var", cfg_get(data.cfg, "fudai_detail_template_threshold"), from_=0.0, to=1.0, increment=0.01), + spin_spec("开奖页红色占比阈值", "lucky_draw_red_ratio_threshold_var", cfg_get(data.cfg, "lucky_draw_red_ratio_threshold"), from_=0.0, to=1.0, increment=0.01), + spin_spec("优惠券模板匹配阈值", "coupon_template_match_threshold_var", cfg_get(data.cfg, "coupon_template_match_threshold"), from_=0.0, to=1.0, increment=0.01), + spin_spec("参与按钮红色占比阈值", "attend_button_red_ratio_threshold_var", cfg_get(data.cfg, "attend_button_red_ratio_threshold"), from_=0.0, to=1.0, increment=0.01), + spin_spec("粉丝团加入红色占比阈值", "fansclub_join_red_ratio_threshold_var", cfg_get(data.cfg, "fansclub_join_red_ratio_threshold"), from_=0.0, to=1.0, increment=0.01), + spin_spec("购物红包背景红色占比阈值", "shopping_redpacket_red_ratio_threshold_var", cfg_get(data.cfg, "shopping_redpacket_red_ratio_threshold"), from_=0.0, to=1.0, increment=0.01), + spin_spec("详情主体蓝紫比例阈值", "fudai_detail_body_bluepurple_threshold_var", cfg_get(data.cfg, "fudai_detail_body_bluepurple_threshold"), from_=0.0, to=1.0, increment=0.01), + spin_spec("暗色比例阈值", "fudai_detail_body_dark_threshold_var", cfg_get(data.cfg, "fudai_detail_body_dark_threshold"), from_=0.0, to=1.0, increment=0.01), + spin_spec("达成形状匹配阈值", "fudai_task_status_template_threshold_var", cfg_get(data.cfg, "fudai_task_status_template_threshold"), from_=0.0, to=1.0, increment=0.01), ]) @@ -97,9 +97,9 @@ def _diagnostics(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None box.pack(fill="x", pady=(0, 12)) pack_switches(box, ctx, [("详细阶段诊断日志", "stage_diag_verbose_var", False)]) pack_fields(box, ctx, [ - ("滑动速度(毫秒)", "swipe_duration_ms_var", cfg_get(data.cfg, "swipe_duration_ms")), - ("上滑起始Y坐标", "swipe_up_start_y_var", cfg_get(data.cfg, "swipe_up_start_y")), - ("上滑结束Y坐标", "swipe_up_end_y_var", cfg_get(data.cfg, "swipe_up_end_y")), - ("最大诊断时间(秒)", "stage_diag_max_time_var", cfg_get(data.cfg, "stage_diag_max_time")), - ("每步最大耗时(秒)", "stage_diag_max_step_time_var", cfg_get(data.cfg, "stage_diag_max_step_time")), + spin_spec("滑动速度(毫秒)", "swipe_duration_ms_var", cfg_get(data.cfg, "swipe_duration_ms"), from_=100, to=2000, increment=50), + spin_spec("上滑起始Y坐标", "swipe_up_start_y_var", cfg_get(data.cfg, "swipe_up_start_y"), from_=400, to=2000, increment=50), + spin_spec("上滑结束Y坐标", "swipe_up_end_y_var", cfg_get(data.cfg, "swipe_up_end_y"), from_=200, to=1800, increment=50), + spin_spec("最大诊断时间(秒)", "stage_diag_max_time_var", cfg_get(data.cfg, "stage_diag_max_time"), from_=1.0, to=60.0), + spin_spec("每步最大耗时(秒)", "stage_diag_max_step_time_var", cfg_get(data.cfg, "stage_diag_max_step_time"), from_=0.5, to=30.0, increment=0.5), ]) diff --git a/gui/dashboard_app/page_helpers.py b/gui/dashboard_app/page_helpers.py index 6be12a9..2aa5efa 100644 --- a/gui/dashboard_app/page_helpers.py +++ b/gui/dashboard_app/page_helpers.py @@ -4,29 +4,80 @@ import tkinter as tk from . import theme from .page_context import PageContext -from .widgets import card, field, switch_row +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[tuple[str, str, object]], + 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, (label, var_name, fallback) in enumerate(items): - cell = field(grid, label, ctx.var(var_name), value=fallback) + 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 = 3, + columns: int = 2, ) -> None: grid = tk.Frame(parent, bg=theme.SURFACE) grid.pack(fill="x", padx=12, pady=(3, 12)) diff --git a/gui/dashboard_app/widgets.py b/gui/dashboard_app/widgets.py index 9eee796..9dc7124 100644 --- a/gui/dashboard_app/widgets.py +++ b/gui/dashboard_app/widgets.py @@ -103,6 +103,50 @@ def rounded_rect(canvas: tk.Canvas, x1: int, y1: int, x2: int, y2: int, radius: return canvas.create_polygon(points, smooth=True, splinesteps=16, **kwargs) +class RoundedSurface(tk.Frame): + def __init__( + self, + parent, + *, + fill: str = theme.SURFACE, + outline: str = theme.BORDER, + radius: int = theme.RADIUS_MD, + ) -> None: + super().__init__(parent, bg=_parent_bg(parent), highlightthickness=0, bd=0) + self._fill = fill + self._outline = outline + self._radius = radius + self._canvas = tk.Canvas(self, bg=_parent_bg(parent), highlightthickness=0, bd=0) + self._canvas.place(x=0, y=0, relwidth=1, relheight=1) + self._canvas.tk.call("lower", self._canvas._w) + self.bind("", self._draw_surface) + + def _draw_surface(self, _event=None) -> None: + width = max(2, self.winfo_width()) + height = max(2, self.winfo_height()) + self._canvas.delete("all") + rounded_rect( + self._canvas, + 1, + 1, + width - 2, + height - 2, + self._radius, + fill=self._fill, + outline=self._outline, + ) + + +def rounded_surface( + parent, + *, + fill: str = theme.SURFACE, + outline: str = theme.BORDER, + radius: int = theme.RADIUS_MD, +) -> RoundedSurface: + return RoundedSurface(parent, fill=fill, outline=outline, radius=radius) + + class RoundedButton(tk.Canvas): def __init__( self, @@ -209,7 +253,7 @@ def status_pill(parent, text: str, *, color: str = theme.GREEN, bg: str = theme. def card(parent, title: str | None = None, *, padding: int = 14) -> tk.Frame: - frame = tk.Frame(parent, bg=theme.SURFACE, highlightbackground=theme.BORDER, highlightthickness=1) + frame = rounded_surface(parent, fill=theme.SURFACE, outline=theme.BORDER, radius=theme.RADIUS_MD) if title: head = tk.Frame(frame, bg=theme.SURFACE) head.pack(fill="x", padx=padding + 2, pady=(padding, 7)) @@ -230,7 +274,7 @@ def metric(parent, title: str, value: object, hint: str = "", color: str = theme def stat_tile(parent, title: str, value: object, hint: str, *, color: str, soft: str, compact: bool = False) -> tk.Frame: - box = tk.Frame(parent, bg=theme.SURFACE, highlightbackground=theme.BORDER, highlightthickness=1) + box = rounded_surface(parent, fill=theme.SURFACE, outline=theme.BORDER, radius=theme.RADIUS_MD) if compact: box.configure(height=72) accent = tk.Frame(box, bg=color, width=4) @@ -286,6 +330,49 @@ def text_panel(parent, title: str, lines: Iterable[str], height: int = 10) -> tk return box +def log_panel(parent, title: str, lines: Iterable[str], height: int = 24) -> tuple[tk.Frame, tk.Text]: + box = card(parent, title) + wrap = tk.Frame(box, bg=theme.SURFACE_ALT) + wrap.pack(fill="both", expand=True, padx=11, pady=(4, 11)) + wrap.grid_rowconfigure(0, weight=1) + wrap.grid_columnconfigure(0, weight=1) + text = tk.Text( + wrap, + height=height, + wrap="none", + bd=0, + bg=theme.SURFACE_ALT, + fg=theme.TEXT, + insertbackground=theme.TEAL, + font=theme.FONT_MONO, + padx=14, + pady=12, + spacing1=3, + spacing3=4, + ) + yscroll = ttk.Scrollbar(wrap, orient="vertical", command=text.yview) + xscroll = ttk.Scrollbar(wrap, orient="horizontal", command=text.xview) + text.configure(yscrollcommand=yscroll.set, xscrollcommand=xscroll.set) + text.grid(row=0, column=0, sticky="nsew") + yscroll.grid(row=0, column=1, sticky="ns") + xscroll.grid(row=1, column=0, sticky="ew") + content = "\n".join(list(lines)[-1000:]) or "暂无数据" + text.insert("1.0", content) + text.configure(state="disabled") + + def _wheel(event): + text.yview_scroll(int(-1 * (event.delta / 120)), "units") + return "break" + + def _shift_wheel(event): + text.xview_scroll(int(-1 * (event.delta / 120)), "units") + return "break" + + text.bind("", _wheel) + text.bind("", _shift_wheel) + return box, text + + def table_panel(parent, title: str, columns: Sequence[tuple[str, str, int]], rows: Sequence[dict], height: int = 8) -> tk.Frame: box = card(parent, title) tree = ttk.Treeview(box, columns=[c[0] for c in columns], show="headings", height=height) @@ -316,9 +403,14 @@ def mini_table(parent, title: str, rows: Sequence[tuple[str, str, str]], *, colo return box -def field(parent, label: str, variable: tk.Variable | None = None, *, value: object = "", wide: bool = False) -> tk.Frame: - row = tk.Frame(parent, bg=theme.SURFACE_ALT, highlightbackground=theme.BORDER, highlightthickness=1) +def field_shell(parent, label: str) -> tk.Frame: + row = rounded_surface(parent, fill=theme.SURFACE_ALT, outline=theme.BORDER, radius=theme.RADIUS_SM) tk.Label(row, text=label, bg=theme.SURFACE_ALT, fg=theme.MUTED, font=theme.FONT_SMALL).pack(anchor="w", padx=9, pady=(7, 1)) + return row + + +def field(parent, label: str, variable: tk.Variable | None = None, *, value: object = "", wide: bool = False) -> tk.Frame: + row = field_shell(parent, label) if variable is None: variable = tk.StringVar(value=str(value)) entry = tk.Entry( @@ -335,8 +427,59 @@ def field(parent, label: str, variable: tk.Variable | None = None, *, value: obj return row +def select_field( + parent, + label: str, + variable: tk.Variable | None = None, + *, + value: object = "", + values: Sequence[object] = (), + width: int | None = None, +) -> tk.Frame: + row = field_shell(parent, label) + if variable is None: + variable = tk.StringVar(value=str(value)) + combo = ttk.Combobox( + row, + textvariable=variable, + values=[str(item) for item in values], + state="readonly", + width=width, + ) + combo.pack(fill="x", padx=9, pady=(0, 8)) + return row + + +def spin_field( + parent, + label: str, + variable: tk.Variable | None = None, + *, + value: object = "", + from_: float | int = 0, + to: float | int = 99999, + increment: float | int = 1, + width: int | None = None, + state: str = "normal", +) -> tk.Frame: + row = field_shell(parent, label) + if variable is None: + variable = tk.StringVar(value=str(value)) + spin = ttk.Spinbox( + row, + from_=from_, + to=to, + increment=increment, + textvariable=variable, + width=width, + state=state, + ) + spin.pack(fill="x", padx=9, pady=(0, 8)) + return row + + def switch_row(parent, label: str, variable: tk.Variable | None = None, value: bool = False) -> tk.Frame: - row = tk.Frame(parent, bg=theme.SURFACE_ALT, highlightbackground=theme.BORDER, highlightthickness=1) + row = rounded_surface(parent, fill=theme.SURFACE_ALT, outline=theme.BORDER, radius=theme.RADIUS_SM) var = variable if variable is not None else tk.BooleanVar(value=bool(value)) tk.Checkbutton( row, @@ -406,6 +549,13 @@ class ScrollFrame(ttk.Frame): def _wheel(self, event) -> None: try: + widget = event.widget + while widget is not None: + if str(widget.winfo_class()) in {"Text", "Treeview", "Listbox", "TCombobox", "TSpinbox", "Entry"}: + return + if widget is self.canvas: + break + widget = widget.master self.canvas.yview_scroll(int(-1 * (event.delta / 120)), "units") except Exception: pass