From fd2de8b9b6622c028e776dd1d972569b19be37de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Dzi=C4=99giel?= Date: Tue, 27 Apr 2021 14:50:54 +0200 Subject: [PATCH] Add repeat mode options to playlist #52 --- src/player.js | 2 +- src/playlist.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/player.js b/src/player.js index 1f3d76cd..effeb300 100644 --- a/src/player.js +++ b/src/player.js @@ -560,7 +560,7 @@ class ClapperPlayer extends GstClapper.Clapper debug(`end of stream: ${lastTrackId}`); this.emitWs('end_of_stream', lastTrackId); - if(this.playlistWidget.nextTrack()) + if(this.playlistWidget._handleStreamEnded(player)) return; if(settings.get_boolean('close-auto')) { diff --git a/src/playlist.js b/src/playlist.js index 258dbdff..c7093be5 100644 --- a/src/playlist.js +++ b/src/playlist.js @@ -1,5 +1,11 @@ const { Gdk, GLib, GObject, Gst, Gtk, Pango } = imports.gi; +var RepeatMode = { + NONE: 0, + TRACK: 1, + PLAYLIST: 2, +}; + var PlaylistWidget = GObject.registerClass( class ClapperPlaylistWidget extends Gtk.ListBox { @@ -9,6 +15,8 @@ class ClapperPlaylistWidget extends Gtk.ListBox selection_mode: Gtk.SelectionMode.NONE, }); this.activeRowId = -1; + this.repeatMode = RepeatMode.NONE; + this.connect('row-activated', this._onRowActivated.bind(this)); } @@ -88,6 +96,11 @@ class ClapperPlaylistWidget extends Gtk.ListBox ? this.activeRowId - 1 : this.activeRowId + 1; + return this._changeActiveRow(rowId); + } + + _changeActiveRow(rowId) + { const row = this.get_row_at_index(rowId); if(!row) return false; @@ -110,6 +123,29 @@ class ClapperPlaylistWidget extends Gtk.ListBox this.activeRowId = row.get_index(); player.set_uri(row.uri); } + + _handleStreamEnded(player) + { + /* Seek to beginning when repeating track + * or playlist with only one item */ + if( + this.repeatMode === RepeatMode.TRACK + || (this.repeatMode === RepeatMode.PLAYLIST + && this.activeRowId === 0 + && !this.get_row_at_index(1)) + ) { + player.seek(0); + return true; + } + + if(this.nextTrack()) + return true; + + if(this.repeatMode === RepeatMode.PLAYLIST) + return this._changeActiveRow(0); + + return false; + } }); let PlaylistItem = GObject.registerClass(