Skip to content

Commit f8c1e84

Browse files
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 f8c1e84

5 files changed

Lines changed: 30 additions & 7 deletions

File tree

src/libaudcore/drct.cc

Lines changed: 1 addition & 1 deletion
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()

src/libaudcore/playlist-data.cc

Lines changed: 17 additions & 5 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,
@@ -1006,9 +1012,14 @@ void PlaylistData::set_position(int entry_num)
10061012
}
10071013

10081014
bool PlaylistData::prev_song()
1015+
{
1016+
return prev_song(false);
1017+
}
1018+
1019+
bool PlaylistData::prev_song(bool repeat)
10091020
{
10101021
bool shuffle = aud_get_bool("shuffle");
1011-
int pos = pos_before(position(), shuffle);
1022+
int pos = pos_before(position(), shuffle, repeat);
10121023
if (pos < 0)
10131024
return false;
10141025

@@ -1030,6 +1041,7 @@ bool PlaylistData::next_song(bool repeat)
10301041
bool PlaylistData::prev_album()
10311042
{
10321043
bool shuffle = aud_get_bool("shuffle");
1044+
bool repeat = aud_get_bool("repeat");
10331045
int pos = position();
10341046

10351047
// find the start of the previous album
@@ -1041,7 +1053,7 @@ bool PlaylistData::prev_album()
10411053

10421054
while (1)
10431055
{
1044-
auto prev_entry = entry_at(pos_before(pos, shuffle));
1056+
auto prev_entry = entry_at(pos_before(pos, shuffle, repeat));
10451057
if (!prev_entry || !same_album(entry->tuple, prev_entry->tuple))
10461058
break;
10471059

@@ -1053,7 +1065,7 @@ bool PlaylistData::prev_album()
10531065

10541066
// we're at the start of the current album
10551067
// one more song back puts us in the previous album
1056-
pos = pos_before(pos, shuffle);
1068+
pos = pos_before(pos, shuffle, repeat);
10571069
in_prev_album = true;
10581070
}
10591071

src/libaudcore/playlist-data.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class PlaylistData
101101
void set_position(int entry_num);
102102

103103
bool prev_song();
104+
bool prev_song(bool repeat);
104105
bool next_song(bool repeat);
105106
bool prev_album();
106107
bool next_album(bool repeat);
@@ -159,7 +160,7 @@ class PlaylistData
159160
PosChange shuffle_pos_after(int ref_pos, bool by_album) const;
160161
PosChange shuffle_pos_random(bool repeat, bool by_album) const;
161162

162-
int pos_before(int ref_pos, bool shuffle) const;
163+
int pos_before(int ref_pos, bool shuffle, bool repeat) const;
163164
PosChange pos_after(int ref_pos, bool shuffle, bool by_album) const;
164165
PosChange pos_new(bool repeat, bool shuffle, bool by_album,
165166
int hint_pos) const;

src/libaudcore/playlist.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,10 @@ EXPORT bool Playlist::prev_song() const
659659
{
660660
SIMPLE_WRAPPER(bool, false, prev_song);
661661
}
662+
EXPORT bool Playlist::prev_song(bool repeat) const
663+
{
664+
SIMPLE_WRAPPER(bool, false, prev_song, repeat);
665+
}
662666
EXPORT bool Playlist::prev_album() const
663667
{
664668
SIMPLE_WRAPPER(bool, false, prev_album);

src/libaudcore/playlist.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@ 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

0 commit comments

Comments
 (0)