yandex-music-rs/src/engine/widgets.rs

1893 lines
61 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use crate::api::models::Track;
use crate::covers::CoverCache;
use crate::engine::input::{Interaction, WidgetId};
use crate::engine::text::{RichSpan, TextAlign, TextStyle};
use crate::engine::theme::{YmPalette, TRACK_ROW_HEIGHT};
use crate::engine::types::{css_rgba, Color, CornerRadii, CursorKind, Pos2, Rect, Vec2};
use crate::engine::UiContext;
use crate::icons::Icon;
/// Exact 5.129.0 `.s7_MO4NdsYs7nPQALD8W` ContextMenu frame (`5495aae20be2440c.css`):
/// Real Dual Kawase `BlurRegion` + exact `pal.bg_menu` (`rgba(26,26,26,0.70)` dark / `hsla(0,0%,96%,0.70)` light)
/// + `16px` radius + analytical shadow + `8px` inner padding.
/// Returns the inner content `Rect` (`rect.shrink(8.0)`).
pub fn ym_menu_frame(ctx: &mut UiContext, pal: &YmPalette, rect: Rect) -> Rect {
ctx.register_blocker(rect);
let dl = ctx.dl();
// Analytical shadow: `box-shadow: 0 1px 20px 0 var(--ym-shadow-menu)`
dl.push_shadow(rect, 16.0, 20.0, 0.0, Vec2::new(0.0, 2.0), pal.shadow_menu);
// Real Dual Kawase backdrop-filter blur + translucent tint + subtle 1px border
dl.push_backdrop_blur(rect, 16.0, 2.0, pal.bg_menu, 1.0, pal.border_subtle);
rect.shrink(8.0)
}
#[derive(Debug, Clone, Copy, Default)]
pub struct MenuItemResponse {
pub clicked: bool,
pub hovered: bool,
pub rect: Rect,
}
/// Exact 5.129.0 ContextMenu item (`36px` height, `8px` hover radius, `0.30s` transition,
/// `20x20` leading icon, `14px Medium` label, optional active highlight/checkmark & submenu chevron).
#[allow(clippy::too_many_arguments)]
pub fn ym_menu_item(
ctx: &mut UiContext,
pal: &YmPalette,
id: WidgetId,
rect: Rect,
icon: Icon,
label: &str,
active: bool,
has_chevron: bool,
) -> MenuItemResponse {
ym_menu_item_ex(ctx, pal, id, rect, icon, label, active, false, false, has_chevron)
}
/// Full-featured menu item supporting `active` highlight, trailing `checked` checkmark (`CheckXxs`),
/// `disabled` state, and submenu `has_chevron` (`MenuArrowXxs`).
#[allow(clippy::too_many_arguments)]
pub fn ym_menu_item_ex(
ctx: &mut UiContext,
pal: &YmPalette,
id: WidgetId,
rect: Rect,
icon: Icon,
label: &str,
active: bool,
checked: bool,
disabled: bool,
has_chevron: bool,
) -> MenuItemResponse {
let resp = if disabled {
Interaction {
rect,
..Interaction::default()
}
} else {
ctx.interact(id, rect)
};
let hover_t = ctx.animate_bool(id.with("hov"), resp.hovered && !disabled, 0.30);
if hover_t > 0.01 {
let a = (22.0 * hover_t).round() as u8;
let bg = if pal.is_dark {
css_rgba(255, 255, 255, a)
} else {
css_rgba(0, 0, 0, a)
};
ctx.dl().push_rect(rect, 8.0, bg);
}
let fg = if disabled {
pal.text_disabled
} else if active {
if pal.is_dark {
pal.yellow
} else {
pal.outline_selected
}
} else if resp.hovered {
pal.text_bright
} else {
pal.text_primary
};
let icon_rect = Rect::from_center_size(Pos2::new(rect.left() + 18.0, rect.center_y()), Vec2::splat(20.0));
ctx.draw_icon(icon, icon_rect, fg);
let trailing_pad = if has_chevron || checked { 34.0 } else { 10.0 };
let text_rect = Rect::from_xywh(
rect.left() + 38.0,
rect.top(),
(rect.width() - 38.0 - trailing_pad).max(16.0),
rect.height(),
);
ctx.draw_text_ellipsis(label, TextStyle::medium(14.0, fg), text_rect, TextAlign::Left);
if checked {
let chk_col = if pal.is_dark {
pal.yellow
} else {
pal.outline_selected
};
let chk_rect = Rect::from_center_size(Pos2::new(rect.right() - 18.0, rect.center_y()), Vec2::splat(20.0));
ctx.draw_icon(Icon::CheckXxs, chk_rect, chk_col);
} else if has_chevron {
let chev_rect = Rect::from_center_size(Pos2::new(rect.right() - 18.0, rect.center_y()), Vec2::splat(20.0));
ctx.draw_icon(Icon::MenuArrowXxs, chev_rect, fg);
}
if resp.hovered && !disabled {
ctx.set_cursor(CursorKind::Pointer);
}
MenuItemResponse {
clicked: resp.clicked && !disabled,
hovered: resp.hovered && !disabled,
rect,
}
}
/// Exact `.VibeContextMenu_divider`: `1px` `pal.surface_list_active`, `margin-inline: 10px`, `margin-block: 6px` (`13px` slot).
pub fn ym_menu_divider(ctx: &mut UiContext, pal: &YmPalette, rect: Rect) {
let y = rect.center_y().round();
ctx.dl().push_rect(
Rect::from_xywh(rect.left() + 10.0, y, (rect.width() - 20.0).max(0.0), 1.0),
0.0,
pal.surface_list_active,
);
}
/// Interactive circular icon button with full disabled state support.
#[allow(clippy::too_many_arguments)]
pub fn icon_button(
ctx: &mut UiContext,
id: WidgetId,
rect: Rect,
icon: Icon,
icon_size: f32,
color: Color,
hover_color: Color,
hover_bg: Color,
disabled: bool,
tooltip: &str,
) -> Interaction {
if disabled {
let icon_rect = Rect::from_center_size(rect.center(), Vec2::splat(icon_size));
ctx.draw_icon(icon, icon_rect, color);
return Interaction {
rect,
..Interaction::default()
};
}
let resp = ctx.interact(id, rect);
let hover_t = ctx.animate_bool(id.with("hov"), resp.hovered, 0.20);
if hover_t > 0.01 && hover_bg.a > 0 {
let radius = rect.width().min(rect.height()) * 0.5;
ctx.dl()
.push_circle(rect.center(), radius, hover_bg.scale_alpha(hover_t));
}
let tint = color.lerp(hover_color, hover_t);
let icon_rect = Rect::from_center_size(rect.center(), Vec2::splat(icon_size));
ctx.draw_icon(icon, icon_rect, tint);
if resp.hovered {
ctx.set_cursor(CursorKind::Pointer);
if !tooltip.is_empty() {
ctx.show_tooltip(rect, tooltip);
}
}
resp
}
/// Primary bright yellow pill button (`#FFFF00` dark / `#FFEE00` light, `#000000` text & icon).
/// Auto-sizes width from `pos` and `height`; returns `(Interaction, Rect)`.
#[allow(clippy::too_many_arguments)]
pub fn yellow_pill_button(
ctx: &mut UiContext,
pal: &YmPalette,
id: WidgetId,
pos: Pos2,
icon: Option<Icon>,
label: &str,
height: f32,
disabled: bool,
) -> (Interaction, Rect) {
let text_sz = ctx.measure_text(label, TextStyle::medium(14.0, pal.on_yellow), None);
let icon_w = if icon.is_some() { 20.0 + 8.0 } else { 0.0 };
let w = (text_sz.x + icon_w + 36.0).max(100.0);
let rect = Rect::from_min_size(pos, Vec2::new(w, height));
let resp = yellow_pill_button_in(ctx, pal, id, rect, icon, label, disabled);
(resp, rect)
}
/// Primary yellow pill button drawn inside an explicit `rect`.
pub fn yellow_pill_button_in(
ctx: &mut UiContext,
pal: &YmPalette,
id: WidgetId,
rect: Rect,
icon: Option<Icon>,
label: &str,
disabled: bool,
) -> Interaction {
let resp = if disabled {
Interaction {
rect,
..Interaction::default()
}
} else {
ctx.interact(id, rect)
};
let bg = if disabled {
pal.border_control
} else if resp.pressed {
pal.yellow_pressed
} else if resp.hovered {
pal.yellow_hover
} else {
pal.yellow
};
let fg = if disabled { pal.text_disabled } else { pal.on_yellow };
let radius = rect.height() * 0.5;
ctx.dl().push_rect(rect, radius, bg);
let text_sz = ctx.measure_text(label, TextStyle::medium(14.0, fg), None);
let icon_space = if icon.is_some() { 26.0 } else { 0.0 };
let total_inner = icon_space + text_sz.x;
let start_x = rect.center_x() - total_inner * 0.5;
if let Some(ic) = icon {
ctx.draw_icon(
ic,
Rect::from_center_size(Pos2::new(start_x + 10.0, rect.center_y()), Vec2::splat(20.0)),
fg,
);
}
let tx_rect = Rect::from_xywh(
start_x + icon_space,
rect.top(),
(rect.right() - (start_x + icon_space) - 8.0).max(12.0),
rect.height(),
);
ctx.draw_text_ellipsis(label, TextStyle::medium(14.0, fg), tx_rect, TextAlign::Left);
if resp.hovered && !disabled {
ctx.set_cursor(CursorKind::Pointer);
}
resp
}
/// Exact verified 5.129.0 Secondary Pill / Filter Chip (`PlaylistFilter_root` & `Tab_tab_size_m`):
/// - Height: `40.0px`, padding `8px 16px`, gap `8px`, max-width `200px`, full pill radius
/// - Fill: `pal.chip_bg` (`#333` dark / `#E6E6E6` light), hover `pal.chip_hover`
/// - Border: `2px` `pal.outline_selected` (`#FFFF00` dark / `#0449C8` light) when `active`
/// - Font: `YS Text 14px Bold` (`pal.text_primary`)
#[allow(clippy::too_many_arguments)]
pub fn secondary_pill_button(
ctx: &mut UiContext,
pal: &YmPalette,
id: WidgetId,
pos: Pos2,
icon: Option<Icon>,
label: &str,
height: f32,
active: bool,
) -> (Interaction, Rect) {
let text_sz = ctx.measure_text(label, TextStyle::bold(14.0, pal.text_primary), None);
let icon_w = if icon.is_some() { 18.0 + 8.0 } else { 0.0 };
// Exact CSS: `padding: 8px 16px` (32px horizontal) + `2px` border each side (4px) = 36px; max-width 200px
let w = (text_sz.x + icon_w + 36.0).clamp(48.0, 200.0);
let rect = Rect::from_min_size(pos, Vec2::new(w, height));
let resp = secondary_pill_button_in(ctx, pal, id, rect, icon, label, active);
(resp, rect)
}
/// Secondary pill / filter chip drawn inside an explicit `rect`.
pub fn secondary_pill_button_in(
ctx: &mut UiContext,
pal: &YmPalette,
id: WidgetId,
rect: Rect,
icon: Option<Icon>,
label: &str,
active: bool,
) -> Interaction {
let resp = ctx.interact(id, rect);
let bg = if resp.hovered {
pal.chip_hover
} else {
pal.chip_bg
};
let border_col = if active { pal.outline_selected } else { bg };
let radius = rect.height() * 0.5;
ctx.dl()
.push_rect_bordered(rect, radius, bg, 2.0, border_col);
let fg = pal.text_primary;
let text_sz = ctx.measure_text(label, TextStyle::bold(14.0, fg), None);
let icon_space = if icon.is_some() { 24.0 } else { 0.0 };
let avail_inner = (rect.width() - 32.0).max(16.0);
let used_w = (icon_space + text_sz.x).min(avail_inner);
let start_x = rect.center_x() - used_w * 0.5;
if let Some(ic) = icon {
ctx.draw_icon(
ic,
Rect::from_center_size(Pos2::new(start_x + 8.0, rect.center_y()), Vec2::splat(16.0)),
fg,
);
}
let tx_rect = Rect::from_xywh(
start_x + icon_space,
rect.top(),
(avail_inner - icon_space).max(12.0),
rect.height(),
);
ctx.draw_text_ellipsis(label, TextStyle::bold(14.0, fg), tx_rect, TextAlign::Left);
if resp.hovered {
ctx.set_cursor(CursorKind::Pointer);
}
resp
}
/// Exact 4-stop Yandex Plus gradient avatar ring (`#ff5c4d -> #eb469f -> #8341ef -> #3f68f9`).
pub fn draw_plus_avatar_ring(ctx: &mut UiContext, center: Pos2, outer_r: f32, thickness: f32) {
ctx.dl().push_plus_ring(center, outer_r, thickness, 1.0);
}
/// Exact official Yandex Plus pill badge (`#ff5c4d -> #eb469f 30% -> #8341ef 75% -> #3f68f9`).
pub fn draw_plus_badge(
ctx: &mut UiContext,
id: WidgetId,
pos: Pos2,
label: &str,
) -> (Interaction, Rect) {
let clean = label.trim_start_matches('+').trim();
let text_sz = ctx.measure_text(clean, TextStyle::medium(14.0, Color::WHITE), None);
let pill_w = (22.0 + text_sz.x + 10.0).max(58.0);
let rect = Rect::from_min_size(pos, Vec2::new(pill_w, 20.0));
let resp = ctx.interact(id, rect);
let alpha = if resp.hovered { 0.90 } else { 1.0 };
ctx.dl()
.push_plus_gradient_rect(rect, rect.height() * 0.5, alpha);
let emblem_r = Rect::from_center_size(Pos2::new(rect.left() + 11.0, rect.center_y()), Vec2::splat(14.0));
ctx.draw_icon(Icon::Plus, emblem_r, Color::WHITE);
let tx_rect = Rect::from_xywh(
rect.left() + 21.0,
rect.top(),
(rect.width() - 25.0).max(12.0),
rect.height(),
);
ctx.draw_text_ellipsis(clean, TextStyle::medium(14.0, Color::WHITE), tx_rect, TextAlign::Left);
if resp.hovered {
ctx.set_cursor(CursorKind::Pointer);
}
(resp, rect)
}
/// Compute exact CSS `object-fit: cover` UV sub-rect so images never stretch when
/// `(tex_w / tex_h)` differs from `dst.w / dst.h`.
/// Uses `focal_y` (e.g. `0.35`) when cropping vertically so portrait faces/heads stay framed,
/// and `0.5` when cropping horizontally.
#[inline]
pub fn cover_fit_uv(tex_w: u32, tex_h: u32, dst: Rect, focal_y: f32) -> Rect {
if tex_w == 0 || tex_h == 0 || dst.w <= 0.0 || dst.h <= 0.0 {
return Rect::UNIT_UV;
}
let tex_ar = tex_w as f32 / tex_h as f32;
let dst_ar = dst.w / dst.h;
if (tex_ar - dst_ar).abs() < 0.015 {
return Rect::UNIT_UV;
}
if tex_ar > dst_ar {
let uw = (dst_ar / tex_ar).clamp(0.01, 1.0);
let u = (1.0 - uw) * 0.5;
Rect::from_xywh(u, 0.0, uw, 1.0)
} else {
let vh = (tex_ar / dst_ar).clamp(0.01, 1.0);
let v = (1.0 - vh) * focal_y.clamp(0.0, 1.0);
Rect::from_xywh(0.0, v, 1.0, vh)
}
}
/// Draw cover artwork with SDF rounded-rect / circular clip and CSS `object-fit: cover` UV crop:
/// - When `special_likes == true`: exact crimson vignette `#8D0F10 -> #59090A` + centered official `heart.png`
/// with SDF rounded-rect clip (ZERO `+9.5%` y-shift bug!). If a real CDN cover URI is loaded, uses it.
/// - Otherwise: renders real fetched cover texture with SDF rounded/circle clip, or theme-aware neutral placeholder.
#[allow(clippy::too_many_arguments)]
pub fn draw_cover_art(
ctx: &mut UiContext,
pal: &YmPalette,
covers: &mut CoverCache,
rect: Rect,
uri: Option<&str>,
seed: &str,
rounding: f32,
is_artist: bool,
special_likes: bool,
) -> (Color, f32) {
let cr = if is_artist {
CornerRadii::same(rect.width().min(rect.height()) * 0.5)
} else {
CornerRadii::same(rounding)
};
if special_likes {
// Prefer real CDN cover if provided & already loaded
if let Some(u) = uri {
let px = rect.width().max(rect.height()).ceil() as u32;
if let Some((tex_id, dom, hue, tw, th)) = covers.get_or_fetch_with_size(u, px) {
let uv = cover_fit_uv(tw, th, rect, 0.35);
ctx.dl().push_image_uv(rect, tex_id, uv, cr, Color::WHITE);
return (dom, hue);
}
}
// Verified 5.129.0 Favorites crimson vignette (#8D0F10 top -> #59090A bottom) + centered 3D heart.png
// with SDF rounded-rect clip (NO +9.5% y_shift!)
ctx.dl().push_gradient_rect(
rect,
cr,
Color::rgb(0x8D, 0x0F, 0x10),
Color::rgb(0x59, 0x09, 0x0A),
Vec2::new(0.0, 1.0),
);
let heart_tex = ctx.icons.get_official_heart(&mut ctx.textures);
let heart_sz = rect.size();
let heart_center = Pos2::new(
rect.center_x() + rect.width() * 0.02,
rect.center_y() + rect.height() * 0.075,
);
let heart_rect = Rect::from_center_size(heart_center, heart_sz);
ctx.dl().push_clip(rect, cr.tl);
ctx.dl().push_image(heart_rect, heart_tex, 0.0, Color::WHITE);
ctx.dl().pop_clip();
return (Color::rgb(0x8D, 0x0F, 0x10), 0.0);
}
if let Some(u) = uri {
let px = rect.width().max(rect.height()).ceil() as u32;
if let Some((tex_id, dom, hue, tw, th)) = covers.get_or_fetch_with_size(u, px) {
let uv = cover_fit_uv(tw, th, rect, 0.35);
ctx.dl().push_image_uv(rect, tex_id, uv, cr, Color::WHITE);
return (dom, hue);
}
}
// Theme-aware neutral placeholder (zero hardcoded dark boxes in Light theme!)
let placeholder_bg = if pal.is_dark {
Color::rgb(0x22, 0x22, 0x22)
} else {
Color::rgb(0xE8, 0xE8, 0xE8)
};
let emblem_col = if pal.is_dark {
css_rgba(255, 255, 255, 68)
} else {
css_rgba(0, 0, 0, 68)
};
ctx.dl()
.push_rect_bordered(rect, cr, placeholder_bg, 1.0, pal.border_subtle);
let emblem_sz = (rect.width() * 0.36).clamp(14.0, 48.0);
let ic = if is_artist || seed.contains("day") || seed.contains("dejavu") || seed.contains("vibe") {
Icon::MyVibe
} else {
Icon::Note
};
ctx.draw_icon(ic, Rect::from_center_size(rect.center(), Vec2::splat(emblem_sz)), emblem_col);
(placeholder_bg, 0.12)
}
/// Actions emitted by `track_row`.
#[derive(Debug, Clone, PartialEq)]
pub enum TrackRowAction {
None,
Play,
ToggleLike,
ToggleDislike,
OpenArtist(String, String),
OpenAlbum(String),
/// Left-click on `•••` OR right-click anywhere on the track row -> opens full `TrackContextMenu` at `Pos2`!
OpenContextMenu(Pos2),
}
#[derive(Debug, Clone, Copy)]
pub struct TrackRowOptions {
pub index: Option<usize>,
pub show_chart_rank: bool,
/// When `true` (default), renders the `40x40` track cover thumbnail;
/// when `false` (official Album page numbered tracklist), renders track number `1`, `2`, `3` instead.
pub show_cover: bool,
pub is_current: bool,
pub is_playing: bool,
pub is_liked: bool,
pub is_disliked: bool,
pub is_downloaded: bool,
pub is_downloading: bool,
pub download_progress: f32,
pub menu_open: bool,
}
impl Default for TrackRowOptions {
fn default() -> Self {
Self {
index: None,
show_chart_rank: false,
show_cover: true,
is_current: false,
is_playing: false,
is_liked: false,
is_disliked: false,
is_downloaded: false,
is_downloading: false,
download_progress: 0.0,
menu_open: false,
}
}
}
/// Pixel-accurate Yandex Music 5.129.0 TrackRow:
/// - `56px` height (`TRACK_ROW_HEIGHT`)
/// - `40x40` cover (`4px` radius) + hover Play/Pause or pulsing `8px` yellow dot when playing
/// - Clickable individual artists via `SeparatedArtists` (`draw_rich_text`)
/// - Dislike button, Like button, Duration <-> `•••` hover swap
/// - RIGHT-CLICK anywhere on row opens the exact same full `TrackContextMenu`!
#[allow(clippy::too_many_arguments)]
pub fn track_row(
ctx: &mut UiContext,
pal: &YmPalette,
covers: &mut CoverCache,
id: WidgetId,
rect: Rect,
track: &Track,
opts: TrackRowOptions,
) -> TrackRowAction {
if !ctx.is_visible_in_clip(rect, 16.0) {
return TrackRowAction::None;
}
let mut action = TrackRowAction::None;
let row_resp = ctx.interact(id, rect);
let hovered = row_resp.hovered || opts.menu_open;
let hover_t = ctx.animate_bool(id.with("hov"), hovered, 0.30);
let bg_rect = Rect::from_xywh(rect.left() - 8.0, rect.top(), rect.width() + 16.0, rect.height());
if opts.is_current {
ctx.dl().push_rect(bg_rect, 6.0, pal.surface_list_active);
} else if hover_t > 0.01 {
let a = (20.0 * hover_t).round() as u8;
let bg = if pal.is_dark {
css_rgba(255, 255, 255, a)
} else {
css_rgba(0, 0, 0, a)
};
ctx.dl().push_rect(bg_rect, 6.0, bg);
}
// Cover sits flush with `rect.left()` (matching official CSS `margin-inline: -8px; padding-inline: 8px`)
let mut x = rect.left();
let cy = rect.center_y();
// Optional chart rank column
if opts.show_chart_rank {
let col_w = 36.0;
let pos_num = track
.chart_position
.unwrap_or((opts.index.unwrap_or(0) + 1) as u32);
let num_rect = Rect::from_xywh(x, rect.top() + 8.0, 26.0, 20.0);
ctx.draw_text_ellipsis(
&pos_num.to_string(),
TextStyle::medium(14.0, pal.text_primary),
num_rect,
TextAlign::Center,
);
let (ch_icon, ch_col) = match track.chart_progress.as_deref() {
Some("up") => (Icon::ChartUp, pal.success),
Some("down") => (Icon::ChartDown, pal.accent_coral),
Some("new") => (Icon::ChartNew, pal.yellow),
_ => (Icon::ChartSame, pal.text_disabled),
};
ctx.draw_icon(
ch_icon,
Rect::from_center_size(Pos2::new(x + 13.0, cy + 12.0), Vec2::splat(10.0)),
ch_col,
);
x += col_w + 4.0;
}
// 40x40 rounded (rad=4px) Cover (`PlayButtonWithCover_root`) OR official Album numbered track slot (`!show_cover`)
let cover_rect = Rect::from_center_size(Pos2::new(x + 20.0, cy), Vec2::splat(40.0));
if opts.show_cover {
draw_cover_art(
ctx,
pal,
covers,
cover_rect,
track.cover_uri.as_deref(),
&track.id,
4.0,
false,
false,
);
if hovered || opts.is_current {
ctx.dl().push_rect(cover_rect, 4.0, css_rgba(0, 0, 0, 132));
if opts.is_current && !hovered {
// Exact `.PlayingAnimation_root`: 8px pulsing yellow dot (`scale 1.0 <-> 1.6`, 0.7s)
let t = ctx.input.time as f32;
let pulse = if opts.is_playing {
ctx.request_repaint_after(std::time::Duration::from_millis(40));
0.5 + 0.5 * (t * (std::f32::consts::TAU / 0.7)).sin()
} else {
0.0
};
let r = 4.0 * (1.0 + 0.6 * pulse);
ctx.dl().push_circle(cover_rect.center(), r, pal.yellow);
} else {
let play_icon = if opts.is_current && opts.is_playing {
Icon::PauseSmall
} else {
Icon::PlayFilledXs
};
ctx.draw_icon(
play_icon,
Rect::from_center_size(cover_rect.center(), Vec2::splat(24.0)),
Color::WHITE,
);
}
}
x += 52.0; // 40px cover + 12px gap
} else {
let num_slot = Rect::from_center_size(Pos2::new(x + 18.0, cy), Vec2::splat(36.0));
if hovered {
let play_icon = if opts.is_current && opts.is_playing {
Icon::PauseSmall
} else {
Icon::PlayFilledXs
};
ctx.draw_icon(
play_icon,
Rect::from_center_size(num_slot.center(), Vec2::splat(22.0)),
pal.text_bright,
);
} else if opts.is_current {
let t = ctx.input.time as f32;
let pulse = if opts.is_playing {
ctx.request_repaint_after(std::time::Duration::from_millis(40));
0.5 + 0.5 * (t * (std::f32::consts::TAU / 0.7)).sin()
} else {
0.0
};
let r = 4.0 * (1.0 + 0.6 * pulse);
ctx.dl().push_circle(num_slot.center(), r, pal.yellow);
} else {
let num = opts.index.unwrap_or(1);
ctx.draw_text_ellipsis(
&num.to_string(),
TextStyle::medium(14.0, pal.text_secondary),
num_slot,
TextAlign::Center,
);
}
x += 44.0; // 36px number slot + 8px gap
}
// Right controls area: Dislike (40px) + Like (40px, 56px left of duration) + Duration/More swap slot
// Exact 1280x800 CDP measurements against `favorites.jpg`: duration cx = right - 32.5, heart cx = duration cx - 56.0
let slot_more_cx = rect.right() - 32.5;
let slot_like_cx = slot_more_cx - 56.0;
let slot_dislike_cx = slot_like_cx - 40.0;
let right_reserved = if hovered || opts.is_disliked { 160.0 } else { 124.0 };
let title_col_w = (rect.right() - right_reserved - x - 8.0).max(80.0);
let title_color = if opts.is_disliked && !hovered {
pal.text_disabled
} else {
pal.text_primary
};
let ver_color = if opts.is_disliked && !hovered {
pal.text_disabled
} else {
pal.text_secondary
};
// Title + optional version + explicit badge + downloaded/downloading badge
let badge_w = if track.is_explicit { 22.0 } else { 0.0 }
+ if opts.is_downloaded || opts.is_downloading {
20.0
} else {
0.0
};
let title_rect = Rect::from_xywh(x, rect.top() + 9.5, (title_col_w - badge_w).max(40.0), 20.0);
let version_text = track
.version
.as_deref()
.filter(|v| !v.is_empty())
.map(|v| format!(" {}", v));
let mut title_spans = vec![RichSpan::new(&track.title, TextStyle::medium(14.0, title_color))];
if let Some(ref vt) = version_text {
title_spans.push(RichSpan::new(vt, TextStyle::medium(14.0, ver_color)));
}
let measured_title_w = ctx
.measure_text(
&match &version_text {
Some(vt) => format!("{}{}", track.title, vt),
None => track.title.clone(),
},
TextStyle::medium(14.0, title_color),
None,
)
.x
.min(title_rect.width());
ctx.draw_rich_text(&title_spans, title_rect, Some(1), TextAlign::Left);
let mut badge_x = title_rect.left() + measured_title_w + 4.0;
if track.is_explicit {
ctx.draw_icon(
Icon::Explicit,
Rect::from_center_size(Pos2::new(badge_x + 9.0, title_rect.center_y()), Vec2::splat(18.0)),
ver_color,
);
badge_x += 22.0;
}
if opts.is_downloading {
draw_downloading_progress(
ctx,
pal,
Pos2::new(badge_x + 8.0, title_rect.center_y()),
16.0,
opts.download_progress,
false,
false,
);
} else if opts.is_downloaded {
ctx.draw_icon(
Icon::Downloaded,
Rect::from_center_size(Pos2::new(badge_x + 8.0, title_rect.center_y()), Vec2::splat(16.0)),
pal.success,
);
}
// Individually clickable multi-artists (`SeparatedArtists`)!
let artist_rect = Rect::from_xywh(x, rect.top() + 30.0, title_col_w, 20.0);
let mut clicked_sub_control = false;
if let Some((aid, aname)) = separated_artists(
ctx,
pal,
id.with("artists"),
artist_rect,
&track.artists,
14.0,
if opts.is_disliked && !hovered {
pal.text_disabled
} else {
pal.text_secondary
},
pal.text_primary,
TextAlign::Left,
) {
action = TrackRowAction::OpenArtist(aid, aname);
clicked_sub_control = true;
}
// Dislike button (visible on hover or when disliked)
let dislike_rect = Rect::from_center_size(Pos2::new(slot_dislike_cx, cy), Vec2::splat(36.0));
if hovered || opts.is_disliked {
let d_resp = ctx.interact(id.with("dislike"), dislike_rect);
let d_col = if opts.is_disliked {
pal.accent_coral
} else if d_resp.hovered {
pal.text_primary
} else {
pal.text_secondary
};
ctx.draw_icon(
if opts.is_disliked {
Icon::DislikedXxs
} else {
Icon::DislikeXxs
},
Rect::from_center_size(dislike_rect.center(), Vec2::splat(20.0)),
d_col,
);
if d_resp.hovered {
ctx.set_cursor(CursorKind::Pointer);
}
if d_resp.clicked {
action = TrackRowAction::ToggleDislike;
clicked_sub_control = true;
}
}
// Like button (40x40 slot) — official 5.129.0 renders muted gray hearts in track lists (`favorites.jpg`)
let like_rect = Rect::from_center_size(Pos2::new(slot_like_cx, cy), Vec2::splat(36.0));
if hovered || opts.is_liked {
let l_resp = ctx.interact(id.with("like"), like_rect);
let l_col = if l_resp.hovered {
pal.text_primary
} else {
pal.text_secondary
};
ctx.draw_icon(
if opts.is_liked {
Icon::LikedXxs
} else {
Icon::LikeXxs
},
Rect::from_center_size(like_rect.center(), Vec2::splat(20.0)),
l_col,
);
if l_resp.hovered {
ctx.set_cursor(CursorKind::Pointer);
}
if l_resp.clicked {
action = TrackRowAction::ToggleLike;
clicked_sub_control = true;
}
}
// Right-most 40x40 slot: Duration (idle) <-> `•••` (hover / menu open)
let more_rect = Rect::from_center_size(Pos2::new(slot_more_cx, cy), Vec2::splat(40.0));
let more_resp = ctx.interact(id.with("more"), more_rect);
if hovered || opts.menu_open {
let m_col = if more_resp.hovered || opts.menu_open {
pal.text_primary
} else {
pal.text_secondary
};
ctx.draw_icon(
Icon::More,
Rect::from_center_size(more_rect.center(), Vec2::splat(24.0)),
m_col,
);
if more_resp.hovered {
ctx.set_cursor(CursorKind::Pointer);
}
if more_resp.clicked {
action = TrackRowAction::OpenContextMenu(Pos2::new(more_rect.right(), more_rect.bottom() + 4.0));
clicked_sub_control = true;
}
} else {
ctx.draw_text_ellipsis(
&track.duration_formatted(),
TextStyle::medium(
14.0,
if opts.is_disliked {
pal.text_disabled
} else {
pal.text_secondary
},
),
more_rect,
TextAlign::Center,
);
}
// RIGHT-CLICK anywhere on track row opens exact same full TrackContextMenu!
if row_resp.right_clicked {
let pos = ctx
.input
.pointer_pos
.unwrap_or(Pos2::new(rect.right() - 28.0, rect.bottom()));
action = TrackRowAction::OpenContextMenu(pos);
} else if !clicked_sub_control && row_resp.clicked {
action = TrackRowAction::Play;
}
if hovered {
ctx.set_cursor(CursorKind::Pointer);
}
let _ = TRACK_ROW_HEIGHT;
action
}
/// Clickable multi-artist link line (`SeparatedArtists` from `1374` mod `13946`).
/// Each artist renders as an independent hoverable/clickable span separated by `", "`.
/// Returns `Some((artist_id, artist_name))` when an individual artist is clicked!
#[allow(clippy::too_many_arguments)]
pub fn separated_artists(
ctx: &mut UiContext,
_pal: &YmPalette,
id: WidgetId,
rect: Rect,
artists: &[crate::api::models::ArtistSummary],
font_size: f32,
normal_color: Color,
hover_color: Color,
align: TextAlign,
) -> Option<(String, String)> {
if artists.is_empty() {
ctx.draw_text_ellipsis(
"—",
TextStyle::medium(font_size, normal_color),
rect,
align,
);
return None;
}
// Measure approximate spans first to highlight hovered individual artist cleanly
let pointer = ctx.input.pointer_pos;
let clip = {
let (cr, _) = ctx.dl().current_clip();
if cr.is_empty() { None } else { Some(cr) }
};
let in_line = ctx.input.is_hovered_in(ctx.current_layer(), rect, clip);
// Pre-pass: estimate horizontal span positions for single-line hover tinting
let sep_w = ctx
.measure_text(", ", TextStyle::medium(font_size, normal_color), None)
.x;
let mut total_w = 0.0f32;
let mut widths = Vec::with_capacity(artists.len());
for (i, a) in artists.iter().enumerate() {
let w = ctx
.measure_text(&a.name, TextStyle::medium(font_size, normal_color), None)
.x;
widths.push(w);
total_w += w;
if i + 1 < artists.len() {
total_w += sep_w;
}
}
let start_x = match align {
TextAlign::Left => rect.x,
TextAlign::Center => rect.x + ((rect.w - total_w.min(rect.w)) * 0.5).max(0.0),
TextAlign::Right => (rect.right() - total_w.min(rect.w)).max(rect.x),
};
let mut hovered_idx: Option<usize> = None;
if in_line {
if let Some(p) = pointer {
let mut cx = start_x;
for (i, &w) in widths.iter().enumerate() {
if p.x >= cx && p.x <= (cx + w).min(rect.right()) {
hovered_idx = Some(i);
break;
}
cx += w + sep_w;
}
}
}
let mut spans: Vec<RichSpan<'_>> = Vec::with_capacity(artists.len() * 2);
for (i, a) in artists.iter().enumerate() {
if i > 0 {
spans.push(RichSpan::new(", ", TextStyle::medium(font_size, normal_color)));
}
let col = if hovered_idx == Some(i) {
hover_color
} else {
normal_color
};
spans.push(RichSpan::tagged(
&a.name,
TextStyle::medium(font_size, col),
i + 1,
));
}
let span_rects = ctx.draw_rich_text(&spans, rect, Some(1), align);
for (meta, s_rect) in span_rects {
let idx = meta - 1;
if let Some(artist) = artists.get(idx) {
let hit = s_rect.intersect(rect);
let resp = ctx.interact(id.with(idx), hit);
if resp.hovered {
ctx.set_cursor(CursorKind::Pointer);
// Crisp subtle hover underline matching Chromium link hover
let uy = (hit.bottom() - 2.0).min(rect.bottom() - 1.0);
ctx.dl().push_rect(
Rect::from_xywh(hit.x, uy, hit.w, 1.0),
0.0,
hover_color.scale_alpha(0.75),
);
}
if resp.clicked {
return Some((artist.id.clone(), artist.name.clone()));
}
}
}
None
}
#[derive(Debug, Clone, Copy, Default)]
pub struct MediaCardAction {
pub clicked: bool,
pub play_clicked: bool,
pub right_clicked_pos: Option<Pos2>,
}
/// Square Playlist / Album / Circular Artist card matching Yandex Music 5.129.0:
/// - Smooth `rgba(0,0,0,0.64)` hover scrim + `48px` white filled Play button at bottom-left
/// - Supports right-click context menu!
#[allow(clippy::too_many_arguments)]
pub fn media_card(
ctx: &mut UiContext,
pal: &YmPalette,
covers: &mut CoverCache,
id: WidgetId,
rect: Rect,
title: &str,
subtitle: &str,
cover_uri: Option<&str>,
seed: &str,
is_artist: bool,
special_likes: bool,
) -> MediaCardAction {
if !ctx.is_visible_in_clip(rect, 16.0) {
return MediaCardAction::default();
}
let width = rect.width();
let resp = ctx.interact(id, rect);
let cover_rect = Rect::from_min_size(rect.min(), Vec2::splat(width));
draw_cover_art(
ctx,
pal,
covers,
cover_rect,
cover_uri,
seed,
6.0,
is_artist,
special_likes,
);
let mut play_clicked = false;
let hov_t = ctx.animate_bool(id.with("hov"), resp.hovered, 0.30);
if hov_t > 0.01 {
let cr = if is_artist {
CornerRadii::same(width * 0.5)
} else {
CornerRadii::same(6.0)
};
ctx.dl()
.push_rect(cover_rect, cr, css_rgba(0, 0, 0, (163.0 * hov_t).round() as u8));
let btn_center = Pos2::new(cover_rect.left() + 36.0, cover_rect.bottom() - 36.0);
let btn_rect = Rect::from_center_size(btn_center, Vec2::splat(48.0));
let play_resp = ctx.interact(id.with("play"), btn_rect);
let btn_hov_t = ctx.animate_bool(id.with("play_hov"), play_resp.hovered, 0.25);
let icon_sz = (44.0 + 4.0 * hov_t) * (1.0 + 0.05 * btn_hov_t);
ctx.draw_icon(
Icon::PlayFilled,
Rect::from_center_size(btn_center, Vec2::splat(icon_sz)),
Color::WHITE.with_alpha_f32(hov_t),
);
if play_resp.clicked {
play_clicked = true;
}
}
let align = if is_artist {
TextAlign::Center
} else {
TextAlign::Left
};
let t_rect = Rect::from_xywh(rect.left(), cover_rect.bottom() + 8.0, width, 20.0);
ctx.draw_text_ellipsis(title, TextStyle::medium(13.0, pal.text_primary), t_rect, align);
if !subtitle.is_empty() {
let s_rect = Rect::from_xywh(rect.left(), t_rect.bottom() + 2.0, width, 18.0);
ctx.draw_text_ellipsis(subtitle, TextStyle::medium(13.0, pal.text_secondary), s_rect, align);
}
if resp.hovered {
ctx.set_cursor(CursorKind::Pointer);
}
MediaCardAction {
clicked: resp.clicked && !play_clicked,
play_clicked,
right_clicked_pos: if resp.right_clicked {
ctx.input.pointer_pos.or(Some(rect.center()))
} else {
None
},
}
}
/// Exact verified 5.129.0 `.MixesGridMixCard` (`pages_api_spec.md` §4.7 & `themes_i18n_icons_spec.md` §4.3):
/// - Folder tab: exact `8px` tall (`y = top..top+8`), `8px` inset each side, `6px 6px 0 0` radius,
/// **`50%` opacity** (`tab_col.with_alpha_f32(0.50)`), `1px solid rgba(255,255,255,0.08)` border
/// - Cover: starts strictly at `y = top + 8.0`, square `size x size`, `6px` radius
/// - Title: `20px Bold`, `pal.text_primary`, centered below cover!
#[allow(clippy::too_many_arguments)]
pub fn stacked_mix_card(
ctx: &mut UiContext,
pal: &YmPalette,
covers: &mut CoverCache,
id: WidgetId,
rect: Rect,
title: &str,
tab_col: Color,
c1: Color,
c2: Color,
cover_uri: Option<&str>,
_seed: &str,
) -> Interaction {
if !ctx.is_visible_in_clip(rect, 24.0) {
return Interaction {
rect,
..Interaction::default()
};
}
let size = rect.width();
let resp = ctx.interact(id, rect);
let hover_t = ctx.animate_bool(id.with("hov"), resp.hovered, 0.30);
// Subtle 1.05 hover scale matching `.MixesGridMixCard_root:hover .MixesGridMixCard_plate`
let scale = 1.0 + 0.04 * hover_t;
let plate_w = size * scale;
let plate_x = rect.center_x() - plate_w * 0.5;
let plate_y = rect.top() - (plate_w - size) * 0.5;
// Exact 8px tall folder tab at 50% opacity, 8px inset each side, `6px 6px 0 0` radius
let tab_r = Rect::from_xywh(plate_x + 8.0, plate_y, (plate_w - 16.0).max(8.0), 8.0);
let tab_cr = CornerRadii::new(6.0, 6.0, 0.0, 0.0);
let tab_fill = tab_col.with_alpha_f32(0.50);
let tab_border = if pal.is_dark {
css_rgba(255, 255, 255, 20)
} else {
css_rgba(0, 0, 0, 20)
};
ctx.dl()
.push_rect_bordered(tab_r, tab_cr, tab_fill, 1.0, tab_border);
// Main square cover starting strictly at `plate_y + 8.0` (`margin-block-start: 0.5rem`)
let card_r = Rect::from_xywh(plate_x, plate_y + 8.0, plate_w, plate_w);
let mut drew_tex = false;
if let Some(u) = cover_uri {
if let Some((tex_id, _, _, tw, th)) = covers.get_or_fetch_with_size(u, 200) {
let uv = cover_fit_uv(tw, th, card_r, 0.35);
ctx.dl().push_image_uv(card_r, tex_id, uv, 6.0, Color::WHITE);
drew_tex = true;
}
}
if !drew_tex {
ctx.dl()
.push_gradient_rect(card_r, 6.0, c1, c2, Vec2::new(0.4, 1.0));
ctx.draw_icon(
Icon::MusicLogo,
Rect::from_center_size(
Pos2::new(card_r.right() - 15.0, card_r.top() + 15.0),
Vec2::splat(14.0),
),
pal.yellow,
);
}
if hover_t > 0.01 {
ctx.dl()
.push_rect(card_r, 6.0, css_rgba(0, 0, 0, (44.0 * hover_t).round() as u8));
}
let label_w = size + 44.0;
let label_rect = Rect::from_xywh(
rect.center_x() - label_w * 0.5,
rect.top() + 8.0 + size + 7.0,
label_w,
26.0,
);
ctx.draw_text_ellipsis(
title,
TextStyle::bold(20.0, pal.text_primary),
label_rect,
TextAlign::Center,
);
if resp.hovered {
ctx.set_cursor(CursorKind::Pointer);
}
resp
}
#[derive(Debug, Clone, Copy, Default)]
pub struct PromoCardAction {
pub card_clicked: bool,
pub listen_clicked: bool,
pub right_clicked_pos: Option<Pos2>,
}
/// Exact verified 5.129.0 `AlbumPromoCard` ("Recommended by the Artist" on For You page):
/// - Full-bleed artist photo (`aspect-ratio: 1.5`, `8px` radius)
/// - Bottom dark scrim (`40px` gradient)
/// - **ALWAYS draws `80x80` release cover in bottom-left ON TOP of loaded artist photo** (`12px` inset, `4px` radius, `1px` border)
/// - Localized `"Слушать"` / `"Listen"` light pill button in bottom-right (`48px` height, `#E6E6E6` -> `#FFFFFF` hover, `#000000` fg)
#[allow(clippy::too_many_arguments)]
pub fn artist_promo_card(
ctx: &mut UiContext,
pal: &YmPalette,
covers: &mut CoverCache,
id: WidgetId,
rect: Rect,
banner_height: f32,
title: &str,
artist_name: &str,
listen_label: &str,
artist_photo_uri: Option<&str>,
album_cover_uri: Option<&str>,
is_explicit: bool,
is_playing: bool,
seed: &str,
) -> PromoCardAction {
if !ctx.is_visible_in_clip(rect, 16.0) {
return PromoCardAction::default();
}
let card_resp = ctx.interact(id, rect);
let img_r = Rect::from_xywh(rect.x, rect.y, rect.w, banner_height);
// 1. Full-bleed artist photo (or theme-aware gradient placeholder)
let mut has_photo = false;
if let Some(u) = artist_photo_uri {
if let Some((tex_id, _, _, tw, th)) = covers.get_or_fetch_with_size(u, 400) {
let uv = cover_fit_uv(tw, th, img_r, 0.35);
ctx.dl().push_image_uv(img_r, tex_id, uv, 8.0, Color::WHITE);
has_photo = true;
}
}
if !has_photo {
let (g_top, g_bot) = if pal.is_dark {
(Color::rgb(0x2A, 0x2A, 0x2E), Color::rgb(0x16, 0x16, 0x18))
} else {
(Color::rgb(0xDC, 0xDC, 0xE2), Color::rgb(0xB8, 0xB8, 0xC2))
};
ctx.dl()
.push_gradient_rect(img_r, 8.0, g_top, g_bot, Vec2::new(0.0, 1.0));
}
// 2. Bottom dark scrim ONLY for unloaded placeholder (never darken loaded artist photos!)
if !has_photo {
let scrim_h = (banner_height * 0.45).clamp(48.0, 96.0);
let scrim_r = Rect::from_xywh(img_r.x, img_r.bottom() - scrim_h, img_r.w, scrim_h);
ctx.dl().push_gradient_rect(
scrim_r,
CornerRadii::bottom(8.0),
Color::TRANSPARENT,
css_rgba(0, 0, 0, 168),
Vec2::new(0.0, 1.0),
);
}
// 3. Draw the 80x80 release cover in bottom-left when a distinct `album_cover_uri` is provided (or when artist photo is unloaded)
let cov_sz = 80.0f32.min(banner_height - 24.0).max(48.0);
if album_cover_uri.is_some() || !has_photo {
let inset_cov =
Rect::from_xywh(img_r.left() + 12.0, img_r.bottom() - cov_sz - 12.0, cov_sz, cov_sz);
ctx.dl().push_shadow(
inset_cov,
4.0,
10.0,
0.0,
Vec2::new(0.0, 2.0),
css_rgba(0, 0, 0, 110),
);
draw_cover_art(
ctx,
pal,
covers,
inset_cov,
album_cover_uri,
seed,
4.0,
false,
false,
);
ctx.dl().push_rect_bordered(
inset_cov,
4.0,
Color::TRANSPARENT,
1.0,
css_rgba(255, 255, 255, 28),
);
}
// 4. Localized "Слушать" / "Listen" light pill in bottom-right (`48px` tall, `#E6E6E6` fill)
let text_sz = ctx.measure_text(listen_label, TextStyle::medium(14.0, Color::BLACK), None);
let btn_w = (text_sz.x + 20.0 + 38.0).clamp(100.0, (img_r.w - cov_sz - 36.0).max(100.0));
let btn_h = 44.0f32.min(banner_height - 24.0);
let btn_r = Rect::from_xywh(
img_r.right() - btn_w - 12.0,
img_r.bottom() - btn_h - 12.0,
btn_w,
btn_h,
);
let btn_resp = ctx.interact(id.with("listen"), btn_r);
let btn_bg = if btn_resp.hovered {
Color::WHITE
} else {
Color::rgb(0xE6, 0xE6, 0xE6)
};
ctx.dl().push_rect(btn_r, btn_h * 0.5, btn_bg);
let play_ic = if is_playing {
Icon::PauseXxs
} else {
Icon::PlayXxs
};
let inner_w = 20.0 + 6.0 + text_sz.x;
let start_x = btn_r.center_x() - inner_w * 0.5;
ctx.draw_icon(
play_ic,
Rect::from_center_size(Pos2::new(start_x + 10.0, btn_r.center_y()), Vec2::splat(20.0)),
Color::BLACK,
);
ctx.draw_text_ellipsis(
listen_label,
TextStyle::medium(14.0, Color::BLACK),
Rect::from_xywh(start_x + 26.0, btn_r.y, text_sz.x + 6.0, btn_r.h),
TextAlign::Left,
);
// 5. Title + Explicit + Artist below banner (100% theme-aware!)
let title_y = img_r.bottom() + 8.0;
let title_w_max = rect.w - if is_explicit { 22.0 } else { 0.0 };
let t_rect = Rect::from_xywh(rect.left(), title_y, title_w_max, 18.0);
let measured = ctx.draw_text_ellipsis(
title,
TextStyle::medium(13.0, pal.text_primary),
t_rect,
TextAlign::Left,
);
if is_explicit {
let ex_x = (rect.left() + measured.x.min(title_w_max) + 12.0).min(rect.right() - 9.0);
ctx.draw_icon(
Icon::Explicit,
Rect::from_center_size(Pos2::new(ex_x, title_y + 9.0), Vec2::splat(18.0)),
pal.text_secondary,
);
}
ctx.draw_text_ellipsis(
artist_name,
TextStyle::medium(13.0, pal.text_secondary),
Rect::from_xywh(rect.left(), title_y + 20.0, rect.w, 18.0),
TextAlign::Left,
);
if card_resp.hovered || btn_resp.hovered {
ctx.set_cursor(CursorKind::Pointer);
}
PromoCardAction {
card_clicked: card_resp.clicked && !btn_resp.clicked,
listen_clicked: btn_resp.clicked,
right_clicked_pos: if card_resp.right_clicked {
ctx.input.pointer_pos.or(Some(rect.center()))
} else {
None
},
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct SliderResponse {
pub value: Option<f32>,
pub hovered: bool,
pub dragging: bool,
}
/// Sleek horizontal slider (seekbar / settings / EQ).
#[allow(clippy::too_many_arguments)]
pub fn ym_slider(
ctx: &mut UiContext,
pal: &YmPalette,
id: WidgetId,
rect: Rect,
value: f32,
buffered: f32,
accent_yellow: bool,
on_content_bg: bool,
) -> SliderResponse {
let resp = ctx.interact(id, rect);
let active = resp.hovered || resp.pressed || resp.dragged;
let hov_t = ctx.animate_bool(id.with("hov"), active, 0.16);
let track_h = 3.0 + 1.5 * hov_t;
let cy = rect.center_y();
let track_rect = Rect::from_xywh(rect.left(), cy - track_h * 0.5, rect.width(), track_h);
let radius = track_h * 0.5;
ctx.dl().push_rect(track_rect, radius, pal.slider_track);
if buffered > 0.001 {
let bw = track_rect.width() * buffered.clamp(0.0, 1.0);
ctx.dl().push_rect(
Rect::from_xywh(track_rect.x, track_rect.y, bw, track_h),
radius,
pal.slider_buffered,
);
}
let frac = value.clamp(0.0, 1.0);
let fill_col = if accent_yellow {
pal.yellow
} else if on_content_bg {
pal.slider_fill_on_content
} else {
pal.slider_fill
};
let fill_w = track_rect.width() * frac;
if fill_w > 0.5 {
ctx.dl().push_rect(
Rect::from_xywh(track_rect.x, track_rect.y, fill_w, track_h),
radius,
fill_col,
);
}
if hov_t > 0.02 {
let thumb_col = if on_content_bg {
pal.text_bright
} else {
Color::WHITE
};
ctx.dl()
.push_circle(Pos2::new(track_rect.x + fill_w, cy), 5.5 * hov_t, thumb_col);
}
if active {
ctx.set_cursor(CursorKind::Pointer);
}
let mut new_val = None;
if resp.pressed || resp.dragged || resp.clicked {
if let Some(p) = ctx.input.pointer_pos {
new_val = Some(((p.x - track_rect.x) / track_rect.width().max(1.0)).clamp(0.0, 1.0));
}
}
SliderResponse {
value: new_val,
hovered: resp.hovered,
dragging: resp.pressed || resp.dragged,
}
}
/// Official 5.129.0 vertical volume / EQ slider (`5b3a6ee99e2bd1b8.css` `.VibePlayerBar_verticalSlider`
/// & `.ChangeVolume_wrapperSlider`):
/// - `8px` wide centered rounded pill track (`radius = 4px`), unfilled `rgba(255,255,255,0.20)`
/// - Idle fill: `#E6E6E6` white-gray (NOT yellow!) with NO circular thumb ball
/// - Direct hover/drag on slider: smoothly reveals `16px` (`radius = 8px`) circular thumb and transitions
/// fill to `hover_accent` (`Color::WHITE` on My Vibe, `pal.yellow` on Player Bar)
pub fn ym_vertical_slider(
ctx: &mut UiContext,
pal: &YmPalette,
id: WidgetId,
rect: Rect,
value: f32,
glass_no_border: bool,
hover_accent: Color,
) -> SliderResponse {
let resp = ctx.interact(id, rect);
let active = resp.hovered || resp.pressed || resp.dragged;
let hov_t = ctx.animate_bool(id.with("hov"), active, 0.18);
let track_w = 8.0;
let radius = 4.0;
let cx = rect.center_x();
let track_rect = Rect::from_xywh(cx - track_w * 0.5, rect.top(), track_w, rect.height());
let unfilled_col = if glass_no_border || pal.is_dark {
css_rgba(255, 255, 255, 51) // rgba(255,255,255,0.20)
} else {
css_rgba(0, 0, 0, 41)
};
ctx.dl().push_rect(track_rect, radius, unfilled_col);
let frac = value.clamp(0.0, 1.0);
let fill_h = track_rect.height() * frac;
let fill_y = track_rect.bottom() - fill_h;
let idle_fill = if glass_no_border || pal.is_dark {
Color::rgb(0xE6, 0xE6, 0xE6)
} else {
Color::rgb(0x66, 0x66, 0x66)
};
let fill_col = idle_fill.lerp(hover_accent, hov_t);
if fill_h > 0.5 {
ctx.dl().push_rect(
Rect::from_xywh(track_rect.x, fill_y, track_w, fill_h),
radius,
fill_col,
);
}
// Thumb is HIDDEN when idle; reveals crisp 16px (r=8px) circular thumb ONLY when directly hovered/dragged
if hov_t > 0.02 {
let thumb_r = 8.0 * hov_t;
let thumb_col = if glass_no_border || pal.is_dark {
Color::WHITE
} else {
pal.text_bright
};
let thumb_y = fill_y.clamp(track_rect.top(), track_rect.bottom());
ctx.dl()
.push_circle(Pos2::new(cx, thumb_y), thumb_r, thumb_col);
}
if active {
ctx.set_cursor(CursorKind::Pointer);
}
let mut new_val = None;
if resp.pressed || resp.dragged || resp.clicked {
if let Some(p) = ctx.input.pointer_pos {
new_val =
Some((1.0 - (p.y - track_rect.top()) / track_rect.height().max(1.0)).clamp(0.0, 1.0));
}
}
SliderResponse {
value: new_val,
hovered: resp.hovered,
dragging: resp.pressed || resp.dragged,
}
}
/// Complete official 5.129.0 `44×216px` (`radius = 22px`) vertical volume popover capsule + slider:
/// - `glass_no_border == true` (My Vibe `.VibePlayerBar_verticalSlider`): `hsla(0,0%,100%,0.08)` Kawase glass, ZERO border, `#FFFFFF` hover accent
/// - `glass_no_border == false` (Player Bar `.ChangeVolume_wrapperSlider`): `pal.bg_popover` Kawase glass + subtle `1px rgba(..., 0.08)` border, `pal.yellow` hover accent
pub fn ym_volume_capsule(
ctx: &mut UiContext,
pal: &YmPalette,
id: WidgetId,
capsule_rect: Rect,
value: f32,
glass_no_border: bool,
hover_accent: Color,
) -> SliderResponse {
ctx.register_blocker(capsule_rect);
let radius = capsule_rect.width() * 0.5;
if glass_no_border {
ctx.dl().push_backdrop_blur(
capsule_rect,
radius,
2.0,
css_rgba(255, 255, 255, 20), // hsla(0,0%,100%,0.08)
0.0,
Color::TRANSPARENT,
);
} else {
let subtle_border = if pal.is_dark {
css_rgba(255, 255, 255, 20) // rgba(255,255,255,0.08)
} else {
css_rgba(0, 0, 0, 20) // rgba(0,0,0,0.08)
};
ctx.dl().push_shadow(
capsule_rect,
radius,
18.0,
0.0,
Vec2::new(0.0, 4.0),
pal.shadow_menu,
);
ctx.dl().push_backdrop_blur(
capsule_rect,
radius,
2.0,
pal.bg_popover.with_alpha(232),
1.0,
subtle_border,
);
}
// Track spans full capsule width for direct capsule hover detection, with 18px top/bottom inset
let track_r = Rect::from_xywh(
capsule_rect.left(),
capsule_rect.top() + 18.0,
capsule_rect.width(),
(capsule_rect.height() - 36.0).max(24.0),
);
ym_vertical_slider(ctx, pal, id, track_r, value, glass_no_border, hover_accent)
}
#[derive(Debug, Clone, Copy)]
pub struct TextInputOptions<'a> {
pub placeholder: &'a str,
pub leading_icon: Option<Icon>,
pub show_clear_button: bool,
/// When `true` and the input is empty & unfocused, renders NO placeholder text (matches Search landing `search.jpg`!).
pub hide_placeholder_when_unfocused: bool,
/// When `true`, renders transparent background when unfocused (matches Favorites Track Search outline pill).
pub transparent_bg: bool,
pub font_size: f32,
pub pill: bool,
}
impl<'a> Default for TextInputOptions<'a> {
fn default() -> Self {
Self {
placeholder: "",
leading_icon: None,
show_clear_button: true,
hide_placeholder_when_unfocused: false,
transparent_bg: false,
font_size: 14.0,
pill: true,
}
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct TextInputResponse {
pub changed: bool,
pub submitted: bool,
pub cleared: bool,
pub focused: bool,
pub hovered: bool,
}
/// Full theme-aware RU/EN text input widget (`SearchInput`, Favorites track filter, Create/Rename Playlist).
pub fn text_input_widget(
ctx: &mut UiContext,
pal: &YmPalette,
id: WidgetId,
rect: Rect,
value: &mut String,
opts: TextInputOptions<'_>,
) -> TextInputResponse {
let resp = ctx.interact(id, rect);
let radius = if opts.pill { rect.height() * 0.5 } else { 10.0 };
// Focus handling
if resp.pressed {
ctx.input.set_focus(Some(id));
} else if ctx.input.is_focused(id) && ctx.input.clicked_outside(rect) {
ctx.input.set_focus(None);
}
let mut focused = ctx.input.is_focused(id);
// Clear button rect (right side)
let has_clear = opts.show_clear_button && !value.is_empty();
let clear_rect = Rect::from_center_size(
Pos2::new(rect.right() - 20.0, rect.center_y()),
Vec2::splat(28.0),
);
let mut cleared = false;
if has_clear {
let c_resp = ctx.interact(id.with("clear"), clear_rect);
if c_resp.clicked {
value.clear();
cleared = true;
ctx.input.set_focus(Some(id));
focused = true;
}
}
// Background & border (exact `5495aae20be2440c.css` SearchInput states!)
let bg = if focused {
pal.surface_secondary
} else if opts.transparent_bg {
Color::TRANSPARENT
} else {
pal.input_bg
};
let (border_w, border_col) = if focused {
(2.0, pal.text_primary)
} else if resp.hovered {
(2.0, pal.input_border_hover)
} else {
(1.0, pal.input_border)
};
ctx.dl()
.push_rect_bordered(rect, radius, bg, border_w, border_col);
// Optional leading icon (e.g. Search magnifying glass)
let mut left_x = rect.left() + 14.0;
if let Some(ic) = opts.leading_icon {
let ic_col = if focused {
pal.text_primary
} else {
pal.input_border
};
let ic_sz = if rect.height() >= 44.0 { 22.0 } else { 18.0 };
ctx.draw_icon(
ic,
Rect::from_center_size(Pos2::new(rect.left() + 24.0, rect.center_y()), Vec2::splat(ic_sz)),
ic_col,
);
left_x = rect.left() + 44.0;
}
let right_pad = if has_clear { 38.0 } else { 14.0 };
let text_rect = Rect::from_xywh(
left_x,
rect.top(),
(rect.right() - right_pad - left_x).max(20.0),
rect.height(),
);
let style = TextStyle::medium(opts.font_size, pal.text_primary);
let mut ev_changed = cleared;
let mut ev_submitted = false;
// Update persistent TextInputState owned by UiContext
let time = ctx.input.time;
{
let state = ctx.text_inputs.entry(id).or_insert_with(|| {
crate::engine::text::TextInputState::new(value)
});
state.sync_external(&mut ctx.text, value, style);
if !cleared {
let ev = state.handle_input(&mut ctx.text, &ctx.input, text_rect, style, focused);
if ev.changed {
*value = state.text();
ev_changed = true;
}
if ev.submitted {
ev_submitted = true;
}
if ev.escaped {
ctx.input.set_focus(None);
focused = false;
}
}
let ph = if opts.hide_placeholder_when_unfocused && !focused && value.is_empty() {
""
} else {
opts.placeholder
};
let ph_col = Color::rgb(0x80, 0x80, 0x80);
let sel_bg = if pal.is_dark {
pal.yellow.with_alpha(90)
} else {
pal.outline_selected.with_alpha(64)
};
let caret_col = pal.text_bright;
let layer_dl = &mut ctx.layers[ctx.current_layer.index()];
state.draw(
layer_dl,
&mut ctx.textures,
&mut ctx.text,
text_rect,
style,
ph,
ph_col,
sel_bg,
caret_col,
focused,
time,
);
}
if focused {
// Keep blinking caret alive at low 4 FPS instead of 60 FPS!
ctx.request_repaint_after(std::time::Duration::from_millis(250));
}
if has_clear {
let c_hov = ctx.is_hovered(clear_rect);
ctx.draw_icon(
Icon::CloseXxs,
Rect::from_center_size(clear_rect.center(), Vec2::splat(16.0)),
if c_hov {
pal.text_primary
} else {
pal.text_secondary
},
);
if c_hov {
ctx.set_cursor(CursorKind::Pointer);
}
} else if resp.hovered {
ctx.set_cursor(CursorKind::Text);
}
TextInputResponse {
changed: ev_changed,
submitted: ev_submitted,
cleared,
focused,
hovered: resp.hovered,
}
}
/// Animated 3-bar audio-reactive equalizer (e.g. Queue / Track indicator).
pub fn draw_playing_eq_bars(
ctx: &mut UiContext,
rect: Rect,
color: Color,
playing: bool,
energy: f32,
) {
let t = ctx.input.time as f32;
let bar_w = 3.0;
let gap = 2.5;
let total_w = bar_w * 3.0 + gap * 2.0;
let start_x = rect.center_x() - total_w * 0.5;
let max_h = 14.0;
let base_y = rect.center_y() + max_h * 0.5;
for i in 0..3 {
let phase = t * (6.5 + i as f32 * 2.3) + i as f32 * 1.7;
let h = if playing {
let osc = (phase.sin() * 0.5 + 0.5) * (0.55 + energy * 0.65);
(3.0 + osc * (max_h - 3.0)).clamp(3.0, max_h)
} else {
4.0 + (i as f32 * 2.0)
};
let r = Rect::from_xywh(start_x + i as f32 * (bar_w + gap), base_y - h, bar_w, h);
ctx.dl().push_rect(r, 1.5, color);
}
if playing {
ctx.request_repaint_after(std::time::Duration::from_millis(40));
}
}
/// Smooth circular progress ring matching official Yandex Music 5.129.0 `CircleProgress` (`68254`).
pub fn draw_circular_progress_ring(
ctx: &mut UiContext,
center: Pos2,
radius: f32,
stroke_w: f32,
progress: f32,
track_color: Color,
progress_color: Color,
) {
use std::f32::consts::{FRAC_PI_2, TAU};
ctx.dl()
.push_circle_stroke(center, radius, stroke_w, track_color);
let frac = progress.clamp(0.0, 1.0);
let (start_angle, sweep) = if frac < 0.03 {
if ctx.input.time > 0.001 {
ctx.request_repaint_after(std::time::Duration::from_millis(24));
}
((ctx.input.time as f32 * 3.2) - FRAC_PI_2, 0.24 * TAU)
} else {
(-FRAC_PI_2, frac * TAU)
};
let segments = ((sweep / TAU) * 32.0).ceil().max(4.0) as usize;
let step = sweep / segments as f32;
for i in 0..segments {
let a0 = start_angle + i as f32 * step;
let a1 = start_angle + (i + 1) as f32 * step;
let p0 = Pos2::new(center.x + radius * a0.cos(), center.y + radius * a0.sin());
let p1 = Pos2::new(center.x + radius * a1.cos(), center.y + radius * a1.sin());
ctx.dl().push_line(p0, p1, stroke_w, progress_color);
}
}
/// Official Yandex Music 5.129.0 `DownloadingProgress` (`79262` / `63298c27abf81dc6.css:210-218`):
/// yellow circular progress ring with optional centered `close` (`×`) cancel icon.
pub fn draw_downloading_progress(
ctx: &mut UiContext,
pal: &YmPalette,
center: Pos2,
size: f32,
progress: f32,
with_cancel_icon: bool,
hovered: bool,
) {
let stroke_w = if size >= 22.0 { 2.2 } else { 1.8 };
let radius = ((size - stroke_w) * 0.5).max(3.0);
draw_circular_progress_ring(
ctx,
center,
radius,
stroke_w,
progress,
pal.border_control,
pal.yellow,
);
if with_cancel_icon {
let icon_sz = (size * 0.42).clamp(8.0, 12.0);
let tint = if hovered {
pal.text_bright
} else {
pal.text_primary
};
ctx.draw_icon(
Icon::Close,
Rect::from_center_size(center, Vec2::splat(icon_sz)),
tint,
);
}
}