-
Notifications
You must be signed in to change notification settings - Fork 0
/
MusicPlayer.py
944 lines (867 loc) · 34 KB
/
MusicPlayer.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
print("Music Player")
import sys
import mili
import time
import pygame
import pathlib
import faulthandler
pygame.mixer.pre_init(buffer=2048)
from ui.common import *
import moviepy.editor as moviepy
from ui.history import HistoryUI
from ui.settings import SettingsUI
from ui.list_viewer import ListViewerUI
from ui.edit_keybinds import EditKeybindsUI
from health_check import main as health_check
from ui.music_controls import MusicControlsUI
from ui.playlist_viewer import PlaylistViewerUI
from ui.discord_presence import DiscordPresence
from ui.music_fullscreen import MusicFullscreenUI
from ui.data import (
HistoryData,
MusicData,
Playlist,
NotCached,
PlaylistGroup,
)
try:
faulthandler.enable()
except RuntimeError:
...
if "win" in sys.platform or os.name == "nt":
import ctypes
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(
"damusss.mili_musicplayer.1.0"
)
class MusicPlayerApp(mili.GenericApp):
def __init__(self):
self.init_pygame()
self.init_attributes()
self.init_data_folder_check()
self.init_load_settings()
self.init_loading_screen()
self.init_load_icons()
self.init_load_data()
self.init_mili_settings()
self.init_sld2()
self.init_try_set_icon_mac()
self.make_bg_image()
health_check()
def init_pygame(self):
pygame.mixer.init()
pygame.font.init()
super().__init__(
pygame.Window(
"Music Player",
PREFERRED_SIZES,
resizable=True,
borderless=True,
)
)
self.window.minimum_size = WIN_MIN_SIZE
pygame.key.set_repeat(500, 30)
print(f"MILI {mili.VERSION_STR}")
if mili.VERSION < (1, 0, 0) or pygame.vernum < (2, 5, 1):
pygame.display.message_box(
"Outdated dependencies",
"The core dependencies of the music player are outdated, please update them to the latest version. "
f"pygame-ce: needed >=2.5.1, found {pygame.ver}. MILI: needed >=1.0.0, found {mili.VERSION_STR}. "
"The application will now quit.",
"error",
None,
("Understood",),
)
pygame.quit()
sys.exit()
def init_attributes(self):
# components
self.playlist_viewer = PlaylistViewerUI(self)
self.list_viewer = ListViewerUI(self)
self.music_controls = MusicControlsUI(self)
self.settings = SettingsUI(self)
self.history = HistoryUI(self)
self.discord_presence = DiscordPresence(self)
self.keybinds = Keybinds(self)
self.edit_keybinds = EditKeybindsUI(self)
self.music_fullscreen = MusicFullscreenUI(self)
self.prefabs = UIComponent(self)
# settings
self.user_framerate = 60
self.volume = 1
self.loops = True
self.shuffle = False
self.custom_title = True
self.before_maximize_data = None
self.maximized = False
self.strip_youtube_id = False
self.taskbar_height = 0
# status
self.start_style = mili.PADLESS | {"spacing": 0}
self.start_time = time.time()
self.vol_before_mute = 1
self.view_state = "list"
self.modal_state = "none"
self.playlists: list[Playlist] = []
self.history_data: list[HistoryData] = []
self.focused = True
self.ui_mult = 1
self.input_stolen = False
self.listening_key = False
self.last_save = SAVE_COOLDOWN
self.stolen_cursor = False
self.cursor_hover = False
self.tooltip_hover_time = 0
self.tooltip_data = None
# be effect/mili
self.bg_effect_image = None
self.bg_black_image = None
self.bg_effect = False
self.bg_cache = mili.ImageCache()
self.anims = [animation(-3) for i in range(4)]
self.anim_settings = animation(-5)
# menu
self.menu_open = False
self.menu_data: Playlist | MusicData | PlaylistGroup = None
self.menu_buttons = None
self.menu_pos = None
# music
self.music: MusicData = None
self.music_paused = False
self.music_index = -1
self.music_play_time = 0
self.music_play_offset = 0
self.music_loops = False
self.music_videoclip = None
self.music_start_time = None
# custom title
self.tbarh = 0
self.custom_borders = mili.CustomWindowBorders(
self.window,
RESIZE_SIZE,
RESIZE_SIZE * 2,
30,
True,
RATIO_MIN,
on_resize=lambda: (self.make_bg_image(), self.on_resize_move()),
on_move=self.on_resize_move,
)
def init_load_icons(self):
self.close_image = load_icon("close")
self.playlistadd_image = load_icon("playlist_add")
self.music_cover_image = load_icon("music")
self.playlist_cover = load_icon("playlist")
self.settings_image = load_icon("settings")
self.loading_image = load_icon("loading")
self.confirm_image = load_icon("confirm")
self.back_image = load_icon("back")
self.delete_image = load_icon("delete")
self.rename_image = load_icon("edit")
self.loopon_image = load_icon("loopon")
self.loopoff_image = load_icon("loopoff")
self.minimize_image = load_icon("minimize")
self.maximize_image = load_icon("maximize")
self.resize_image = load_icon("resize")
self.reset_image = load_icon("reset")
self.playbars_image = load_icon("playbars")
self.window.set_icon(self.playlist_cover)
def init_load_data(self):
for name in ["mp3_converted", "covers", "music_covers"]:
if not os.path.exists(f"data/{name}"):
os.mkdir(f"data/{name}")
playlist_data = load_json("data/playlists.json", [])
write_json("data/playlists_backup.json", playlist_data)
history_data = load_json("data/history.json", [])
for pdata in playlist_data:
name = pdata["name"]
paths = [
pathlib.Path(path)
if isinstance(path, str)
else [pathlib.Path(path[0]), path[1]]
for path in pdata["paths"]
]
self.playlists.append(
Playlist(name, paths, pdata.get("groups", []), self.loading_image)
)
for hdata in history_data:
obj = HistoryData.load_from_data(hdata, self)
if obj is not None:
self.history_data.append(obj)
def init_sld2(self):
try:
import sdl2
self.sdl2 = sdl2
except (ModuleNotFoundError, ImportError):
self.sdl2 = None
print(
"\nWARNING: PySDL2 not installed. Miniplayer hover feature is disabled"
)
def init_mili_settings(self):
self.mili.default_styles(
text={
"sysfont": False,
"name": "data/ytfont.ttf",
"growx": True,
"growy": True,
},
line={"color": (255,) * 3},
circle={"antialias": True},
image={"smoothscale": True},
)
minimum_caches = (
(
sum(
[
len(playlist.musiclist) + len(playlist.groups)
for playlist in self.playlists
]
)
+ len(self.playlists)
)
* 3
) + 100
mili.ImageCache.preallocate_caches(max(1000, minimum_caches))
def init_load_settings(self):
custom_title = True
win_pos = self.window.position
win_size = self.window.size
discord_presence = False
default_binds = self.keybinds.get_save_data()
minip_data = [MINIP_PREFERRED_SIZES, None, True]
data = load_json(
"data/settings.json",
{
"volume": 1,
"loops": True,
"shuffle": False,
"fps": 60,
"custom_title": True,
"win_pos": win_pos,
"win_size": win_size,
"before_maximize_data": None,
"maximized": False,
"discord_presence": discord_presence,
"strip_youtube_id": False,
"taskbar_height": 0,
"miniplayer": minip_data,
"keybinds": default_binds,
},
)
if isinstance(data, dict):
self.volume = data.get("volume", 1)
self.loops = data.get("loops", True)
self.shuffle = data.get("shuffle", False)
self.user_framerate = data.get("fps", 60)
custom_title = data.get("custom_title", True)
win_pos = data.get("win_pos", win_pos)
win_size = data.get("win_size", win_size)
discord_presence = data.get("discord_presence", False)
self.keybinds.load_from_data(data.get("keybinds", default_binds))
self.maximized = data.get("maximized", False)
self.before_maximize_data = data.get("before_maximize_data", None)
self.strip_youtube_id = data.get("strip_youtube_id", False)
self.taskbar_height = data.get("taskbar_height", 0)
minip = self.music_controls.minip
minip.last_size, minip.last_pos, minip.last_borderless = data.get(
"miniplayer", minip_data
)
self.target_framerate = self.user_framerate
if win_pos != self.window.position:
self.window.position = win_pos
if win_size != self.window.size:
self.window.size = win_size
if not custom_title:
self.toggle_custom_title()
if discord_presence:
self.discord_presence.start()
def init_data_folder_check(self):
if not os.path.exists("data"):
pygame.display.message_box(
"Data folder not found",
"Data folder not found. The folder is required to load icons and to store user data. "
"If you moved the application, remember to move the data folder aswell. The application will now quit.",
"error",
None,
("Understood",),
)
pygame.quit()
raise SystemExit
def init_loading_screen(self):
screen = self.window.get_surface()
screen.fill((BG_CV,) * 3)
txt = pygame.font.Font("data/ytfont.ttf", 30).render(
"Loading music and covers...",
True,
"white",
wraplength=int(screen.width / 1.2),
)
screen.blit(txt, txt.get_rect(center=(screen.width / 2, screen.height / 2)))
self.window.flip()
def init_try_set_icon_mac(self):
if not (os.name == "posix" and sys.platform == "darwin"):
return
try:
from AppKit import NSApplication, NSImage
from Foundation import NSURL
app = NSApplication.sharedApplication()
icon_image = NSImage.alloc().initByReferencingFile_(
"data/icons/playlist.png"
)
app.setApplicationIconImage_(icon_image)
except ImportError:
if not os.path.exists("ignore-pyobjc-dep.txt"):
btn = pygame.display.message_box(
"Could not set taskbar icon",
"The module 'pyobjc' is required to set the taskbar icon on MacOS. Please make sure the module is "
"installed the next time you run the application. Create a file named 'ignore-pyobjc-dep.txt' "
"to suppress this warning.",
"warn",
None,
("Understood", "Create ignore file"),
)
if btn == 1:
with open("ignore-pyobjc-dep.txt", "w") as file:
file.write("")
except Exception:
pass
def toggle_custom_title(self):
if self.custom_title:
self.custom_title = False
self.window.borderless = False
self.window.resizable = True
else:
self.custom_title = True
self.window.borderless = True
self.window.resizable = True
def get_music_pos(self):
return (
self.music_play_offset
+ (pygame.time.get_ticks() - self.music_play_time) / 1000
)
def set_music_pos(self, pos):
if (
self.music is None
or not self.music.pos_supported
or self.music.duration in [None, NotCached]
):
return
self.music_play_time = pygame.time.get_ticks()
self.music_play_offset = pos
pygame.mixer.music.set_pos(pos)
def add_to_history(self):
pos = self.get_music_pos()
data = HistoryData(self.music, pos, self.music.duration)
for olddata in self.history_data.copy():
if olddata.music is self.music:
self.history_data.remove(olddata)
self.history_data.append(data)
if len(self.history_data) > HISTORY_LEN:
self.history_data.pop(0)
def remove_from_history(self, music: MusicData):
for olddata in self.history_data.copy():
if olddata.music is music:
self.history_data.remove(olddata)
def play_music(self, music: MusicData, idx):
if music.pending:
self.end_music()
return
if self.music is not None:
self.add_to_history()
if not os.path.exists(music.audiopath):
music.playlist.remove(music.audiopath)
pygame.display.message_box(
"Failed playing music",
"The request music was renamed or deleted externally, therefore the path was removed from the playlist.",
"error",
None,
("Understood",),
)
return
self.music = music
self.music_paused = False
self.music_index = idx
self.music_start_time = time.time()
self.music_play_offset = 0
if self.music.duration is NotCached:
self.music.cache_duration()
self.music_videoclip = None
if self.music.isvideo:
try:
self.music_videoclip = moviepy.VideoFileClip(str(self.music.realpath))
except Exception:
pass
self.music_controls.offset = 0
self.music_controls.offset_restart_time = pygame.time.get_ticks()
self.music_controls.music_videoclip_cover = None
self.music_controls.last_videoclip_cover = None
pygame.mixer.music.load(self.music.audiopath)
pygame.mixer.music.play(0)
pygame.mixer.music.set_endevent(MUSIC_ENDEVENT)
pygame.mixer.music.set_volume(self.volume)
self.music_play_time = pygame.time.get_ticks()
self.discord_presence.update()
def end_music(self):
self.close_menu()
if self.modal_state == "fullscreen":
self.modal_state = "none"
if self.music is not None:
self.add_to_history()
self.music = None
self.music_paused = False
self.bg_effect = False
pygame.mixer.music.stop()
pygame.mixer.music.unload()
if self.music_controls.minip.window is not None:
self.music_controls.minip.close()
if self.music_videoclip is not None:
self.music_videoclip.close()
self.music_videoclip = None
self.discord_presence.update()
def save(self):
if self.music is not None:
self.add_to_history()
playlist_data = [
{
"name": p.name,
"paths": [
[str(m.realpath), "converted"] if m.converted else str(m.realpath)
for m in p.musiclist
],
"groups": [group.get_save_data() for group in p.groups],
}
for p in self.playlists
]
history_data = [history.get_save_data() for history in self.history_data]
write_json("data/playlists.json", playlist_data)
write_json("data/history.json", history_data)
minip = self.music_controls.minip
minip.save_state()
write_json(
"data/settings.json",
{
"volume": self.volume,
"loops": self.loops,
"shuffle": self.shuffle,
"fps": self.user_framerate,
"custom_title": self.custom_title,
"win_pos": self.window.position,
"win_size": self.window.size,
"before_maximize_data": self.before_maximize_data,
"maximized": self.maximized,
"discord_presence": self.discord_presence.active,
"strip_youtube_id": self.strip_youtube_id,
"taskbar_height": self.taskbar_height,
"miniplayer": [minip.last_size, minip.last_pos, minip.last_borderless],
"keybinds": self.keybinds.get_save_data(),
},
)
for playlist in self.playlists:
if playlist.cover is not None:
if not os.path.exists(f"data/covers/{playlist.name}.png"):
pygame.image.save(
playlist.cover, f"data/covers/{playlist.name}.png"
)
print("Data saved correctly")
def update(self):
if pygame.time.get_ticks() - self.last_save >= SAVE_COOLDOWN:
self.last_save = pygame.time.get_ticks()
self.save()
self.target_framerate = self.user_framerate
if (
not self.focused
and (
self.music_controls.music_videoclip_cover is None
or self.music_paused
or self.music is None
)
and (
not self.music_controls.minip.focused
or self.music_controls.minip.window is None
)
):
self.target_framerate = 10
self.stolen_cursor = False
self.cursor_hover = False
if self.custom_title and self.can_abs_interact():
self.stolen_cursor = self.custom_borders.update()
ratio = self.window.size[0] / self.window.size[1]
if ratio < RATIO_MIN:
self.window.size = (self.window.size[1] * RATIO_MIN, self.window.size[1])
self.make_bg_image()
multx = self.window.size[0] / UI_SIZES[0]
multy = self.window.size[1] / UI_SIZES[1]
self.ui_mult = min(1.2, max(0.4, (multx * 0.1 + multy * 1) / 1.1))
if self.custom_title and not self.music_controls.super_fullscreen:
self.tbarh = 30
else:
self.tbarh = 0
self.start_style = mili.PADLESS | {"spacing": int(self.ui_mult * 3)}
mili.animation.update_all()
self.input_stolen = False
if (
self.discord_presence.active
and pygame.time.get_ticks() - self.discord_presence.last_update
>= DISCORD_COOLDOWN
):
self.discord_presence.update()
if self.discord_presence.connect_error is not None:
self.discord_presence.show_error()
else:
if self.discord_presence.connecting:
self.discord_presence.update_connecting()
if len(mili.get_font_cache()) > 20:
mili.clear_font_cache()
def ui(self):
self.mili.rect({"color": (BG_CV,) * 3, "border_radius": 0})
if self.custom_title:
self.mili.rect(
{
"color": (BORDER_CV,) * 3,
"outline": 1,
"draw_above": True,
"border_radius": 0,
}
)
self.ui_bg_effect()
self.ui_top()
with self.mili.begin(None, {"fillx": True, "filly": True} | mili.PADLESS):
self.mili.id_checkpoint(20)
if self.modal_state != "fullscreen":
if self.view_state == "list":
self.list_viewer.ui()
elif self.view_state == "playlist":
self.playlist_viewer.ui()
else:
if self.view_state == "list":
self.list_viewer.ui_check()
elif self.view_state == "playlist":
self.playlist_viewer.ui_check()
self.mili.element(None, {"filly": True})
if self.modal_state == "settings":
self.settings.ui()
elif self.modal_state == "fullscreen":
self.music_fullscreen.ui()
elif self.modal_state == "history":
self.history.ui()
elif self.modal_state == "keybinds":
self.edit_keybinds.ui()
self.mili.id_checkpoint(5000)
self.music_controls.ui()
self.mili.id_checkpoint(5100)
if (
self.playlist_viewer.modal_state == "none"
and self.list_viewer.modal_state == "none"
and self.modal_state == "none"
):
self.prefabs.ui_overlay_btn(
self.anim_settings,
self.open_settings,
self.settings_image,
"bottom",
tooltip="Open settings",
)
if (
self.view_state == "list"
and self.custom_title
and pygame.key.get_pressed()[pygame.K_BACKSLASH]
):
self.mili.text_element(
f"developer version {DEV_VERSION}",
{"size": self.mult(13), "color": (100,) * 3},
None,
mili.FLOATING,
)
if self.modal_state != "none" and self.menu_data != "controls":
self.close_menu()
self.mili.id_checkpoint(5200)
if self.menu_open:
self.ui_menu()
self.mili.id_checkpoint(5500)
if not self.cursor_hover:
self.tooltip_data = None
elif self.tooltip_data:
if pygame.time.get_ticks() - self.tooltip_hover_time >= TOOLTIP_COOLDOWN:
self.ui_tooltip()
if not self.stolen_cursor and self.cursor_hover and self.focused:
pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_HAND)
elif not self.stolen_cursor and self.focused:
pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_ARROW)
if not self.custom_borders.dragging and not self.custom_borders.resizing:
self.custom_borders.cumulative_relative = pygame.Vector2()
def ui_tooltip(self):
pad = self.mult(2)
txtstyle = {
"size": self.mult(13),
"color": (120,) * 3,
"slow_grow": True,
"pady": pad,
"padx": pad,
}
size = self.mili.text_size(self.tooltip_data, txtstyle)
if size.x > self.window.size[0] / 1.1:
txtstyle["wraplen"] = self.window.size[0] / 1.1
size = self.mili.text_size(self.tooltip_data, txtstyle)
mpos = pygame.Vector2(pygame.mouse.get_pos())
posx = min(mpos.x, self.window.size[0] - size.x - pad * 4)
posy = max(mpos.y - pad * 3 - size.y, pad * 2)
if self.mili.element(
((posx, posy), (0, 0)),
{"blocking": False, "ignore_grid": True, "parent_id": 0, "z": 999999},
):
self.mili.rect({"color": (10,) * 3, "border_radius": 0})
self.mili.text(self.tooltip_data, txtstyle)
self.mili.rect({"color": (30,) * 3, "outline": 1, "border_radius": 0})
def ui_top(self):
if self.custom_title:
with self.mili.begin(
(0, 0, 0, self.tbarh), {"fillx": True, "blocking": False}
):
self.mili.rect({"border_radius": 0, "color": (BORDER_CV / 8,) * 3})
self.prefabs.ui_overlay_top_btn(
self.anims[0],
self.quit,
self.close_image,
"right",
red=True,
tooltip="Quit app",
)
self.prefabs.ui_overlay_top_btn(
self.anims[1],
self.action_maximize,
self.maximize_image,
"right",
1,
tooltip="Restore window" if self.maximized else "Maximize window",
)
self.prefabs.ui_overlay_top_btn(
self.anims[2],
self.action_minimize,
self.minimize_image,
"right",
2,
tooltip="Minimize window",
)
self.prefabs.ui_overlay_top_btn(
self.anims[3],
self.toggle_custom_title,
self.resize_image,
"right",
3,
tooltip="Disable custom borders",
)
else:
self.prefabs.ui_overlay_top_btn(
self.anims[0], self.quit, self.close_image, "right", tooltip="Quit app"
)
if self.view_state == "playlist":
self.playlist_viewer.ui_top_buttons()
elif self.view_state == "list":
self.list_viewer.ui_top_buttons()
def ui_bg_effect(self):
if not self.bg_effect:
return
self.mili.image(self.bg_effect_image)
self.mili.image(self.bg_black_image, {"cache": self.bg_cache})
def ui_menu(self):
with self.mili.begin(
(self.menu_pos, (0, 0)),
{
"resizex": True,
"resizey": True,
"ignore_grid": True,
"parent_id": 0,
"axis": "x",
"z": 9999,
"padx": self.mult(7),
"pady": self.mult(7),
},
) as menu:
self.mili.rect({"color": (MENU_CV[0],) * 3, "border_radius": "50"})
self.mili.rect(
{"color": (MENU_CV[1],) * 3, "border_radius": "50", "outline": 1}
)
for bdata in self.menu_buttons:
br = "50"
tooltip = None
if len(bdata) == 3:
bimage, baction, banim = bdata
elif len(bdata) == 4:
if len(bdata[-1]) <= 2:
bimage, baction, banim, br = bdata
else:
bimage, baction, banim, tooltip = bdata
elif len(bdata) == 5:
bimage, baction, banim, br, tooltip = bdata
self.prefabs.ui_image_btn(bimage, baction, banim, 40, br, tooltip)
if (
not menu.absolute_hover
and any([btn is True for btn in pygame.mouse.get_pressed()])
and (
self.music_controls.dots_rect is None
or not self.music_controls.dots_rect.collidepoint(
pygame.mouse.get_pos()
)
)
):
self.close_menu()
def tick_tooltip(self, text):
if text != self.tooltip_data:
self.tooltip_data = text
self.tooltip_hover_time = pygame.time.get_ticks()
def open_settings(self):
self.modal_state = "settings"
def change_state(self, state):
self.view_state = state
self.close_menu()
self.mili.clear_memory()
self.discord_presence.update()
def close_menu(self):
if self.menu_buttons is not None:
for btndata in self.menu_buttons:
anim = btndata[2]
anim.goto_a()
self.menu_open = False
self.menu_buttons = None
self.menu_pos = None
def open_menu(self, data, *buttons, pos=None):
self.menu_open = True
self.menu_data = data
self.menu_buttons = buttons
if pos is None:
self.menu_pos = pygame.mouse.get_pos()
else:
self.menu_pos = pos
def mult(self, size):
return max(1, int(size * self.ui_mult))
def action_maximize(self):
if self.maximized:
do_drag = pygame.Rect(0, 0, self.window.size[0], 30).collidepoint(
pygame.mouse.get_pos()
)
self.window.position = self.before_maximize_data[0]
self.window.size = self.before_maximize_data[1]
self.maximized = False
self.before_maximize_data = None
self.custom_borders.resizing = self.custom_borders.dragging = False
if do_drag:
self.custom_borders.dragging = True
pygame.mouse.set_pos((self.window.size[0] / 2, 15))
self.custom_borders._press_rel = pygame.mouse.get_pos()
self.custom_borders._press_global = (
pygame.Vector2(pygame.mouse.get_pos()) + self.window.position
)
self.custom_borders._start_val = pygame.Vector2(self.window.position)
else:
self.before_maximize_data = self.window.position, self.window.size
self.window.position = (0, 0)
desktop_size = pygame.display.get_desktop_sizes()[0]
self.window.size = (desktop_size[0], desktop_size[1] - self.taskbar_height)
self.maximized = True
self.make_bg_image()
def action_minimize(self):
self.window.minimize()
def on_resize_move(self):
if self.maximized and self.custom_borders.relative.length() != 0:
self.action_maximize()
def can_interact(self):
return (
self.can_abs_interact()
and not self.custom_borders.resizing
and not self.custom_borders.dragging
and self.custom_borders.cumulative_relative.length() == 0
)
def can_abs_interact(self):
if self.music_controls.minip.window is None:
return True
return not self.music_controls.minip.focused and self.focused
def make_bg_image(self):
self.bg_black_image = pygame.Surface(self.window.size, pygame.SRCALPHA)
self.bg_effect_image = pygame.Surface(self.window.size, pygame.SRCALPHA)
for i in range(self.bg_black_image.height):
alpha = pygame.math.lerp(0, 255, i / (self.bg_black_image.height / 1.5))
self.bg_black_image.fill(
(0, 0, 0, alpha), (0, i, self.bg_black_image.width, 1)
)
def event(self, event):
self.shortcuts_event(event)
if event.type == pygame.WINDOWFOCUSGAINED and event.window == self.window:
self.focused = True
if event.type == pygame.WINDOWFOCUSLOST and event.window == self.window:
self.focused = False
self.close_menu()
if event.type == pygame.WINDOWRESIZED and event.window == self.window:
self.make_bg_image()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
self.tooltip_data = None
if self.menu_open:
self.close_menu()
return
self.music_controls.event(event)
if not self.can_interact():
return
if self.modal_state == "settings":
if self.settings.event(event):
return
elif self.modal_state == "history":
if self.history.event(event):
return
elif self.modal_state == "keybinds":
if self.edit_keybinds.event(event):
return
elif self.modal_state == "fullscreen":
if self.music_fullscreen.event(event):
return
if self.view_state == "list":
self.list_viewer.event(event)
elif self.view_state == "playlist":
self.playlist_viewer.event(event)
def shortcuts_event(self, event):
if self.listening_key:
return
if event.type == pygame.KEYDOWN:
if Keybinds.check("quit", event):
self.quit()
elif Keybinds.check("save", event):
self.save()
elif self.can_interact():
if Keybinds.check("toggle_settings", event):
if self.modal_state == "settings":
self.settings.close()
else:
self.open_settings()
elif Keybinds.check("new/add", event):
if self.view_state == "list":
if self.list_viewer.modal_state != "new_playlist":
self.list_viewer.action_new()
elif self.view_state == "playlist":
if self.playlist_viewer.modal_state != "add":
self.playlist_viewer.action_add_music()
elif Keybinds.check("open_history", event):
self.open_settings()
self.settings.action_history()
elif Keybinds.check("open_keybinds", event):
self.open_settings()
self.settings.action_keybinds()
elif Keybinds.check("minimize_window", event):
self.action_minimize()
elif Keybinds.check("maximize_window", event):
self.action_maximize()
def quit(self):
for playlist in self.playlists:
for music in playlist.musiclist:
if music.pending:
btn = pygame.display.message_box(
"Wait before closing",
"Some tracks are still being converted. Please wait until they are converted "
"before closing the application, otherwise the files will be corrupted.",
"warn",
None,
("Understood", "Close Anyways"),
)
if btn == 0:
return
self.save()
print("Application quit")
pygame.quit()
raise SystemExit
if __name__ == "__main__":
MusicPlayerApp().run()