feat: shuffle play an album, artist or playlist with a long press
Holding South on a row, or shift+s on the keyboard, plays its tracks in random order with track shuffle on, so turning shuffle off goes back to their own order. On a song row it shuffles the page the song is on. An artist plays every track of every album, fetched a few albums at a time. South now acts on release, so a long press can mean something else. A long press on a row with nothing to shuffle activates as before.
This commit is contained in:
+36
-2
@@ -27,6 +27,8 @@ const REPEAT_DELAY: Duration = Duration::from_millis(380);
|
||||
const REPEAT_SLOW: Duration = Duration::from_millis(110);
|
||||
const REPEAT_FAST: Duration = Duration::from_millis(45);
|
||||
const FAST_AFTER: Duration = Duration::from_millis(1500);
|
||||
/// How long South is held before it counts as a long press.
|
||||
const LONG_PRESS: Duration = Duration::from_millis(500);
|
||||
const STICK_ON: f32 = 0.55;
|
||||
const STICK_OFF: f32 = 0.35;
|
||||
|
||||
@@ -37,6 +39,14 @@ enum Source {
|
||||
RightStick,
|
||||
}
|
||||
|
||||
/// South acts on release, so a long press can mean something else.
|
||||
struct SouthPress {
|
||||
family: Family,
|
||||
since: Instant,
|
||||
/// Sent as a long press already, so the release sends nothing.
|
||||
long: bool,
|
||||
}
|
||||
|
||||
struct Held {
|
||||
source: Source,
|
||||
action: Action,
|
||||
@@ -66,6 +76,7 @@ pub fn spawn_gamepad(emit: impl Fn(PadEvent) + Send + 'static) {
|
||||
}
|
||||
emit(PadEvent::Present(present(&gilrs, &mut families)));
|
||||
let mut held: Option<Held> = None;
|
||||
let mut south: Option<SouthPress> = None;
|
||||
loop {
|
||||
while let Some(ev) = gilrs.next_event_blocking(Some(Duration::from_millis(15))) {
|
||||
if matches!(ev.event, EventType::Connected | EventType::Disconnected) {
|
||||
@@ -77,6 +88,20 @@ pub fn spawn_gamepad(emit: impl Fn(PadEvent) + Send + 'static) {
|
||||
}
|
||||
let family = families.family(&gilrs, ev.id);
|
||||
match ev.event {
|
||||
EventType::ButtonPressed(Button::South, _) => {
|
||||
south = Some(SouthPress {
|
||||
family,
|
||||
since: Instant::now(),
|
||||
long: false,
|
||||
});
|
||||
}
|
||||
EventType::ButtonReleased(Button::South, _) => {
|
||||
if let Some(p) = south.take()
|
||||
&& !p.long
|
||||
{
|
||||
emit(PadEvent::Action(Action::Activate, p.family));
|
||||
}
|
||||
}
|
||||
EventType::ButtonPressed(b, _) => {
|
||||
if let Some(a) = dpad_action(b) {
|
||||
held = Some(start_hold(Source::DPad, a, family));
|
||||
@@ -108,10 +133,20 @@ pub fn spawn_gamepad(emit: impl Fn(PadEvent) + Send + 'static) {
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
EventType::Disconnected => held = None,
|
||||
EventType::Disconnected => {
|
||||
held = None;
|
||||
south = None;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if let Some(p) = south.as_mut()
|
||||
&& !p.long
|
||||
&& p.since.elapsed() >= LONG_PRESS
|
||||
{
|
||||
p.long = true;
|
||||
emit(PadEvent::Action(Action::HoldActivate, p.family));
|
||||
}
|
||||
if let Some(h) = held.as_mut() {
|
||||
let now = Instant::now();
|
||||
if now >= h.next {
|
||||
@@ -216,7 +251,6 @@ fn dpad_action(b: Button) -> Option<Action> {
|
||||
|
||||
fn button_action(b: Button) -> Option<Action> {
|
||||
Some(match b {
|
||||
Button::South => Action::Activate,
|
||||
Button::East => Action::Back,
|
||||
Button::West => Action::PlayPause,
|
||||
Button::North => Action::Queue,
|
||||
|
||||
Reference in New Issue
Block a user