Skip to main content

koan_core/player/
commands.rs

1use std::path::PathBuf;
2
3use crossbeam_channel::{Receiver, Sender, bounded};
4
5use super::state::{PlaylistItem, QueueItemId};
6
7/// Commands from the UI layer to the audio engine.
8#[derive(Debug)]
9pub enum PlayerCommand {
10    /// Set cursor + start playback. Replaces Play/SkipTo/SkipBack/PlayInterrupt.
11    Play(QueueItemId),
12    Pause,
13    Resume,
14    Stop,
15    Seek(u64), // position in ms
16    NextTrack,
17    PrevTrack,
18    AddToPlaylist(Vec<PlaylistItem>),
19    RemoveFromPlaylist(QueueItemId),
20    /// Batch remove: delete multiple items as a single undoable operation.
21    RemoveFromPlaylistBatch(Vec<QueueItemId>),
22    MoveInPlaylist {
23        id: QueueItemId,
24        target: QueueItemId,
25        after: bool,
26    },
27    /// Batch move: extract `ids` and reinsert them at `target` position.
28    MoveItemsInPlaylist {
29        ids: Vec<QueueItemId>,
30        target: QueueItemId,
31        after: bool,
32    },
33    /// Put the queue in exactly this order, keeping every item.
34    ///
35    /// A queue locked to a playlist follows it, and following a reorder means
36    /// moving the items that are already there — rebuilding them would issue
37    /// new ids and throw away what has played, what is mid-download and where
38    /// the cursor is. Ids not named keep their relative order at the end.
39    ReorderPlaylist(Vec<QueueItemId>),
40    /// Update file paths for playlist items after an organize operation.
41    /// On Unix, rename() doesn't invalidate open FDs so playback continues.
42    UpdatePaths(Vec<(QueueItemId, PathBuf)>),
43    /// Insert items after a specific queue item (for drag/drop at cursor position).
44    InsertInPlaylist {
45        items: Vec<PlaylistItem>,
46        after: QueueItemId,
47    },
48    /// Clear the entire playlist (stop + remove all items).
49    ClearPlaylist,
50    /// Replace the playlist and start playing at `start`, as one operation.
51    ///
52    /// Doing this as ClearPlaylist + AddToPlaylist + Play sends three commands
53    /// down a bounded channel, and the player acts on each as it arrives: the
54    /// first track starts, then the cursor jumps. Clicking track nine of an
55    /// album visibly flashed track one as playing first. It is also three undo
56    /// entries for one user action.
57    ///
58    /// `start` past the end starts at the beginning.
59    ReplacePlaylist {
60        items: Vec<PlaylistItem>,
61        start: usize,
62    },
63    /// Download complete — check if cursor is waiting on this item.
64    TrackReady(QueueItemId),
65    /// Enough data buffered for streaming playback — check if cursor is waiting.
66    TrackStreamReady(QueueItemId),
67    /// Download failed — a cursor parked on this item must stop waiting.
68    ///
69    /// Without it the player sits on a `Pending` item forever: `Ready` is the
70    /// only thing it listens for, and a track that cannot be fetched never
71    /// becomes Ready. That is the offline-library stall.
72    TrackFailed(QueueItemId),
73    /// Decode thread exhausted the playlist — auto-advance or stop.
74    DecodeFinished,
75    /// Undo the last reversible playlist operation.
76    Undo,
77    /// Redo the last undone operation.
78    Redo,
79    /// Begin collecting undo entries into a single batch (e.g. drag operations).
80    BeginUndoBatch,
81    /// End the batch — collapse collected entries into one undo step.
82    EndUndoBatch,
83    /// Switch output audio device by name. Restarts engine on current track.
84    SetOutputDevice(String),
85    /// Clear the configured output device, reverting to system default.
86    ClearOutputDevice,
87}
88
89/// Bounded SPSC command channel.
90///
91/// Small capacity — we don't want commands queuing up. If the engine is busy,
92/// the UI should know about it, not silently buffer 50 seeks.
93pub struct CommandChannel {
94    pub tx: Sender<PlayerCommand>,
95    pub rx: Receiver<PlayerCommand>,
96}
97
98impl Default for CommandChannel {
99    fn default() -> Self {
100        Self::new()
101    }
102}
103
104impl CommandChannel {
105    pub fn new() -> Self {
106        let (tx, rx) = bounded(16);
107        Self { tx, rx }
108    }
109}