feat(player): implement playback context system

- Add PlaybackContext module with ContextManager
- Context tracks semantic origin (album, playlist, artist top, etc.)
- Separate from queue - defines what 'next' means
- Backend: Rust struct with track IDs and position tracking
- Frontend: Svelte store with reactive subscriptions
- Tauri commands for context CRUD operations
- Context is nullable (single track playback has no context)

cp
This commit is contained in:
vicrodh committed 2026-01-18 00:26:54 -06:00
1 parent 02469aa7b2
commit 49fe9e6b11
5 files changed
+440

No files matched your search

+152
View File
@@ -0,0 +1,152 @@
/**
* Playback Context Store
*
* Manages the semantic origin of playback (album, playlist, artist top, etc.)
* Determines what "next" means and displays the stack icon.
*/
import { invoke } from '@tauri-apps/api/core';
// ============ Types ============
export type ContextType = 'album' | 'playlist' | 'artist_top' | 'home_list' | 'favorites';
export type ContentSource = 'qobuz' | 'local';
export interface PlaybackContext {
type: ContextType;
id: string;
label: string;
source: ContentSource;
track_ids: number[];
current_position: number;
}
// ============ State ============
let currentContext: PlaybackContext | null = null;
const listeners = new Set<() => void>();
function notifyListeners(): void {
for (const listener of listeners) {
listener();
}
}
// ============ Public API ============
/**
* Set a playback context (e.g., when playing from album/playlist)
*/
export async function setPlaybackContext(
contextType: ContextType,
id: string,
label: string,
source: ContentSource,
trackIds: number[],
startPosition: number
): Promise<void> {
await invoke('set_playback_context', {
contextType,
id,
label,
source,
trackIds,
startPosition
});
// Update local state
currentContext = {
type: contextType,
id,
label,
source,
track_ids: trackIds,
current_position: startPosition
};
notifyListeners();
}
/**
* Clear the playback context (e.g., when playing single track)
*/
export async function clearPlaybackContext(): Promise<void> {
await invoke('clear_playback_context');
currentContext = null;
notifyListeners();
}
/**
* Get the current playback context
*/
export async function getPlaybackContext(): Promise<PlaybackContext | null> {
const context = await invoke<PlaybackContext | null>('get_playback_context');
currentContext = context;
notifyListeners();
return context;
}
/**
* Check if a context is active
*/
export async function hasPlaybackContext(): Promise<boolean> {
return await invoke<boolean>('has_playback_context');
}
/**
* Get the current context (cached, no backend call)
*/
export function getCurrentContext(): PlaybackContext | null {
return currentContext;
}
/**
* Get display info for the current context
*/
export function getContextDisplayInfo(): string | null {
if (!currentContext) return null;
const typeStr = {
album: 'Album',
playlist: 'Playlist',
artist_top: 'Artist Top Songs',
home_list: 'Home List',
favorites: 'Favorites'
}[currentContext.type];
return `${typeStr} · ${currentContext.label}`;
}
/**
* Subscribe to context changes
*/
export function subscribe(listener: () => void): () => void {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
}
/**
* Initialize context store (call on app startup)
*/
export async function initPlaybackContextStore(): Promise<void> {
// Sync initial state from backend
await getPlaybackContext();
}
// Export a reactive getter for Svelte components
export function usePlaybackContext() {
let context = currentContext;
const unsubscribe = subscribe(() => {
context = currentContext;
});
return {
get context() {
return context;
},
unsubscribe
};
}