Musyx Mobile: Android TV player for Yandex Music
Kotlin, Compose for TV and Media3 port of the desktop Musyx, with device-code sign-in, Home, My Wave, search, collection, detail pages, Now Playing with queue and lyrics, and settings for streaming quality, appearance, font size (small, medium, large) and language. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
46610d95fa
217 files changed
+26144
No files matched your search
@@ -0,0 +1,66 @@
|
||||
//! Cover cards for albums, artists and playlists, laid out in wrapping rows.
|
||||
|
||||
use iced::widget::{Row, button, column, container, row};
|
||||
use iced::{Alignment, Element, Fill, Padding};
|
||||
|
||||
use crate::app::Message;
|
||||
use crate::covers::{self, Covers};
|
||||
use crate::ui::icons::{Icon, icon};
|
||||
use crate::ui::line;
|
||||
use crate::ui::theme::{self, Tokens, radius, size};
|
||||
|
||||
/// Card width; covers are square at this size.
|
||||
pub const WIDTH: f32 = 168.0;
|
||||
|
||||
pub struct Card {
|
||||
pub title: String,
|
||||
pub subtitle: String,
|
||||
pub cover: Option<String>,
|
||||
pub hues: (f32, f32),
|
||||
/// Artists get a round cover.
|
||||
pub round: bool,
|
||||
pub on_press: Message,
|
||||
}
|
||||
|
||||
pub fn view<'a>(t: &'static Tokens, covers: &Covers, card: Card) -> Element<'a, Message> {
|
||||
build(t, covers, card, None)
|
||||
}
|
||||
|
||||
/// A card with a heart beside its title.
|
||||
pub fn with_like<'a>(t: &'static Tokens, covers: &Covers, card: Card, liked: bool, on_like: Message) -> Element<'a, Message> {
|
||||
let heart = button(icon(
|
||||
if liked { Icon::HeartFilled } else { Icon::Heart },
|
||||
if liked { t.like } else { t.muted },
|
||||
))
|
||||
.padding(4)
|
||||
.style(theme::icon_button(None))
|
||||
.on_press(on_like);
|
||||
build(t, covers, card, Some(heart.into()))
|
||||
}
|
||||
|
||||
fn build<'a>(t: &'static Tokens, covers: &Covers, card: Card, action: Option<Element<'a, Message>>) -> Element<'a, Message> {
|
||||
let corner = if card.round { WIDTH / 2.0 } else { radius::COVER };
|
||||
let text = column![
|
||||
line(card.title, theme::MEDIUM, size::BODY, t.fg),
|
||||
line(card.subtitle, theme::BODY, size::META, t.muted),
|
||||
]
|
||||
.spacing(2)
|
||||
.width(Fill)
|
||||
.clip(true);
|
||||
button(
|
||||
column![
|
||||
covers::view(covers, card.cover, card.hues, WIDTH, corner),
|
||||
row![text].push(action).spacing(4).align_y(Alignment::Center).width(WIDTH),
|
||||
]
|
||||
.spacing(9),
|
||||
)
|
||||
.padding(Padding::from(6))
|
||||
.style(theme::nav_item(false))
|
||||
.on_press(card.on_press)
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Cards in rows that wrap to the available width.
|
||||
pub fn grid<'a>(cards: impl IntoIterator<Item = Element<'a, Message>>) -> Element<'a, Message> {
|
||||
container(Row::with_children(cards).spacing(12).wrap().vertical_spacing(14)).into()
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
//! Right-hand panel with the play queue and lyrics.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use iced::widget::{Column, button, column, container, row, scrollable, space};
|
||||
use ym_api::{Lyrics, Track};
|
||||
use iced::{Alignment, Element, Fill, Padding};
|
||||
|
||||
use crate::app::Message;
|
||||
use crate::covers::{self, Covers};
|
||||
use crate::player::{Msg, Player, Queue};
|
||||
use crate::screens::Loadable;
|
||||
use crate::ui::icons::{Icon, icon};
|
||||
use crate::ui::theme::{self, Tokens, radius, shell, size};
|
||||
use crate::widgets::player_bar::clock;
|
||||
use crate::ui::{line, para};
|
||||
use crate::i18n::{tr, trf};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Panel {
|
||||
Queue,
|
||||
Lyrics,
|
||||
}
|
||||
|
||||
impl Panel {
|
||||
fn label(self) -> &'static str {
|
||||
match self {
|
||||
Panel::Queue => tr("Queue"),
|
||||
Panel::Lyrics => tr("Lyrics"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The lyrics scrollable, so playback can keep the current line in view.
|
||||
pub const LYRICS_ID: &str = "lyrics";
|
||||
|
||||
pub fn view<'a>(
|
||||
t: &'static Tokens,
|
||||
panel: Panel,
|
||||
player: &'a Player,
|
||||
covers: &Covers,
|
||||
lyrics: Option<&'a Loadable<Option<Arc<Lyrics>>>>,
|
||||
) -> Element<'a, Message> {
|
||||
let tabs = row![tab(t, Panel::Queue, panel), tab(t, Panel::Lyrics, panel)].spacing(4);
|
||||
|
||||
let head = row![
|
||||
tabs,
|
||||
space().width(Fill),
|
||||
button(icon(Icon::Close, t.muted))
|
||||
.padding(8)
|
||||
.style(theme::icon_button(None))
|
||||
.on_press(Message::CloseDrawer),
|
||||
]
|
||||
.align_y(Alignment::Center)
|
||||
.padding(Padding::default().top(14).bottom(8).left(18).right(14));
|
||||
|
||||
let body: Element<'a, Message> = match (panel, player.queue()) {
|
||||
(Panel::Queue, Some(queue)) => queue_list(t, queue, covers),
|
||||
(Panel::Queue, None) => container(para(
|
||||
tr("Nothing queued yet. Tracks you play, and the ones lined up after them, show up here."),
|
||||
size::META,
|
||||
t.muted,
|
||||
))
|
||||
.padding([4, 20])
|
||||
.into(),
|
||||
(Panel::Lyrics, None) => note(t, tr("Play a track to see its lyrics.")),
|
||||
(Panel::Lyrics, Some(_)) => match lyrics {
|
||||
Some(Loadable::Loaded(Some(l))) => lyrics_view(t, l, player.position()),
|
||||
Some(Loadable::Loaded(None)) => note(t, tr("Yandex Music has no lyrics for this track.")),
|
||||
Some(Loadable::Failed(_)) => column![
|
||||
note(t, tr("Couldn't load the lyrics. Check your connection and try again.")),
|
||||
container(
|
||||
button(line(tr("Try again"), theme::SEMIBOLD, size::BODY, t.accent_ink))
|
||||
.padding([9, 16])
|
||||
.style(theme::primary)
|
||||
.on_press(Message::RetryLyrics),
|
||||
)
|
||||
.padding([0, 20]),
|
||||
]
|
||||
.spacing(12)
|
||||
.into(),
|
||||
_ => note(t, tr("Loading lyrics…")),
|
||||
},
|
||||
};
|
||||
|
||||
container(column![head, body])
|
||||
.width(shell::DRAWER)
|
||||
.height(Fill)
|
||||
.style(theme::panel)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn note<'a>(t: &'static Tokens, text: &'a str) -> Element<'a, Message> {
|
||||
container(para(text, size::META, t.muted)).padding([4, 20]).into()
|
||||
}
|
||||
|
||||
/// Synced lyrics highlight the line being sung and seek on click; plain lyrics just scroll.
|
||||
fn lyrics_view<'a>(t: &'static Tokens, l: &'a Lyrics, position: Duration) -> Element<'a, Message> {
|
||||
let current = l.current(position);
|
||||
let lines = l.lines.iter().enumerate().map(|(i, lyric)| -> Element<'a, Message> {
|
||||
let (colour, font, sz) = match current {
|
||||
Some(c) if c == i => (t.fg, theme::SEMIBOLD, 17.0),
|
||||
Some(c) if i < c => (t.muted.scale_alpha(0.6), theme::MEDIUM, 15.0),
|
||||
_ if l.synced => (t.muted, theme::MEDIUM, 15.0),
|
||||
_ => (t.fg, theme::BODY, 14.0),
|
||||
};
|
||||
let text = iced::widget::text(if lyric.text.is_empty() { "♪".to_owned() } else { lyric.text.clone() })
|
||||
.font(font)
|
||||
.size(sz)
|
||||
.color(colour)
|
||||
.line_height(1.35);
|
||||
match lyric.at {
|
||||
Some(at) => button(text)
|
||||
.width(Fill)
|
||||
.padding([5, 8])
|
||||
.style(theme::nav_item(false))
|
||||
.on_press(Message::Player(Msg::Seek(at)))
|
||||
.into(),
|
||||
None => container(text).padding([2, 8]).into(),
|
||||
}
|
||||
});
|
||||
let mut list = Column::with_children(lines).spacing(2);
|
||||
if !l.writers.is_empty() {
|
||||
list = list.push(
|
||||
container(para(trf("Written by {}", &[&l.writers.join(", ")]), size::SMALL, t.muted))
|
||||
.padding(iced::Padding::default().top(18).left(8).right(8)),
|
||||
);
|
||||
}
|
||||
scrollable(list.padding(Padding::default().left(10).right(10).bottom(18)))
|
||||
.id(LYRICS_ID)
|
||||
.height(Fill)
|
||||
.style(theme::scroll)
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Upcoming items shown at once; a long liked list would otherwise build thousands of rows.
|
||||
const SHOWN: usize = 100;
|
||||
|
||||
fn queue_list<'a>(t: &'static Tokens, queue: &'a Queue<Track>, covers: &Covers) -> Element<'a, Message> {
|
||||
let heading = |s: String| {
|
||||
container(line(s, theme::MEDIUM, size::SMALL, t.muted)).padding(Padding::default().top(14).bottom(8).left(8))
|
||||
};
|
||||
let item = |track: &Track, now: bool, msg: Option<Message>| {
|
||||
button(
|
||||
row![
|
||||
covers::view(covers, track.cover_url(covers::SMALL), track.hues(), 40.0, radius::SMALL_COVER),
|
||||
column![
|
||||
line(track.full_title(), theme::MEDIUM, size::BODY, if now { t.accent } else { t.fg }),
|
||||
line(track.artist_line(), theme::BODY, size::SMALL, t.muted),
|
||||
]
|
||||
.spacing(1)
|
||||
.width(Fill)
|
||||
.clip(true),
|
||||
line(clock(track.duration.as_secs_f32()), theme::BODY, size::SMALL, t.muted),
|
||||
]
|
||||
.spacing(10)
|
||||
.align_y(Alignment::Center),
|
||||
)
|
||||
.width(Fill)
|
||||
.padding([6, 8])
|
||||
.style(theme::nav_item(false))
|
||||
.on_press_maybe(msg)
|
||||
};
|
||||
|
||||
let remaining = queue.upcoming().count();
|
||||
let mut list = column![heading(tr("Now playing").into()), item(queue.current(), true, None)];
|
||||
list = list.push(heading(if remaining == 0 {
|
||||
tr("Nothing after this").into()
|
||||
} else {
|
||||
trf("Next up · {}", &[&remaining])
|
||||
}));
|
||||
for (pos, track) in queue.upcoming().take(SHOWN) {
|
||||
list = list.push(item(track, false, Some(Message::Player(Msg::JumpTo(pos)))));
|
||||
}
|
||||
if remaining > SHOWN {
|
||||
list = list.push(
|
||||
container(line(trf("and {} more", &[&(remaining - SHOWN)]), theme::BODY, size::SMALL, t.muted))
|
||||
.padding([10, 8]),
|
||||
);
|
||||
}
|
||||
|
||||
scrollable(list.padding(Padding::default().left(10).right(10).bottom(18)))
|
||||
.height(Fill)
|
||||
.style(theme::scroll)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn tab<'a>(t: &'static Tokens, panel: Panel, selected: Panel) -> Element<'a, Message> {
|
||||
let on = panel == selected;
|
||||
button(
|
||||
column![
|
||||
line(panel.label(), theme::MEDIUM, size::BODY, if on { t.fg } else { t.muted }),
|
||||
container(space()).width(Fill).height(2).style(theme::tab_underline(on)),
|
||||
]
|
||||
.spacing(7)
|
||||
.width(iced::Shrink),
|
||||
)
|
||||
.padding(Padding::default().left(10).right(10).top(6))
|
||||
.style(theme::tab(on))
|
||||
.on_press(Message::ToggleDrawer(panel))
|
||||
.into()
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
pub mod card;
|
||||
pub mod drawer;
|
||||
pub mod player_bar;
|
||||
pub mod sheet;
|
||||
pub mod sidebar;
|
||||
pub mod topbar;
|
||||
pub mod track_list;
|
||||
pub mod track_row;
|
||||
@@ -0,0 +1,227 @@
|
||||
//! The persistent bar along the bottom: now playing, transport, extras.
|
||||
|
||||
use iced::widget::{button, column, container, row, slider, space};
|
||||
use iced::{Alignment, Element, Fill, Length};
|
||||
|
||||
use crate::app::Message;
|
||||
use crate::covers::{self, Covers};
|
||||
use crate::player::{Msg, Player, Repeat, Status};
|
||||
use crate::ui::icons::{Icon, icon, sized};
|
||||
use crate::ui::line;
|
||||
use crate::ui::theme::{self, Tokens, radius, size};
|
||||
use crate::widgets::drawer::Panel;
|
||||
use crate::i18n::tr;
|
||||
|
||||
fn send(msg: Msg) -> Message {
|
||||
Message::Player(msg)
|
||||
}
|
||||
|
||||
pub fn view<'a>(
|
||||
t: &'static Tokens,
|
||||
p: &'a Player,
|
||||
liked: bool,
|
||||
drawer: Option<Panel>,
|
||||
covers: &Covers,
|
||||
) -> Element<'a, Message> {
|
||||
row![
|
||||
container(now_playing(t, p, liked, covers)).width(Length::FillPortion(2)),
|
||||
container(transport(t, p)).width(Length::FillPortion(3)).center_x(Fill),
|
||||
container(extras(t, p, drawer)).width(Length::FillPortion(2)).align_right(Fill),
|
||||
]
|
||||
.spacing(18)
|
||||
.align_y(Alignment::Center)
|
||||
.padding([0, 18])
|
||||
.height(Fill)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn now_playing<'a>(t: &'static Tokens, p: &'a Player, liked: bool, covers: &Covers) -> Element<'a, Message> {
|
||||
let Some(track) = p.track() else {
|
||||
let blank = container(space()).width(52).height(52).style(move |_| container::Style {
|
||||
background: Some(t.raised.into()),
|
||||
border: iced::Border { radius: radius::CONTROL.into(), ..Default::default() },
|
||||
..Default::default()
|
||||
});
|
||||
let (title, sub) = match &p.notice {
|
||||
Some(notice) => (tr("Can't play"), notice.as_str()),
|
||||
None => (tr("Nothing playing"), tr("Pick a track to start")),
|
||||
};
|
||||
return row![
|
||||
blank,
|
||||
column![
|
||||
line(title, theme::MEDIUM, size::BODY, t.fg),
|
||||
line(sub, theme::BODY, size::META, t.muted),
|
||||
]
|
||||
.spacing(2),
|
||||
]
|
||||
.spacing(12)
|
||||
.align_y(Alignment::Center)
|
||||
.clip(true)
|
||||
.into();
|
||||
};
|
||||
|
||||
let cover = covers::view(covers, track.cover_url(covers::SMALL), track.hues(), 52.0, radius::CONTROL);
|
||||
|
||||
// A problem with the previous track shows in place of the artist line until the next one loads.
|
||||
let (sub, sub_colour) = match (&p.notice, p.status()) {
|
||||
(Some(notice), Status::Loading | Status::Paused) => (notice.clone(), t.like),
|
||||
_ => (track.artist_line(), t.muted),
|
||||
};
|
||||
|
||||
row![
|
||||
cover,
|
||||
column![
|
||||
line(track.full_title(), theme::MEDIUM, size::BODY, t.fg),
|
||||
line(sub, theme::BODY, size::META, sub_colour),
|
||||
]
|
||||
.spacing(2)
|
||||
.width(Length::Shrink),
|
||||
button(icon(
|
||||
if liked { Icon::HeartFilled } else { Icon::Heart },
|
||||
if liked { t.like } else { t.muted },
|
||||
))
|
||||
.padding(8)
|
||||
.style(theme::icon_button(None))
|
||||
.on_press(Message::LikeTrack(track.clone(), !liked)),
|
||||
button(icon(Icon::Dislike, t.muted))
|
||||
.padding(8)
|
||||
.style(theme::icon_button(None))
|
||||
.on_press(Message::DislikeTrack(track.clone(), true)),
|
||||
]
|
||||
.spacing(12)
|
||||
.align_y(Alignment::Center)
|
||||
.clip(true)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn transport<'a>(t: &'static Tokens, p: &'a Player) -> Element<'a, Message> {
|
||||
let has_track = p.track().is_some();
|
||||
|
||||
let toggle = |glyph: Icon, on: bool, msg: Msg| {
|
||||
button(icon(glyph, if on { t.accent } else { t.muted }))
|
||||
.padding(8)
|
||||
.style(theme::icon_button(on.then_some(t.accent)))
|
||||
.on_press(send(msg))
|
||||
};
|
||||
let skip = |glyph: Icon, msg: Msg| {
|
||||
button(icon(glyph, if has_track { t.fg } else { t.muted.scale_alpha(0.45) }))
|
||||
.padding(8)
|
||||
.style(theme::icon_button(None))
|
||||
.on_press_maybe(has_track.then_some(send(msg)))
|
||||
};
|
||||
|
||||
let (glyph, tint) = match p.status() {
|
||||
_ if !has_track => (Icon::Play, t.muted),
|
||||
Status::Playing => (Icon::Pause, t.bg),
|
||||
Status::Loading => (Icon::Pause, t.bg.scale_alpha(0.5)),
|
||||
Status::Paused | Status::Ended => (Icon::Play, t.bg),
|
||||
};
|
||||
let disc = button(container(sized(glyph, 18.0, tint)).center(40))
|
||||
.width(40)
|
||||
.height(40)
|
||||
.padding(0)
|
||||
.style(theme::play_disc)
|
||||
.on_press_maybe(has_track.then_some(send(Msg::Toggle)));
|
||||
|
||||
let ctrls = row![
|
||||
toggle(Icon::Shuffle, p.shuffle(), Msg::ToggleShuffle),
|
||||
skip(Icon::Previous, Msg::Previous),
|
||||
disc,
|
||||
skip(Icon::Next, Msg::Next),
|
||||
toggle(
|
||||
if p.repeat == Repeat::One { Icon::RepeatOne } else { Icon::Repeat },
|
||||
p.repeat != Repeat::Off,
|
||||
Msg::CycleRepeat,
|
||||
),
|
||||
]
|
||||
.spacing(8)
|
||||
.align_y(Alignment::Center);
|
||||
|
||||
let duration = p.duration().as_secs_f32();
|
||||
let position = p.position().as_secs_f32().min(duration);
|
||||
let bar: Element<'a, Message> = if has_track && duration > 0.0 {
|
||||
slider(0.0..=duration, position, |v| send(Msg::SeekPreview(v)))
|
||||
.on_release(send(Msg::SeekCommit))
|
||||
.step(0.5_f32)
|
||||
.width(Fill)
|
||||
.style(theme::seek_bar)
|
||||
.into()
|
||||
} else {
|
||||
container(space())
|
||||
.width(Fill)
|
||||
.height(4)
|
||||
.style(move |_| container::Style {
|
||||
background: Some(t.raised.into()),
|
||||
border: iced::Border { radius: 2.0.into(), ..Default::default() },
|
||||
..Default::default()
|
||||
})
|
||||
.into()
|
||||
};
|
||||
|
||||
let seek = row![
|
||||
container(line(clock(position), theme::BODY, 12.0, t.muted)).width(42).align_right(42),
|
||||
bar,
|
||||
container(line(clock(duration), theme::BODY, 12.0, t.muted)).width(42),
|
||||
]
|
||||
.spacing(10)
|
||||
.align_y(Alignment::Center)
|
||||
.width(Fill);
|
||||
|
||||
column![ctrls, seek].spacing(4).align_x(Alignment::Center).max_width(560).into()
|
||||
}
|
||||
|
||||
fn extras<'a>(t: &'static Tokens, p: &'a Player, drawer: Option<Panel>) -> Element<'a, Message> {
|
||||
let panel_button = |glyph: Icon, panel: Panel| {
|
||||
let on = drawer == Some(panel);
|
||||
button(icon(glyph, if on { t.accent } else { t.muted }))
|
||||
.padding(8)
|
||||
.style(theme::icon_button(on.then_some(t.accent)))
|
||||
.on_press(Message::ToggleDrawer(panel))
|
||||
};
|
||||
|
||||
let mut items = row![].spacing(4).align_y(Alignment::Center);
|
||||
if let Some(stream) = p.stream() {
|
||||
let label = if stream.preview { tr("Preview").to_owned() } else { stream.label() };
|
||||
let ink = if stream.lossless() { t.accent } else { t.muted };
|
||||
items = items.push(container(line(label, theme::MEDIUM, 11.5, ink)).padding([2, 6]).style(theme::badge));
|
||||
}
|
||||
|
||||
let volume = if p.muted { 0.0 } else { p.volume };
|
||||
items
|
||||
.push(panel_button(Icon::Lyrics, Panel::Lyrics))
|
||||
.push(panel_button(Icon::Queue, Panel::Queue))
|
||||
.push(
|
||||
button(icon(if p.muted { Icon::Muted } else { Icon::Volume }, t.muted))
|
||||
.padding(8)
|
||||
.style(theme::icon_button(None))
|
||||
.on_press(send(Msg::ToggleMute)),
|
||||
)
|
||||
.push(
|
||||
slider(0.0..=1.0, volume, |v| send(Msg::SetVolume(v)))
|
||||
.step(0.01_f32)
|
||||
.width(96)
|
||||
.style(theme::volume_bar),
|
||||
)
|
||||
.into()
|
||||
}
|
||||
|
||||
/// `m:ss`, or `h:mm:ss` for podcast-length items.
|
||||
pub fn clock(seconds: f32) -> String {
|
||||
let s = seconds.max(0.0) as u64;
|
||||
let (h, m, s) = (s / 3600, (s / 60) % 60, s % 60);
|
||||
if h > 0 { format!("{h}:{m:02}:{s:02}") } else { format!("{m}:{s:02}") }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::clock;
|
||||
|
||||
#[test]
|
||||
fn clock_formats() {
|
||||
assert_eq!(clock(0.0), "0:00");
|
||||
assert_eq!(clock(225.4), "3:45");
|
||||
assert_eq!(clock(3132.0), "52:12");
|
||||
assert_eq!(clock(3725.0), "1:02:05");
|
||||
assert_eq!(clock(-3.0), "0:00");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,310 @@
|
||||
//! Small dialogs over the window: track actions, new/rename playlist, delete confirmation.
|
||||
|
||||
use iced::widget::{Column, button, column, container, mouse_area, opaque, row, space, text_input};
|
||||
use iced::{Alignment, Element, Fill, Length};
|
||||
use ym_api::{PlaylistId, PlaylistSummary, Track};
|
||||
|
||||
use crate::app::Message;
|
||||
use crate::route::Route;
|
||||
use crate::ui::icons::{Icon, icon};
|
||||
use crate::ui::theme::{self, Tokens, radius, size};
|
||||
use crate::ui::{line, para};
|
||||
use crate::i18n::{tr, trf};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Sheet {
|
||||
Track {
|
||||
track: Track,
|
||||
/// Set when opened from one of the user's own playlists: its id, the row, its length.
|
||||
playlist: Option<(PlaylistId, usize, usize)>,
|
||||
/// Showing the "Add to playlist" list instead of the actions.
|
||||
choosing: bool,
|
||||
},
|
||||
NewPlaylist { name: String, add: Option<Track> },
|
||||
Rename { id: PlaylistId, name: String },
|
||||
ConfirmDelete { id: PlaylistId, title: String },
|
||||
}
|
||||
|
||||
pub const INPUT_ID: &str = "sheet-input";
|
||||
|
||||
/// What the sheet needs to know about the library.
|
||||
pub struct Ctx<'a> {
|
||||
/// The menu is for the track playing now: "next" means skipping ahead.
|
||||
pub playing: bool,
|
||||
pub liked: bool,
|
||||
pub disliked: bool,
|
||||
pub own_playlists: &'a [PlaylistSummary],
|
||||
}
|
||||
|
||||
/// The dimmed backdrop plus the dialog; clicking the backdrop closes it.
|
||||
pub fn overlay<'a>(t: &'static Tokens, sheet: &'a Sheet, ctx: Ctx<'a>) -> Element<'a, Message> {
|
||||
let backdrop = mouse_area(
|
||||
container(space()).width(Fill).height(Fill).style(|_| container::Style {
|
||||
background: Some(iced::Color::BLACK.scale_alpha(0.45).into()),
|
||||
..Default::default()
|
||||
}),
|
||||
)
|
||||
.on_press(Message::CloseSheet);
|
||||
|
||||
let card = container(body(t, sheet, ctx))
|
||||
.width(380)
|
||||
.padding(18)
|
||||
.style(move |_| container::Style {
|
||||
background: Some(t.panel.into()),
|
||||
border: iced::Border { color: t.line, width: 1.0, radius: radius::CARD.into() },
|
||||
shadow: iced::Shadow {
|
||||
color: iced::Color::BLACK.scale_alpha(0.35),
|
||||
offset: iced::Vector::new(0.0, 18.0),
|
||||
blur_radius: 50.0,
|
||||
},
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
iced::widget::stack![backdrop, container(opaque(card)).center(Fill)].into()
|
||||
}
|
||||
|
||||
fn body<'a>(t: &'static Tokens, sheet: &'a Sheet, ctx: Ctx<'a>) -> Element<'a, Message> {
|
||||
match sheet {
|
||||
Sheet::Track { track, playlist, choosing: false } => {
|
||||
let mut items: Vec<Element<'a, Message>> = vec![
|
||||
if ctx.playing {
|
||||
item(t, Icon::Next, tr("Next track"), Some(Message::SkipFromMenu))
|
||||
} else {
|
||||
item(t, Icon::Next, tr("Play next"), Some(Message::PlayNext(track.clone())))
|
||||
},
|
||||
item(t, Icon::Queue, tr("Add to queue"), Some(Message::AddToQueue(track.clone()))),
|
||||
item(
|
||||
t,
|
||||
if ctx.liked { Icon::HeartFilled } else { Icon::Heart },
|
||||
if ctx.liked { tr("Remove from Liked tracks") } else { tr("Add to Liked tracks") },
|
||||
Some(Message::LikeTrack(track.clone(), !ctx.liked)),
|
||||
),
|
||||
item(
|
||||
t,
|
||||
Icon::Plus,
|
||||
tr("Add to playlist…"),
|
||||
Some(Message::OpenSheet(Sheet::Track { track: track.clone(), playlist: *playlist, choosing: true })),
|
||||
),
|
||||
item(t, Icon::Wave, tr("Play similar tracks"), Some(Message::PlaySimilar(track.clone()))),
|
||||
];
|
||||
if let Some(album) = &track.album {
|
||||
items.push(item(t, Icon::Disc, tr("Go to album"), Some(Message::Navigate(Route::Album(album.id)))));
|
||||
}
|
||||
for artist in track.artists.iter().filter(|a| a.id.is_some()).take(3) {
|
||||
items.push(item(
|
||||
t,
|
||||
Icon::Forward,
|
||||
tr("Go to artist"),
|
||||
artist.id.map(|id| Message::Navigate(Route::Artist(id))),
|
||||
));
|
||||
}
|
||||
items.push(item(
|
||||
t,
|
||||
Icon::Dislike,
|
||||
if ctx.disliked { tr("Recommend this track again") } else { tr("Don't recommend this track") },
|
||||
Some(Message::DislikeTrack(track.clone(), !ctx.disliked)),
|
||||
));
|
||||
if let Some((id, index, len)) = *playlist {
|
||||
items.push(divider());
|
||||
if index > 0 {
|
||||
items.push(item(t, Icon::Back, tr("Move up"), Some(Message::MovePlaylistTrack {
|
||||
id, track: track.clone(), from: index, to: index - 1,
|
||||
})));
|
||||
}
|
||||
if index + 1 < len {
|
||||
items.push(item(t, Icon::Forward, tr("Move down"), Some(Message::MovePlaylistTrack {
|
||||
id, track: track.clone(), from: index, to: index + 1,
|
||||
})));
|
||||
}
|
||||
items.push(item(t, Icon::Trash, tr("Remove from this playlist"), Some(Message::RemoveFromPlaylist(id, index))));
|
||||
}
|
||||
frame(t, track.full_title(), Some(track.artist_line()), Column::with_children(items).spacing(2).into())
|
||||
}
|
||||
Sheet::Track { track, choosing: true, .. } => {
|
||||
let mut items: Vec<Element<'a, Message>> = vec![item(
|
||||
t,
|
||||
Icon::Plus,
|
||||
tr("New playlist…"),
|
||||
Some(Message::OpenSheet(Sheet::NewPlaylist { name: String::new(), add: Some(track.clone()) })),
|
||||
)];
|
||||
items.extend(ctx.own_playlists.iter().map(|p| {
|
||||
item(t, Icon::Queue, &p.title, Some(Message::AddToPlaylist(p.id, track.clone())))
|
||||
}));
|
||||
frame(
|
||||
t,
|
||||
tr("Add to playlist").into(),
|
||||
Some(track.full_title()),
|
||||
iced::widget::scrollable(Column::with_children(items).spacing(2)).height(Length::Shrink).into(),
|
||||
)
|
||||
}
|
||||
Sheet::NewPlaylist { name, add } => {
|
||||
let can = !name.trim().is_empty();
|
||||
let hint = if add.is_some() { tr("The track goes in once it's created.") } else { tr("Only you can see it until you make it public.") };
|
||||
frame(
|
||||
t,
|
||||
tr("New playlist").into(),
|
||||
None,
|
||||
column![
|
||||
input(tr("Playlist name"), name, can.then_some(Message::CreatePlaylist)),
|
||||
para(hint, size::META, t.muted),
|
||||
actions(t, tr("Create"), can.then_some(Message::CreatePlaylist), false),
|
||||
]
|
||||
.spacing(14)
|
||||
.into(),
|
||||
)
|
||||
}
|
||||
Sheet::Rename { name, .. } => {
|
||||
let can = !name.trim().is_empty();
|
||||
frame(
|
||||
t,
|
||||
tr("Rename playlist").into(),
|
||||
None,
|
||||
column![
|
||||
input(tr("Playlist name"), name, can.then_some(Message::RenamePlaylist)),
|
||||
actions(t, tr("Rename"), can.then_some(Message::RenamePlaylist), false),
|
||||
]
|
||||
.spacing(14)
|
||||
.into(),
|
||||
)
|
||||
}
|
||||
Sheet::ConfirmDelete { id, title } => frame(
|
||||
t,
|
||||
trf("Delete “{}”?", &[title]),
|
||||
None,
|
||||
column![
|
||||
para(tr("The playlist and its track order go away for good. The tracks stay in Yandex Music."), size::BODY, t.muted),
|
||||
actions(t, tr("Delete"), Some(Message::DeletePlaylist(*id)), true),
|
||||
]
|
||||
.spacing(16)
|
||||
.into(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn frame<'a>(t: &'static Tokens, title: String, subtitle: Option<String>, content: Element<'a, Message>) -> Element<'a, Message> {
|
||||
let mut head = column![line(title, theme::SEMIBOLD, size::SECTION, t.fg)].spacing(2);
|
||||
if let Some(sub) = subtitle {
|
||||
head = head.push(line(sub, theme::BODY, size::META, t.muted));
|
||||
}
|
||||
column![
|
||||
row![
|
||||
head.width(Fill).clip(true),
|
||||
button(icon(Icon::Close, t.muted)).padding(6).style(theme::icon_button(None)).on_press(Message::CloseSheet),
|
||||
]
|
||||
.align_y(Alignment::Start),
|
||||
content,
|
||||
]
|
||||
.spacing(14)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn item<'a>(t: &'static Tokens, glyph: Icon, label: &str, msg: Option<Message>) -> Element<'a, Message> {
|
||||
button(row![icon(glyph, t.muted), line(label.to_owned(), theme::MEDIUM, size::BODY, t.fg)].spacing(12).align_y(Alignment::Center))
|
||||
.width(Fill)
|
||||
.padding([9, 10])
|
||||
.style(theme::nav_item(false))
|
||||
.on_press_maybe(msg)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn divider<'a>() -> Element<'a, Message> {
|
||||
container(space()).width(Fill).height(1).style(theme::divider).into()
|
||||
}
|
||||
|
||||
fn input<'a>(placeholder: &'a str, value: &'a str, submit: Option<Message>) -> Element<'a, Message> {
|
||||
text_input(placeholder, value)
|
||||
.id(INPUT_ID)
|
||||
.on_input(Message::SheetText)
|
||||
.on_submit_maybe(submit)
|
||||
.padding([10, 14])
|
||||
.style(theme::search_field)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn actions<'a>(t: &'static Tokens, confirm: &'a str, msg: Option<Message>, danger: bool) -> Element<'a, Message> {
|
||||
let ink = if danger { iced::Color::WHITE } else { t.accent_ink };
|
||||
let confirm = button(line(confirm, theme::SEMIBOLD, size::BODY, if msg.is_some() { ink } else { t.muted }))
|
||||
.padding([10, 18])
|
||||
.on_press_maybe(msg);
|
||||
let confirm = if danger {
|
||||
confirm.style(move |_, status| button::Style {
|
||||
background: Some(match status {
|
||||
button::Status::Hovered => theme::mix(t.like, iced::Color::WHITE, 0.1),
|
||||
_ => t.like,
|
||||
}.into()),
|
||||
text_color: iced::Color::WHITE,
|
||||
border: iced::Border { radius: radius::PILL.into(), ..Default::default() },
|
||||
..Default::default()
|
||||
})
|
||||
} else {
|
||||
confirm.style(theme::primary)
|
||||
};
|
||||
row![
|
||||
space().width(Fill),
|
||||
button(line(tr("Cancel"), theme::MEDIUM, size::BODY, t.fg)).padding([10, 18]).style(theme::ghost).on_press(Message::CloseSheet),
|
||||
confirm,
|
||||
]
|
||||
.spacing(8)
|
||||
.into()
|
||||
}
|
||||
|
||||
/// The status line that floats above the player bar after an action.
|
||||
pub fn toast<'a>(t: &'static Tokens, text: &'a str) -> Element<'a, Message> {
|
||||
container(
|
||||
container(line(text, theme::MEDIUM, size::BODY, t.bg))
|
||||
.padding([10, 18])
|
||||
.style(move |_| container::Style {
|
||||
background: Some(t.fg.into()),
|
||||
border: iced::Border { radius: radius::PILL.into(), ..Default::default() },
|
||||
shadow: iced::Shadow {
|
||||
color: iced::Color::BLACK.scale_alpha(0.3),
|
||||
offset: iced::Vector::new(0.0, 6.0),
|
||||
blur_radius: 18.0,
|
||||
},
|
||||
..Default::default()
|
||||
}),
|
||||
)
|
||||
.width(Fill)
|
||||
.height(Fill)
|
||||
.align_x(Alignment::Center)
|
||||
.align_y(iced::alignment::Vertical::Bottom)
|
||||
.padding(iced::Padding::default().bottom(96))
|
||||
.into()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ui::theme::DARK;
|
||||
|
||||
fn track_sheet() -> Sheet {
|
||||
let track = Track::from_json(r#"{"id":"7","title":"Хочешь?","artists":[{"id":3,"name":"Земфира"}],"albums":[{"id":5,"title":"A"}]}"#).unwrap();
|
||||
Sheet::Track { track, playlist: None, choosing: false }
|
||||
}
|
||||
|
||||
fn click(label: &str) -> Vec<String> {
|
||||
let sheet = track_sheet();
|
||||
let mut ui = iced_test::simulator(overlay(&DARK, &sheet, Ctx { playing: false, liked: false, disliked: false, own_playlists: &[] }));
|
||||
ui.click(label).unwrap_or_else(|e| panic!("can't click {label}: {e:?}"));
|
||||
ui.into_messages().map(|m| format!("{m:?}").chars().take(40).collect()).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn menu_for_the_playing_track_offers_next_track() {
|
||||
let sheet = track_sheet();
|
||||
let mut ui = iced_test::simulator(overlay(&DARK, &sheet, Ctx { playing: true, liked: false, disliked: false, own_playlists: &[] }));
|
||||
assert!(ui.find("Play next").is_err());
|
||||
ui.click("Next track").expect("next track item");
|
||||
let messages: Vec<String> = ui.into_messages().map(|m| format!("{m:?}")).collect();
|
||||
assert_eq!(messages, ["SkipFromMenu"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn menu_items_send_their_action_and_not_close() {
|
||||
for (label, expected) in [("Play next", "PlayNext("), ("Add to queue", "AddToQueue("), ("Go to album", "Navigate(Album(5))"), ("Play similar tracks", "PlaySimilar(")] {
|
||||
let messages = click(label);
|
||||
assert!(messages.first().is_some_and(|m| m.starts_with(expected)), "{label}: {messages:?}");
|
||||
assert!(!messages.iter().any(|m| m.starts_with("CloseSheet")), "{label} also closed the sheet: {messages:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
//! Left column: wordmark, main navigation, playlists and the account chip.
|
||||
|
||||
use iced::widget::{Column, button, column, container, row, scrollable, space};
|
||||
use iced::{Alignment, Element, Fill, Padding};
|
||||
|
||||
use ym_api::{Account, LibraryPlaylists};
|
||||
|
||||
use crate::app::Message;
|
||||
use crate::covers::{self, Covers};
|
||||
use crate::route::Route;
|
||||
use crate::ui::icons::{Icon, icon};
|
||||
use crate::ui::theme::{self, Tokens, radius, shell, size};
|
||||
use crate::ui::{line, para};
|
||||
use crate::i18n::tr;
|
||||
|
||||
pub fn view<'a>(
|
||||
t: &'static Tokens,
|
||||
current: Route,
|
||||
me: &'a Account,
|
||||
playlists: Option<&'a LibraryPlaylists>,
|
||||
covers: &Covers,
|
||||
) -> Element<'a, Message> {
|
||||
let nav = Column::with_children(Route::NAV.map(|route| nav_item(t, route, route == current)))
|
||||
.spacing(2);
|
||||
|
||||
let playlists_head = row![
|
||||
line(tr("Playlists"), theme::MEDIUM, size::SMALL, t.muted),
|
||||
space().width(Fill),
|
||||
button(icon(Icon::Plus, t.muted))
|
||||
.padding(4)
|
||||
.style(theme::icon_button(None))
|
||||
.on_press(Message::OpenSheet(crate::widgets::sheet::Sheet::NewPlaylist { name: String::new(), add: None })),
|
||||
]
|
||||
.align_y(Alignment::Center)
|
||||
.padding(Padding::default().left(10).right(2));
|
||||
|
||||
let list: Element<'a, Message> = match playlists {
|
||||
Some(p) if !p.own.is_empty() || !p.liked.is_empty() => {
|
||||
let items = p.own.iter().chain(&p.liked).map(|pl| {
|
||||
let route = Route::Playlist(pl.id);
|
||||
let active = route == current;
|
||||
button(
|
||||
row![
|
||||
covers::view(covers, pl.cover_url(covers::SMALL), pl.hues(), 30.0, radius::SMALL_COVER),
|
||||
line(pl.title.clone(), theme::BODY, size::BODY, if active { t.fg } else { t.muted }),
|
||||
]
|
||||
.spacing(10)
|
||||
.align_y(Alignment::Center),
|
||||
)
|
||||
.width(Fill)
|
||||
.padding([5, 10])
|
||||
.style(theme::nav_item(active))
|
||||
.on_press(Message::Navigate(route))
|
||||
.into()
|
||||
});
|
||||
scrollable(Column::with_children(items).spacing(1))
|
||||
.height(Fill)
|
||||
.style(theme::scroll)
|
||||
.into()
|
||||
}
|
||||
Some(_) => container(para(tr("Playlists you make or like show up here."), size::META, t.muted))
|
||||
.padding(Padding::default().left(10).right(10))
|
||||
.into(),
|
||||
None => container(para(tr("Loading playlists…"), size::META, t.muted))
|
||||
.padding(Padding::default().left(10).right(10))
|
||||
.into(),
|
||||
};
|
||||
|
||||
column![
|
||||
wordmark(t),
|
||||
nav,
|
||||
column![playlists_head, list].spacing(6).height(Fill),
|
||||
account(t, current, me),
|
||||
]
|
||||
.spacing(22)
|
||||
.padding(Padding::default().top(18).bottom(12).left(12).right(12))
|
||||
.width(shell::SIDEBAR)
|
||||
.height(Fill)
|
||||
.into()
|
||||
}
|
||||
|
||||
pub fn wordmark<'a, M: 'a>(t: &'static Tokens) -> Element<'a, M> {
|
||||
row![
|
||||
line("musyx", theme::DISPLAY, size::WORDMARK, t.fg),
|
||||
container(space()).width(7).height(7).style(theme::accent_dot),
|
||||
]
|
||||
.spacing(3)
|
||||
.align_y(Alignment::End)
|
||||
.padding(Padding::default().left(10).top(4).bottom(4))
|
||||
.into()
|
||||
}
|
||||
|
||||
fn nav_item<'a>(t: &'static Tokens, route: Route, active: bool) -> Element<'a, Message> {
|
||||
let tint = if active { t.accent } else { t.muted };
|
||||
button(
|
||||
row![
|
||||
icon(route.icon(), tint),
|
||||
line(route.label(), theme::MEDIUM, size::BODY, if active { t.fg } else { t.muted }),
|
||||
]
|
||||
.spacing(12)
|
||||
.align_y(Alignment::Center),
|
||||
)
|
||||
.width(Fill)
|
||||
.padding([9, 10])
|
||||
.style(theme::nav_item(active))
|
||||
.on_press(Message::Navigate(route))
|
||||
.into()
|
||||
}
|
||||
|
||||
fn account<'a>(t: &'static Tokens, current: Route, account: &'a Account) -> Element<'a, Message> {
|
||||
let avatar = container(line(account.initial().to_string(), theme::SEMIBOLD, size::BODY, iced::Color::from_rgb8(0x1C, 0x1A, 0x22)))
|
||||
.center(32)
|
||||
.style(theme::avatar);
|
||||
|
||||
let (plan, plan_colour) = if account.has_plus {
|
||||
(tr("Plus subscription"), t.ok)
|
||||
} else {
|
||||
(tr("No Plus: 30-second previews"), t.muted)
|
||||
};
|
||||
|
||||
let settings_active = current == Route::Settings;
|
||||
column![
|
||||
container(space()).width(Fill).height(1).style(theme::divider),
|
||||
row![
|
||||
avatar,
|
||||
column![
|
||||
line(&account.name, theme::MEDIUM, 13.5, t.fg),
|
||||
line(plan, theme::BODY, 12.0, plan_colour),
|
||||
]
|
||||
.spacing(1)
|
||||
.width(Fill)
|
||||
.clip(true),
|
||||
button(icon(Icon::Settings, if settings_active { t.accent } else { t.muted }))
|
||||
.padding(8)
|
||||
.style(theme::icon_button(None))
|
||||
.on_press(Message::Navigate(Route::Settings)),
|
||||
]
|
||||
.spacing(10)
|
||||
.align_y(Alignment::Center)
|
||||
.padding(Padding::default().left(10).right(2)),
|
||||
]
|
||||
.spacing(14)
|
||||
.into()
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
//! Back button and search field above the content.
|
||||
|
||||
use iced::widget::{button, row, text_input};
|
||||
use iced::{Alignment, Element};
|
||||
|
||||
use crate::app::Message;
|
||||
use crate::screens::search::Msg;
|
||||
use crate::ui::icons::{Icon, icon};
|
||||
use crate::ui::theme::{self, Tokens, size, space};
|
||||
use crate::i18n::tr;
|
||||
|
||||
pub const SEARCH_ID: &str = "search";
|
||||
|
||||
pub fn view<'a>(t: &'static Tokens, can_go_back: bool, query: &str) -> Element<'a, Message> {
|
||||
let back = button(icon(Icon::Back, if can_go_back { t.fg } else { t.muted }))
|
||||
.padding(8)
|
||||
.style(theme::icon_button(None))
|
||||
.on_press_maybe(can_go_back.then_some(Message::Back));
|
||||
|
||||
let search = text_input(tr("Search artists, tracks, podcasts"), query)
|
||||
.id(SEARCH_ID)
|
||||
.on_input(|text| Message::Search(Msg::Changed(text)))
|
||||
.on_submit(Message::Search(Msg::Submit))
|
||||
.font(theme::BODY)
|
||||
.size(size::BODY)
|
||||
.padding([10, 16])
|
||||
.width(420)
|
||||
.style(theme::search_field);
|
||||
|
||||
row![back, search]
|
||||
.spacing(10)
|
||||
.align_y(Alignment::Center)
|
||||
.padding([14.0, space::GUTTER])
|
||||
.into()
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
//! A whole track list, built once and reused (`lazy`) until something it
|
||||
//! shows changes. Position ticks four times a second must not rebuild 1000 rows.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use iced::widget::{Column, lazy};
|
||||
use iced::Element;
|
||||
use ym_api::{LikedIds, PlaylistId, Track};
|
||||
|
||||
use crate::app::Message;
|
||||
use crate::covers::{self, Covers};
|
||||
use crate::screens::collection::Liked;
|
||||
use crate::widgets::sheet::Sheet;
|
||||
use crate::ui::theme::{self, Tokens, radius};
|
||||
use crate::widgets::track_row::{self, Columns};
|
||||
|
||||
/// What every list needs from app state.
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Env<'a> {
|
||||
pub t: &'static Tokens,
|
||||
pub covers: &'a Covers,
|
||||
pub liked: &'a Liked,
|
||||
pub likes: &'a LikedIds,
|
||||
pub playing: Option<&'a str>,
|
||||
/// The signed-in user's uid, to tell their own playlists apart.
|
||||
pub me: i64,
|
||||
}
|
||||
|
||||
#[derive(Hash)]
|
||||
struct Key {
|
||||
/// Identity of the list contents: the slice address and length.
|
||||
list: (usize, usize),
|
||||
columns: Columns,
|
||||
playing: Option<String>,
|
||||
covers: u64,
|
||||
liked: u64,
|
||||
dark: bool,
|
||||
playlist: Option<PlaylistId>,
|
||||
lang: crate::i18n::Lang,
|
||||
}
|
||||
|
||||
pub fn view<'a>(env: Env<'a>, tracks: &Arc<[Track]>, columns: Columns) -> Element<'a, Message> {
|
||||
build(env, tracks, columns, None, None)
|
||||
}
|
||||
|
||||
/// [`view`] with chart movements, one per track.
|
||||
pub fn view_with<'a>(
|
||||
env: Env<'a>,
|
||||
tracks: &Arc<[Track]>,
|
||||
columns: Columns,
|
||||
shifts: Option<Arc<[i32]>>,
|
||||
) -> Element<'a, Message> {
|
||||
build(env, tracks, columns, shifts, None)
|
||||
}
|
||||
|
||||
/// One of the user's own playlists: rows get move and remove actions.
|
||||
pub fn view_own_playlist<'a>(env: Env<'a>, tracks: &Arc<[Track]>, id: PlaylistId) -> Element<'a, Message> {
|
||||
build(env, tracks, Columns::FULL, None, Some(id))
|
||||
}
|
||||
|
||||
fn build<'a>(
|
||||
env: Env<'a>,
|
||||
tracks: &Arc<[Track]>,
|
||||
columns: Columns,
|
||||
shifts: Option<Arc<[i32]>>,
|
||||
playlist: Option<PlaylistId>,
|
||||
) -> Element<'a, Message> {
|
||||
let Env { t, covers, liked, playing, .. } = env;
|
||||
let key = Key {
|
||||
list: (tracks.as_ptr() as usize, tracks.len()),
|
||||
columns,
|
||||
// Only matters when the playing track is in this list.
|
||||
playing: playing.filter(|id| tracks.iter().any(|tr| tr.id == *id)).map(str::to_owned),
|
||||
covers: if columns.cover { covers.version() } else { 0 },
|
||||
liked: liked.version(),
|
||||
dark: t == &theme::DARK,
|
||||
playlist,
|
||||
lang: crate::i18n::lang(),
|
||||
};
|
||||
let tracks = tracks.clone();
|
||||
|
||||
lazy(key, move |key: &Key| {
|
||||
let rows = tracks.iter().enumerate().map(|(index, track)| {
|
||||
let cover = columns.cover.then(|| {
|
||||
covers::view(covers, track.cover_url(covers::SMALL), track.hues(), 40.0, radius::SMALL_COVER)
|
||||
});
|
||||
track_row::view(
|
||||
t,
|
||||
track_row::Row {
|
||||
index,
|
||||
track,
|
||||
playing: key.playing.as_deref() == Some(track.id.as_str()),
|
||||
liked: liked.contains(&track.id),
|
||||
on_like: Message::LikeTrack(track.clone(), !liked.contains(&track.id)),
|
||||
on_more: Message::OpenSheet(Sheet::Track {
|
||||
track: track.clone(),
|
||||
playlist: key.playlist.map(|id| (id, index, tracks.len())),
|
||||
choosing: false,
|
||||
}),
|
||||
cover,
|
||||
columns,
|
||||
shift: shifts.as_ref().and_then(|s| s.get(index).copied()).unwrap_or(0),
|
||||
on_press: Message::PlayTracks { tracks: tracks.clone(), start: index, shuffle: false },
|
||||
},
|
||||
)
|
||||
});
|
||||
Column::with_children(std::iter::once(track_row::header(t, columns)).chain(rows)).spacing(2)
|
||||
})
|
||||
.into()
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
//! One row in a track list: number (or a playing marker), cover, title and
|
||||
//! artists, album, like state, duration. Clicking anywhere plays it.
|
||||
|
||||
use iced::widget::{button, column, container, row, space};
|
||||
use iced::{Alignment, Element, Fill, Length};
|
||||
use ym_api::Track;
|
||||
|
||||
use crate::app::Message;
|
||||
use crate::ui::icons::{Icon, icon, sized};
|
||||
use crate::ui::line;
|
||||
use crate::ui::theme::{self, Tokens, size};
|
||||
use crate::widgets::player_bar::clock;
|
||||
use crate::i18n::tr;
|
||||
|
||||
pub const HEIGHT: f32 = 52.0;
|
||||
|
||||
/// Which columns a list shows. Album pages drop the cover and album.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct Columns {
|
||||
pub cover: bool,
|
||||
pub album: bool,
|
||||
/// Chart lists show how far each track moved.
|
||||
pub shift: bool,
|
||||
}
|
||||
|
||||
impl Columns {
|
||||
pub const FULL: Columns = Columns { cover: true, album: true, shift: false };
|
||||
pub const ALBUM_PAGE: Columns = Columns { cover: false, album: false, shift: false };
|
||||
pub const CHART: Columns = Columns { cover: true, album: true, shift: true };
|
||||
}
|
||||
|
||||
pub struct Row<'a> {
|
||||
pub index: usize,
|
||||
pub track: &'a Track,
|
||||
pub playing: bool,
|
||||
pub liked: bool,
|
||||
pub cover: Option<Element<'static, Message>>,
|
||||
pub columns: Columns,
|
||||
/// Places moved in the chart; shown when `columns.shift`.
|
||||
pub shift: i32,
|
||||
pub on_press: Message,
|
||||
pub on_like: Message,
|
||||
pub on_more: Message,
|
||||
}
|
||||
|
||||
/// Builds an owned (`'static`) row so lists can sit inside `lazy`.
|
||||
pub fn view(t: &'static Tokens, r: Row<'_>) -> Element<'static, Message> {
|
||||
let track = r.track;
|
||||
let number: Element<'static, Message> = if r.playing {
|
||||
sized(Icon::Wave, 16.0, t.accent).into()
|
||||
} else {
|
||||
line((r.index + 1).to_string(), theme::BODY, size::META, t.muted).into()
|
||||
};
|
||||
|
||||
let unavailable = !track.available;
|
||||
let title_colour = match (unavailable, r.playing) {
|
||||
(true, _) => t.muted,
|
||||
(false, true) => t.accent,
|
||||
(false, false) => t.fg,
|
||||
};
|
||||
|
||||
let mut content = row![container(number).width(36).center_x(36)]
|
||||
.spacing(12)
|
||||
.align_y(Alignment::Center)
|
||||
.height(HEIGHT);
|
||||
if r.columns.shift {
|
||||
content = content.push(crate::screens::browse::shift_marker(t, r.shift));
|
||||
}
|
||||
if r.columns.cover {
|
||||
content = content.push(r.cover.unwrap_or_else(|| space().width(40).into()));
|
||||
}
|
||||
content = content.push(
|
||||
column![
|
||||
line(track.full_title(), theme::MEDIUM, size::BODY, title_colour),
|
||||
line(track.artist_line(), theme::BODY, size::META, t.muted),
|
||||
]
|
||||
.spacing(1)
|
||||
.width(Length::FillPortion(22))
|
||||
.clip(true),
|
||||
);
|
||||
if r.columns.album {
|
||||
content = content.push(
|
||||
container(line(
|
||||
track.album.as_ref().map(|a| a.title.clone()).unwrap_or_default(),
|
||||
theme::BODY,
|
||||
size::META,
|
||||
t.muted,
|
||||
))
|
||||
.width(Length::FillPortion(14))
|
||||
.clip(true),
|
||||
);
|
||||
}
|
||||
content = content
|
||||
.push(
|
||||
button(icon(
|
||||
if r.liked { Icon::HeartFilled } else { Icon::Heart },
|
||||
if r.liked { t.like } else { t.muted.scale_alpha(0.35) },
|
||||
))
|
||||
.padding(8)
|
||||
.style(theme::icon_button(None))
|
||||
.on_press(r.on_like),
|
||||
)
|
||||
.push(
|
||||
container(line(clock(track.duration.as_secs_f32()), theme::BODY, size::META, t.muted))
|
||||
.width(52)
|
||||
.align_right(52),
|
||||
)
|
||||
.push(
|
||||
button(icon(Icon::More, t.muted))
|
||||
.padding(8)
|
||||
.style(theme::icon_button(None))
|
||||
.on_press(r.on_more),
|
||||
);
|
||||
|
||||
button(content)
|
||||
.width(Fill)
|
||||
.padding([0, 10])
|
||||
.style(theme::nav_item(false))
|
||||
.on_press_maybe((!unavailable).then_some(r.on_press))
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Column headings above a track list, aligned with [`view`].
|
||||
pub fn header(t: &'static Tokens, columns: Columns) -> Element<'static, Message> {
|
||||
let label = |s: &'static str| line(s, theme::BODY, size::SMALL, t.muted);
|
||||
let mut heads = row![container(label("#")).width(36).center_x(36)]
|
||||
.spacing(12)
|
||||
.align_y(Alignment::Center)
|
||||
.padding([0, 10]);
|
||||
if columns.shift {
|
||||
heads = heads.push(space().width(14));
|
||||
}
|
||||
if columns.cover {
|
||||
heads = heads.push(space().width(40));
|
||||
}
|
||||
heads = heads.push(container(label(tr("Title"))).width(Length::FillPortion(22)));
|
||||
if columns.album {
|
||||
heads = heads.push(container(label(tr("Album"))).width(Length::FillPortion(14)));
|
||||
}
|
||||
heads = heads
|
||||
.push(space().width(34))
|
||||
.push(container(icon(Icon::Clock, t.muted)).width(52).align_right(52))
|
||||
.push(space().width(34));
|
||||
|
||||
column![heads, container(space()).width(Fill).height(1).style(theme::divider)]
|
||||
.spacing(8)
|
||||
.into()
|
||||
}
|
||||
Reference in new issue
Block a user