Mulberry/src/bus.rs

108 lines
3.2 KiB
Rust

//! Messages from worker threads to the UI thread.
use crate::song::Song;
use eframe::egui;
use std::sync::mpsc::Sender;
pub enum Msg {
ScanProgress { done: usize, total: usize },
ScanFinished { added: usize, updated: usize, removed: usize },
/// Cover for the playing song, already scaled for the GPU (None = no art).
Art { ident: String, art: Option<Box<ArtData>>, path: String },
/// Lyrics text and source label ("loading", "error", "none:<label>", … are special sources).
Lyrics { token: u64, text: String, source: String },
ArtistInfo { artist: String, text: String },
Toast(String),
/// Track decoded for the visualizer (mono, TRACK_RATE).
Decoded { token: u64, samples: Vec<f32>, done: bool },
/// Songs gathered from paths/playlist files, ready to be inserted.
Added { job: AddJob, songs: Vec<Song> },
Resolved { token: u64, result: Result<crate::online::Resolved, String> },
/// Tag edit finished on a worker thread.
TagsWritten(Box<TagsDone>),
Engine(u8, crate::player::EngineEvent),
Mpris(MprisCmd),
/// A file/folder dialog finished (it runs on its own thread).
Picked { purpose: String, paths: Vec<String> },
Raise,
OpenArgs(Vec<String>),
CoverCandidates { token: u64, items: Result<Vec<crate::online::CoverCand>, String> },
CoverThumb { token: u64, idx: usize, image: Option<egui::ColorImage> },
CoverFull { token: u64, bytes: Result<Vec<u8>, String> },
MbResults { token: u64, items: Result<Vec<crate::online::MbRelease>, String> },
/// Result of a background job the UI shows as a toast.
Done { toast: String },
ConnectLastFm(Result<(String, String), String>),
}
#[derive(Clone, Debug, Default)]
pub struct AddJob {
pub pl: u64,
pub row: Option<usize>,
pub play: bool,
pub replace: bool,
}
pub struct TagsDone {
/// (song before the edit, song after the edit, old values of the changed fields)
pub ok: Vec<(Song, Song, Vec<(crate::tags::Field, String)>)>,
pub errors: Vec<String>,
pub label: String,
/// True when this was an undo (don't offer another undo).
pub is_undo: bool,
}
pub struct ArtData {
pub cover: egui::ColorImage,
pub blur: egui::ColorImage,
pub accent: Option<egui::Color32>,
}
#[derive(Clone, Debug)]
pub enum MprisCmd {
Play,
Pause,
PlayPause,
Stop,
Next,
Prev,
Seek(f64),
SetPos(f64),
SetVolume(f64),
Raise,
ToggleVisible,
Quit,
SetShuffle(bool),
SetLoop(String),
}
/// Channel to the UI thread. The egui context can be swapped, so the bus survives the window
/// being closed (background mode) and reopened.
#[derive(Clone)]
pub struct Bus {
pub tx: Sender<Msg>,
ctx: std::sync::Arc<std::sync::Mutex<egui::Context>>,
}
impl Bus {
pub fn new(tx: Sender<Msg>, ctx: egui::Context) -> Bus {
Bus { tx, ctx: std::sync::Arc::new(std::sync::Mutex::new(ctx)) }
}
pub fn set_ctx(&self, ctx: egui::Context) {
*self.ctx.lock().unwrap() = ctx;
}
pub fn repaint(&self) {
self.ctx.lock().unwrap().request_repaint();
}
pub fn send(&self, m: Msg) {
let _ = self.tx.send(m);
self.repaint();
}
pub fn toast(&self, s: impl Into<String>) {
self.send(Msg::Toast(s.into()));
}
}