refactor(gui): 抽日志搜索工具栏到 components/log_search_toolbar (1813 → 1739)
This commit is contained in:
1 parent
dc1744a93f
commit
5e3015f59b
2 files changed
+142
-83
No files matched your search
@@ -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("<KeyPress>", 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,
|
||||
)
|
||||
+8
-83
@@ -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("<KeyPress>", 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(
|
||||
|
||||
Reference in new issue
Block a user