-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_ui.py
293 lines (223 loc) · 11.8 KB
/
custom_ui.py
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
import tkinter as tk, pywinstyles, winaccent, sys, hPyT, util, threading
from tkinter import ttk
entry_select = winaccent.accent_normal
def update_colors():
global light_theme, bg, bg_hover, bg_press, fg, entry_focus, entry_bd, entry_bg, button_bg, button_hover, button_press, button_bd, button_bd_active, tooltip_bg, tooltip_bd, tooltip_fg, accent, accent_link, fg_desc
light_theme = winaccent.apps_use_light_theme if util.theme == "default" else True if util.theme == "light" else False
if light_theme:
bg = "#f0f0f0"
bg_hover = "#e0e0e0"
bg_press = "#cecece"
fg = "#000000"
fg_desc = "#2c2c2c"
entry_focus = winaccent.accent_dark
entry_bd = "#8d8d8d"
entry_bg = "#ffffff"
button_bg = "#ffffff"
button_hover = "#ebebeb"
button_press = "#dbdbdb"
button_bd = "#d0d0d0"
button_bd_active = winaccent.accent_dark
tooltip_bg = "#ffffff"
tooltip_bd = "#8f8f8f"
tooltip_fg = "#505050"
accent = winaccent.accent_dark
accent_link = winaccent.accent_dark_2
else:
bg = "#202020"
bg_hover = "#333333"
bg_press = "#292929"
fg = "#ffffff"
fg_desc = "#dadada"
entry_focus = "#ffffff"
entry_bd = "#6e6e6e"
entry_bg = "#404040"
button_bg = "#333333"
button_hover = "#454545"
button_press = "#676767"
button_bd = "#9b9b9b"
button_bd_active = "#ffffff"
tooltip_bg = "#2b2b2b"
tooltip_bd = "#747474"
tooltip_fg = "#ffffff"
accent = winaccent.accent_light
accent_link = winaccent.accent_light_3
update_colors()
class CommandLink(tk.Frame):
def __init__(self, master, title: str = "", description: str = "", command: callable = None, *args, **kwargs):
super().__init__(master, padx = 8, pady = 8, background = bg, *args, **kwargs)
ver = sys.getwindowsversion()
if ver.major == 10 and ver.build >= 22000:
self.arrow = ttk.Label(self, text = "\ue651 ", font = ("Segoe Fluent Icons", 13), padding = (0, 4, 0, 0), foreground = accent_link)
else:
self.arrow = ttk.Label(self, text = "\ue0ad ", font = ("Segoe MDL2 Assets", 13), padding = (0, 4, 0, 0), foreground = accent_link)
self.arrow.pack(side = "left", anchor = "w")
self.text = tk.Frame(self, background = bg)
self.text.pack(fill = "x")
self.title_w = ttk.Label(self.text, text = title, font = ("Segoe UI Semibold", 13), foreground = accent_link)
self.title_w.pack(anchor = "w", fill = "x")
self.description_w = ttk.Label(self.text, text = description, foreground = fg_desc)
self.description_w.pack(anchor = "w", fill = "x")
is_touched = False
def on_enter(event):
global is_touched
is_touched = True
self.configure(background = bg_hover)
self.arrow.configure(background = bg_hover, foreground = accent_link)
self.title_w.configure(background = bg_hover, foreground = accent_link)
self.description_w.configure(background = bg_hover)
self.text.configure(background = bg_hover)
def on_leave(event):
global is_touched
is_touched = False
self.configure(background = bg)
self.arrow.configure(background = bg, foreground = accent_link)
self.title_w.configure(background = bg, foreground = accent_link)
self.description_w.configure(background = bg)
self.text.configure(background = bg)
def on_click(event):
global is_touched
is_touched = True
self.configure(background = bg_press)
self.arrow.configure(background = bg_press, foreground = accent)
self.title_w.configure(background = bg_press, foreground = accent)
self.description_w.configure(background = bg_press)
self.text.configure(background = bg_press)
def on_click_release(event):
global is_touched
self.configure(background = bg_hover)
self.arrow.configure(background = bg_hover, foreground = accent_link)
self.title_w.configure(background = bg_hover, foreground = accent_link)
self.description_w.configure(background = bg_hover)
self.text.configure(background = bg_hover)
if not command is None and is_touched: command(); is_touched = False
self.bind("<Enter>", on_enter)
self.bind("<Leave>", on_leave)
self.bind("<Button-1>", on_click)
self.bind("<ButtonRelease-1>", on_click_release)
self.arrow.bind("<Enter>", on_enter)
self.arrow.bind("<Leave>", on_leave)
self.arrow.bind("<Button-1>", on_click)
self.arrow.bind("<ButtonRelease-1>", on_click_release)
self.title_w.bind("<Enter>", on_enter)
self.title_w.bind("<Leave>", on_leave)
self.title_w.bind("<Button-1>", on_click)
self.title_w.bind("<ButtonRelease-1>", on_click_release)
self.description_w.bind("<Enter>", on_enter)
self.description_w.bind("<Leave>", on_leave)
self.description_w.bind("<Button-1>", on_click)
self.description_w.bind("<ButtonRelease-1>", on_click_release)
def update_colors(self):
self["background"] = bg
self.arrow["background"] = bg
self.title_w["background"] = bg
self.text["background"] = bg
self.description_w["background"] = bg
self.arrow["foreground"] = accent_link
self.title_w["foreground"] = accent_link
self.description_w["foreground"] = fg_desc
class Toolbutton(tk.Button):
def __init__(self, master, text: str = "", command: callable = None, link: bool = False, icononly: bool = False, *args, **kwargs):
super().__init__(master, text = text, command = command, padx = 2 if icononly else 4, pady = 2, background = bg,
foreground = accent_link if link else fg, border = 0, relief = "solid",
activebackground = bg_press, activeforeground = accent if link else fg,
cursor = "hand2" if link and not icononly else "", *args, **kwargs)
self.link = link
if icononly: self.configure(width = 2)
self.bind("<Enter>", lambda event: self.configure(background = bg_hover))
self.bind("<Leave>", lambda event: self.configure(background = bg))
def update_colors(self):
self.configure(background = bg, foreground = accent_link if self.link else fg, activebackground = bg_press, activeforeground = accent if self.link else fg)
class Button(tk.Button):
def __init__(self, master, text: str = "", command: callable = None, *args, **kwargs):
super().__init__(master, text = text, command = command, padx = 4, pady = 3, background = button_bg,
foreground = fg, border = 0, relief = "solid", activebackground = button_press,
activeforeground = fg, highlightthickness = 1, highlightbackground = button_bd,
highlightcolor = button_bd, *args, **kwargs)
if self["width"] == 0:
if len(self["text"]) >= 10: self.configure(width = len(self["text"]))
else: self.configure(width = 10)
if self["default"] == "active":
self.configure(highlightbackground = button_bd_active, highlightcolor = button_bd_active)
self.is_active = True
else:
self.configure(default = "active")
self.is_active = False
self.bind("<Enter>", lambda event: self.configure(background = button_hover))
self.bind("<Leave>", lambda event: self.configure(background = button_bg))
def update_colors(self):
self.configure(background = button_bg, foreground = fg, activebackground = button_press,
activeforeground = fg, highlightbackground = button_bd_active if self.is_active else button_bd,
highlightcolor = button_bd_active if self.is_active else button_bd)
ttk.Button = Button
class OptionMenu(tk.OptionMenu):
def __init__(self, master, variable, value, *values):
super().__init__(master, variable, value, *values)
if light_theme: self.arrow = tk.PhotoImage(file = f"{util.internal}icons/dropdown_light.png")
else: self.arrow = tk.PhotoImage(file = f"{util.internal}icons/dropdown_dark.png")
self.configure(background = button_bg, foreground = fg, activebackground = button_hover,
activeforeground = fg, highlightbackground = button_bd, highlightcolor = button_bd,
image = self.arrow, compound = "right", indicatoron = False, border = 0, relief = "solid",
highlightthickness = 1, pady = 4, padx = 7)
self["menu"].configure(activebackground = winaccent.accent_normal)
def update_colors(self):
if light_theme: self.arrow = tk.PhotoImage(file = f"{util.internal}icons/dropdown_light.png")
else: self.arrow = tk.PhotoImage(file = f"{util.internal}icons/dropdown_dark.png")
self.configure(background = button_bg, foreground = fg, activebackground = button_hover,
activeforeground = fg, highlightbackground = button_bd, highlightcolor = button_bd,
image = self.arrow)
self["menu"].configure(activebackground = winaccent.accent_normal)
class App(tk.Tk):
def set_theme(self):
pywinstyles.apply_style(self, "light" if light_theme else "dark")
pywinstyles.change_header_color(self, bg)
style = ttk.Style()
style.configure(".", background = bg, foreground = fg)
self.configure(background = bg)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.withdraw()
self.iconbitmap(default = util.internal + "icon.ico")
self.update()
self.set_theme()
def resizable(self, width: bool = None, height: bool = None):
value = super().resizable(width, height)
self.set_theme()
return value
def mainloop(self, n = 0):
self.deiconify()
super().mainloop(n)
class Toplevel(tk.Toplevel):
def set_titlebar_theme(self):
self.update()
self.configure(background = bg)
pywinstyles.apply_style(self, "light" if light_theme else "dark")
pywinstyles.change_header_color(self, bg)
hPyT.maximize_minimize_button.hide(self)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.withdraw()
self.grab_set()
self.focus_set()
self.bind("<Escape>", lambda event: self.destroy())
self.set_titlebar_theme()
def resizable(self, width: bool = None, height: bool = None):
value = super().resizable(width, height)
self.set_titlebar_theme()
self.deiconify()
return value
def sync_colors(window):
update_colors()
if isinstance(window, App): window.set_theme()
elif isinstance(window, Toplevel): window.set_titlebar_theme()
for widget in window.winfo_children():
if isinstance(widget, (CommandLink, Toolbutton, Button, OptionMenu)):
widget.update_colors()
elif isinstance(widget, tk.Entry):
widget.configure(background = entry_bg, foreground = fg, highlightcolor = entry_bg, highlightbackground = entry_bg, insertbackground = fg, selectbackground = entry_select)
widget.master.configure(highlightbackground = entry_bd, highlightcolor = entry_focus)
elif isinstance(widget, tk.Canvas):
widget.configure(background = bg)
elif isinstance(widget, (Toplevel, ttk.Frame, tk.Frame)):
sync_colors(widget)
def sync_colors_with_system(window): threading.Thread(target = lambda: winaccent.on_appearance_changed(lambda: sync_colors(window)), daemon = True).start()