-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpreview.py
More file actions
315 lines (273 loc) · 10.7 KB
/
Copy pathpreview.py
File metadata and controls
315 lines (273 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# -*- coding: utf-8 -*-
from __future__ import annotations
import json
import os
import tkinter as tk
from pathlib import Path
from tkinter import messagebox, ttk
from PIL import Image, ImageTk
class ChatPreview(tk.Toplevel):
PAGE_SIZE = 100
def __init__(self, parent, json_path):
super().__init__(parent)
self.title("聊天预览")
self.geometry("920x720")
self.minsize(720, 520)
self.json_path = Path(json_path)
self.chat_dir = self.json_path.parent
with self.json_path.open("r", encoding="utf-8-sig") as f:
data = json.load(f)
self.chat_name = data.get("chat_name") or self.chat_dir.name
self.messages = data.get("messages") or []
self.start_index = max(0, len(self.messages) - self.PAGE_SIZE)
# Thumbnail cache avoids decoding the same image again when loading older pages.
self.image_cache = {}
self.embedded_widgets = []
self.separator_widgets = []
self.grid_rowconfigure(1, weight=1)
self.grid_columnconfigure(0, weight=1)
top = ttk.Frame(self, padding=(14, 12, 14, 8))
top.grid(row=0, column=0, sticky="ew")
top.grid_columnconfigure(1, weight=1)
ttk.Label(
top,
text=self.chat_name,
font=("Microsoft YaHei UI", 14, "bold"),
).grid(row=0, column=0, sticky="w")
controls = ttk.Frame(top)
controls.grid(row=0, column=1, sticky="e")
self.load_btn = ttk.Button(
controls,
text="加载更早消息",
command=self._load_older,
)
self.load_btn.pack(side="left", padx=(0, 12))
self.count_label = ttk.Label(controls, text="")
self.count_label.pack(side="left")
body = ttk.Frame(self)
body.grid(row=1, column=0, sticky="nsew")
body.grid_rowconfigure(0, weight=1)
body.grid_columnconfigure(0, weight=1)
# Use a native Text widget instead of hundreds of nested Frames on a Canvas.
# It scrolls much more smoothly for long chats and avoids transparent ttk
# areas exposing a black Canvas background on some Windows themes.
bg = self.cget("background")
self.text = tk.Text(
body,
wrap="word",
undo=False,
borderwidth=0,
highlightthickness=0,
relief="flat",
background=bg,
foreground="black",
insertbackground="black",
padx=22,
pady=12,
spacing1=0,
spacing2=0,
spacing3=0,
cursor="arrow",
)
self.scrollbar = ttk.Scrollbar(body, orient="vertical", command=self.text.yview)
self.text.configure(yscrollcommand=self.scrollbar.set)
self.text.grid(row=0, column=0, sticky="nsew")
self.scrollbar.grid(row=0, column=1, sticky="ns")
self.text.bind("<Configure>", self._resize_separators, add="+")
self._configure_tags()
self.protocol("WM_DELETE_WINDOW", self._close)
self._render()
self.after(80, lambda: self.text.yview_moveto(1.0))
def _configure_tags(self):
self.text.tag_configure(
"date",
font=("Microsoft YaHei UI", 10, "bold"),
justify="center",
spacing1=14,
spacing3=8,
)
self.text.tag_configure(
"sender",
font=("Microsoft YaHei UI", 10, "bold"),
spacing1=10,
spacing3=3,
)
self.text.tag_configure(
"content",
font=("Microsoft YaHei UI", 10),
lmargin1=0,
lmargin2=0,
spacing3=4,
)
self.text.tag_configure(
"transcript",
font=("Microsoft YaHei UI", 9),
foreground="#555555",
lmargin1=16,
lmargin2=16,
spacing3=4,
)
def _close(self):
self.image_cache.clear()
self.embedded_widgets.clear()
self.destroy()
def _set_editable(self, editable: bool):
self.text.configure(state="normal" if editable else "disabled")
def _render(self):
# Rebuilding a Text document is cheap even for hundreds/thousands of
# normal text messages, unlike rebuilding thousands of nested widgets.
self._set_editable(True)
self.text.delete("1.0", "end")
self.embedded_widgets.clear()
self.separator_widgets.clear()
shown = len(self.messages) - self.start_index
self.count_label.configure(text=f"显示 {shown}/{len(self.messages)} 条")
remaining = self.start_index
if remaining > 0:
n = min(self.PAGE_SIZE, remaining)
self.load_btn.configure(
state="normal",
text=f"加载更早 {n} 条消息",
)
else:
self.load_btn.configure(
state="disabled",
text="已加载全部消息",
)
last_date = None
for msg in self.messages[self.start_index:]:
time_text = str(msg.get("time") or "")
date = time_text[:10] if len(time_text) >= 10 else ""
if date and date != last_date:
if self.text.index("end-1c") != "1.0":
self.text.insert("end", "\n")
self.text.insert("end", f"{date}\n", "date")
last_date = date
self._render_message(msg)
self._set_editable(False)
def _load_older(self):
if self.start_index <= 0:
return
self.start_index = max(0, self.start_index - self.PAGE_SIZE)
self._render()
# New page starts at the top so the newly loaded older messages are visible.
self.after_idle(lambda: self.text.yview_moveto(0.0))
def _render_message(self, msg):
time_text = str(msg.get("time") or "")
time_only = time_text[11:19] if len(time_text) >= 19 else time_text
sender = msg.get("sender") or "未知"
self.text.insert("end", f"{time_only} | {sender}\n", "sender")
content = str(msg.get("content") or "")
if content and content not in {"[图片]", "[语音]", "[视频]"}:
self.text.insert("end", content.rstrip() + "\n", "content")
media = msg.get("media") or {}
if media.get("available") and media.get("path"):
full = self.chat_dir / Path(media["path"])
kind = media.get("kind")
if kind == "image":
self._render_image(full)
elif kind == "file":
label = media.get("name") or full.name
self._insert_button(
f"打开文件:{label}",
lambda p=full: self._open_path(p),
)
elif kind == "voice":
label = "播放语音" if full.suffix.lower() == ".wav" else "打开语音文件"
self._insert_button(
label,
lambda p=full: self._open_path(p),
)
elif kind == "video":
self._insert_button(
"播放视频",
lambda p=full: self._open_path(p),
)
transcript = msg.get("transcript") or media.get("transcript")
if transcript:
source = msg.get("transcript_source") or media.get("transcript_source")
label = "语音转文字(本地识别)" if source == "local_asr" else "语音转文字"
self.text.insert(
"end",
f"{label}:{transcript}\n",
"transcript",
)
self._insert_separator()
def _insert_separator(self):
# One lightweight 1 px line per message. This keeps the original
# "solid line + breathing room" look without rebuilding each message
# as a nested Frame/Label tree.
self.text.insert("end", "\n")
line = tk.Frame(
self.text,
height=1,
borderwidth=0,
highlightthickness=0,
background="#B8B8B8",
)
self.separator_widgets.append(line)
self.embedded_widgets.append(line)
self.text.window_create("end", window=line, padx=0, pady=0)
self.text.insert("end", "\n\n")
self.after_idle(self._resize_separators)
def _resize_separators(self, _event=None):
if not self.separator_widgets:
return
width = max(120, self.text.winfo_width() - 48)
for line in self.separator_widgets:
try:
if line.winfo_exists():
line.configure(width=width)
except tk.TclError:
pass
def _insert_button(self, text, command):
button = ttk.Button(self.text, text=text, command=command)
self.embedded_widgets.append(button)
self.text.window_create("end", window=button, padx=0, pady=4)
self.text.insert("end", "\n")
def _render_image(self, path: Path):
if not path.exists():
self.text.insert("end", "[图片文件不存在]\n", "content")
return
key = str(path.resolve()).lower()
photo = self.image_cache.get(key)
if photo is None:
try:
with Image.open(path) as im:
image = im.convert("RGB")
image.thumbnail((600, 420), Image.Resampling.LANCZOS)
photo = ImageTk.PhotoImage(image)
self.image_cache[key] = photo
except Exception:
self._insert_button(
"打开图片",
lambda p=path: self._open_path(p),
)
return
# Only image messages create a small widget; normal text messages no longer do.
label = tk.Label(
self.text,
image=photo,
borderwidth=0,
highlightthickness=0,
cursor="hand2",
background=self.text.cget("background"),
)
label.bind("<Button-1>", lambda _e, p=path: self._open_path(p))
self.embedded_widgets.append(label)
self.text.window_create("end", window=label, padx=0, pady=5)
self.text.insert("end", "\n")
def _open_path(self, path: Path):
if not path.exists():
messagebox.showwarning("聊天预览", f"文件不存在:\n{path}", parent=self)
return
try:
os.startfile(str(path))
except Exception as exc:
messagebox.showerror("聊天预览", f"无法打开:\n{exc}", parent=self)
def open_chat_preview(parent, json_path):
path = Path(json_path)
if not path.exists():
messagebox.showwarning("聊天预览", "没有找到导出的 JSON。", parent=parent)
return None
return ChatPreview(parent, path)