-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathvideo.py
457 lines (386 loc) · 18 KB
/
video.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import tkinter
import webbrowser
import customtkinter as ctk
from typing import List, Union, Tuple
from tkinter import PhotoImage
import pyperclip
from utils import (
ValueConvertUtility,
)
from widgets.components.thumbnail_button import (
ThumbnailButton,
)
from services import (
ThemeManager, LanguageManager
)
from settings import (
AppearanceSettings
)
class Video(ctk.CTkFrame):
"""A class representing a video widget."""
default_thumbnails: Tuple[tkinter.PhotoImage, tkinter.PhotoImage] = (None, None)
def __init__(
self,
root: ctk.CTk,
master: Union[ctk.CTkFrame, ctk.CTkScrollableFrame],
width: int = 0,
height: int = 0,
# video info
video_url: str = "",
video_title: str = "-------",
channel: str = "-------",
thumbnails: List[PhotoImage] = (None, None),
notification_thumbnail_image_path: str = "",
original_thumbnail_image_path: str = "",
# Hsitory thumbnail images
history_normal_thumbnail_image_path: str = "",
history_hover_thumbnail_image_path: str = "",
channel_url: str = "-------",
length: int = 0):
super().__init__(
master=master,
height=height,
width=width,
border_width=1,
corner_radius=8,
)
self.root = root
self.master_frame = master
self.height: int = height
self.width: int = width
# video details
self.video_url: str = video_url
self.video_title: str = video_title
self.channel: str = channel
self.channel_url: str = channel_url
self.length: int = length
self.thumbnails: List[PhotoImage] = thumbnails
self.original_thumbnail_image_path: str = original_thumbnail_image_path
# widgets
self.info_frame: Union[ctk.CTkFrame, None] = None
self.url_label: Union[ctk.CTkLabel, None] = None
self.video_title_label: Union[ctk.CTkLabel, None] = None
self.channel_btn: Union[ctk.CTkButton, None] = None
self.video_length_label: Union[ctk.CTkLabel, None] = None
self.thumbnail_btn: Union[ThumbnailButton, None] = None
self.remove_btn: Union[ctk.CTkButton, None] = None
self.notification_thumbnail_image_path: str = notification_thumbnail_image_path # for notification
self.history_normal_thumbnail_image_path: str = history_normal_thumbnail_image_path # for history
self.history_hover_thumbnail_image_path: str = history_hover_thumbnail_image_path # for history
from widgets import ContextMenu
self.context_menu: Union['ContextMenu', None] = None
# initialize the object
self.create_widgets()
self.set_widgets_texts()
self.set_widgets_sizes()
self.set_widgets_fonts()
self.set_widgets_colors()
self.set_tk_widgets_colors()
self.set_widgets_accent_color()
self.place_widgets()
self.bind_widgets_events()
# register to Theme Manger for accent color updates & widgets colors updates
ThemeManager.register_widget(self)
LanguageManager.register_widget(self)
def set_video_data(self):
"""Display video data on widgets."""
self.video_title_label.configure(text=f"{LanguageManager.data['title']} : {self.video_title}")
self.channel_btn.configure(text=f"{LanguageManager.data['channel']} : {self.channel}", state="normal")
self.url_label.configure(text=self.video_url)
self.video_length_label.configure(text=ValueConvertUtility.convert_time(self.length))
self.thumbnail_btn.stop_loading_animation()
self.thumbnail_btn.configure_thumbnail(thumbnails=self.thumbnails)
self.thumbnail_btn.configure(state="normal")
def on_mouse_enter_thumbnail_btn(event):
# self.on_mouse_enter_self(event)
self.thumbnail_btn.on_mouse_enter(event)
def on_mouse_leave_thumbnail_btn(event):
self.on_mouse_leave_self(event)
self.thumbnail_btn.on_mouse_leave(event)
self.thumbnail_btn.bind("<Enter>", on_mouse_enter_thumbnail_btn)
self.thumbnail_btn.bind("<Leave>", on_mouse_leave_thumbnail_btn)
self.video_length_label.bind("<Enter>", on_mouse_enter_thumbnail_btn)
self.video_length_label.bind("<Leave>", on_mouse_leave_thumbnail_btn)
def copy_url(self):
pyperclip.copy(self.video_url)
self.close_context_menu_directly("event")
def open_in_web_browser(self):
webbrowser.open(self.video_url)
self.close_context_menu_directly("event")
def remove(self):
self.close_context_menu_directly("event")
self.kill()
def create_widgets(self):
"""Create widgets."""
from widgets import ContextMenu
self.thumbnail_btn = ThumbnailButton(
master=self,
state="disabled",
command=lambda: webbrowser.open(self.video_url),
)
self.video_length_label = ctk.CTkLabel(master=self, text=ValueConvertUtility.convert_time(self.length))
self.info_frame = ctk.CTkFrame(master=self)
self.video_title_label = ctk.CTkLabel(master=self.info_frame, anchor="w")
self.channel_btn = ctk.CTkButton(
master=self.info_frame,
anchor="w",
command=lambda: webbrowser.open(self.channel_url),
state="disabled",
hover=False
)
self.url_label = ctk.CTkLabel(master=self.info_frame, anchor="w", text=self.video_url)
self.remove_btn = ctk.CTkButton(master=self, command=self.kill, text="X", hover=False)
self.context_menu = ContextMenu(
master=self.root,
options_texts=["copy_url", "open_in_browser", "remove"],
options_commands=[
self.copy_url,
self.open_in_web_browser,
self.remove
]
)
def set_widgets_texts(self):
self.video_title_label.configure(
text=f"{LanguageManager.data['title']} : {self.video_title}"
)
self.channel_btn.configure(
text=f"{LanguageManager.data['channel']} : {self.channel}"
)
def update_widgets_text(self):
self.set_widgets_texts()
def set_widgets_fonts(self):
"""Set fonts for widgets."""
scale = AppearanceSettings.settings["scale_r"]
self.thumbnail_btn.configure(font=("arial", int(14 * scale), "bold"))
self.video_length_label.configure(font=("arial", int(11 * scale), "bold"))
self.video_title_label.configure(font=('arial', int(13 * scale), 'bold'))
self.channel_btn.configure(font=('arial', int(13 * scale), 'bold'))
font_style = ctk.CTkFont(family="arial", size=int(13 * scale), slant="italic", underline=True)
self.url_label.configure(font=font_style)
self.remove_btn.configure(font=("arial", 12 * scale, "bold"))
self.context_menu.configure(font=("Segoe UI", 12 * scale, "bold"))
def set_widgets_sizes(self):
"""Set sizes for widgets."""
scale = AppearanceSettings.settings["scale_r"]
self.video_length_label.configure(height=1, width=1)
self.info_frame.configure(height=self.height-3)
label_height = int((self.height - 2) / 3)
self.video_title_label.configure(height=label_height, width=2)
self.channel_btn.configure(height=label_height, width=2)
self.url_label.configure(height=label_height, width=2)
self.remove_btn.configure(width=22 * scale, height=22 * scale, border_spacing=0)
self.context_menu.configure(
width=int(130 * AppearanceSettings.settings["scale_r"]),
height=int(80 * AppearanceSettings.settings["scale_r"]),
)
def set_widgets_accent_color(self):
"""Set accent color for widgets."""
self.configure(border_color=AppearanceSettings.settings["root"]["accent_color"]["normal"])
self.thumbnail_btn.configure(
fg=(AppearanceSettings.settings["root"]["accent_color"]["normal"]),
)
self.url_label.configure(text_color=AppearanceSettings.settings["root"]["accent_color"]["normal"])
def update_widgets_accent_color(self):
"""Update accent color for widgets."""
self.set_widgets_accent_color()
def set_tk_widgets_colors(self):
"""Set colors for the Tk widgets."""
self.thumbnail_btn.configure(
bg=ThemeManager.get_color_based_on_theme_mode(
AppearanceSettings.settings["video_object"]["fg_color"]["normal"]
),
disabledforeground=ThemeManager.get_color_based_on_theme_mode(
AppearanceSettings.settings["video_object"]["text_color"]["normal"]
),
activebackground=ThemeManager.get_color_based_on_theme_mode(
AppearanceSettings.settings["video_object"]["fg_color"]["normal"]
)
)
def update_widgets_colors(self):
"""Update colors for widgets."""
self.set_tk_widgets_colors()
def set_widgets_colors(self):
"""Set colors for widgets."""
self.configure(fg_color=AppearanceSettings.settings["video_object"]["fg_color"]["normal"])
self.info_frame.configure(
fg_color=AppearanceSettings.settings["video_object"]["fg_color"]["normal"]
)
self.video_length_label.configure(
text_color=AppearanceSettings.settings["video_object"]["text_color"]["normal"]
)
self.video_title_label.configure(
text_color=AppearanceSettings.settings["video_object"]["text_color"]["normal"]
)
self.channel_btn.configure(
text_color=AppearanceSettings.settings["video_object"]["btn_text_color"]["normal"],
fg_color=AppearanceSettings.settings["video_object"]["fg_color"]["normal"]
)
self.remove_btn.configure(
fg_color=AppearanceSettings.settings["video_object"]["error_color"]["normal"],
text_color=AppearanceSettings.settings["video_object"]["remove_btn_text_color"]["normal"]
)
def on_mouse_enter_self(self, event):
"""Handle mouse enter event for self."""
# Disable due to UI Performance issue
"""
self.configure(
fg_color=AppearanceSettings.settings["video_object"]["fg_color"]["hover"],
border_color=AppearanceSettings.settings["root"]["accent_color"]["hover"]
)
self.thumbnail_btn.configure(
bg=ThemeManager.get_color_based_on_theme_mode(
AppearanceSettings.settings["video_object"]["fg_color"]["hover"]
)
)
self.info_frame.configure(
fg_color=AppearanceSettings.settings["video_object"]["fg_color"]["hover"],
)
self.channel_btn.configure(
fg_color=AppearanceSettings.settings["video_object"]["fg_color"]["hover"]
)
"""
def on_mouse_leave_self(self, event):
"""Handle mouse leave event for self."""
# Disable due to UI Performance issue
"""
self.configure(
fg_color=AppearanceSettings.settings["video_object"]["fg_color"]["normal"],
border_color=AppearanceSettings.settings["root"]["accent_color"]["normal"]
)
self.thumbnail_btn.configure(
bg=ThemeManager.get_color_based_on_theme_mode(
AppearanceSettings.settings["video_object"]["fg_color"]["normal"]
)
)
self.info_frame.configure(
fg_color=AppearanceSettings.settings["video_object"]["fg_color"]["normal"],
)
self.channel_btn.configure(
fg_color=AppearanceSettings.settings["video_object"]["fg_color"]["normal"]
)
"""
def open_context_menu(self, _event):
from widgets import ContextMenu
x = self.root.winfo_pointerx() - self.root.winfo_rootx()
y = self.root.winfo_pointery() - self.root.winfo_rooty()
ContextMenu.close_all_menus()
self.context_menu.set_open()
self.context_menu.place(x=x-10, y=y-10)
def close_context_menu_directly(self, _event):
self.context_menu.set_closed()
self.context_menu.place_forget()
def close_context_menu(self, _event):
x = self.root.winfo_pointerx() - self.root.winfo_rootx()
y = self.root.winfo_pointery() - self.root.winfo_rooty()
if (x <= self.context_menu.winfo_x() + 10 or
x >= (self.context_menu.winfo_x() + self.context_menu.winfo_width() - 10) or
y <= self.context_menu.winfo_y() + 10 or
y >= (self.context_menu.winfo_y() + self.context_menu.winfo_height() - 10)):
self.close_context_menu_directly("event")
def bind_widgets_events(self):
"""Bind events for widgets."""
self.bind("<Configure>", self.configure_widget_sizes)
self.thumbnail_btn.bind("<Button-3>", self.open_context_menu)
self.thumbnail_btn.bind("<Button-2>", self.open_context_menu)
self.thumbnail_btn.bind("<Button-1>", self.close_context_menu_directly)
self.context_menu.bind_widgets_events("<Leave>", self.close_context_menu)
""" Disable Due to UI Performance
self.bind("<Enter>", self.on_mouse_enter_self)
self.bind("<Leave>", self.on_mouse_leave_self)
for child_widgets in self.winfo_children():
child_widgets.bind("<Enter>", self.on_mouse_enter_self)
child_widgets.bind("<Leave>", self.on_mouse_leave_self)
try:
sub_child_widget = None
for sub_child_widget in child_widgets.winfo_children():
sub_child_widget.bind("<Enter>", self.on_mouse_enter_self)
sub_child_widget.bind("<Leave>", self.on_mouse_leave_self)
try:
for sub_sub_child_widget in sub_child_widget.winfo_children():
sub_sub_child_widget.bind("<Enter>", self.on_mouse_enter_self)
sub_sub_child_widget.bind("<Leave>", self.on_mouse_leave_self)
except Exception as error:
print(f"Video.py : {error}")
except Exception as error:
print(f"Video.py : {error}")
"""
def on_mouse_enter_channel_btn(_event):
self.channel_btn.configure(
text_color=AppearanceSettings.settings["video_object"]["btn_text_color"]["hover"]
)
# self.on_mouse_enter_self(event)
def on_mouse_leave_channel_btn(_event):
self.channel_btn.configure(
text_color=AppearanceSettings.settings["video_object"]["btn_text_color"]["normal"]
)
self.channel_btn.bind("<Enter>", on_mouse_enter_channel_btn)
self.channel_btn.bind("<Leave>", on_mouse_leave_channel_btn)
def on_mouse_enter_remove_btn(_event):
self.remove_btn.configure(
fg_color=AppearanceSettings.settings["video_object"]["error_color"]["hover"],
text_color=AppearanceSettings.settings["video_object"]["remove_btn_text_color"]["hover"]
)
# self.on_mouse_enter_self(event)
def on_mouse_leave_remove_btn(event):
self.remove_btn.configure(
fg_color=AppearanceSettings.settings["video_object"]["error_color"]["normal"],
text_color=AppearanceSettings.settings["video_object"]["remove_btn_text_color"]["normal"]
)
self.on_mouse_leave_self(event)
self.remove_btn.bind("<Enter>", on_mouse_enter_remove_btn)
self.remove_btn.bind("<Leave>", on_mouse_leave_remove_btn)
def place_widgets(self):
"""Place widgets."""
scale = AppearanceSettings.settings["scale_r"]
thumbnail_width = int((self.height - 4) / 9 * 16)
self.thumbnail_btn.place(x=5, y=1, width=thumbnail_width, height=self.height - 4)
self.video_length_label.place(rely=1, x=thumbnail_width + 5, anchor="se", y=-2)
self.info_frame.place(x=thumbnail_width + 10 * scale, y=1)
self.video_title_label.place(x=0, rely=0.2, anchor="w")
self.channel_btn.place(x=0, rely=0.5, anchor="w")
self.url_label.place(x=0, rely=0.8, anchor="w")
self.remove_btn.place(relx=1, x=-25 * scale, y=3 * scale)
def configure_widget_sizes(self, _event):
...
def __del__(self):
"""Clear the Memory."""
self.root = None
self.master_frame = None
del self.master_frame
del self.root
del self.height
del self.width
# video details
del self.video_url
del self.video_title
del self.channel
del self.channel_url
del self.length
del self.thumbnails
# widgets
del self.info_frame
del self.url_label
del self.video_title_label
del self.channel_btn
del self.video_length_label
del self.remove_btn
del self.context_menu
del self.thumbnail_btn
del self
def destroy_widgets(self):
"""Destroy the child widget."""
self.video_length_label.destroy()
self.info_frame.destroy()
self.video_title_label.destroy()
self.channel_btn.destroy()
self.url_label.destroy()
self.remove_btn.destroy()
self.destroy()
super().destroy()
def kill(self):
"""Destroy the widget."""
ThemeManager.unregister_widget(self)
LanguageManager.unregister_widget(self)
self.pack_forget()
self.destroy_widgets()
self.__del__()