"""mapping_table 内部辅助函数。 把 mapping_table.create_mapping_tab 中重复或自包含的子块抽出来: - 弹出条目详情窗口(在两处出现,逻辑完全一致) - 计算自适应列数与卡片宽度 - 卡片背景色批量切换 - card hover/click/dbl 事件处理器工厂 - 渲染单张卡片内的标题 + 多行 label """ from __future__ import annotations import tkinter as tk from tkinter import ttk from typing import Any, Callable, List, Optional def open_entry_detail_window( root: tk.Tk, content: str, scrolled_text_cls: Optional[type] = None, ) -> None: """打开"条目详情"弹窗。如果 ScrolledText 不可用则退化成 Label。""" if not content: return win = tk.Toplevel(root) win.title("条目详情") try: if scrolled_text_cls is not None: st = scrolled_text_cls(win, width=100, height=20) st.pack(fill=tk.BOTH, expand=True, padx=6, pady=6) st.insert("1.0", content) st.configure(state="disabled") return lbl = ttk.Label(win, text=content, justify="left") lbl.pack(fill=tk.BOTH, expand=True, padx=6, pady=6) except Exception: lbl = ttk.Label(win, text=content, justify="left") lbl.pack(fill=tk.BOTH, expand=True, padx=6, pady=6) def compute_card_layout(canvas_width: int, min_card_w: int = 300, h_gap: int = 12, max_columns: int = 3) -> tuple[int, int]: """根据画布宽度计算列数与单卡片可用宽度。 返回 (columns, card_width)。 """ try: columns = max(1, int((canvas_width + h_gap) / (min_card_w + h_gap))) except Exception: columns = 1 columns = min(columns, max_columns) try: col_total_gap = columns * h_gap col_width = max(220, int((canvas_width - col_total_gap) / columns)) except Exception: col_width = 800 return columns, col_width def set_card_bg(widget: tk.Widget, bg: str) -> None: """把 widget 自身和所有直接子 widget 的背景色改为 bg。""" try: try: widget.configure(**{"bg": bg}) except Exception: widget.configure(**{"background": bg}) except Exception: pass for ch in widget.winfo_children(): try: try: ch.configure(**{"bg": bg}) except Exception: ch.configure(**{"background": bg}) except Exception: pass def make_card_event_handlers( card_index: int, card: tk.Widget, cards: List[tk.Widget], last_selected: dict, entries_text: List[str], root: tk.Tk, normal_bg: str, hover_bg: str, selected_bg: str, scrolled_text_cls: Optional[type] = None, ) -> tuple[Callable, Callable, Callable, Callable]: """生成单张卡片的 click / dblclick / enter / leave 事件处理器。""" def on_click(_e: object | None = None) -> None: try: for i, w in enumerate(cards): bg = normal_bg if i != card_index else selected_bg set_card_bg(w, bg) last_selected["idx"] = card_index except Exception: pass def on_dbl(_e: object | None = None) -> None: try: content = entries_text[card_index] except Exception: content = "" open_entry_detail_window(root, content, scrolled_text_cls) def on_enter(_e: object | None = None) -> None: try: bg = ( selected_bg if last_selected.get("idx") == card_index else hover_bg ) set_card_bg(card, bg) except Exception: pass def on_leave(_e: object | None = None) -> None: try: bg = ( selected_bg if last_selected.get("idx") == card_index else normal_bg ) set_card_bg(card, bg) except Exception: pass return on_click, on_dbl, on_enter, on_leave def render_card_labels( card: tk.Widget, lines: List[str], title_font: Any, meta_font: Any, body_font: Any, bg: str, wraplength: int, show_title_raw: bool, ) -> None: """在单张 card 内逐行渲染 title + meta + body label。""" if not lines: return # 标题行 try: tk.Label( card, text=lines[0], font=title_font, bg=bg, fg="#0b3d91", anchor="w", justify="left", wraplength=wraplength, ).pack(fill="x", padx=10, pady=(6, 1)) except Exception: try: tk.Label(card, text=lines[0], bg=bg).pack(fill="x", padx=10, pady=(6, 1)) except Exception: pass meta_line_count = 3 if show_title_raw else 2 for li, ln in enumerate(lines[1:], start=1): try: is_meta = li in tuple(range(1, meta_line_count + 1)) font = meta_font if is_meta else body_font fg = "#555555" if is_meta else "#222222" tk.Label( card, text=ln, font=font, fg=fg, bg=bg, anchor="w", justify="left", wraplength=wraplength, ).pack(fill="x", padx=10, pady=(0, 1)) except Exception: try: tk.Label(card, text=ln, bg=bg).pack(fill="x", padx=10, pady=(0, 1)) except Exception: pass