-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_ui.py
446 lines (390 loc) · 17 KB
/
main_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
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
import os
import gi
import static_strings as ss
import tools.data_tools
from tools.data_wrapper import TrackData, PlaybackInfo, DataWithImage
from special_widgets import ArtistButton
from thread_tools.delayed_thread import DelayedThread
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk, Adw, GdkPixbuf, GLib
class SpotifyGtkUI:
play_button: Gtk.Button = None
next_button: Gtk.Button = None
previous_button: Gtk.Button = None
position_slider: Gtk.Scale = None
loudness_slider: Gtk.Scale = None
upper_box: Gtk.Box = None
track_title: Gtk.Button = None
track_artists: Gtk.Box = None
track_image: Gtk.Image = None
loading_box: Gtk.Box = None
main_box: Gtk.Box = None
player_box: Gtk.Box = None
artist_page: Gtk.Box = None
overlay_container: Gtk.Overlay = None
loading_spinner: Gtk.Spinner = None
loading_label: Gtk.Label = None
leaflet_container: Adw.Leaflet = None
artist_image: Gtk.Image = None
artist_popularity: Gtk.ProgressBar = None
artist_follower: Gtk.Label = None
artist_name: Gtk.Label = None
callbacks: [()] = None
volume_change_delay: DelayedThread = None
position_change_delay: DelayedThread = None
play_state_change_delay: DelayedThread = None
track_duration: int = None
is_playing = False
device_ready = False
controllable_widgets: [Gtk.Widget] = None
def __init__(self):
self.file_dir = os.path.dirname(os.path.abspath(__file__))
self.app = Adw.Application(application_id='de.flxtea.spotify')
man = Adw.StyleManager.get_default()
man.set_color_scheme(Adw.ColorScheme.PREFER_DARK)
self.controllable_widgets = []
def on_activate(self, appl):
global css_provider
css_provider = Gtk.CssProvider()
css_provider.load_from_path(self.file_dir + "/style.css")
win = Adw.ApplicationWindow(application=appl)
win.css_provider = css_provider
self.main_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
headerbar = Gtk.HeaderBar()
play_here_button = Gtk.Button(icon_name="computer-symbolic")
play_here_button.connect("clicked", self.play_here)
headerbar.pack_end(play_here_button)
# Upper part
self.upper_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
self.upper_box.add_css_class("toolbar")
self.upper_box.add_css_class("osd")
self.upper_box.set_margin_bottom(20)
self.upper_box.set_margin_start(15)
self.upper_box.set_margin_end(15)
self.upper_box.set_margin_top(20)
self.track_title = Gtk.Button(label="Loading...")
self.track_title.add_css_class("track_title")
self.track_title.add_css_class("flat")
self.track_title.set_hexpand(False)
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=ss.image_cache + ".no_album", width=200, height=200,
preserve_aspect_ratio=True)
self.track_image = Gtk.Image.new_from_pixbuf(pixbuf)
# self.track_image.set_vexpand(True)
self.track_image.set_margin_start(-5)
# self.track_image.set_hexpand(True)
self.track_image.set_size_request(100, 100)
self.upper_box.append(self.track_image)
title_artist_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
title_box = Gtk.Box()
title_box.set_hexpand(False)
title_box.append(self.track_title)
title_artist_box.append(title_box)
self.track_artists = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
self.track_artists.set_margin_start(10)
# self.track_artists.append(Gtk.Button(label="ARTIST 1"))
title_artist_box.append(self.track_artists)
self.upper_box.append(title_artist_box)
# Playback bar
self.create_playback_bar()
# Artist page
self.create_artist_page()
# Main box contains all content besides the headerbar
self.main_box.append(self.upper_box)
self.main_box.append(self.player_box)
self.main_box.set_hexpand(True)
self.main_box.set_vexpand(True)
# Overlay container for loading screen
self.overlay_container = Gtk.Overlay()
self.overlay_container.set_hexpand(True)
self.overlay_container.set_vexpand(True)
self.overlay_container.set_child(self.main_box)
self.loading_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.loading_box.set_hexpand(True)
self.loading_box.set_vexpand(True)
self.loading_box.set_halign(Gtk.Align.CENTER)
self.loading_box.set_valign(Gtk.Align.CENTER)
self.main_box.add_css_class("blur")
self.loading_spinner = Gtk.Spinner()
self.loading_spinner.set_size_request(50, 50)
self.loading_spinner.start()
self.loading_label = Gtk.Label(label="Loading...")
self.loading_box.append(self.loading_spinner)
self.loading_box.append(self.loading_label)
self.overlay_container.add_overlay(self.loading_box)
# Window Box contains headerbar + everything else(self.main_box)
window_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
window_box.append(headerbar)
window_box.append(self.overlay_container)
self.leaflet_container = Adw.Leaflet()
self.leaflet_container.set_can_navigate_back(True)
self.leaflet_container.set_can_unfold(False)
self.leaflet_container.append(window_box)
self.leaflet_container.append(self.artist_page)
win.set_content(self.leaflet_container)
add_styling({self.leaflet_container})
win.connect("close-request", lambda x: win.destroy())
win.set_title("SpotifyGTK")
self.controllable_widgets.append(self.play_button)
self.controllable_widgets.append(self.position_slider)
self.controllable_widgets.append(self.loudness_slider)
self.controllable_widgets.append(self.next_button)
self.controllable_widgets.append(self.previous_button)
self.controllable_widgets.append(self.track_title)
self.controllable_widgets.append(self.track_artists)
self.controllable_widgets.append(play_here_button)
for widget in self.controllable_widgets:
if widget is None:
print(str(widget) + " is None.")
continue
widget.set_sensitive(False)
win.present()
def create_playback_bar(self):
self.player_box = Gtk.Box()
self.player_box.add_css_class("toolbar")
self.player_box.add_css_class("osd")
self.player_box.set_vexpand(True)
self.player_box.set_valign(Gtk.Align.END)
self.player_box.set_margin_bottom(15)
self.player_box.set_margin_start(15)
self.player_box.set_margin_end(15)
self.player_box.set_margin_top(15)
self.player_box.set_size_request(500, 0)
player_controls = Gtk.Box()
# player_controls.add_css_class("linked")
player_controls.add_css_class("osd")
player_controls.add_css_class("round")
player_controls.set_margin_top(5)
player_controls.set_margin_bottom(5)
self.previous_button = Gtk.Button(icon_name="media-skip-backward-symbolic")
self.previous_button.connect("clicked", self.skip_previous)
self.play_button = Gtk.Button(icon_name="media-playback-pause-symbolic")
self.play_button.connect("clicked", self.toggle_play)
self.next_button = Gtk.Button(icon_name="media-skip-forward-symbolic")
self.next_button.connect("clicked", self.skip_next)
self.play_button.add_css_class("play-button")
self.play_button.add_css_class("no-vert-padding")
self.play_button.set_size_request(40, 40)
self.previous_button.add_css_class("no-vert-padding")
self.previous_button.set_size_request(40, 40)
self.next_button.add_css_class("no-vert-padding")
self.next_button.set_size_request(40, 40)
self.play_button.add_css_class("circular")
self.play_button.add_css_class("raised")
self.previous_button.add_css_class("circular")
self.next_button.add_css_class("circular")
player_controls.append(self.previous_button)
player_controls.append(self.play_button)
player_controls.append(self.next_button)
self.player_box.append(player_controls)
self.position_slider = Gtk.Scale()
self.position_slider.set_range(0, 100)
self.position_slider.set_hexpand(True)
self.position_slider.set_value(0)
self.position_slider.connect("change-value", lambda a, b, c: self.position_change())
self.player_box.append(self.position_slider)
volume_button_box = Gtk.EventControllerMotion()
volume_button = Gtk.Button(icon_name="audio-volume-high")
context = volume_button.get_style_context()
context.add_provider(css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)
volume_button.add_controller(volume_button_box)
volume_button.add_css_class("circular")
volume_button.add_css_class("fadeout")
volume_button.set_vexpand(False)
volume_button.set_can_focus(False)
self.loudness_slider = Gtk.Scale()
self.loudness_slider.set_inverted(True)
self.loudness_slider.set_range(0, 100)
self.loudness_slider.set_hexpand(False)
self.loudness_slider.set_value(100)
self.loudness_slider.add_css_class("fadein")
self.loudness_slider.connect("value-changed", lambda a: self.volume_change())
self.player_box.append(self.loudness_slider)
def create_artist_page(self):
self.artist_page = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
artist_page_headerbar = Gtk.HeaderBar()
go_back_button = Gtk.Button(icon_name="go-previous-symbolic")
go_back_button.connect("clicked", lambda d: self.leaflet_container.navigate(Adw.NavigationDirection.BACK))
artist_page_headerbar.pack_start(go_back_button)
artist_page_content = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=ss.image_cache + ".no_album", width=200, height=200,
preserve_aspect_ratio=True)
self.artist_image = Gtk.Image.new_from_pixbuf(pixbuf)
self.artist_image.set_size_request(100, 100)
artist_name_pop_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
artist_name_pop_box.set_hexpand(True)
artist_name_pop_follower_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
# artist_name_pop_follower_box.set_halign(Gtk.Align.START)
artist_name_pop_follower_box.set_margin_start(5)
artist_name_pop_follower_box.set_margin_end(10)
artist_name_pop_follower_box.set_margin_top(10)
artist_name_pop_follower_box.set_margin_bottom(10)
artist_name_pop_follower_box.set_hexpand(True)
artist_name_pop_follower_image_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
artist_name_pop_follower_image_box.set_margin_start(10)
artist_name_pop_follower_image_box.set_margin_end(10)
artist_name_pop_follower_image_box.set_margin_top(10)
artist_name_pop_follower_image_box.set_margin_bottom(10)
artist_name_pop_follower_image_box.set_hexpand(True)
artist_page_topline = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.artist_name = Gtk.Label(label="Artist Name")
self.artist_name.add_css_class("title-1")
# artist_name.set_valign(Gtk.Align.CENTER)
self.artist_name.set_margin_end(25)
artist_popularity_box = Gtk.Box()
self.artist_popularity = Gtk.ProgressBar()
self.artist_popularity.set_halign(Gtk.Align.END)
self.artist_popularity.set_valign(Gtk.Align.CENTER)
self.artist_popularity.set_fraction(0.75)
artist_popularity_box.append(self.artist_popularity)
artist_popularity_box.set_hexpand(True)
artist_popularity_box.set_halign(Gtk.Align.END)
artist_name_pop_box.append(self.artist_name)
artist_name_pop_box.append(artist_popularity_box)
artist_name_pop_follower_box.append(artist_name_pop_box)
self.artist_follower = Gtk.Label(label="69,420 follower")
self.artist_follower.set_halign(Gtk.Align.START)
artist_name_pop_follower_box.append(self.artist_follower)
artist_name_pop_follower_image_box.append(self.artist_image)
artist_name_pop_follower_image_box.append(artist_name_pop_follower_box)
artist_page_topline.append(artist_name_pop_follower_image_box)
artist_genres = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
# artist_genres.append(Gtk.Button(label="Genre"))
# artist_genres.append(Gtk.Button(label="Other Genre"))
# artist_page_topline.append(artist_genres)
artist_name_pop_follower_box.append(artist_genres)
artist_page_content.append(artist_page_topline)
sep = Gtk.Separator()
sep.set_margin_top(5)
sep.set_margin_end(5)
artist_page_content.append(sep)
artist_page_content.append(Gtk.Label(label="Top Tracks here?"))
# artist_popularity_box.add_css_class("debug")
# artist_name_pop_follower_box.add_css_class("debug")
# artist_name_pop_follower_image_box.add_css_class("debug")
# artist_name_pop_box.add_css_class("debug")
self.artist_page.append(artist_page_headerbar)
self.artist_page.append(artist_page_content)
def backend_ready_callback(self):
def f(): GLib.idle_add(self.backend_ready)
DelayedThread(f, [], 400)
self.update_loading_message("Ready!")
self.loading_spinner.stop()
def backend_ready(self):
def set_ready():
self.track_title.set_label("No song loaded.")
for widget in self.controllable_widgets:
widget.set_sensitive(True)
self.main_box.remove_css_class("blur")
self.overlay_container.remove_overlay(self.loading_box)
self.device_ready = True
GLib.idle_add(set_ready)
def report_state_callback(self, info: PlaybackInfo):
self.track_duration = info.duration
percent = 0
if info.duration != 0:
percent = (info.position / info.duration) * 100
if not self.position_change_delay or not self.position_change_delay.is_running():
self.position_slider.set_value(percent)
# Since we're now doing this with get_track_info() I think we could remove this
# self.track_title.set_label(info.title)
if info.playing:
if self.play_state_change_delay is None or not self.play_state_change_delay.is_running():
self.play_button.set_icon_name("media-playback-pause-symbolic")
self.is_playing = True
else:
if self.play_state_change_delay is None or not self.play_state_change_delay.is_running():
self.play_button.set_icon_name("media-playback-start-symbolic")
self.is_playing = False
def toggle_play(self, d):
self.is_playing = not self.is_playing
if self.play_state_change_delay is None or not self.play_state_change_delay.is_running():
self.play_state_change_delay = DelayedThread(None, [], 300)
if self.is_playing:
self.play_button.set_icon_name("media-playback-pause-symbolic")
else:
self.play_button.set_icon_name("media-playback-start-symbolic")
self.callbacks["toggle_play"](d)
def position_change(self):
if self.position_change_delay is not None and self.position_change_delay.is_running():
self.position_change_delay.args = [self.position_slider.get_value() / 100 * self.track_duration]
else:
self.position_change_delay = \
DelayedThread(self.callbacks["set_position"],
[self.position_slider.get_value() / 100 * self.track_duration], 300)
def volume_change(self):
if self.volume_change_delay is not None and self.volume_change_delay.is_running():
self.volume_change_delay.args = [self.loudness_slider.get_value()]
else:
self.volume_change_delay = \
DelayedThread(self.callbacks["set_volume"], [self.loudness_slider.get_value()], 200)
def play_here(self, d):
self.callbacks["play_here"]()
def update_loading_message(self, message: str):
self.loading_label.set_text(message)
def get_track_info(self, track_info: TrackData):
def set_data():
self.track_title.set_label(track_info.name)
had_child = True
while had_child is True:
had_child = False
# noinspection PyTypeChecker
for child in self.track_artists:
self.track_artists.remove(child)
had_child = True
for artist in track_info.artists:
artist_button = ArtistButton(label=artist.name)
artist_button.data_id = artist.data_id
def artist_button_action(d, data_id: str):
self.update_artist_page(data_id, lambda: self.leaflet_container.navigate(Adw.NavigationDirection.FORWARD))
artist_button.connect("clicked", artist_button_action, artist.data_id)
artist_button.set_margin_end(5)
self.track_artists.append(artist_button)
GLib.idle_add(set_data)
print("New track data loaded")
self.try_set_image(self.track_image, track_info)
def update_artist_page(self, artist_id: str, on_success: ()):
def load_data():
artist_data = tools.data_tools.get_artist_info(artist_id)
if artist_data is None:
print("Received None as artist_data")
return
def update_widgets():
self.artist_name.set_label(artist_data.name)
self.artist_popularity.set_fraction(artist_data.popularity / 100)
self.artist_follower.set_label(f"{artist_data.followers} follower")
GLib.idle_add(update_widgets)
if self.try_set_image(self.artist_image, artist_data):
on_success()
DelayedThread(load_data, [], 1)
def try_set_image(self, image_widget: Gtk.Image, data: DataWithImage):
file_name = data.get_image()
if file_name is not None:
def set_image():
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=file_name, width=200, height=200,
preserve_aspect_ratio=True)
image_widget.set_from_pixbuf(pixbuf)
GLib.idle_add(set_image)
# print("New image set")
return True
else:
print("Could not load file :(")
return False
def skip_next(self, d):
self.callbacks["skip_next"]()
def skip_previous(self, d):
self.callbacks["skip_previous"]()
def run_ui(self, callbacks):
self.callbacks = callbacks
self.app.connect('activate', self.on_activate)
try:
self.app.run(None)
except KeyboardInterrupt:
pass
css_provider = None
def add_styling(widget):
global css_provider
for widget in widget:
context = widget.get_style_context()
context.add_provider(css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)
add_styling(widget)