From 2d8471dea0a2d853cb4f549030bb8044fdde6673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Dzi=C4=99giel?= Date: Thu, 29 Apr 2021 12:26:40 +0200 Subject: [PATCH] Add playlist shuffle repeat mode #52 --- src/playlist.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/playlist.js b/src/playlist.js index 21904776..9d874f80 100644 --- a/src/playlist.js +++ b/src/playlist.js @@ -7,12 +7,14 @@ var RepeatMode = { NONE: 0, TRACK: 1, PLAYLIST: 2, + SHUFFLE: 3, }; const repeatIcons = [ 'media-playlist-consecutive-symbolic', 'media-playlist-repeat-song-symbolic', 'media-playlist-repeat-symbolic', + 'media-playlist-shuffle-symbolic', ]; var PlaylistWidget = GObject.registerClass( @@ -176,6 +178,36 @@ class ClapperPlaylistWidget extends Gtk.ListBox return true; } + if(this.repeatMode === RepeatMode.SHUFFLE) { + const playlistIds = []; + let index = 0; + + debug('selecting random playlist item'); + + while(this.get_row_at_index(index)) { + /* We prefer to not repeat the same track */ + if(index !== this.activeRowId) + playlistIds.push(index); + + index++; + } + + if(playlistIds.length) { + const randomId = playlistIds[ + Math.floor(Math.random() * playlistIds.length) + ]; + debug(`selected random playlist item: ${randomId}`); + + return this.changeActiveRow(randomId); + } + else { + debug('only one playlist item, playing again'); + + player.seek(0); + return true; + } + } + if(this.nextTrack()) return true;