diff --git a/ui/browsing/playlistpage.go b/ui/browsing/playlistpage.go index 01221d0a..c330961c 100644 --- a/ui/browsing/playlistpage.go +++ b/ui/browsing/playlistpage.go @@ -208,15 +208,7 @@ func (a *PlaylistPage) doSetNewTrackOrder(ids []string, newPos int) { } func (a *PlaylistPage) onRemoveSelectedFromPlaylist() { - ids := a.tracklist.SelectedTrackIDs() - sel := sharedutil.ToSet(ids) - idxs := make([]int, 0, len(sel)) - for i, tr := range a.tracks { - if _, ok := sel[tr.ID]; ok { - idxs = append(idxs, i) - } - } - a.sm.Server.RemovePlaylistTracks(a.playlistID, idxs) + a.sm.Server.RemovePlaylistTracks(a.playlistID, a.tracklist.SelectedTrackIndexes()) a.tracklist.UnselectAll() a.Reload() } diff --git a/ui/widgets/tracklist.go b/ui/widgets/tracklist.go index 96e2d82d..450cc578 100644 --- a/ui/widgets/tracklist.go +++ b/ui/widgets/tracklist.go @@ -692,6 +692,30 @@ func (t *Tracklist) SelectedTrackIDs() []string { return util.SelectedItemIDs(t.tracks) } +// SelectedTrackIndexes returns the indexes of the selected tracks in the +// original sort order (ie if tracklist is sorted by some column), the indexes +// returned will correspond to the order of tracks when the list was initialized. +func (t *Tracklist) SelectedTrackIndexes() []int { + t.tracksMutex.RLock() + defer t.tracksMutex.RUnlock() + + if t.sorting.SortOrder == SortNone { + idx := -1 + return sharedutil.FilterMapSlice(t.tracks, func(t *util.TrackListModel) (int, bool) { + idx++ + return idx, t.Selected + }) + } + ids := sharedutil.ToSet(util.SelectedItemIDs(t.tracks)) + idxs := make([]int, 0, len(ids)) + for id := range ids { + idxs = append(idxs, slices.IndexFunc(t.tracks, func(t *util.TrackListModel) bool { + return t.Item.Metadata().ID == id + })) + } + return idxs +} + func (t *Tracklist) lenTracks() int { t.tracksMutex.RLock() defer t.tracksMutex.RUnlock()