commit 758bbe21fd7dd10e08b6d71e1a08cd6f72fab276
parent 84c1de8fb7f013d21a079caef4d7e5e563a808c8
Author: Paco Esteban <paco@e1e0.net>
Date: Thu, 17 Jun 2021 19:20:39 +0200
implement basic play/pause and better navigation
Diffstat:
2 files changed, 48 insertions(+), 7 deletions(-)
diff --git a/subclient/playback.py b/subclient/playback.py
@@ -20,3 +20,9 @@ class Player:
def exit(self):
self.player.terminate()
+
+ def is_paused(self):
+ return self.player.pause
+
+ def set_pause(self, state):
+ self.player.pause = state
diff --git a/subclient/subclient.py b/subclient/subclient.py
@@ -19,8 +19,11 @@ class SubClient:
self.subsonic = subsonic.Subsonic(self.config['subclient'])
self.player = playback.Player()
self.master = master
+ self.current_song = None
self.master.run_on_exit(self.player.exit)
+
+ self.master.add_key_command(py_cui.keys.KEY_SPACE, self.pause)
# The scrolled list cells that will contain our tasks in each of the
# three categories
self.artist_scroll_cell = self.master.add_scroll_menu(
@@ -37,16 +40,35 @@ class SubClient:
self.update_albums)
self.artist_scroll_cell.add_key_command(py_cui.keys.KEY_TAB,
self._switch_to_next)
+ self.artist_scroll_cell.add_key_command(py_cui.keys.KEY_SPACE,
+ self.pause)
+ self.artist_scroll_cell.add_key_command(py_cui.keys.KEY_Q_LOWER,
+ self.quit)
self.album_scroll_cell.add_key_command(py_cui.keys.KEY_ENTER,
self.update_songs)
self.album_scroll_cell.add_key_command(py_cui.keys.KEY_TAB,
self._switch_to_next)
+ self.album_scroll_cell.add_key_command(py_cui.keys.KEY_SPACE,
+ self.pause)
+ self.album_scroll_cell.add_key_command(py_cui.keys.KEY_Q_LOWER,
+ self.quit)
self.song_scroll_cell.add_key_command(py_cui.keys.KEY_ENTER,
self.play_song)
self.song_scroll_cell.add_key_command(py_cui.keys.KEY_TAB,
self._switch_to_first)
+ self.song_scroll_cell.add_key_command(py_cui.keys.KEY_SPACE,
+ self.pause)
+ self.song_scroll_cell.add_key_command(py_cui.keys.KEY_Q_LOWER,
+ self.quit)
+
+ status_bar_text = ('Quit - q | Pause - space | '
+ 'Select - enter | Cycle list - tab')
+ self.artist_scroll_cell.set_focus_text(status_bar_text)
+ self.album_scroll_cell.set_focus_text(status_bar_text)
+ self.song_scroll_cell.set_focus_text(status_bar_text)
self.update_artists()
+ self.master.move_focus(self.artist_scroll_cell)
def update_artists(self):
artists = self.subsonic.get_artists()
@@ -65,13 +87,14 @@ class SubClient:
self.song_scroll_cell.add_item_list(songs)
def play_song(self):
- song = self.song_scroll_cell.get()
- self.now_playing_block.set_title(self._now_playing_format(song))
- stream = self.subsonic.get_song_stream(song)
+ self.current_song = self.song_scroll_cell.get()
+ self.now_playing_block.set_title(
+ self._now_playing_format('Now Playing', self.current_song))
+ stream = self.subsonic.get_song_stream(self.current_song)
self.player.play(stream)
- def _now_playing_format(self, song):
- return (f'Now playing:\n'
+ def _now_playing_format(self, state, song):
+ return (f'{state}:\n'
f'{song.title} ({helpers.format_duration(song.duration)})')
def _switch_to_next(self):
@@ -80,10 +103,22 @@ class SubClient:
neighbors = self.master._get_horizontal_neighbors(
current_widget,
py_cui.keys.KEY_RIGHT_ARROW)
- self.master.set_selected_widget(all_widgets[neighbors[0]].get_id())
+ self.master.move_focus(all_widgets[neighbors[0]])
def _switch_to_first(self):
- self.master.set_selected_widget(self.artist_scroll_cell.get_id())
+ self.master.move_focus(self.artist_scroll_cell)
+
+ def pause(self):
+ paused = self.player.is_paused()
+ state = 'Paused'
+ if paused:
+ state = 'Now Playing'
+ self.now_playing_block.set_title(
+ self._now_playing_format(state, self.current_song))
+ self.player.set_pause(not paused)
+
+ def quit(self):
+ self.master.stop()
def main():