Stable shuffle wrap-around, keyboard shortcuts, window/sidebar size memory, cleanups

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
astar76 2026-10-24 14:48:52 -05:00
commit f793fe2695
6 changed files with 81 additions and 25 deletions

View file

@ -1,3 +1,4 @@
#![allow(deprecated)]
pub mod app;
pub mod bus;
pub mod cd;

View file

@ -133,6 +133,7 @@ pub struct Playlist {
undo: Vec<Vec<Entry>>,
redo: Vec<Vec<Entry>>,
shuffle: Vec<u64>,
wrap: Option<(Option<u64>, Vec<u64>)>,
pub queue_pos: std::collections::HashMap<u64, usize>,
pub sort: Option<(usize, bool)>,
}
@ -154,6 +155,7 @@ impl Playlist {
undo: vec![],
redo: vec![],
shuffle: vec![],
wrap: None,
queue_pos: Default::default(),
sort: None,
}
@ -371,33 +373,50 @@ impl Playlist {
pub fn reset_shuffle(&mut self) {
self.shuffle.clear();
self.wrap = None;
}
pub fn next_uid(&mut self, shuffle: bool, repeat: &str) -> Option<u64> {
if self.entries.is_empty() {
return None;
}
let mut seq: Vec<u64> = if shuffle {
self.ensure_shuffle();
self.shuffle.clone()
} else {
self.entries.iter().map(|e| e.uid).collect()
};
let idx = self.current.and_then(|c| seq.iter().position(|u| *u == c));
let mut nxt = idx.map(|i| i + 1).unwrap_or(0);
if nxt >= seq.len() {
if repeat == "playlist" {
if shuffle {
self.shuffle.clear();
self.ensure_shuffle();
seq = self.shuffle.clone();
if shuffle {
// a reshuffle made when the list wrapped becomes the active order once its first track plays
if let Some((_, order)) = &self.wrap {
if self.current == order.first().copied() {
self.shuffle = order.clone();
self.wrap = None;
}
nxt = 0;
} else {
return None;
}
self.ensure_shuffle();
}
let seq: Vec<u64> = if shuffle { self.shuffle.clone() } else { self.entries.iter().map(|e| e.uid).collect() };
let idx = self.current.and_then(|c| seq.iter().position(|u| *u == c));
let nxt = idx.map(|i| i + 1).unwrap_or(0);
if nxt < seq.len() {
return Some(seq[nxt]);
}
if repeat != "playlist" {
return None;
}
if !shuffle {
return seq.first().copied();
}
// wrapped around while shuffling: pick a fresh order once (stable across repeated peeks)
let cur = self.current;
if let Some((c, order)) = &self.wrap {
if *c == cur {
return order.first().copied();
}
}
seq.get(nxt).copied()
let mut order: Vec<u64> = self.entries.iter().map(|e| e.uid).collect();
order.shuffle(&mut rand::rng());
if order.len() > 1 && order.first().copied() == cur {
order.swap(0, 1);
}
let first = order.first().copied();
self.wrap = Some((cur, order));
first
}
pub fn prev_uid(&self, shuffle: bool) -> Option<u64> {
@ -737,3 +756,47 @@ impl Manager {
format!("{n} track{} · {}", if n != 1 { "s" } else { "" }, crate::config::fmt_time_h(p.total_length(), true))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn pl(n: usize) -> Playlist {
let songs = (0..n).map(|i| Song::new(format!("/music/{i}.flac"))).collect();
Playlist::new("t", songs, -1, false)
}
#[test]
fn sequential_and_wrap() {
let mut p = pl(3);
let ids: Vec<u64> = p.entries.iter().map(|e| e.uid).collect();
p.current = Some(ids[0]);
assert_eq!(p.next_uid(false, "off"), Some(ids[1]));
p.current = Some(ids[2]);
assert_eq!(p.next_uid(false, "off"), None);
assert_eq!(p.next_uid(false, "playlist"), Some(ids[0]));
}
#[test]
fn shuffle_visits_everything_each_cycle_and_peeks_are_stable() {
let mut p = pl(6);
let all: std::collections::HashSet<u64> = p.entries.iter().map(|e| e.uid).collect();
p.current = Some(p.entries[0].uid);
for _cycle in 0..4 {
let mut seen = std::collections::HashSet::new();
seen.insert(p.current.unwrap());
for _ in 0..all.len() - 1 {
let a = p.next_uid(true, "playlist").unwrap();
assert_eq!(a, p.next_uid(true, "playlist").unwrap(), "peek must be stable");
assert!(seen.insert(a), "track repeated inside one cycle");
p.current = Some(a);
}
assert_eq!(seen, all);
// wrap: the next track must differ from the one that just played and stay stable
let w = p.next_uid(true, "playlist").unwrap();
assert_eq!(w, p.next_uid(true, "playlist").unwrap());
assert_ne!(Some(w), p.current);
p.current = Some(w);
}
}
}

View file

@ -13,7 +13,6 @@ use crate::song::Song;
use crate::tags::{self, Field};
use crate::theme::with_alpha;
use eframe::egui::{self, Align2, Color32, Context, Id, Pos2, Rect, RichText, Sense, TextureHandle, Ui, Vec2};
use std::sync::Arc;
pub const EQ_PRESETS: &[(&str, [f64; 10])] = &[
("Flat", [0.0; 10]),

View file

@ -7,7 +7,6 @@ use crate::app::App;
use crate::config::{fmt_time, settings};
use crate::online::parse_lrc;
use crate::song::Song;
use crate::spectrum::BANDS;
use crate::theme::{fade, mix, with_alpha};
use eframe::egui::{self, Align2, Color32, Id, Mesh, Pos2, Rect, Sense, Stroke, Ui, Vec2};
use std::time::Instant;

View file

@ -1,6 +1,5 @@
//! Top-level window layout: menu bar, sidebar, hero, playlists, bottom bar and overlays.
use super::gfx::*;
use super::widgets::*;
use super::{hero, menus, playlist_view, sidebar};
use crate::app::App;

View file

@ -343,11 +343,6 @@ fn draw_toolbar(app: &mut App, ui: &mut Ui) {
}
// ---------------------------------------------------------------------- list
struct RowInfo {
uid: u64,
song: Song,
}
fn draw_list(app: &mut App, ui: &mut Ui) {
let avail = ui.available_rect_before_wrap();
let list_rect = Rect::from_min_max(avail.min + Vec2::new(2.0, 2.0), avail.max);