From 5e3015f59be1885b8d4d7a8d0def97a25f4074ef Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 30 May 2026 16:36:43 +0800 Subject: [PATCH] =?UTF-8?q?refactor(gui):=20=E6=8A=BD=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E5=B7=A5=E5=85=B7=E6=A0=8F=E5=88=B0=20compon?= =?UTF-8?q?ents/log=5Fsearch=5Ftoolbar=20(1813=20=E2=86=92=201739)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gui/components/log_search_toolbar.py | 134 +++++++++++++++++++++++++++ gui/gui_app.py | 91 ++---------------- 2 files changed, 142 insertions(+), 83 deletions(-) create mode 100644 gui/components/log_search_toolbar.py diff --git a/gui/components/log_search_toolbar.py b/gui/components/log_search_toolbar.py new file mode 100644 index 0000000..385f5a5 --- /dev/null +++ b/gui/components/log_search_toolbar.py @@ -0,0 +1,134 @@ +"""运行日志 Tab 的搜索工具栏(关键字筛选 + 文本搜索)。 + +从 ``gui/gui_app.py`` 抽出。原本是 ``main()`` 内的搜索栏 widget 创建段 + +5 个共享 ``nonlocal`` matches state 的搜索闭包。 + +抽出后用一个 ``LogSearchToolbarHandles`` namedtuple 暴露 main 仍要用的引用: +- ``log_filter_var`` - 关键字筛选 StringVar(被 refresh_run_log/auto_refresh 用) +- ``refresh_run_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 Any, Callable + +from gui.components.run_log_view import refresh_run_log as _refresh_run_log_impl +from gui.components.text_search import generic_search_widget + + +@dataclass +class LogSearchToolbarHandles: + """``main()`` 后续仍要用的 handles。""" + + log_filter_var: tk.StringVar + refresh_run_log: Callable[[], None] + + +def setup_log_search_toolbar( + parent: tk.Misc, + *, + log_text: tk.Text, + runner: Any, + theme_getter: Callable[[], str], +) -> LogSearchToolbarHandles: + """构建运行日志 Tab 顶部的搜索工具栏。 + + 依赖通过显式参数注入: + - ``parent`` - 工具栏父容器(``log_box``) + - ``log_text`` - 日志文本 widget(搜索高亮 + 内容刷新的目标) + - ``runner`` - 运行控制器(暴露 ``_lock`` / ``_log`` 给刷新回调) + - ``theme_getter`` - ``() -> str`` 返回当前 GUI 主题名 + """ + log_search_frame = ttk.Frame(parent) + log_search_frame.pack(fill=X, padx=10, pady=(10, 5)) + + # 关键字筛选(与行为日志一致) + log_filter_var = tk.StringVar(value="") + ttk.Label(log_search_frame, text="筛选:").pack(side=LEFT) + ttk.Entry(log_search_frame, textvariable=log_filter_var, width=16).pack( + side=LEFT, padx=(0, 8) + ) + + log_search_var = tk.StringVar(value="") + log_search_entry = ttk.Entry( + log_search_frame, textvariable=log_search_var, width=20 + ) + + def refresh_run_log() -> None: + _refresh_run_log_impl( + runner, + text_widget=log_text, + filter_getter=lambda: log_filter_var.get().strip(), + theme_getter=theme_getter, + ) + + # 搜索状态用可变容器持有,5 个内层闭包共享。 + state = { + "matches": [], + "current_index": -1, + } + + def search_in_log_text(direction: int = 1) -> None: + """在运行日志中搜索文本(调用通用搜索实现)""" + state["current_index"] = generic_search_widget( + log_text, + log_search_var, + state["matches"], + state["current_index"], + direction, + ) + + def search_next_in_log() -> None: + """搜索下一个""" + search_in_log_text(1) + + def search_prev_in_log() -> None: + """搜索上一个""" + search_in_log_text(-1) + + def clear_search_in_log() -> None: + """清除搜索高亮""" + log_search_var.set("") + log_text.tag_remove("search_highlight", "1.0", "end") + log_text.tag_remove("search_current", "1.0", "end") + state["matches"] = [] + state["current_index"] = -1 + + # 绑定回车键搜索 + def on_log_search_entry_key(event): + if event.keysym == "Return": + search_next_in_log() + + log_search_entry.bind("", on_log_search_entry_key) + + # 将 刷新 放在筛选后面,搜索区域与行为日志一致(带分隔条) + ttk.Button(log_search_frame, text="刷新", command=refresh_run_log).pack( + side=LEFT, padx=(0, 8) + ) + ttk.Separator(log_search_frame, orient="vertical").pack( + side=LEFT, fill=Y, padx=5, pady=3 + ) + ttk.Label(log_search_frame, text="搜索:").pack(side=LEFT) + # 把搜索输入框放在分隔符后 + log_search_entry.pack(side=LEFT, padx=(0, 5)) + ttk.Button(log_search_frame, text="搜索", command=search_next_in_log).pack( + side=LEFT, padx=(0, 2) + ) + ttk.Button(log_search_frame, text="上一个", command=search_prev_in_log).pack( + side=LEFT, padx=(0, 2) + ) + ttk.Button(log_search_frame, text="下一个", command=search_next_in_log).pack( + side=LEFT, padx=(0, 2) + ) + ttk.Button(log_search_frame, text="清除", command=clear_search_in_log).pack( + side=LEFT, padx=(0, 5) + ) + + return LogSearchToolbarHandles( + log_filter_var=log_filter_var, + refresh_run_log=refresh_run_log, + ) diff --git a/gui/gui_app.py b/gui/gui_app.py index 7f79633..66434ae 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -95,6 +95,7 @@ from gui.components.run_tab import setup_run_tab from gui.components.filter_tab import setup_filter_tab 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 # 导入日志颜色配置模块 try: @@ -1193,90 +1194,14 @@ def main(): log_text.pack(fill=BOTH, expand=True, padx=10, pady=(0, 10)) # 搜索工具栏 - log_search_frame = ttk.Frame(log_box) - log_search_frame.pack(fill=X, padx=10, pady=(10, 5)) - - # 关键字筛选(与行为日志一致) - log_filter_var = tk.StringVar(value="") - ttk.Label(log_search_frame, text="筛选:").pack(side=LEFT) - ttk.Entry(log_search_frame, textvariable=log_filter_var, width=16).pack( - side=LEFT, padx=(0, 8) - ) - - log_search_var = tk.StringVar(value="") - log_search_entry = ttk.Entry( - log_search_frame, textvariable=log_search_var, width=20 - ) - - def refresh_run_log(): - _refresh_run_log_impl( - runner, - text_widget=log_text, - filter_getter=lambda: log_filter_var.get().strip(), - theme_getter=lambda: str(gui_theme_var.get() or "litera"), - ) - - log_search_matches = [] # 存储所有匹配的位置 - log_current_match_index = -1 # 当前匹配的索引 - - - def search_in_log_text(direction=1): - """在运行日志中搜索文本(调用通用搜索实现)""" - nonlocal log_search_matches, log_current_match_index - log_current_match_index = _generic_search_widget_impl( - log_text, - log_search_var, - log_search_matches, - log_current_match_index, - direction, - ) - - def search_next_in_log(): - """搜索下一个""" - search_in_log_text(1) - - def search_prev_in_log(): - """搜索上一个""" - search_in_log_text(-1) - - def clear_search_in_log(): - """清除搜索高亮""" - nonlocal log_search_matches, log_current_match_index - log_search_var.set("") - log_text.tag_remove("search_highlight", "1.0", "end") - log_text.tag_remove("search_current", "1.0", "end") - log_search_matches = [] - log_current_match_index = -1 - - # 绑定回车键搜索 - def on_log_search_entry_key(event): - if event.keysym == "Return": - search_next_in_log() - - log_search_entry.bind("", on_log_search_entry_key) - - # 将 刷新 放在筛选后面,搜索区域与行为日志一致(带分隔条) - ttk.Button(log_search_frame, text="刷新", command=refresh_run_log).pack( - side=LEFT, padx=(0, 8) - ) - ttk.Separator(log_search_frame, orient="vertical").pack( - side=LEFT, fill=Y, padx=5, pady=3 - ) - ttk.Label(log_search_frame, text="搜索:").pack(side=LEFT) - # 把搜索输入框放在分隔符后 - log_search_entry.pack(side=LEFT, padx=(0, 5)) - ttk.Button(log_search_frame, text="搜索", command=search_next_in_log).pack( - side=LEFT, padx=(0, 2) - ) - ttk.Button(log_search_frame, text="上一个", command=search_prev_in_log).pack( - side=LEFT, padx=(0, 2) - ) - ttk.Button(log_search_frame, text="下一个", command=search_next_in_log).pack( - side=LEFT, padx=(0, 2) - ) - ttk.Button(log_search_frame, text="清除", command=clear_search_in_log).pack( - side=LEFT, padx=(0, 5) + _log_search_handles = setup_log_search_toolbar( + log_box, + log_text=log_text, + runner=runner, + theme_getter=lambda: str(gui_theme_var.get() or "litera"), ) + log_filter_var = _log_search_handles.log_filter_var + refresh_run_log = _log_search_handles.refresh_run_log configure_log_text_colors(