Skip to content

Commit f888cd7

Browse files
radioactivemanjlindgren90
authored andcommitted
Allow wrapping to last track with Previous button. Closes: #1726
When at the first track and "Previous" is pressed, jump to the last track in the playlist if "Repeat Playlist" is active. This makes the behavior more intuitive and consistent with the "Next" button.
1 parent 05292ad commit f888cd7

5 files changed

Lines changed: 40 additions & 14 deletions

File tree

src/libaudcore/drct.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ EXPORT void aud_drct_pl_prev()
217217
if (playlist == Playlist())
218218
playlist = Playlist::active_playlist();
219219

220-
playlist.prev_song();
220+
playlist.prev_song(aud_get_bool("repeat"));
221221
}
222222

223223
EXPORT void aud_drct_pl_prev_album()
@@ -226,7 +226,7 @@ EXPORT void aud_drct_pl_prev_album()
226226
if (playlist == Playlist())
227227
playlist = Playlist::active_playlist();
228228

229-
playlist.prev_album();
229+
playlist.prev_album(aud_get_bool("repeat"));
230230
}
231231

232232
static void add_list(Index<PlaylistAddItem> && items, int at, bool to_temp,

src/libaudcore/playlist-data.cc

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -866,12 +866,18 @@ PlaylistData::PosChange PlaylistData::shuffle_pos_random(bool repeat,
866866
return NO_POS;
867867
}
868868

869-
int PlaylistData::pos_before(int ref_pos, bool shuffle) const
869+
int PlaylistData::pos_before(int ref_pos, bool shuffle, bool repeat) const
870870
{
871871
if (shuffle)
872872
return shuffle_pos_before(ref_pos);
873873

874-
return (ref_pos > 0) ? ref_pos - 1 : -1;
874+
if (ref_pos > 0)
875+
return ref_pos - 1;
876+
877+
if (repeat)
878+
return m_entries.len() - 1;
879+
880+
return -1;
875881
}
876882

877883
PlaylistData::PosChange PlaylistData::pos_after(int ref_pos, bool shuffle,
@@ -1005,10 +1011,10 @@ void PlaylistData::set_position(int entry_num)
10051011
queue_position_change();
10061012
}
10071013

1008-
bool PlaylistData::prev_song()
1014+
bool PlaylistData::prev_song(bool repeat)
10091015
{
10101016
bool shuffle = aud_get_bool("shuffle");
1011-
int pos = pos_before(position(), shuffle);
1017+
int pos = pos_before(position(), shuffle, repeat);
10121018
if (pos < 0)
10131019
return false;
10141020

@@ -1027,7 +1033,7 @@ bool PlaylistData::next_song(bool repeat)
10271033
return true;
10281034
}
10291035

1030-
bool PlaylistData::prev_album()
1036+
bool PlaylistData::prev_album(bool repeat)
10311037
{
10321038
bool shuffle = aud_get_bool("shuffle");
10331039
int pos = position();
@@ -1041,7 +1047,7 @@ bool PlaylistData::prev_album()
10411047

10421048
while (1)
10431049
{
1044-
auto prev_entry = entry_at(pos_before(pos, shuffle));
1050+
auto prev_entry = entry_at(pos_before(pos, shuffle, repeat));
10451051
if (!prev_entry || !same_album(entry->tuple, prev_entry->tuple))
10461052
break;
10471053

@@ -1053,7 +1059,7 @@ bool PlaylistData::prev_album()
10531059

10541060
// we're at the start of the current album
10551061
// one more song back puts us in the previous album
1056-
pos = pos_before(pos, shuffle);
1062+
pos = pos_before(pos, shuffle, repeat);
10571063
in_prev_album = true;
10581064
}
10591065

src/libaudcore/playlist-data.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ class PlaylistData
100100

101101
void set_position(int entry_num);
102102

103-
bool prev_song();
103+
bool prev_song(bool repeat);
104104
bool next_song(bool repeat);
105-
bool prev_album();
105+
bool prev_album(bool repeat);
106106
bool next_album(bool repeat);
107107

108108
int next_unscanned_entry(int entry_num) const;
@@ -159,7 +159,7 @@ class PlaylistData
159159
PosChange shuffle_pos_after(int ref_pos, bool by_album) const;
160160
PosChange shuffle_pos_random(bool repeat, bool by_album) const;
161161

162-
int pos_before(int ref_pos, bool shuffle) const;
162+
int pos_before(int ref_pos, bool shuffle, bool repeat) const;
163163
PosChange pos_after(int ref_pos, bool shuffle, bool by_album) const;
164164
PosChange pos_new(bool repeat, bool shuffle, bool by_album,
165165
int hint_pos) const;

src/libaudcore/playlist.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,11 +657,19 @@ EXPORT void Playlist::set_position(int entry_num) const
657657
}
658658
EXPORT bool Playlist::prev_song() const
659659
{
660-
SIMPLE_WRAPPER(bool, false, prev_song);
660+
SIMPLE_WRAPPER(bool, false, prev_song, false);
661+
}
662+
EXPORT bool Playlist::prev_song(bool repeat) const
663+
{
664+
SIMPLE_WRAPPER(bool, false, prev_song, repeat);
661665
}
662666
EXPORT bool Playlist::prev_album() const
663667
{
664-
SIMPLE_WRAPPER(bool, false, prev_album);
668+
SIMPLE_WRAPPER(bool, false, prev_album, false);
669+
}
670+
EXPORT bool Playlist::prev_album(bool repeat) const
671+
{
672+
SIMPLE_WRAPPER(bool, false, prev_album, repeat);
665673
}
666674
EXPORT bool Playlist::next_song(bool repeat) const
667675
{

src/libaudcore/playlist.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,24 @@ class Playlist
253253
* true on success, false if playlist position was not changed. */
254254
bool prev_song() const;
255255

256+
/* Returns the playlist position to the previous entry in playback order.
257+
* At the beginning of the playlist, wraps around to the end if <repeat>
258+
* is true. Returns true on success, false if playlist position was not
259+
* changed. */
260+
bool prev_song(bool repeat) const;
261+
256262
/* Returns the playlist position to the first entry in playback order where
257263
* the album is not the current album. Does not support wrapping past the
258264
* beginning of the playlist. Returns true on success, false if playlist
259265
* position was not changed. */
260266
bool prev_album() const;
261267

268+
/* Returns the playlist position to the first entry of the previous album
269+
* in playback order, taking current shuffle settings into account. At the
270+
* beginning of the playlist, wraps around to the end if <repeat> is true.
271+
* Returns true on success, false if playlist position was not changed. */
272+
bool prev_album(bool repeat) const;
273+
262274
/* Gets/sets the entry which has keyboard focus (-1 = no entry). */
263275
int get_focus() const;
264276
void set_focus(int entry) const;

0 commit comments

Comments
 (0)