134 lines
4.2 KiB
Python
134 lines
4.2 KiB
Python
"""行为日志 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("<KeyPress>", 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,
|
|
)
|