Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.
Rust desktop-appdiscorddiscord-clienteguielectron-alternativelightweightlinuxmacosnative-apprustwindows
//! Settings → Experimental → Spotify Controller: a small player above the
//! user panel that controls your Spotify (the app on your PC, or whichever
//! device is playing) through Spotify Connect: play / pause, next, previous,
//! shuffle, repeat, the seek bar, volume, and your playlists. The music itself plays
//! in Spotify; RustCord only sends the commands (see `discord::spotify`).
use crate::app::RustCord;
use crate::backend::{Command, SpotifyRequest};
use crate::style::{icon_font, Theme};
use discord::spotify::{Command as Sp, Device, Player, Playlist};
use eframe::egui::{self, pos2, vec2, Align2, Color32, FontFamily, FontId, Order, Rect, ScrollArea, Sense, Stroke, Ui};
use std::time::{Duration, Instant};
/// Heights of the panel: playing, and the one-line "nothing playing" form.
const FULL: f32 = 112.0;
const SHORT: f32 = 56.0;
const GREEN: Color32 = Color32::from_rgb(0x1d, 0xb9, 0x54);
const PLAY: &[u8] = br#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="currentColor" d="M9.25 3.35C7.87 2.45 6 3.38 6 4.96v14.08c0 1.58 1.87 2.5 3.25 1.61l10.85-7.04a1.9 1.9 0 0 0 0-3.22L9.25 3.35Z"/></svg>"#;
const PAUSE: &[u8] = br#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="currentColor" d="M6 4a1 1 0 0 0-1 1v14a1 1 0 0 0 1 1h3a1 1 0 0 0 1-1V5a1 1 0 0 0-1-1H6ZM15 4a1 1 0 0 0-1 1v14a1 1 0 0 0 1 1h3a1 1 0 0 0 1-1V5a1 1 0 0 0-1-1h-3Z"/></svg>"#;
const NEXT: &[u8] = br#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="currentColor" d="M4.5 5.27v13.46a1 1 0 0 0 1.54.84l9.98-6.73a1 1 0 0 0 0-1.68L6.04 4.43a1 1 0 0 0-1.54.84Z"/><rect fill="currentColor" x="17" y="4" width="2.5" height="16" rx="1.25"/></svg>"#;
const PREVIOUS: &[u8] = br#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="currentColor" d="M19.5 5.27v13.46a1 1 0 0 1-1.54.84L7.98 12.84a1 1 0 0 1 0-1.68l9.98-6.73a1 1 0 0 1 1.54.84Z"/><rect fill="currentColor" x="4.5" y="4" width="2.5" height="16" rx="1.25"/></svg>"#;
const SHUFFLE: &[u8] = br#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 7h3c3.5 0 5 10 9.5 10H20"/><path d="M3 17h3c1.6 0 2.7-2 3.7-4.3"/><path d="M14.3 9.6C15.2 8.2 16.2 7 18 7h2"/><path d="m17 4 3 3-3 3"/><path d="m17 14 3 3-3 3"/></svg>"#;
const REPEAT: &[u8] = br#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m17 2 3 3-3 3"/><path d="M4 11V9a4 4 0 0 1 4-4h12"/><path d="m7 22-3-3 3-3"/><path d="M20 13v2a4 4 0 0 1-4 4H4"/></svg>"#;
const LIST: &[u8] = br#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M3 6h12M3 11h12M3 16h7"/><path d="M19 5v10.5"/><circle cx="16.5" cy="16" r="2.5" fill="currentColor" stroke="none"/></svg>"#;
/// Discord's SpotifyNeutralIcon.
const SPOTIFY: &[u8] = br#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="currentColor" fill-rule="evenodd" d="M12 1C5.94 1 1 5.94 1 12s4.94 11 11 11 11-4.94 11-11S18.06 1 12 1Zm4.8 15.79a.68.68 0 0 1-.95.23c-2.57-1.58-5.8-1.93-9.62-1.06a.68.68 0 0 1-.3-1.33c4.17-.95 7.75-.54 10.64 1.22.32.2.42.62.22.94Zm1.33-2.98a.85.85 0 0 1-1.17.28c-2.94-1.81-7.43-2.34-10.91-1.28a.86.86 0 0 1-.5-1.63c3.98-1.2 8.92-.62 12.3 1.45.4.25.53.78.28 1.18Zm1.52-3.47c-.29.49-.92.65-1.4.36C14.72 8.6 8.9 8.41 5.53 9.44a1.02 1.02 0 1 1-.6-1.96C8.8 6.3 15.23 6.53 19.3 8.94c.5.29.65.92.36 1.4Z"/></svg>"#;
/// The controller's state.
#[derive(Default)]
pub struct SpotifyPanel {
now: Option<Player>,
/// When `now` arrived: progress runs on from it while playing.
at: Option<Instant>,
devices: Vec<Device>,
error: Option<String>,
/// When Spotify was last asked, and whether that answer is still coming.
asked: Option<Instant>,
waiting: bool,
playlists: Option<Result<Vec<Playlist>, String>>,
playlists_open: bool,
/// The seek bar being dragged: where to, 0–1.
drag: Option<f32>,
/// The volume slider is open, and the level it is being dragged to (0–100).
volume_open: bool,
volume_drag: Option<u8>,
}
fn time(ms: u64) -> String {
let s = ms / 1000;
format!("{}:{:02}", s / 60, s % 60)
}
fn semibold(size: f32) -> FontId {
FontId::new(size, FontFamily::Name(crate::style::BOLD.into()))
}
/// A round icon button (`key` names it and its tinted icon); returns whether it was clicked.
fn icon_button(ui: &mut Ui, center: egui::Pos2, svg: &'static [u8], key: &str, color: Color32, hover_color: Color32, tip: &str) -> bool {
let r = Rect::from_center_size(center, vec2(28.0, 28.0));
let resp = ui.interact(r, egui::Id::new(("spotify_btn", key)), Sense::click()).on_hover_cursor(egui::CursorIcon::PointingHand).on_hover_text(tip);
let c = if resp.hovered() { hover_color } else { color };
egui::Image::new(super::add_server::tinted_svg(key, svg, c)).paint_at(ui, Rect::from_center_size(center, vec2(18.0, 18.0)));
resp.clicked()
}
impl SpotifyPanel {
/// Where the track is now (moving on by itself while it plays).
fn progress(&self) -> u64 {
let Some(p) = &self.now else { return 0 };
let ran = match (p.playing, self.at) {
(true, Some(at)) => at.elapsed().as_millis() as u64,
_ => 0,
};
(p.progress_ms + ran).min(p.duration_ms)
}
}
impl RustCord {
fn spotify_on(&self) -> bool {
self.prefs.experiments.spotify_player
}
/// The controller's height in the panel stack (0 while switched off).
pub fn spotify_panel_height(&self) -> f32 {
match (self.spotify_on(), &self.spotify_panel.now) {
(false, _) => 0.0,
(true, Some(_)) => FULL,
(true, None) => SHORT,
}
}
/// Ask Spotify what's playing every few seconds while it plays (less
/// often otherwise); the bar moves on by itself in between.
pub fn spotify_panel_tick(&mut self, ctx: &egui::Context) {
if !self.spotify_on() || self.conn != crate::app::ConnState::Online {
return;
}
let sp = &self.spotify_panel;
let playing = sp.now.as_ref().is_some_and(|p| p.playing);
let focused = ctx.input(|i| i.focused);
let every = if sp.error.is_some() {
Duration::from_secs(30)
} else if playing && focused {
Duration::from_secs(3)
} else {
Duration::from_secs(10)
};
// A track that has just ended: the next one is asked for straight away.
let ended = playing && sp.now.as_ref().is_some_and(|p| p.duration_ms > 0 && sp.progress() >= p.duration_ms);
let due = sp.asked.is_none_or(|t| t.elapsed() >= every || (ended && t.elapsed() >= Duration::from_secs(1)));
if due && !sp.waiting {
self.spotify_panel.asked = Some(Instant::now());
self.spotify_panel.waiting = true;
self.backend.send(Command::Spotify(SpotifyRequest::State));
}
if playing {
ctx.request_repaint_after(Duration::from_millis(500));
}
}
pub fn spotify_player_answer(&mut self, result: Result<(Option<Player>, Vec<Device>), String>) {
let sp = &mut self.spotify_panel;
sp.waiting = false;
match result {
Ok((now, devices)) => {
sp.now = now;
sp.at = Some(Instant::now());
sp.devices = devices;
sp.error = None;
}
Err(e) => {
log::info!("spotify controller: {e}");
sp.error = Some(e);
}
}
}
pub fn spotify_playlists_answer(&mut self, result: Result<Vec<Playlist>, String>) {
self.spotify_panel.playlists = Some(result);
}
/// Something to tell Spotify: shown at once, sent, then the real state follows.
fn spotify_do(&mut self, cmd: Sp) {
let progress = self.spotify_panel.progress();
if let Some(p) = self.spotify_panel.now.as_mut() {
p.progress_ms = progress;
match &cmd {
Sp::Play => p.playing = true,
Sp::Pause => p.playing = false,
Sp::Seek(ms) => p.progress_ms = *ms,
Sp::Shuffle(on) => p.shuffle = *on,
Sp::Repeat(m) => p.repeat = m.clone(),
Sp::Volume(v) => p.volume = Some(*v),
_ => {}
}
self.spotify_panel.at = Some(Instant::now());
}
self.spotify_panel.asked = Some(Instant::now());
self.backend.send(Command::Spotify(SpotifyRequest::Do(cmd)));
}
/// The controller, drawn into `rect` at the top of the user panel.
pub fn spotify_panel_ui(&mut self, ui: &mut Ui, rect: Rect, t: &Theme) {
let mut act: Option<Sp> = None;
let mut toggle_list = false;
let sp = &self.spotify_panel;
let Some(p) = sp.now.clone() else {
self.spotify_idle_ui(ui, rect, t);
return;
};
// ---- cover, title, artists, playlists ----
let art = Rect::from_min_size(pos2(rect.left() + 8.0, rect.top() + 8.0), vec2(40.0, 40.0));
match &p.art {
Some(url) => {
egui::Image::new(url.as_str()).rounding(4.0).paint_at(ui, art);
}
None => {
ui.painter().rect_filled(art, 4.0, crate::style::overlay(0.08));
egui::Image::new(super::add_server::tinted_svg("sp-logo", SPOTIFY, GREEN)).paint_at(ui, art.shrink(10.0));
}
}
let text_left = art.right() + 10.0;
let text_right = rect.right() - 40.0;
let clip = ui.painter_at(Rect::from_min_max(pos2(text_left, rect.top()), pos2(text_right, art.bottom() + 2.0)));
clip.text(pos2(text_left, art.top() + 11.0), Align2::LEFT_CENTER, &p.track, semibold(14.0), t.header_primary);
let sub = match &p.device {
Some(d) => format!("{} · on {d}", p.artists),
None => p.artists.clone(),
};
clip.text(pos2(text_left, art.top() + 30.0), Align2::LEFT_CENTER, sub, FontId::proportional(12.0), t.text_muted);
if let Some(link) = &p.link {
let title = Rect::from_min_max(pos2(text_left, art.top()), pos2(text_right, art.top() + 22.0));
if ui.interact(title, egui::Id::new("spotify_title"), Sense::click()).on_hover_cursor(egui::CursorIcon::PointingHand).on_hover_text("Open in Spotify").clicked() {
ui.ctx().open_url(egui::OpenUrl::new_tab(link));
}
}
let list_at = pos2(rect.right() - 22.0, art.center().y);
if icon_button(ui, list_at, LIST, "sp-list", t.interactive_normal, t.header_primary, "Your playlists") {
toggle_list = true;
}
// ---- shuffle, previous, play / pause, next, repeat ----
let cy = rect.top() + 68.0;
let cx = rect.center().x;
let active = |on: bool| if on { GREEN } else { t.interactive_normal };
if icon_button(ui, pos2(cx - 84.0, cy), SHUFFLE, "sp-shuffle", active(p.shuffle), if p.shuffle { GREEN } else { t.header_primary }, if p.shuffle { "Shuffle: on" } else { "Shuffle: off" }) {
act = Some(Sp::Shuffle(!p.shuffle));
}
if icon_button(ui, pos2(cx - 44.0, cy), PREVIOUS, "sp-prev", t.interactive_normal, t.header_primary, "Previous") {
act = Some(Sp::Previous);
}
let play = Rect::from_center_size(pos2(cx, cy), vec2(32.0, 32.0));
let resp = ui.interact(play, egui::Id::new("spotify_play"), Sense::click()).on_hover_cursor(egui::CursorIcon::PointingHand);
ui.painter().circle_filled(play.center(), if resp.hovered() { 17.0 } else { 16.0 }, Color32::WHITE);
let glyph = if p.playing { (PAUSE, "sp-pause") } else { (PLAY, "sp-play") };
egui::Image::new(super::add_server::tinted_svg(glyph.1, glyph.0, Color32::BLACK)).paint_at(ui, Rect::from_center_size(play.center(), vec2(16.0, 16.0)));
if resp.on_hover_text(if p.playing { "Pause" } else { "Play" }).clicked() {
act = Some(if p.playing { Sp::Pause } else { Sp::Play });
}
if icon_button(ui, pos2(cx + 44.0, cy), NEXT, "sp-next", t.interactive_normal, t.header_primary, "Next") {
act = Some(Sp::Next);
}
let repeat_tip = match p.repeat.as_str() {
"context" => "Repeat: on",
"track" => "Repeat: this song",
_ => "Repeat: off",
};
let repeat_at = pos2(cx + 84.0, cy);
if icon_button(ui, repeat_at, REPEAT, "sp-repeat", active(p.repeat != "off"), if p.repeat != "off" { GREEN } else { t.header_primary }, repeat_tip) {
// Spotify's order: off → the playlist → this song → off.
let next = match p.repeat.as_str() {
"off" => "context",
"context" => "track",
_ => "off",
};
act = Some(Sp::Repeat(next.into()));
}
if p.repeat == "track" {
ui.painter().text(repeat_at + vec2(8.0, -8.0), Align2::CENTER_CENTER, "1", semibold(9.0), GREEN);
}
for (on, at) in [(p.shuffle, pos2(cx - 84.0, cy)), (p.repeat != "off", repeat_at)] {
if on {
ui.painter().circle_filled(at + vec2(0.0, 13.0), 1.5, GREEN);
}
}
// ---- the seek bar ----
let by = rect.top() + 96.0;
let progress = sp.progress();
let bar = Rect::from_min_max(pos2(rect.left() + 44.0, by - 2.0), pos2(rect.right() - 44.0, by + 2.0));
let hit = bar.expand2(vec2(4.0, 6.0));
let resp = ui.interact(hit, egui::Id::new("spotify_seek"), Sense::click_and_drag()).on_hover_cursor(egui::CursorIcon::PointingHand);
let at_pointer = |resp: &egui::Response| resp.interact_pointer_pos().map(|pos| ((pos.x - bar.left()) / bar.width()).clamp(0.0, 1.0));
let mut drag = self.spotify_panel.drag;
if resp.dragged() || resp.is_pointer_button_down_on() {
drag = at_pointer(&resp).or(drag);
}
if resp.drag_stopped() || resp.clicked() {
if let Some(f) = at_pointer(&resp).or(drag) {
act = Some(Sp::Seek((f as f64 * p.duration_ms as f64) as u64));
}
drag = None;
}
let shown = drag.unwrap_or(if p.duration_ms > 0 { progress as f32 / p.duration_ms as f32 } else { 0.0 });
let hot = resp.hovered() || drag.is_some();
ui.painter().rect_filled(bar, 2.0, crate::style::overlay(0.2));
let filled = Rect::from_min_max(bar.min, pos2(bar.left() + bar.width() * shown, bar.bottom()));
ui.painter().rect_filled(filled, 2.0, if hot { GREEN } else { t.header_primary });
if hot {
ui.painter().circle_filled(pos2(filled.right(), bar.center().y), 6.0, Color32::WHITE);
}
let shown_ms = drag.map_or(progress, |f| (f as f64 * p.duration_ms as f64) as u64);
ui.painter().text(pos2(rect.left() + 38.0, by), Align2::RIGHT_CENTER, time(shown_ms), FontId::proportional(11.0), t.text_muted);
ui.painter().text(pos2(rect.right() - 38.0, by), Align2::LEFT_CENTER, time(p.duration_ms), FontId::proportional(11.0), t.text_muted);
self.spotify_panel.drag = drag;
// ---- volume, at the right end of the controls row ----
if let Some(volume) = self.spotify_volume_ui(ui, pos2(rect.right() - 22.0, cy), p.volume, t) {
act = Some(Sp::Volume(volume));
}
if toggle_list {
self.toggle_spotify_playlists();
}
if let Some(cmd) = act {
self.spotify_do(cmd);
}
self.spotify_playlists_ui(ui.ctx(), rect, t);
}
/// The speaker button on the controls row and, while open, its slider above it.
/// Returns the volume to send: once, when the slider is let go (one request to
/// Spotify per change, not one per frame of the drag).
fn spotify_volume_ui(&mut self, ui: &mut Ui, at: egui::Pos2, volume: Option<u8>, t: &Theme) -> Option<u8> {
let button = Rect::from_center_size(at, vec2(24.0, 24.0));
let resp = ui.interact(button, egui::Id::new("spotify_volume"), Sense::click());
let Some(volume) = volume else {
// Phones and some speakers don't let Spotify Connect set their volume.
ui.painter().text(at, Align2::CENTER_CENTER, crate::style::icon::SPEAK, icon_font(16.0), t.text_muted.gamma_multiply(0.5));
resp.on_hover_text("This device's volume can't be changed from here");
self.spotify_panel.volume_open = false;
return None;
};
let shown = self.spotify_panel.volume_drag.unwrap_or(volume);
let resp = resp.on_hover_cursor(egui::CursorIcon::PointingHand);
let hot = resp.hovered() || self.spotify_panel.volume_open;
let glyph = if shown == 0 { crate::style::icon::MUTE } else { crate::style::icon::SPEAK };
ui.painter().text(at, Align2::CENTER_CENTER, glyph, icon_font(16.0), if hot { t.header_primary } else { t.interactive_normal });
if resp.on_hover_text(format!("Volume: {shown}%")).clicked() {
self.spotify_panel.volume_open = !self.spotify_panel.volume_open;
}
if !self.spotify_panel.volume_open {
return None;
}
let pop = Rect::from_min_size(pos2(at.x - 30.0, button.top() - 164.0), vec2(60.0, 156.0));
let mut send = None;
let mut drag = self.spotify_panel.volume_drag;
let area = egui::Area::new(egui::Id::new("spotify_volume_pop")).order(Order::Foreground).fixed_pos(pop.min).constrain(true).show(ui.ctx(), |ui| {
let (r, _) = ui.allocate_exact_size(pop.size(), Sense::hover());
ui.painter().rect_filled(r, 10.0, t.bg_floating);
ui.painter().rect_stroke(r, 10.0, Stroke::new(1.0_f32, t.border_subtle));
let track = Rect::from_center_size(pos2(r.center().x, r.top() + 72.0), vec2(6.0, 110.0));
let tr = ui.interact(track.expand2(vec2(18.0, 8.0)), egui::Id::new("spotify_volume_track"), Sense::click_and_drag()).on_hover_cursor(egui::CursorIcon::PointingHand);
let level = |pos: egui::Pos2| (((track.bottom() - pos.y) / track.height()).clamp(0.0, 1.0) * 100.0).round() as u8;
if tr.dragged() || tr.is_pointer_button_down_on() {
drag = tr.interact_pointer_pos().map(level).or(drag);
}
if tr.drag_stopped() || tr.clicked() {
send = tr.interact_pointer_pos().map(level).or(drag);
drag = None;
}
let v = send.or(drag).unwrap_or(volume);
let fill_h = track.height() * f32::from(v) / 100.0;
ui.painter().rect_filled(track, 3.0, crate::style::overlay(0.2));
ui.painter().rect_filled(Rect::from_min_max(pos2(track.left(), track.bottom() - fill_h), track.max), 3.0, GREEN);
ui.painter().circle_filled(pos2(track.center().x, track.bottom() - fill_h), 8.0, Color32::WHITE);
ui.painter().text(pos2(r.center().x, r.bottom() - 16.0), Align2::CENTER_CENTER, format!("{v}%"), FontId::proportional(12.0), t.text_normal);
});
self.spotify_panel.volume_drag = drag;
// A press anywhere else closes it.
let outside = ui.input(|i| i.pointer.any_pressed() && i.pointer.interact_pos().is_some_and(|p| !area.response.rect.contains(p) && !button.contains(p)));
if outside {
self.spotify_panel.volume_open = false;
}
send.filter(|v| *v != volume)
}
/// Nothing is playing (or Spotify can't be reached): say so, and offer to
/// start it on this PC.
fn spotify_idle_ui(&mut self, ui: &mut Ui, rect: Rect, t: &Theme) {
let sp = &self.spotify_panel;
let logo = Rect::from_center_size(pos2(rect.left() + 24.0, rect.center().y), vec2(24.0, 24.0));
egui::Image::new(super::add_server::tinted_svg("sp-logo", SPOTIFY, GREEN)).paint_at(ui, logo);
let (title, sub) = match (&sp.error, sp.asked) {
(Some(e), _) => ("Spotify", e.clone()),
(None, None) => ("Spotify", "Connecting…".to_string()),
(None, Some(_)) => ("Nothing playing", "Play something in Spotify.".to_string()),
};
let pc = sp.devices.iter().find(|d| d.kind.eq_ignore_ascii_case("computer")).cloned();
let button_w = if sp.error.is_none() { 92.0 } else { 0.0 };
let clip = ui.painter_at(Rect::from_min_max(pos2(logo.right() + 10.0, rect.top()), pos2(rect.right() - button_w - 12.0, rect.bottom())));
clip.text(pos2(logo.right() + 10.0, rect.center().y - 9.0), Align2::LEFT_CENTER, title, semibold(14.0), t.header_primary);
clip.text(pos2(logo.right() + 10.0, rect.center().y + 9.0), Align2::LEFT_CENTER, &sub, FontId::proportional(12.0), t.text_muted);
if !sub.is_empty() && sub.len() > 40 {
ui.interact(rect, egui::Id::new("spotify_idle_tip"), Sense::hover()).on_hover_text(sub);
}
if sp.error.is_some() {
return;
}
let b = Rect::from_min_size(pos2(rect.right() - button_w - 8.0, rect.center().y - 14.0), vec2(button_w, 28.0));
let resp = ui.interact(b, egui::Id::new("spotify_idle_button"), Sense::click()).on_hover_cursor(egui::CursorIcon::PointingHand);
ui.painter().rect_filled(b, 6.0, if resp.hovered() { crate::style::overlay(0.2) } else { crate::style::overlay(0.12) });
let (label, tip) = match &pc {
Some(d) => ("Use this PC", format!("Play on {}", d.name)),
None => ("Playlists", "Your playlists".to_string()),
};
ui.painter().text(b.center(), Align2::CENTER_CENTER, label, crate::style::medium(13.0), t.header_primary);
if resp.on_hover_text(tip).clicked() {
match pc {
Some(d) => self.spotify_do(Sp::Transfer(d.id)),
None => self.toggle_spotify_playlists(),
}
}
self.spotify_playlists_ui(ui.ctx(), rect, t);
}
fn toggle_spotify_playlists(&mut self) {
let sp = &mut self.spotify_panel;
sp.playlists_open = !sp.playlists_open;
if sp.playlists_open && !matches!(sp.playlists, Some(Ok(_))) {
sp.playlists = None;
self.backend.send(Command::Spotify(SpotifyRequest::Playlists));
}
}
/// Your playlists, in a list over the chat right of the panel: a click plays one.
fn spotify_playlists_ui(&mut self, ctx: &egui::Context, panel: Rect, t: &Theme) {
if !self.spotify_panel.playlists_open {
return;
}
let mut pick: Option<String> = None;
let mut close = ctx.input(|i| i.key_pressed(egui::Key::Escape));
// Right of the panel, its bottom level with the panel's (its height
// worked out here: an area placed by its bottom would use last frame's).
let rows = match &self.spotify_panel.playlists {
Some(Ok(list)) if !list.is_empty() => (list.len() as f32 * 40.0).min(320.0),
_ => 24.0,
};
let height = 8.0 + 20.0 + 6.0 + rows + 8.0;
let at = pos2(panel.right() + 12.0, (panel.bottom() - height).max(ctx.screen_rect().top() + 40.0));
let area = egui::Area::new(egui::Id::new("spotify_playlists")).order(Order::Foreground).fixed_pos(at).show(ctx, |ui| {
egui::Frame::none().fill(t.bg_floating).rounding(8.0).stroke(Stroke::new(1.0_f32, t.border_subtle)).inner_margin(8.0).show(ui, |ui| {
ui.set_width(260.0);
ui.label(egui::RichText::new("Your playlists").font(semibold(14.0)).color(t.header_primary));
ui.add_space(6.0);
match &self.spotify_panel.playlists {
None => {
ui.spinner();
}
Some(Err(e)) => {
ui.label(egui::RichText::new(e).size(13.0).color(t.danger));
}
Some(Ok(list)) if list.is_empty() => {
ui.label(egui::RichText::new("No playlists on your Spotify.").size(13.0).color(t.text_muted));
}
Some(Ok(list)) => {
ScrollArea::vertical().max_height(rows).min_scrolled_height(rows).show(ui, |ui| {
for pl in list {
let (r, resp) = ui.allocate_exact_size(vec2(260.0, 40.0), Sense::click());
if resp.hovered() {
ui.painter().rect_filled(r, 4.0, crate::style::overlay(0.08));
}
let cover = Rect::from_min_size(pos2(r.left() + 4.0, r.top() + 4.0), vec2(32.0, 32.0));
match &pl.image {
Some(url) => {
egui::Image::new(url.as_str()).rounding(4.0).paint_at(ui, cover);
}
None => {
ui.painter().rect_filled(cover, 4.0, crate::style::overlay(0.1));
}
}
let clip = ui.painter_at(Rect::from_min_max(pos2(cover.right() + 8.0, r.top()), r.max));
clip.text(pos2(cover.right() + 8.0, r.center().y), Align2::LEFT_CENTER, &pl.name, crate::style::medium(14.0), t.text_normal);
if resp.on_hover_cursor(egui::CursorIcon::PointingHand).clicked() {
pick = Some(pl.uri.clone());
}
}
});
}
}
});
});
close |= super::pressed_outside(ctx, area.response.rect) && !ctx.input(|i| i.pointer.interact_pos().is_some_and(|p| panel.contains(p)));
if let Some(uri) = pick {
self.spotify_do(Sp::PlayContext(uri));
close = true;
}
if close {
self.spotify_panel.playlists_open = false;
}
}
}