Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change: Don't change music track when toggling shuffle mode. #11504

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/music_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ struct MusicSystem {
void PlaylistClear();

private:
void SetPositionBySetIndex(uint set_index);
void ChangePlaylistPosition(int ofs);
int playlist_position;

Expand Down Expand Up @@ -184,30 +185,47 @@ void MusicSystem::ChangeMusicSet(const std::string &set_name)
InvalidateWindowData(WC_MUSIC_WINDOW, 0, 1, true);
}

/** Enable shuffle mode and restart playback */
/**
* Set playlist position by set index.
* @param set_index Set index to select.
*/
void MusicSystem::SetPositionBySetIndex(uint set_index)
{
auto it = std::find_if(std::begin(this->active_playlist), std::end(this->active_playlist), [&set_index](const PlaylistEntry &ple) { return ple.set_index == set_index; });
if (it != std::end(this->active_playlist)) this->playlist_position = std::distance(std::begin(this->active_playlist), it);
}

/**
* Enable shuffle mode.
*/
void MusicSystem::Shuffle()
{
_settings_client.music.shuffle = true;

uint set_index = this->active_playlist[this->playlist_position].set_index;
this->active_playlist = this->displayed_playlist;
for (size_t i = 0; i < this->active_playlist.size(); i++) {
size_t shuffle_index = InteractiveRandom() % (this->active_playlist.size() - i);
std::swap(this->active_playlist[i], this->active_playlist[i + shuffle_index]);
}
this->SetPositionBySetIndex(set_index);

if (_settings_client.music.playing) this->Play();

InvalidateWindowData(WC_MUSIC_TRACK_SELECTION, 0);
InvalidateWindowData(WC_MUSIC_WINDOW, 0);
}

/** Disable shuffle and restart playback */
/**
* Disable shuffle mode.
*/
void MusicSystem::Unshuffle()
{
_settings_client.music.shuffle = false;
this->active_playlist = this->displayed_playlist;

if (_settings_client.music.playing) this->Play();
uint set_index = this->active_playlist[this->playlist_position].set_index;
this->active_playlist = this->displayed_playlist;
this->SetPositionBySetIndex(set_index);

InvalidateWindowData(WC_MUSIC_TRACK_SELECTION, 0);
InvalidateWindowData(WC_MUSIC_WINDOW, 0);
}

Expand Down