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:
1 parent
02469aa7b2
commit
49fe9e6b11
5 files changed
+440
No files matched your search
@@ -10,6 +10,7 @@ pub mod favorites;
|
|||||||
pub mod lastfm;
|
pub mod lastfm;
|
||||||
pub mod notification;
|
pub mod notification;
|
||||||
pub mod playback;
|
pub mod playback;
|
||||||
|
pub mod playback_context;
|
||||||
pub mod playlist;
|
pub mod playlist;
|
||||||
pub mod playlist_import;
|
pub mod playlist_import;
|
||||||
pub mod queue;
|
pub mod queue;
|
||||||
@@ -24,6 +25,7 @@ pub use favorites::*;
|
|||||||
pub use lastfm::*;
|
pub use lastfm::*;
|
||||||
pub use notification::*;
|
pub use notification::*;
|
||||||
pub use playback::*;
|
pub use playback::*;
|
||||||
|
pub use playback_context::*;
|
||||||
pub use playlist::*;
|
pub use playlist::*;
|
||||||
pub use playlist_import::*;
|
pub use playlist_import::*;
|
||||||
pub use queue::*;
|
pub use queue::*;
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
//! Playback context commands
|
||||||
|
|
||||||
|
use tauri::State;
|
||||||
|
use crate::playback_context::{PlaybackContext, ContextType, ContentSource};
|
||||||
|
use crate::AppState;
|
||||||
|
|
||||||
|
/// Get the current playback context
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn get_playback_context(state: State<'_, AppState>) -> Option<PlaybackContext> {
|
||||||
|
state.context.get_context()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set a new playback context
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn set_playback_context(
|
||||||
|
context_type: String,
|
||||||
|
id: String,
|
||||||
|
label: String,
|
||||||
|
source: String,
|
||||||
|
track_ids: Vec<u64>,
|
||||||
|
start_position: usize,
|
||||||
|
state: State<'_, AppState>,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
// Parse context type
|
||||||
|
let ctx_type = match context_type.as_str() {
|
||||||
|
"album" => ContextType::Album,
|
||||||
|
"playlist" => ContextType::Playlist,
|
||||||
|
"artist_top" => ContextType::ArtistTop,
|
||||||
|
"home_list" => ContextType::HomeList,
|
||||||
|
"favorites" => ContextType::Favorites,
|
||||||
|
_ => return Err(format!("Invalid context type: {}", context_type)),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Parse source
|
||||||
|
let content_source = match source.as_str() {
|
||||||
|
"qobuz" => ContentSource::Qobuz,
|
||||||
|
"local" => ContentSource::Local,
|
||||||
|
_ => return Err(format!("Invalid source: {}", source)),
|
||||||
|
};
|
||||||
|
|
||||||
|
let context = PlaybackContext::new(
|
||||||
|
ctx_type,
|
||||||
|
id,
|
||||||
|
label,
|
||||||
|
content_source,
|
||||||
|
track_ids,
|
||||||
|
start_position,
|
||||||
|
);
|
||||||
|
|
||||||
|
state.context.set_context(context);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear the current playback context
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn clear_playback_context(state: State<'_, AppState>) {
|
||||||
|
state.context.clear_context();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check if a playback context is active
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn has_playback_context(state: State<'_, AppState>) -> bool {
|
||||||
|
state.context.has_context()
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@ pub mod lyrics;
|
|||||||
pub mod media_controls;
|
pub mod media_controls;
|
||||||
pub mod network;
|
pub mod network;
|
||||||
pub mod offline;
|
pub mod offline;
|
||||||
|
pub mod playback_context;
|
||||||
pub mod player;
|
pub mod player;
|
||||||
pub mod playlist_import;
|
pub mod playlist_import;
|
||||||
pub mod queue;
|
pub mod queue;
|
||||||
@@ -35,6 +36,7 @@ use api::QobuzClient;
|
|||||||
use cache::{AudioCache, PlaybackCache};
|
use cache::{AudioCache, PlaybackCache};
|
||||||
use lastfm::LastFmClient;
|
use lastfm::LastFmClient;
|
||||||
use media_controls::{MediaControlsManager, TrackInfo};
|
use media_controls::{MediaControlsManager, TrackInfo};
|
||||||
|
use playback_context::ContextManager;
|
||||||
use player::Player;
|
use player::Player;
|
||||||
use queue::QueueManager;
|
use queue::QueueManager;
|
||||||
use share::SongLinkClient;
|
use share::SongLinkClient;
|
||||||
@@ -44,6 +46,7 @@ pub struct AppState {
|
|||||||
pub client: Arc<Mutex<QobuzClient>>,
|
pub client: Arc<Mutex<QobuzClient>>,
|
||||||
pub player: Player,
|
pub player: Player,
|
||||||
pub queue: QueueManager,
|
pub queue: QueueManager,
|
||||||
|
pub context: ContextManager,
|
||||||
pub media_controls: MediaControlsManager,
|
pub media_controls: MediaControlsManager,
|
||||||
pub audio_cache: Arc<AudioCache>,
|
pub audio_cache: Arc<AudioCache>,
|
||||||
pub lastfm: Arc<Mutex<LastFmClient>>,
|
pub lastfm: Arc<Mutex<LastFmClient>>,
|
||||||
@@ -83,6 +86,7 @@ impl AppState {
|
|||||||
client: Arc::new(Mutex::new(QobuzClient::default())),
|
client: Arc::new(Mutex::new(QobuzClient::default())),
|
||||||
player: Player::new(device_name, audio_settings),
|
player: Player::new(device_name, audio_settings),
|
||||||
queue: QueueManager::new(),
|
queue: QueueManager::new(),
|
||||||
|
context: ContextManager::new(),
|
||||||
media_controls: MediaControlsManager::new(),
|
media_controls: MediaControlsManager::new(),
|
||||||
audio_cache,
|
audio_cache,
|
||||||
lastfm: Arc::new(Mutex::new(LastFmClient::default())),
|
lastfm: Arc::new(Mutex::new(LastFmClient::default())),
|
||||||
@@ -352,6 +356,11 @@ pub fn run() {
|
|||||||
commands::set_repeat,
|
commands::set_repeat,
|
||||||
commands::get_repeat,
|
commands::get_repeat,
|
||||||
commands::get_queue_state,
|
commands::get_queue_state,
|
||||||
|
// Playback context commands
|
||||||
|
commands::get_playback_context,
|
||||||
|
commands::set_playback_context,
|
||||||
|
commands::clear_playback_context,
|
||||||
|
commands::has_playback_context,
|
||||||
// Playlist commands
|
// Playlist commands
|
||||||
commands::get_user_playlists,
|
commands::get_user_playlists,
|
||||||
commands::get_playlist,
|
commands::get_playlist,
|
||||||
|
|||||||
@@ -0,0 +1,213 @@
|
|||||||
|
//! Playback context module
|
||||||
|
//!
|
||||||
|
//! Manages the semantic origin of playback to determine what "next" means.
|
||||||
|
//! A playback context is NOT the queue - it's the source that defines
|
||||||
|
//! the boundaries and continuation behavior for playback.
|
||||||
|
//!
|
||||||
|
//! Examples:
|
||||||
|
//! - Album: next track in the album
|
||||||
|
//! - Playlist: next track in the playlist
|
||||||
|
//! - Artist Top Songs: next ranked track
|
||||||
|
//! - Single track: no context, playback ends when track finishes
|
||||||
|
|
||||||
|
use std::sync::Mutex;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// Type of playback context
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum ContextType {
|
||||||
|
Album,
|
||||||
|
Playlist,
|
||||||
|
ArtistTop,
|
||||||
|
HomeList,
|
||||||
|
Favorites,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Source of the content (Qobuz or Local Library)
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum ContentSource {
|
||||||
|
Qobuz,
|
||||||
|
Local,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Playback context - the semantic origin of playback
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct PlaybackContext {
|
||||||
|
/// Type of context (album, playlist, etc.)
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
pub context_type: ContextType,
|
||||||
|
/// Unique identifier for this context
|
||||||
|
pub id: String,
|
||||||
|
/// Human-readable label
|
||||||
|
pub label: String,
|
||||||
|
/// Source of content
|
||||||
|
pub source: ContentSource,
|
||||||
|
/// Original track list for this context (track IDs)
|
||||||
|
pub track_ids: Vec<u64>,
|
||||||
|
/// Current position in the context
|
||||||
|
pub current_position: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PlaybackContext {
|
||||||
|
/// Create a new playback context
|
||||||
|
pub fn new(
|
||||||
|
context_type: ContextType,
|
||||||
|
id: String,
|
||||||
|
label: String,
|
||||||
|
source: ContentSource,
|
||||||
|
track_ids: Vec<u64>,
|
||||||
|
start_position: usize,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
context_type,
|
||||||
|
id,
|
||||||
|
label,
|
||||||
|
source,
|
||||||
|
track_ids,
|
||||||
|
current_position: start_position,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the next track ID in this context (if any)
|
||||||
|
pub fn next_track_id(&self) -> Option<u64> {
|
||||||
|
let next_pos = self.current_position + 1;
|
||||||
|
if next_pos < self.track_ids.len() {
|
||||||
|
self.track_ids.get(next_pos).copied()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get multiple upcoming track IDs in this context
|
||||||
|
pub fn upcoming_track_ids(&self, count: usize) -> Vec<u64> {
|
||||||
|
let start_pos = self.current_position + 1;
|
||||||
|
self.track_ids
|
||||||
|
.iter()
|
||||||
|
.skip(start_pos)
|
||||||
|
.take(count)
|
||||||
|
.copied()
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Advance to the next track in context
|
||||||
|
pub fn advance(&mut self) -> bool {
|
||||||
|
let next_pos = self.current_position + 1;
|
||||||
|
if next_pos < self.track_ids.len() {
|
||||||
|
self.current_position = next_pos;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check if context has more tracks
|
||||||
|
pub fn has_next(&self) -> bool {
|
||||||
|
self.current_position + 1 < self.track_ids.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get total tracks in context
|
||||||
|
pub fn total_tracks(&self) -> usize {
|
||||||
|
self.track_ids.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get context info for display
|
||||||
|
pub fn display_info(&self) -> String {
|
||||||
|
let type_str = match self.context_type {
|
||||||
|
ContextType::Album => "Album",
|
||||||
|
ContextType::Playlist => "Playlist",
|
||||||
|
ContextType::ArtistTop => "Artist Top Songs",
|
||||||
|
ContextType::HomeList => "Home List",
|
||||||
|
ContextType::Favorites => "Favorites",
|
||||||
|
};
|
||||||
|
format!("{} · {}", type_str, self.label)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Manager for the current playback context
|
||||||
|
pub struct ContextManager {
|
||||||
|
current: Mutex<Option<PlaybackContext>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ContextManager {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ContextManager {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
current: Mutex::new(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set the current playback context
|
||||||
|
pub fn set_context(&self, context: PlaybackContext) {
|
||||||
|
let mut current = self.current.lock().unwrap();
|
||||||
|
*current = Some(context);
|
||||||
|
log::info!("Playback context set: {:?}", current.as_ref().map(|c| &c.label));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear the current context (for single track playback)
|
||||||
|
pub fn clear_context(&self) {
|
||||||
|
let mut current = self.current.lock().unwrap();
|
||||||
|
*current = None;
|
||||||
|
log::info!("Playback context cleared");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the current context (cloned)
|
||||||
|
pub fn get_context(&self) -> Option<PlaybackContext> {
|
||||||
|
self.current.lock().unwrap().clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check if a context is active
|
||||||
|
pub fn has_context(&self) -> bool {
|
||||||
|
self.current.lock().unwrap().is_some()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the next track ID from the current context
|
||||||
|
pub fn next_track_id(&self) -> Option<u64> {
|
||||||
|
self.current
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|ctx| ctx.next_track_id())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get multiple upcoming track IDs from the current context
|
||||||
|
pub fn upcoming_track_ids(&self, count: usize) -> Vec<u64> {
|
||||||
|
self.current
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.as_ref()
|
||||||
|
.map(|ctx| ctx.upcoming_track_ids(count))
|
||||||
|
.unwrap_or_default()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Advance the context to the next track
|
||||||
|
pub fn advance_context(&self) -> bool {
|
||||||
|
let mut current = self.current.lock().unwrap();
|
||||||
|
if let Some(ctx) = current.as_mut() {
|
||||||
|
let advanced = ctx.advance();
|
||||||
|
if !advanced {
|
||||||
|
log::info!("Playback context ended (no more tracks)");
|
||||||
|
}
|
||||||
|
advanced
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update current track position in context (when user seeks within context)
|
||||||
|
pub fn set_position(&self, track_id: u64) {
|
||||||
|
let mut current = self.current.lock().unwrap();
|
||||||
|
if let Some(ctx) = current.as_mut() {
|
||||||
|
if let Some(pos) = ctx.track_ids.iter().position(|&id| id == track_id) {
|
||||||
|
ctx.current_position = pos;
|
||||||
|
log::debug!("Context position updated to {}", pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in new issue
Block a user