refactor(web): the play bar and Now Playing share one set of transport rules (DRY W1)

PlayerBar and the Now Playing page each carried their own copy of when
Previous and Next are enabled, the repeat label, the volume icon and the
range-input handlers, and the 3-second "restart rather than go back"
threshold was written in both and again in store.skipPrev.
lib/player/controls.ts now holds them once (canSkipPrev, canSkipNext,
repeatLabel, volumeIcon, inputNumber, RESTART_THRESHOLD_SEC), and the store
reads its threshold from there. Behaviour is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-27 03:06:36 -05:00
co-authored by Claude Opus 5.5
parent 02cecc4eb7
commit 14afd86b8e
5 changed files with 78 additions and 50 deletions
+9 -25
View File
@@ -1,8 +1,8 @@
<script lang="ts"> <script lang="ts">
import { canSkipPrev, canSkipNext, repeatLabel, volumeIcon, inputNumber } from '#lib/player/controls.js';
import { import {
SkipBack, SkipForward, Play, Pause, Loader2, SkipBack, SkipForward, Play, Pause, Loader2,
Shuffle, Repeat, Repeat1, Shuffle, Repeat, Repeat1,
Volume2, Volume1, VolumeX,
ListMusic ListMusic
} from 'lucide-svelte'; } from 'lucide-svelte';
import { import {
@@ -25,29 +25,13 @@
// Reset on track/play-state change via the helper's $effect. // Reset on track/play-state change via the helper's $effect.
const smoothed = useSmoothPosition(); const smoothed = useSmoothPosition();
const skipPrevDisabled = $derived(player.index === 0 && player.position < 3); const skipPrevDisabled = $derived(!canSkipPrev(player));
const skipNextDisabled = $derived( const skipNextDisabled = $derived(!canSkipNext(player));
player.index === player.queue.length - 1 && player.repeat === 'off' const repeatText = $derived(repeatLabel(player.repeat));
); const VolumeIcon = $derived(volumeIcon(player.volume));
const repeatLabel = $derived( const onSeekInput = (e: Event) => seekTo(inputNumber(e));
player.repeat === 'off' ? 'Repeat off' : const onVolumeInput = (e: Event) => setVolume(inputNumber(e));
player.repeat === 'all' ? 'Repeat all' : 'Repeat one'
);
const VolumeIcon = $derived(
player.volume === 0 ? VolumeX :
player.volume < 0.5 ? Volume1 : Volume2
);
function onSeekInput(e: Event) {
const v = Number((e.currentTarget as HTMLInputElement).value);
seekTo(v);
}
function onVolumeInput(e: Event) {
const v = Number((e.currentTarget as HTMLInputElement).value);
setVolume(v);
}
function onCoverError(e: Event) { function onCoverError(e: Event) {
(e.currentTarget as HTMLImageElement).src = FALLBACK_COVER; (e.currentTarget as HTMLImageElement).src = FALLBACK_COVER;
} }
@@ -191,7 +175,7 @@
</button> </button>
<button <button
type="button" type="button"
aria-label={repeatLabel} aria-label={repeatText}
onclick={cycleRepeat} onclick={cycleRepeat}
class="flex h-8 w-8 items-center justify-center rounded-full transition-colors {player.repeat !== 'off' class="flex h-8 w-8 items-center justify-center rounded-full transition-colors {player.repeat !== 'off'
? 'bg-accent-tint text-accent-fg' ? 'bg-accent-tint text-accent-fg'
@@ -403,7 +387,7 @@
</button> </button>
<button <button
type="button" type="button"
aria-label={repeatLabel} aria-label={repeatText}
onclick={cycleRepeat} onclick={cycleRepeat}
class="flex h-9 w-9 items-center justify-center rounded-full transition-colors {player.repeat !== 'off' class="flex h-9 w-9 items-center justify-center rounded-full transition-colors {player.repeat !== 'off'
? 'bg-accent-tint text-accent-fg' ? 'bg-accent-tint text-accent-fg'
+24
View File
@@ -0,0 +1,24 @@
import { describe, expect, test } from 'vitest';
import { canSkipNext, canSkipPrev, repeatLabel, RESTART_THRESHOLD_SEC } from './controls';
const queue = ['a', 'b', 'c'];
describe('transport rules', () => {
test('Previous is off only on the first track before the restart threshold', () => {
expect(canSkipPrev({ index: 0, position: 0 })).toBe(false);
expect(canSkipPrev({ index: 0, position: RESTART_THRESHOLD_SEC })).toBe(true);
expect(canSkipPrev({ index: 1, position: 0 })).toBe(true);
});
test('Next is off at the end of the queue unless it wraps', () => {
expect(canSkipNext({ index: 1, queue, repeat: 'off' })).toBe(true);
expect(canSkipNext({ index: 2, queue, repeat: 'off' })).toBe(false);
expect(canSkipNext({ index: 2, queue, repeat: 'all' })).toBe(true);
});
test('repeat labels name each mode', () => {
expect(repeatLabel('off')).toBe('Repeat off');
expect(repeatLabel('all')).toBe('Repeat all');
expect(repeatLabel('one')).toBe('Repeat one');
});
});
+35
View File
@@ -0,0 +1,35 @@
import { Volume1, Volume2, VolumeX } from 'lucide-svelte';
import type { RepeatMode } from './store.svelte';
// The transport controls' rules, shared by every player surface (the play
// bar and Now Playing) so a button is enabled exactly when the store's
// action would do something.
/** Back within this many seconds of a track's start goes to the one
before; later, it restarts the track (store.skipPrev). */
export const RESTART_THRESHOLD_SEC = 3;
/** Whether Previous does anything: there is a track before this one, or
this one is far enough in to restart. */
export function canSkipPrev(p: { index: number; position: number }): boolean {
return !(p.index === 0 && p.position < RESTART_THRESHOLD_SEC);
}
/** Whether Next does anything: there is a track after this one, or the
queue wraps round. */
export function canSkipNext(p: { index: number; queue: readonly unknown[]; repeat: RepeatMode }): boolean {
return !(p.index === p.queue.length - 1 && p.repeat === 'off');
}
export function repeatLabel(mode: RepeatMode): string {
return mode === 'off' ? 'Repeat off' : mode === 'all' ? 'Repeat all' : 'Repeat one';
}
export function volumeIcon(volume: number): typeof Volume2 {
return volume === 0 ? VolumeX : volume < 0.5 ? Volume1 : Volume2;
}
/** The number a range input moved to. */
export function inputNumber(e: Event): number {
return Number((e.currentTarget as HTMLInputElement).value);
}
+2 -1
View File
@@ -1,5 +1,6 @@
import type { TrackRef, RadioResponse } from '#lib/api/types.js'; import type { TrackRef, RadioResponse } from '#lib/api/types.js';
import { playSource } from './playSource'; import { playSource } from './playSource';
import { RESTART_THRESHOLD_SEC } from './controls';
import { untrack } from 'svelte'; import { untrack } from 'svelte';
import { api } from '#lib/api/client.js'; import { api } from '#lib/api/client.js';
import { user } from '#lib/auth/user.svelte.js'; import { user } from '#lib/auth/user.svelte.js';
@@ -230,7 +231,7 @@ export function skipNext(): void {
export function skipPrev(): void { export function skipPrev(): void {
if (_queue.length === 0) return; if (_queue.length === 0) return;
if (_position < 3 && _index > 0) { if (_position < RESTART_THRESHOLD_SEC && _index > 0) {
_index--; _index--;
_position = 0; _position = 0;
_duration = 0; _duration = 0;
+8 -24
View File
@@ -1,8 +1,8 @@
<script lang="ts"> <script lang="ts">
import { canSkipPrev, canSkipNext, repeatLabel, volumeIcon, inputNumber } from '#lib/player/controls.js';
import { import {
ArrowLeft, SkipBack, SkipForward, Play, Pause, Loader2, ArrowLeft, SkipBack, SkipForward, Play, Pause, Loader2,
Shuffle, Repeat, Repeat1, Shuffle, Repeat, Repeat1,
Volume2, Volume1, VolumeX,
ListMusic ListMusic
} from 'lucide-svelte'; } from 'lucide-svelte';
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
@@ -27,20 +27,10 @@
// smoothly between server ticks. Shares the helper with PlayerBar. // smoothly between server ticks. Shares the helper with PlayerBar.
const smoothed = useSmoothPosition(); const smoothed = useSmoothPosition();
const skipPrevDisabled = $derived(player.index === 0 && player.position < 3); const skipPrevDisabled = $derived(!canSkipPrev(player));
const skipNextDisabled = $derived( const skipNextDisabled = $derived(!canSkipNext(player));
player.index === player.queue.length - 1 && player.repeat === 'off' const repeatText = $derived(repeatLabel(player.repeat));
); const VolumeIcon = $derived(volumeIcon(player.volume));
const repeatLabel = $derived(
player.repeat === 'off' ? 'Repeat off' :
player.repeat === 'all' ? 'Repeat all' : 'Repeat one'
);
const VolumeIcon = $derived(
player.volume === 0 ? VolumeX :
player.volume < 0.5 ? Volume1 : Volume2
);
// The backdrop takes the cover's dominant colour, as Android's Now // The backdrop takes the cover's dominant colour, as Android's Now
// Playing does. The colour is held until the next cover's // Playing does. The colour is held until the next cover's
@@ -58,14 +48,8 @@
}); });
}); });
function onSeekInput(e: Event) { const onSeekInput = (e: Event) => seekTo(inputNumber(e));
const v = Number((e.currentTarget as HTMLInputElement).value); const onVolumeInput = (e: Event) => setVolume(inputNumber(e));
seekTo(v);
}
function onVolumeInput(e: Event) {
const v = Number((e.currentTarget as HTMLInputElement).value);
setVolume(v);
}
function onCoverError(e: Event) { function onCoverError(e: Event) {
(e.currentTarget as HTMLImageElement).src = FALLBACK_COVER; (e.currentTarget as HTMLImageElement).src = FALLBACK_COVER;
} }
@@ -249,7 +233,7 @@
<button <button
type="button" type="button"
onclick={cycleRepeat} onclick={cycleRepeat}
aria-label={repeatLabel} aria-label={repeatText}
aria-pressed={player.repeat !== 'off'} aria-pressed={player.repeat !== 'off'}
class="flex items-center justify-center min-h-[44px] min-w-[44px] rounded class="flex items-center justify-center min-h-[44px] min-w-[44px] rounded
{player.repeat !== 'off' ? 'text-accent-fg' : 'text-text-secondary hover:text-text-primary'} {player.repeat !== 'off' ? 'text-accent-fg' : 'text-text-secondary hover:text-text-primary'}