135 lines
4.4 KiB
Python
135 lines
4.4 KiB
Python
"""运行日志 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,
|
|
)
|