first commit

This commit is contained in:
marwy 2026-11-27 13:16:36 +03:00
commit a408df2a76
218 changed files with 52376 additions and 0 deletions

753
src/views/for_you.rs Normal file
View file

@ -0,0 +1,753 @@
use crate::app::state::{AppAction, AppState, CollectionTab, ForYouTab, Route};
use crate::covers::CoverCache;
use crate::engine::input::WidgetId;
use crate::engine::text::{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::widgets::{
artist_promo_card, draw_cover_art, media_card, track_row, TrackRowAction, TrackRowOptions,
};
use crate::engine::UiContext;
use crate::icons::Icon;
use crate::views::overlays::ShellUiState;
/// Exact Yandex Music Desktop 5.129.0 "For you and Trends" page (`vibe_wheel_foryou_spec.md` §4):
/// - Persistent heading ("Для вас и Тренды" / "For you and Trends") + IN-PLACE switcher tabs ("Для вас" / "Тренды")
/// - For You tab:
/// * Quick cards: "Мне нравится >" (right 58x58 cover = last liked track `liked_tracks.first()`) &
/// "История >" (navigates to `NavigateCollectionTab(CollectionTab::History)`)
/// * "Recommended by the artist" (`artist_promo_card` with 80x80 release cover ON TOP of artist photo + localized "Слушать"/"Listen")
/// * "Made for you" personal playlists carousel
/// * "New releases" albums carousel
/// - Trends tab (in-place keeping heading & switcher visible!):
/// * Chart tracks (with rank badges & click to play) + clickable heading to full Chart
/// * New releases carousel
/// * Popular playlists carousel
pub fn render_for_you(
ctx: &mut UiContext,
pal: &YmPalette,
covers: &mut CoverCache,
app: &mut AppState,
shell: &mut ShellUiState,
rect: Rect,
actions: &mut Vec<AppAction>,
) {
let t = app.i18n();
let ru = t.is_ru();
let is_playing = app.is_audio_playing();
let current_track_id = app.current_track().map(|tr| tr.id.clone());
let scroll_id = WidgetId::new("foryou_page_scroll");
let scroll_y = ctx.begin_scroll_v(scroll_id, rect, 0.0);
// `views/mod.rs` already insets `inner_r` by (25, 13, 25, 0) -> rect starts at exact CDP (249, 45)!
let x = rect.left();
let avail_w = rect.width().max(320.0);
let mut y = rect.top() + 8.5 - scroll_y;
// 0. Persistent Page Heading
ctx.draw_text_ellipsis(
t.for_you_and_trends(),
TextStyle::headline(32.0, pal.text_primary),
Rect::from_xywh(x, y, avail_w, 38.0),
TextAlign::Left,
);
y += 66.0;
// 1. In-Place Switcher Tabs ("Для вас" / "Тренды")
let for_you_sub: String = {
let names: Vec<String> = app
.landing
.vibe_tracks
.iter()
.take(2)
.map(|tr| tr.artists_string())
.collect();
if names.is_empty() {
if ru {
"Персональные рекомендации".into()
} else {
"Personal picks".into()
}
} else {
names.join(", ")
}
};
let trends_sub: String = {
let names: Vec<String> = app
.landing
.chart_tracks
.iter()
.take(2)
.map(|tr| tr.artists_string())
.collect();
if names.is_empty() {
if ru {
"Чарт и новинки".into()
} else {
"Chart & new hits".into()
}
} else {
names.join(", ")
}
};
let fy_c1 = app
.landing
.vibe_tracks
.first()
.and_then(|tr| tr.cover_uri.clone());
let fy_c2 = app
.landing
.vibe_tracks
.get(1)
.and_then(|tr| tr.cover_uri.clone());
let tr_c1 = app
.landing
.chart_tracks
.first()
.and_then(|tr| tr.cover_uri.clone());
let tr_c2 = app
.landing
.chart_tracks
.get(1)
.and_then(|tr| tr.cover_uri.clone());
let mut tab_x = x;
let tabs = [
(
ForYouTab::ForYou,
t.for_you(),
for_you_sub.as_str(),
Color::rgb(0x6E, 0x7A, 0x86),
Color::rgb(0x42, 0x46, 0x4E),
fy_c1.as_deref(),
fy_c2.as_deref(),
),
(
ForYouTab::Trends,
t.trends(),
trends_sub.as_str(),
Color::rgb(0x52, 0x52, 0x56),
Color::rgb(0x98, 0x94, 0x90),
tr_c1.as_deref(),
tr_c2.as_deref(),
),
];
for (tab_kind, title, sub, c1, c2, av1_uri, av2_uri) in tabs {
let active = app.for_you_tab == tab_kind;
let sub_w = ctx
.measure_text(sub, TextStyle::medium(13.0, pal.text_secondary), None)
.x
.min(220.0);
let pill_w = (86.0 + sub_w + 18.0).max(201.0);
let pill_r = Rect::from_xywh(tab_x, y, pill_w, 54.0);
let resp = ctx.interact(WidgetId::from_pair("fy_tab", tab_kind as u8), pill_r);
if active || resp.hovered {
let bg = if active {
pal.surface_hover
} else {
pal.surface_secondary
};
ctx.dl().push_rect(pill_r, 27.0, bg);
}
// Overlapping 36x36 circular avatars
let a1 = Pos2::new(pill_r.left() + 34.0, pill_r.center_y());
let a2 = Pos2::new(pill_r.left() + 60.0, pill_r.center_y());
for (pos, uri_opt, fallback) in [(a2, av2_uri, c2), (a1, av1_uri, c1)] {
let av_r = Rect::from_center_size(pos, Vec2::splat(36.0));
let mut drew = false;
if let Some(uri) = uri_opt {
if let Some((tex_id, _, _)) = covers.get_or_fetch(uri, 100) {
ctx.dl().push_image(av_r, tex_id, 18.0, Color::WHITE);
drew = true;
}
}
if !drew {
ctx.dl().push_circle(pos, 18.0, fallback);
}
}
let tx_l = pill_r.left() + 86.0;
let tx_w = (pill_r.right() - tx_l - 14.0).max(40.0);
ctx.draw_text_ellipsis(
title,
TextStyle::bold(14.0, pal.text_primary),
Rect::from_xywh(tx_l, pill_r.top() + 8.0, tx_w, 18.0),
TextAlign::Left,
);
ctx.draw_text_ellipsis(
sub,
TextStyle::medium(13.0, pal.text_secondary),
Rect::from_xywh(tx_l, pill_r.top() + 28.0, tx_w, 18.0),
TextAlign::Left,
);
if resp.hovered {
ctx.set_cursor(CursorKind::Pointer);
}
if resp.clicked && !active {
actions.push(AppAction::SetForYouTab(tab_kind));
}
tab_x = pill_r.right() + 8.0;
}
y += 54.0 + 41.5;
// -------------------------------------------------------------------------
// IN-PLACE TAB CONTENT
// -------------------------------------------------------------------------
match app.for_you_tab {
ForYouTab::ForYou => {
// 2. Two Side-by-Side Quick Cards ("Мне нравится >" & "История >") — exact CDP 480px width, 17.5px gap
let card_gap = 17.5;
let half_w = ((avail_w.min(977.5) - card_gap) * 0.5).max(260.0);
// Card A: My Favorites > (right 58x58 mini-cover = last liked track!)
let r1 = Rect::from_xywh(x, y, half_w, 82.0);
let resp1 = ctx.interact(WidgetId::new("fy_quick_likes"), r1);
ctx.dl().push_rect(
r1,
12.0,
if resp1.hovered {
pal.surface_hover
} else {
pal.surface_secondary
},
);
let heart_r = Rect::from_xywh(r1.left() + 12.0, r1.top() + 12.0, 58.0, 58.0);
draw_cover_art(ctx, pal, covers, heart_r, None, "likes", 8.0, false, true);
let fav_title = t.liked_playlist();
let fav_tw = ctx
.measure_text(fav_title, TextStyle::headline(24.0, pal.text_primary), None)
.x
.min((r1.width() - 180.0).max(80.0));
let fav_ty = r1.top() + 14.0;
ctx.draw_text_ellipsis(
fav_title,
TextStyle::headline(24.0, pal.text_primary),
Rect::from_xywh(r1.left() + 82.0, fav_ty, fav_tw + 4.0, 28.0),
TextAlign::Left,
);
let arrow_shift = if resp1.hovered { 5.0 } else { 0.0 };
ctx.draw_icon(
Icon::ArrowRight,
Rect::from_center_size(
Pos2::new(r1.left() + 82.0 + fav_tw + 14.0 + arrow_shift, fav_ty + 14.0),
Vec2::splat(22.0),
),
pal.text_secondary,
);
ctx.draw_text_ellipsis(
&t.count_tracks(app.liked_tracks.len()),
TextStyle::medium(14.0, pal.text_secondary),
Rect::from_xywh(r1.left() + 82.0, r1.top() + 42.0, (r1.width() - 160.0).max(60.0), 20.0),
TextAlign::Left,
);
// Right 58x58 mini-cover = LAST LIKED TRACK's cover (`liked_tracks.first()`)!
let mini1 = Rect::from_xywh(r1.right() - 70.0, r1.top() + 12.0, 58.0, 58.0);
let fav_mini_uri = app
.liked_tracks
.first()
.and_then(|tr| tr.cover_uri.clone());
draw_cover_art(
ctx,
pal,
covers,
mini1,
fav_mini_uri.as_deref(),
"fav_mini",
6.0,
false,
false,
);
if resp1.hovered {
ctx.set_cursor(CursorKind::Pointer);
}
if resp1.clicked {
actions.push(AppAction::Navigate(Route::LikedTracks));
}
// Card B: History > (navigates to Collection -> History tab!)
let r2 = Rect::from_xywh(r1.right() + card_gap, y, half_w, 82.0);
let resp2 = ctx.interact(WidgetId::new("fy_quick_history"), r2);
ctx.dl().push_rect(
r2,
12.0,
if resp2.hovered {
pal.surface_hover
} else {
pal.surface_secondary
},
);
let hist_box = Rect::from_xywh(r2.left() + 12.0, r2.top() + 12.0, 58.0, 58.0);
let hist_box_bg = if pal.is_dark {
css_rgba(255, 255, 255, 14)
} else {
pal.surface_hover
};
ctx.dl().push_rect(hist_box, 8.0, hist_box_bg);
ctx.draw_icon(
Icon::History,
Rect::from_center_size(hist_box.center(), Vec2::splat(32.0)),
pal.text_primary,
);
let hist_title = t.history();
let hist_tw = ctx
.measure_text(hist_title, TextStyle::headline(24.0, pal.text_primary), None)
.x
.min((r2.width() - 180.0).max(80.0));
let hist_ty = r2.top() + 14.0;
ctx.draw_text_ellipsis(
hist_title,
TextStyle::headline(24.0, pal.text_primary),
Rect::from_xywh(r2.left() + 82.0, hist_ty, hist_tw + 4.0, 28.0),
TextAlign::Left,
);
let h_shift = if resp2.hovered { 5.0 } else { 0.0 };
ctx.draw_icon(
Icon::ArrowRight,
Rect::from_center_size(
Pos2::new(r2.left() + 82.0 + hist_tw + 14.0 + h_shift, hist_ty + 14.0),
Vec2::splat(22.0),
),
pal.text_secondary,
);
let hist_sub = if app.play_history.is_empty() {
if ru {
"Недавно прослушанные треки".into()
} else {
"Recently played tracks".into()
}
} else {
app.play_history
.iter()
.take(3)
.map(|tr| tr.artists_string())
.collect::<Vec<_>>()
.join(", ")
};
ctx.draw_text_ellipsis(
&hist_sub,
TextStyle::medium(14.0, pal.text_secondary),
Rect::from_xywh(r2.left() + 82.0, r2.top() + 42.0, (r2.width() - 160.0).max(60.0), 20.0),
TextAlign::Left,
);
let mini2 = Rect::from_xywh(r2.right() - 70.0, r2.top() + 12.0, 58.0, 58.0);
let hist_mini_uri = app
.play_history
.first()
.and_then(|tr| tr.cover_uri.clone());
draw_cover_art(
ctx,
pal,
covers,
mini2,
hist_mini_uri.as_deref(),
"hist_mini",
6.0,
false,
false,
);
if resp2.hovered {
ctx.set_cursor(CursorKind::Pointer);
}
if resp2.clicked {
actions.push(AppAction::NavigateCollectionTab(CollectionTab::History));
}
y += 82.0 + 35.0;
// 3. "Recommended by the Artist" / Trending Promo Cards (`artist_promo_card`)
if !app.landing.chart_tracks.is_empty() {
ctx.draw_text_ellipsis(
t.recommended_by_artist(),
TextStyle::headline(24.0, pal.text_primary),
Rect::from_xywh(x, y, avail_w, 30.0),
TextAlign::Left,
);
y += 47.0;
let cols = 3usize;
let promo_gap = 16.0;
let promo_w = ((avail_w - promo_gap * (cols as f32 - 1.0)) / cols as f32).max(210.0);
let banner_h = 215.0;
let card_h = banner_h + 50.0;
let chart_tracks = app.landing.chart_tracks.clone();
for (i, tr) in chart_tracks.iter().take(3).enumerate() {
let cx = x + i as f32 * (promo_w + promo_gap);
let card_r = Rect::from_xywh(cx, y, promo_w, card_h);
let distinct_artist_photo = tr
.artists
.first()
.and_then(|a| a.cover_uri.as_deref());
let artist_photo = distinct_artist_photo.or(tr.cover_uri.as_deref());
// Always render 80x80 release cover in bottom-left even when artist photo is missing!
let album_cov = tr.cover_uri.as_deref();
let tr_playing =
is_playing && current_track_id.as_deref() == Some(tr.id.as_str());
let res = artist_promo_card(
ctx,
pal,
covers,
WidgetId::from_pair("fy_promo", &tr.id),
card_r,
banner_h,
&tr.title,
&tr.artists_string(),
t.listen(),
artist_photo,
album_cov,
tr.is_explicit,
tr_playing,
&format!("promo_{}", tr.id),
);
if res.listen_clicked {
if tr_playing {
actions.push(AppAction::TogglePlay);
} else {
actions.push(AppAction::PlayTracks {
tracks: chart_tracks.clone(),
start_index: i,
context_name: t.chart().into(),
context_key: "chart".into(),
is_vibe: false,
});
}
} else if res.card_clicked {
if let Some(ref aid) = tr.album_id {
actions.push(AppAction::Navigate(Route::Album {
id: aid.clone(),
title: tr
.album_title
.clone()
.unwrap_or_else(|| tr.title.clone()),
}));
} else {
actions.push(AppAction::PlayTracks {
tracks: chart_tracks.clone(),
start_index: i,
context_name: t.chart().into(),
context_key: "chart".into(),
is_vibe: false,
});
}
}
}
y += card_h + 30.8;
}
// 4. "Made for you" Personal AI Playlists
if !app.landing.personal_playlists.is_empty() {
ctx.draw_text_ellipsis(
t.made_for_you(),
TextStyle::headline(24.0, pal.text_primary),
Rect::from_xywh(x, y, avail_w, 30.0),
TextAlign::Left,
);
y += 40.0;
let card_w = 164.0;
let card_h = card_w + 54.0;
let row_vp = Rect::from_xywh(x, y, avail_w, card_h);
let h_id = WidgetId::new("foryou_personal_scroll");
let scroll_x = ctx.begin_scroll_h(h_id, row_vp, 0.0);
let playlists = app.landing.personal_playlists.clone();
let mut cx = x - scroll_x;
for pl in &playlists {
let cr = Rect::from_xywh(cx, y, card_w, card_h);
if cr.right() >= row_vp.left() - 20.0 && cr.left() <= row_vp.right() + 20.0 {
let sub = pl.description.as_deref().unwrap_or(t.playlist());
let m = media_card(
ctx,
pal,
covers,
WidgetId::from_pair("fy_pl", format!("{}_{}", pl.owner_uid, pl.kind)),
cr,
&pl.title,
sub,
pl.cover_uri.as_deref(),
&format!("pl_{}_{}", pl.owner_uid, pl.kind),
false,
false,
);
if m.play_clicked {
actions.push(AppAction::PlayPlaylist {
owner_uid: pl.owner_uid.clone(),
kind: pl.kind.clone(),
title: pl.title.clone(),
});
} else if m.clicked {
actions.push(AppAction::Navigate(Route::Playlist {
owner_uid: pl.owner_uid.clone(),
kind: pl.kind.clone(),
title: pl.title.clone(),
}));
}
}
cx += card_w + 16.0;
}
let total_w = (playlists.len() as f32) * (card_w + 16.0);
ctx.end_scroll_h(h_id, pal, total_w, false);
y += card_h + 26.0;
}
// 5. "New Releases"
if !app.landing.new_releases.is_empty() {
y = render_new_releases_carousel(ctx, pal, covers, app, x, y, avail_w, "fy_rel", actions);
}
}
ForYouTab::Trends => {
// IN-PLACE TRENDS TAB: Chart tracks + New releases + Popular playlists!
// 1. Chart Tracks Section (with clickable "Чарт >" heading opening full Chart)
if !app.landing.chart_tracks.is_empty() {
let head_txt = t.chart();
let head_w = ctx
.measure_text(head_txt, TextStyle::headline(24.0, pal.text_primary), None)
.x
+ 32.0;
let head_r = Rect::from_xywh(x, y, head_w, 30.0);
let head_resp = ctx.interact(WidgetId::new("trends_chart_heading"), head_r);
ctx.draw_text_ellipsis(
head_txt,
TextStyle::headline(
24.0,
if head_resp.hovered {
pal.text_bright
} else {
pal.text_primary
},
),
head_r,
TextAlign::Left,
);
ctx.draw_icon(
Icon::ArrowRight,
Rect::from_center_size(
Pos2::new(head_r.right() - 12.0, head_r.center_y()),
Vec2::splat(22.0),
),
pal.text_secondary,
);
if head_resp.hovered {
ctx.set_cursor(CursorKind::Pointer);
}
if head_resp.clicked {
actions.push(AppAction::Navigate(Route::Chart));
}
y += 40.0;
let chart_tracks = app.landing.chart_tracks.clone();
let limit = chart_tracks.len().min(10);
for (idx, tr) in chart_tracks.iter().take(limit).enumerate() {
let row_r = Rect::from_xywh(x, y, avail_w, TRACK_ROW_HEIGHT);
if row_r.bottom() >= rect.top() - 60.0 && row_r.top() <= rect.bottom() + 60.0 {
let is_curr = current_track_id.as_deref() == Some(tr.id.as_str());
match track_row(
ctx,
pal,
covers,
WidgetId::from_pair("trends_chart_row", &tr.id),
row_r,
tr,
TrackRowOptions {
index: Some(idx),
show_chart_rank: true,
show_cover: true,
is_current: is_curr,
is_playing: is_curr && is_playing,
is_liked: app.liked_ids.contains(&tr.id),
is_disliked: app.disliked_ids.contains(&tr.id),
is_downloaded: app.is_track_downloaded(&tr.id),
menu_open: shell.is_track_menu_open(&tr.id),
},
) {
TrackRowAction::Play => {
actions.push(AppAction::PlayTracks {
tracks: chart_tracks.clone(),
start_index: idx,
context_name: t.chart().into(),
context_key: "chart".into(),
is_vibe: false,
});
}
TrackRowAction::ToggleLike => {
actions.push(AppAction::ToggleLike(tr.clone()));
}
TrackRowAction::ToggleDislike => {
actions.push(AppAction::ToggleDislike(tr.clone()));
}
TrackRowAction::OpenArtist(id, name) => {
actions.push(AppAction::Navigate(Route::Artist { id, name }));
}
TrackRowAction::OpenAlbum(id) => {
actions.push(AppAction::Navigate(Route::Album {
id,
title: tr
.album_title
.clone()
.unwrap_or_else(|| t.album().into()),
}));
}
TrackRowAction::OpenContextMenu(pos) => {
shell.open_track_menu(tr.clone(), pos);
}
TrackRowAction::None => {}
}
}
y += TRACK_ROW_HEIGHT;
}
y += 28.0;
}
// 2. New Releases Carousel
if !app.landing.new_releases.is_empty() {
y = render_new_releases_carousel(
ctx,
pal,
covers,
app,
x,
y,
avail_w,
"trends_rel",
actions,
);
}
// 3. Popular Playlists Carousel
if !app.landing.popular_playlists.is_empty() {
ctx.draw_text_ellipsis(
t.popular_playlists(),
TextStyle::headline(24.0, pal.text_primary),
Rect::from_xywh(x, y, avail_w, 30.0),
TextAlign::Left,
);
y += 40.0;
let card_w = 164.0;
let card_h = card_w + 54.0;
let row_vp = Rect::from_xywh(x, y, avail_w, card_h);
let h_id = WidgetId::new("trends_pop_pl_scroll");
let scroll_x = ctx.begin_scroll_h(h_id, row_vp, 0.0);
let playlists = app.landing.popular_playlists.clone();
let mut cx = x - scroll_x;
for pl in &playlists {
let cr = Rect::from_xywh(cx, y, card_w, card_h);
if cr.right() >= row_vp.left() - 20.0 && cr.left() <= row_vp.right() + 20.0 {
let m = media_card(
ctx,
pal,
covers,
WidgetId::from_pair("tr_pl", format!("{}_{}", pl.owner_uid, pl.kind)),
cr,
&pl.title,
&pl.owner_name,
pl.cover_uri.as_deref(),
&format!("trpl_{}_{}", pl.owner_uid, pl.kind),
false,
false,
);
if m.play_clicked {
actions.push(AppAction::PlayPlaylist {
owner_uid: pl.owner_uid.clone(),
kind: pl.kind.clone(),
title: pl.title.clone(),
});
} else if m.clicked {
actions.push(AppAction::Navigate(Route::Playlist {
owner_uid: pl.owner_uid.clone(),
kind: pl.kind.clone(),
title: pl.title.clone(),
}));
}
}
cx += card_w + 16.0;
}
let total_w = (playlists.len() as f32) * (card_w + 16.0);
ctx.end_scroll_h(h_id, pal, total_w, false);
y += card_h + 24.0;
}
}
}
y += 20.0;
let _ = CornerRadii::ZERO;
let total_h = y - (rect.top() - scroll_y);
ctx.end_scroll_v(scroll_id, pal, total_h, true);
}
#[allow(clippy::too_many_arguments)]
fn render_new_releases_carousel(
ctx: &mut UiContext,
pal: &YmPalette,
covers: &mut CoverCache,
app: &AppState,
x: f32,
mut y: f32,
avail_w: f32,
salt: &str,
actions: &mut Vec<AppAction>,
) -> f32 {
let t = app.i18n();
ctx.draw_text_ellipsis(
t.new_releases(),
TextStyle::headline(24.0, pal.text_primary),
Rect::from_xywh(x, y, avail_w, 30.0),
TextAlign::Left,
);
y += 40.0;
let card_w = 164.0;
let card_h = card_w + 54.0;
let row_vp = Rect::from_xywh(x, y, avail_w, card_h);
let h_id = WidgetId::new(salt);
let scroll_x = ctx.begin_scroll_h(h_id, row_vp, 0.0);
let releases = app.landing.new_releases.clone();
let mut cx = x - scroll_x;
for alb in &releases {
let cr = Rect::from_xywh(cx, y, card_w, card_h);
if cr.right() >= row_vp.left() - 20.0 && cr.left() <= row_vp.right() + 20.0 {
let art = alb
.artists
.first()
.map(|a| a.name.as_str())
.unwrap_or(t.album());
let m = media_card(
ctx,
pal,
covers,
WidgetId::from_pair(salt, &alb.id),
cr,
&alb.title,
art,
alb.cover_uri.as_deref(),
&format!("alb_{}", alb.id),
false,
false,
);
if m.play_clicked {
actions.push(AppAction::PlayAlbum {
id: alb.id.clone(),
title: alb.title.clone(),
});
} else if m.clicked {
actions.push(AppAction::Navigate(Route::Album {
id: alb.id.clone(),
title: alb.title.clone(),
}));
}
}
cx += card_w + 16.0;
}
let total_w = (releases.len() as f32) * (card_w + 16.0);
ctx.end_scroll_h(h_id, pal, total_w, false);
y + card_h + 26.0
}