-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelp_window.py.bak
More file actions
215 lines (179 loc) · 6.59 KB
/
help_window.py.bak
File metadata and controls
215 lines (179 loc) · 6.59 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
from __future__ import annotations
from pathlib import Path
from collections.abc import Callable
import tkinter as tk
from tkinter import ttk, messagebox
from tkinter.scrolledtext import ScrolledText
from workbench_core import resolve_tool_path
_APP_NAME = "YT Audio Workbench"
_VERSION = "0.0"
def set_app_meta(app_name: str, version: str) -> None:
"""Called by the main app to set metadata used in dialogs."""
global _APP_NAME, _VERSION
_APP_NAME = app_name or _APP_NAME
_VERSION = version or _VERSION
def _center_on_screen(win: tk.Toplevel) -> None:
win.update_idletasks()
w = win.winfo_width()
h = win.winfo_height()
sw = win.winfo_screenwidth()
sh = win.winfo_screenheight()
x = (sw // 2) - (w // 2)
y = (sh // 3) - (h // 2)
win.geometry(f"+{x}+{y}")
def _tool_info(cmd: str, version_args: list[str]) -> list[str]:
out: list[str] = []
path = resolve_tool_path(cmd)
if not path:
out.append(f"{cmd}: not found")
return out
out.append(f"{cmd}: {path}")
try:
import subprocess # nosec B404: importing subprocess is intentional; we use shell=False
p = subprocess.run(
[path, *version_args],
capture_output=True,
text=True,
check=False,
)
first = (
p.stdout.splitlines()[0]
if p.stdout.strip()
else (p.stderr.splitlines()[0] if p.stderr.strip() else "(no output)")
)
out.append(f"{cmd} version: {first}")
except Exception as e: # pragma: no cover - purely diagnostic
out.append(f"{cmd} version: error: {e}")
return out
def _copy_diagnostics(parent: tk.Misc, get_text: Callable[[str, str], str]) -> None:
lines: list[str] = [
f"{_APP_NAME} v{_VERSION}",
f"Tk version: {tk.TkVersion}",
]
for tool, args in [
("yt-dlp", ["--version"]),
("ffmpeg", ["-version"]),
("ffprobe", ["-version"]),
("mp3gain", ["-v"]),
]:
lines.extend(_tool_info(tool, args))
try:
parent.clipboard_clear()
parent.clipboard_append("\n".join(lines))
except Exception:
pass
messagebox.showinfo(
get_text("dialog.copied.title", "Diagnostics copied"),
get_text("dialog.copied.body", "Diagnostic info copied to clipboard."),
parent=parent,
)
def open_help_window(
parent: tk.Misc,
help_path: Path,
get_text: Callable[[str, str], str],
section: str | None = None,
) -> None:
"""Open a help window rendering the HELP.md text; simple ToC based on headings."""
top = tk.Toplevel(parent)
top.title(get_text("dialog.help.title", f"Help — {_APP_NAME}").format(app=_APP_NAME))
top.geometry("940x640")
top.transient(parent)
container = ttk.Frame(top, padding=6)
container.pack(fill="both", expand=True)
searchf = ttk.Frame(container)
searchf.pack(fill="x")
ttk.Label(searchf, text=get_text("dialog.help.search", "Search:")).pack(side="left")
qvar = tk.StringVar()
q = ttk.Entry(searchf, textvariable=qvar, width=50)
q.pack(side="left", padx=6)
body = ttk.Frame(container)
body.pack(fill="both", expand=True)
toc = tk.Listbox(body, width=28)
toc.pack(side="left", fill="y", padx=(0, 8))
txt = ScrolledText(body, wrap="word")
txt.pack(side="right", fill="both", expand=True)
def do_search(*_args: object) -> None:
term = qvar.get().strip().lower()
if not term:
return
content = txt.get("1.0", "end-1c").lower()
pos = content.find(term)
if pos >= 0:
index = txt.index(f"1.0+{pos}c")
line = index.split(".")[0]
txt.see(f"{line}.0")
txt.tag_remove("sel", "1.0", "end")
txt.tag_add("sel", f"{line}.0", f"{line}.0 lineend")
q.bind("<Return>", do_search)
try:
raw = help_path.read_text(encoding="utf-8", errors="ignore")
except Exception:
raw = "# Help\n\n" + get_text("dialog.help.not_found", "Help file not found.")
txt.insert("end", raw)
txt.config(state="disabled")
_center_on_screen(top)
lines = raw.splitlines()
anchors: list[tuple[str, int, int]] = []
for i, line in enumerate(lines, start=1):
if line.startswith("#"):
depth = len(line) - len(line.lstrip("#"))
title = line.lstrip("#").strip()
anchors.append((title, i, depth))
toc.insert("end", (" " * (depth - 1)) + title)
def jump(_evt: tk.Event | None = None, target: str | None = None) -> None:
lineno: int | None
if target is None:
sel = toc.curselection()
if not sel:
return
idx = sel[0]
_title, lineno, _depth = anchors[idx]
else:
lineno = None
for title, ln, _depth in anchors:
if title.lower().startswith(target.lower()):
lineno = ln
break
if lineno is None:
return
txt.see(f"{lineno}.0")
txt.tag_remove("sel", "1.0", "end")
txt.tag_add("sel", f"{lineno}.0", f"{lineno}.0 lineend")
toc.bind("<<ListboxSelect>>", jump)
if section:
jump(target=section)
# Diagnostics button in the search row (right side)
ttk.Button(
searchf,
text=get_text("dialog.help.copy_diagnostics", "Copy diagnostics"),
command=lambda: _copy_diagnostics(parent, get_text),
).pack(side="right")
top.grab_set()
top.wait_window()
def show_about_dialog(parent: tk.Misc, help_path: Path, get_text: Callable[[str, str], str]) -> None:
top = tk.Toplevel(parent)
top.title(get_text("dialog.about.title", "About"))
top.resizable(False, False)
frm = ttk.Frame(top, padding=12)
frm.pack(fill="both", expand=True)
ttk.Label(frm, text=_APP_NAME, font=("TkDefaultFont", 12, "bold")).pack(anchor="w")
ttk.Label(
frm,
text=get_text("dialog.about.version", "Version: {version}").format(version=_VERSION),
).pack(anchor="w", pady=(0, 8))
ttk.Button(
frm,
text=get_text("dialog.about.view_help", "View Help"),
command=lambda: open_help_window(parent, help_path, get_text),
).pack(anchor="w")
ttk.Button(
frm,
text=get_text("dialog.about.copy_diagnostics", "Copy diagnostics"),
command=lambda: _copy_diagnostics(parent, get_text),
).pack(anchor="w", pady=(8, 0))
ttk.Button(frm, text=get_text("dialog.about.close", "Close"), command=top.destroy).pack(
anchor="e", pady=(8, 0)
)
_center_on_screen(top)
top.grab_set()
top.wait_window()