diff --git a/gui/components/actions_log_toolbar.py b/gui/components/actions_log_toolbar.py new file mode 100644 index 0000000..6fefc37 --- /dev/null +++ b/gui/components/actions_log_toolbar.py @@ -0,0 +1,133 @@ +"""行为日志 Tab 的工具栏(关键字筛选 + 文本搜索)。 + +从 ``gui/gui_app.py`` 抽出。原本是 ``main()`` 内的搜索栏 widget 创建段 + +配色配置 + 4 个共享 ``nonlocal`` matches state 的搜索闭包。 + +抽出后用一个 ``ActionsLogToolbarHandles`` 暴露 ``main()`` 仍要用的引用: +- ``refresh_actions_log`` - 刷新回调(被 root.after 调度用) +""" + +from __future__ import annotations + +import tkinter as tk +from dataclasses import dataclass +from tkinter import ttk +from tkinter.constants import LEFT, X, Y +from typing import Callable + +from gui.components.actions_log_view import ( + configure_actions_log_colors as _configure_actions_log_colors_impl, + refresh_actions_log as _refresh_actions_log_impl, +) +from gui.components.text_search import generic_search_widget + + +@dataclass +class ActionsLogToolbarHandles: + """``main()`` 后续仍要用的 handles。""" + + refresh_actions_log: Callable[[], None] + + +def setup_actions_log_toolbar( + parent: tk.Misc, + *, + actions_log_text: tk.Text, + theme_getter: Callable[[], str], +) -> ActionsLogToolbarHandles: + """构建行为日志 Tab 顶部的工具栏(配色 + 关键字筛选 + 文本搜索)。 + + 依赖通过显式参数注入: + - ``parent`` - 工具栏父容器(``actions_log_top``) + - ``actions_log_text`` - 行为日志文本 widget + - ``theme_getter`` - ``() -> str`` 返回当前 GUI 主题名 + """ + # 配置行为日志颜色标签(使用统一的颜色配置模块) + def configure_actions_log_colors() -> None: + _configure_actions_log_colors_impl( + actions_log_text, + theme_getter=theme_getter, + ) + + # 初始配置 + configure_actions_log_colors() + + actions_filter_var = tk.StringVar(value="") + + def refresh_actions_log() -> None: + _refresh_actions_log_impl( + actions_log_text, + theme_getter=theme_getter, + filter_getter=lambda: actions_filter_var.get().strip(), + ) + + ttk.Label(parent, text="关键字筛选:").pack(side=LEFT) + ttk.Entry(parent, textvariable=actions_filter_var, width=16).pack( + side=LEFT, padx=(0, 8) + ) + ttk.Button(parent, text="刷新", command=refresh_actions_log).pack( + side=LEFT, padx=(0, 10) + ) + + # 行为日志搜索功能 + ttk.Separator(parent, orient="vertical").pack( + side=LEFT, fill=Y, padx=5, pady=3 + ) + ttk.Label(parent, text="搜索:").pack(side=LEFT) + + actions_search_var = tk.StringVar(value="") + actions_search_entry = ttk.Entry( + parent, textvariable=actions_search_var, width=20 + ) + actions_search_entry.pack(side=LEFT, padx=(0, 5)) + + state = { + "matches": [], + "current_index": -1, + } + + def search_in_actions_log(direction: int = 1) -> None: + """在行为日志中搜索文本(使用通用搜索实现)""" + state["current_index"] = generic_search_widget( + actions_log_text, + actions_search_var, + state["matches"], + state["current_index"], + direction, + ) + + def search_next_in_actions() -> None: + search_in_actions_log(1) + + def search_prev_in_actions() -> None: + search_in_actions_log(-1) + + def clear_search_in_actions() -> None: + actions_search_var.set("") + actions_log_text.tag_remove("search_highlight", "1.0", "end") + actions_log_text.tag_remove("search_current", "1.0", "end") + state["matches"] = [] + state["current_index"] = -1 + + def on_actions_search_entry_key(event): + if event.keysym == "Return": + search_next_in_actions() + + actions_search_entry.bind("", on_actions_search_entry_key) + + ttk.Button(parent, text="搜索", command=search_next_in_actions).pack( + side=LEFT, padx=(0, 2) + ) + ttk.Button(parent, text="上一个", command=search_prev_in_actions).pack( + side=LEFT, padx=(0, 2) + ) + ttk.Button(parent, text="下一个", command=search_next_in_actions).pack( + side=LEFT, padx=(0, 2) + ) + ttk.Button(parent, text="清除", command=clear_search_in_actions).pack( + side=LEFT + ) + + return ActionsLogToolbarHandles( + refresh_actions_log=refresh_actions_log, + ) diff --git a/gui/gui_app.py b/gui/gui_app.py index cc7ce04..99ea7a5 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -97,6 +97,7 @@ from gui.components.realtime_tab import setup_realtime_tab from gui.components.log_text_colors import configure_log_text_colors from gui.components.log_search_toolbar import setup_log_search_toolbar from gui.components.win_record_tab import setup_win_record_tab +from gui.components.actions_log_toolbar import setup_actions_log_toolbar # 导入日志颜色配置模块 try: @@ -1473,91 +1474,12 @@ def main(): except Exception: pass - # 配置行为日志颜色标签(使用统一的颜色配置模块) - def configure_actions_log_colors(): - _configure_actions_log_colors_impl( - actions_log_text, - theme_getter=lambda: str(gui_theme_var.get() or "litera"), - ) - - # 初始配置 - configure_actions_log_colors() - - actions_filter_var = tk.StringVar(value="") - - def refresh_actions_log(): - _refresh_actions_log_impl( - actions_log_text, - theme_getter=lambda: str(gui_theme_var.get() or "litera"), - filter_getter=lambda: actions_filter_var.get().strip(), - ) - - ttk.Label(actions_log_top, text="关键字筛选:").pack(side=LEFT) - ttk.Entry(actions_log_top, textvariable=actions_filter_var, width=16).pack( - side=LEFT, padx=(0, 8) - ) - ttk.Button(actions_log_top, text="刷新", command=refresh_actions_log).pack( - side=LEFT, padx=(0, 10) - ) - - # 行为日志搜索功能 - ttk.Separator(actions_log_top, orient="vertical").pack( - side=LEFT, fill=Y, padx=5, pady=3 - ) - ttk.Label(actions_log_top, text="搜索:").pack(side=LEFT) - - actions_search_var = tk.StringVar(value="") - actions_search_entry = ttk.Entry( - actions_log_top, textvariable=actions_search_var, width=20 - ) - actions_search_entry.pack(side=LEFT, padx=(0, 5)) - - actions_search_matches = [] - actions_current_match_index = -1 - - def search_in_actions_log(direction=1): - """在行为日志中搜索文本(使用通用搜索实现)""" - nonlocal actions_search_matches, actions_current_match_index - actions_current_match_index = _generic_search_widget_impl( - actions_log_text, - actions_search_var, - actions_search_matches, - actions_current_match_index, - direction, - ) - - def search_next_in_actions(): - search_in_actions_log(1) - - def search_prev_in_actions(): - search_in_actions_log(-1) - - def clear_search_in_actions(): - nonlocal actions_search_matches, actions_current_match_index - actions_search_var.set("") - actions_log_text.tag_remove("search_highlight", "1.0", "end") - actions_log_text.tag_remove("search_current", "1.0", "end") - actions_search_matches = [] - actions_current_match_index = -1 - - def on_actions_search_entry_key(event): - if event.keysym == "Return": - search_next_in_actions() - - actions_search_entry.bind("", on_actions_search_entry_key) - - ttk.Button(actions_log_top, text="搜索", command=search_next_in_actions).pack( - side=LEFT, padx=(0, 2) - ) - ttk.Button(actions_log_top, text="上一个", command=search_prev_in_actions).pack( - side=LEFT, padx=(0, 2) - ) - ttk.Button(actions_log_top, text="下一个", command=search_next_in_actions).pack( - side=LEFT, padx=(0, 2) - ) - ttk.Button(actions_log_top, text="清除", command=clear_search_in_actions).pack( - side=LEFT + _actions_log_handles = setup_actions_log_toolbar( + actions_log_top, + actions_log_text=actions_log_text, + theme_getter=lambda: str(gui_theme_var.get() or "litera"), ) + refresh_actions_log = _actions_log_handles.refresh_actions_log # OCR结果显示 ocr_results_top = ttk.Frame(ocr_results_tab)