Make the gui-updating code more readable and verbose

This commit is contained in:
Peder Bergebakken Sundt 2018-03-06 16:55:12 +01:00
parent 33ae98289f
commit 90fd2c93b2
1 changed files with 52 additions and 49 deletions

View File

@ -14,7 +14,7 @@ class RemiApp(App):
res_path = os.path.join(os.path.dirname(__file__), 'res') res_path = os.path.join(os.path.dirname(__file__), 'res')
super(RemiApp, self).__init__(*args, static_file_path=res_path) super(RemiApp, self).__init__(*args, static_file_path=res_path)
def make_gui_elements(self): def make_gui_elements(self):#content and behaviour
#logo: #logo:
self.logo_image = gui.Image('/res/logo.png') self.logo_image = gui.Image('/res/logo.png')
@ -150,8 +150,8 @@ class RemiApp(App):
self.mainLoop() self.mainLoop()
return container return container
def mainLoop(self): def mainLoop(self):
#self.playback.seek_slider.get_value()
self.playback_update() self.playback_update()
self.volume_update()
self.playlist_update() self.playlist_update()
Timer(1, self.mainLoop).start() Timer(1, self.mainLoop).start()
@ -211,61 +211,56 @@ class RemiApp(App):
def on_playlist_clear_click(self, row_widget): def on_playlist_clear_click(self, row_widget):
api.playlist_clear() api.playlist_clear()
# playback steps: # gui updaters:
@call_as_thread @call_as_thread
def playback_update(self, times_called=[0]): def playback_update(self):
is_playing = api.is_playing() is_playing = api.is_playing()
self.set_playing(is_playing) self.set_playing(is_playing)
if is_playing: if is_playing: # update seekbar and timestamp
try: try:
playback_pos = api.get_playback_pos() playback_pos = api.get_playback_pos()
except api.APIError: except api.APIError:
playback_pos = None playback_pos = None
if playback_pos: if playback_pos:
slider_pos = playback_pos["current"] / playback_pos["total"] * 100 slider_pos = playback_pos["current"] / playback_pos["total"] * 100
current = seconds_to_timestamp(playback_pos["current"])
total = seconds_to_timestamp(playback_pos["total"])
if self.playback.seek_slider.get_value() != slider_pos: if self.playback.seek_slider.get_value() != slider_pos:
self.playback.seek_slider.set_value(slider_pos) self.playback.seek_slider.set_value(slider_pos)
self.playback.timestamp.set_text(
seconds_to_timestamp(playback_pos["current"]) self.playback.timestamp.set_text(f"{current} - {total}")
+ " - " +
seconds_to_timestamp(playback_pos["total"])
)
else: else:
self.playback.timestamp.set_text("--:-- - --:--") self.playback.timestamp.set_text("--:-- - --:--")
if times_called[0] % 5 == 0:
volume = api.get_volume()
if volume > 100: volume = 100
if self.playback.volume_slider.get_value() != volume:
self.playback.volume_slider.set_value(volume)
times_called[0] += 1
@call_as_thread @call_as_thread
def volume_update(self): def volume_update(self):
self.volume.slider.set_value(api.get_volume()) volume = api.get_volume()
if volume > 100:
volume = 100
if self.playback.volume_slider.get_value() != volume:
self.playback.volume_slider.set_value(volume)
@call_as_thread @call_as_thread
def playlist_update(self): def playlist_update(self):
playlist = api.get_playlist() playlist = api.get_playlist() # json structure
N = len(playlist) N = len(playlist)
table = []
for i, playlist_item in enumerate(playlist):
name = playlist_item["filename"]
length = "--:--"
if "data" in playlist_item:
if "title" in playlist_item["data"]:
name = playlist_item["data"]["title"]
if "duration" in playlist_item["data"]:
length = seconds_to_timestamp(playlist_item["data"]["duration"])
if playlist_item.get("current", False): # update playlist table content:
self.playback.previous.set_enabled(i != 0) table = []
self.playback.next.set_enabled(i != N-1) for playlist_item in playlist:
name = playlist_item["filename"]
length = None
if "data" in playlist_item:
name = playlist_item["data"].get("title", name)
length = playlist_item["data"].get("duration", length)
table.append([ table.append([
playlist_item["index"], playlist_item["index"],
name, name,
length, seconds_to_timestamp(length) if length else "--:--",
ICON_UP, ICON_UP,
ICON_DOWN, ICON_DOWN,
ICON_TRASH, ICON_TRASH,
@ -274,37 +269,45 @@ class RemiApp(App):
self.playlist.table.empty(keep_title=True) self.playlist.table.empty(keep_title=True)
self.playlist.table.append_from_list(table) self.playlist.table.append_from_list(table)
for row_widget, playlist_item in zip( # styling the new table:
map(self.playlist.table.get_child, self.playlist.table._render_children_list[1:]), # for each row element:
playlist): for row_key, playlist_item in zip(self.playlist.table._render_children_list[1:], playlist):
if "current" in playlist_item: row_widget = self.playlist.table.get_child(row_key)
row_widget.set_on_click_listener(self.on_table_row_click, playlist_item)
if playlist_item.get("current", False):
self.playback.previous.set_enabled(playlist_item.get("index") != 0)
self.playback.next.set_enabled(playlist_item.get("index") != N-1)
row_widget.style["background-color"] = COLOR_LIGHT_BLUE row_widget.style["background-color"] = COLOR_LIGHT_BLUE
else: else:
row_widget.style["color"] = COLOR_GRAY_DARK row_widget.style["color"] = COLOR_GRAY_DARK
row_widget.set_on_click_listener(self.on_table_row_click, playlist_item)
for index, (key, item_widget) in enumerate(zip(row_widget._render_children_list,
map(row_widget.get_child, row_widget._render_children_list))): # for each item element in this row:
if index == 1 and "failed" in playlist_item.get("data", {}): for item_index, item_key in enumerate(row_widget._render_children_list):
item_widget = row_widget.get_child(item_key)
if item_index == 1 and "failed" in playlist_item.get("data", {}):
item_widget.style["width"] = "1.1em" item_widget.style["width"] = "1.1em"
item_widget.style["color"] = COLOR_RED item_widget.style["color"] = COLOR_RED
if index >= 3:
if item_index >= 3:
item_widget.style["width"] = "1.1em" item_widget.style["width"] = "1.1em"
item_widget.style["color"] = COLOR_TEAL item_widget.style["color"] = COLOR_TEAL
if index == 3:
if item_index == 3:
item_widget.set_on_click_listener(self.on_table_item_move_click, playlist_item, False) item_widget.set_on_click_listener(self.on_table_item_move_click, playlist_item, False)
if playlist_item["index"] == 0: if playlist_item["index"] == 0:
item_widget.style["color"] = COLOR_GRAY_LIGHT item_widget.style["color"] = COLOR_GRAY_LIGHT
if index == 4: if item_index == 4:
item_widget.set_on_click_listener(self.on_table_item_move_click, playlist_item, True) item_widget.set_on_click_listener(self.on_table_item_move_click, playlist_item, True)
if playlist_item["index"] == N-1: if playlist_item["index"] == N-1:
item_widget.style["color"] = COLOR_GRAY_LIGHT item_widget.style["color"] = COLOR_GRAY_LIGHT
if index == 5:
if item_index == 5:
item_widget.style["color"] = COLOR_RED item_widget.style["color"] = COLOR_RED
item_widget.set_on_click_listener(self.on_table_item_remove_click, playlist_item) item_widget.set_on_click_listener(self.on_table_item_remove_click, playlist_item)
#print(index, key, item_widget) #print(index, key, item_widget)
def set_playing(self, is_playing:bool): # Updates GUI elements
#helpers
def set_playing(self, is_playing:bool):
self.playback.play.set_text(ICON_PAUSE if is_playing else ICON_PLAY) self.playback.play.set_text(ICON_PAUSE if is_playing else ICON_PLAY)
self.playback.seek_slider.set_enabled(is_playing) self.playback.seek_slider.set_enabled(is_playing)