-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsettings_window.py
More file actions
229 lines (190 loc) Β· 8.88 KB
/
settings_window.py
File metadata and controls
229 lines (190 loc) Β· 8.88 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
"""Settings window β CustomTkinter + Pandora Blackboard theme.
Allows the user to configure:
- Recording mode: hold-to-record vs toggle (press to start/stop)
- Max recording duration in seconds (toggle mode only)
"""
import tkinter as tk
import customtkinter as ctk
from PIL import ImageTk
from logger import log
import config
import database as db
import locales
from brand import make_title_bar_image
import theme as T
_WIN_W, _WIN_H = 420, 310
_TITLE_H = 40
class SettingsWindow:
def __init__(self, root: tk.Tk):
self._root = root
self._win = None
self._drag_x = 0
self._drag_y = 0
self._title_eye_tk = None
self._hold_btn = None
self._toggle_btn = None
self._slider = None
self._slider_val_label = None
self._slider_section = None
def show(self):
if self._win is not None:
try:
if self._win.winfo_exists():
self._win.attributes("-topmost", True)
self._win.lift()
self._win.focus_force()
self._win.after(100, lambda: self._win.attributes("-topmost", True)
if self._win and self._win.winfo_exists() else None)
self._sync_ui()
return
except Exception:
pass
self._build()
self._sync_ui()
# ββ Build βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _build(self):
win = ctk.CTkToplevel(self._root)
win.overrideredirect(True)
win.configure(fg_color=T.BG_DEEP)
win.attributes("-topmost", True)
sx = win.winfo_screenwidth()
sy = win.winfo_screenheight()
x = (sx - _WIN_W) // 2
y = (sy - _WIN_H) // 2
win.geometry(f"{_WIN_W}x{_WIN_H}+{x}+{y}")
self._win = win
outer = ctk.CTkFrame(win, fg_color=T.BG_DEEP, border_color=T.BORDER,
border_width=1, corner_radius=0)
outer.pack(fill="both", expand=True)
# ββ Title bar ββββββββββββββββββββββββββββββββββββββββββββββββ
title_bar = ctk.CTkFrame(outer, fg_color=T.TITLE_BG, height=_TITLE_H,
corner_radius=0)
title_bar.pack(fill="x")
title_bar.pack_propagate(False)
eye_img = make_title_bar_image(size=20)
self._title_eye_tk = ImageTk.PhotoImage(eye_img)
eye_lbl = tk.Label(title_bar, image=self._title_eye_tk, bg=T.TITLE_BG)
eye_lbl.pack(side="left", padx=(14, 8))
title_lbl = ctk.CTkLabel(title_bar, text=locales.get("settings_title"),
font=T.FONT_TITLE, text_color=T.FG)
title_lbl.pack(side="left")
close_btn = ctk.CTkButton(
title_bar, text="β", width=44, height=_TITLE_H,
fg_color="transparent", hover_color=T.CLOSE_HOVER,
text_color=T.FG_DIM, font=(T.FONT_FAMILY, 15),
corner_radius=0, command=self._close,
)
close_btn.pack(side="right")
for w in (title_bar, title_lbl):
w.bind("<Button-1>", self._start_drag)
w.bind("<B1-Motion>", self._on_drag)
# ββ Content ββββββββββββββββββββββββββββββββββββββββββββββββββ
content = ctk.CTkFrame(outer, fg_color=T.BG, corner_radius=0)
content.pack(fill="both", expand=True, padx=1, pady=(0, 1))
pad = ctk.CTkFrame(content, fg_color="transparent")
pad.pack(fill="both", expand=True, padx=T.PAD_XL, pady=T.PAD_L)
# Recording mode label
ctk.CTkLabel(pad, text=locales.get("setting_record_mode"),
font=T.FONT_TITLE, text_color=T.FG,
anchor="w").pack(fill="x", pady=(0, T.PAD_M))
# Mode buttons
btn_row = ctk.CTkFrame(pad, fg_color="transparent")
btn_row.pack(fill="x", pady=(0, T.PAD_L))
self._hold_btn = ctk.CTkButton(
btn_row, text=locales.get("setting_hold"),
font=T.FONT_SMALL, height=36, corner_radius=6,
fg_color=T.BG_CARD, hover_color=T.BG_HOVER,
border_color=T.BORDER, border_width=1,
text_color=T.FG, command=lambda: self._set_mode(True),
)
self._hold_btn.pack(side="left", padx=(0, T.PAD_M))
self._toggle_btn = ctk.CTkButton(
btn_row, text=locales.get("setting_toggle"),
font=T.FONT_SMALL, height=36, corner_radius=6,
fg_color=T.BG_CARD, hover_color=T.BG_HOVER,
border_color=T.BORDER, border_width=1,
text_color=T.FG, command=lambda: self._set_mode(False),
)
self._toggle_btn.pack(side="left")
# Separator
ctk.CTkFrame(pad, fg_color=T.BORDER, height=1,
corner_radius=0).pack(fill="x", pady=(0, T.PAD_L))
# Max duration section (toggle mode only)
self._slider_section = ctk.CTkFrame(pad, fg_color="transparent")
self._slider_section.pack(fill="x")
lbl_row = ctk.CTkFrame(self._slider_section, fg_color="transparent")
lbl_row.pack(fill="x", pady=(0, T.PAD_M))
ctk.CTkLabel(lbl_row, text=locales.get("setting_max_duration"),
font=T.FONT_TITLE, text_color=T.FG,
anchor="w").pack(side="left")
self._slider_val_label = ctk.CTkLabel(
lbl_row, text="120s", font=T.FONT_TITLE,
text_color=T.ACCENT, anchor="e",
)
self._slider_val_label.pack(side="right")
self._slider = ctk.CTkSlider(
self._slider_section, from_=30, to=300,
fg_color=T.BG_INPUT, progress_color=T.ACCENT,
button_color=T.ACCENT, button_hover_color=T.ACCENT_HOVER,
height=18, corner_radius=9,
command=self._on_slider_change,
)
self._slider.pack(fill="x")
# ββ Drag ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _start_drag(self, event):
self._drag_x = event.x
self._drag_y = event.y
def _on_drag(self, event):
if self._win:
x = self._win.winfo_x() + (event.x - self._drag_x)
y = self._win.winfo_y() + (event.y - self._drag_y)
self._win.geometry(f"+{x}+{y}")
def _close(self):
if self._win:
try:
self._win.destroy()
except Exception:
pass
self._win = None
# ββ UI sync βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _sync_ui(self):
hold = getattr(config, "HOLD_TO_RECORD", True)
self._update_mode_buttons(hold)
max_sec = getattr(config, "MAX_RECORD_SECONDS", 120)
if self._slider:
self._slider.set(max_sec)
if self._slider_val_label:
self._slider_val_label.configure(text=f"{max_sec}s")
self._update_slider_visibility(hold)
def _update_mode_buttons(self, hold: bool):
if self._hold_btn:
self._hold_btn.configure(
fg_color=T.FG if hold else T.BG_CARD,
text_color=T.BG_DEEP if hold else T.FG,
border_color=T.FG if hold else T.BORDER,
)
if self._toggle_btn:
self._toggle_btn.configure(
fg_color=T.FG if not hold else T.BG_CARD,
text_color=T.BG_DEEP if not hold else T.FG,
border_color=T.FG if not hold else T.BORDER,
)
def _update_slider_visibility(self, hold: bool):
if self._slider_section:
if hold:
self._slider_section.pack_forget()
else:
self._slider_section.pack(fill="x")
# ββ Callbacks βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _set_mode(self, hold: bool):
config.HOLD_TO_RECORD = hold
db.save_setting("hold_to_record", "1" if hold else "0")
self._update_mode_buttons(hold)
self._update_slider_visibility(hold)
log.info("Recording mode set to %s", "hold" if hold else "toggle")
def _on_slider_change(self, value):
seconds = int(float(value))
config.MAX_RECORD_SECONDS = seconds
db.save_setting("max_record_seconds", str(seconds))
if self._slider_val_label:
self._slider_val_label.configure(text=f"{seconds}s")