Show the home's champions as a grid of cards with Played and Win marks (#72)

Add the champion card (portrait, name, "Joué" and "Victoire" marks, greyed with
"Validé" once completed) and a cached, virtualised card grid that replaces the
home's table. Columns follow the window width as in the mockup. Cards are built
once per statistics change; hovering or scrolling only redraws the grid.
Profiling part `home-list` becomes `cards`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
nessarandClaude Opus 5.5 committed 2026-11-20 21:21:16 +01:00
1 parent bd4de1ec05
commit bd9f1c7c98
6 files changed
+927 -48

No files matched your search

+237
View File
@@ -0,0 +1,237 @@
//! The card grid: all the cards a list shows, in rows of the same count, only the visible rows
//! built. It is its own view because it owns the scrolling and the hover of its cards.
use std::ops::Range;
use std::rc::Rc;
use gpui_kit::component::scroll::Scrollbar;
use gpui_kit::component::{ActiveTheme as _, StyledExt as _};
use gpui_kit::*;
use crate::card::{ASPECT, Card, card};
use crate::page::PAGE_PADDING;
/// The widest content column of the home, as in the mockup.
pub const MAX_WIDTH: f32 = 1264.;
/// How many cards fit a row of a window `width` wide, with the mockup's breakpoints.
fn columns(width: Pixels) -> usize {
match width.as_f32() {
w if w >= 1000. => 6,
w if w >= 720. => 4,
w if w >= 480. => 3,
_ => 2,
}
}
/// Smaller gaps in the narrow layouts, as in the mockup.
fn gap(columns: usize) -> f32 {
if columns >= 4 { 14. } else { 10. }
}
/// What a frame needs to know, from the window width alone: `uniform_list` measures its first
/// row without knowing the list's width, so the row height cannot come from the layout.
#[derive(Clone, Copy, Debug, PartialEq)]
struct Metrics {
columns: usize,
gap: f32,
/// A card's height; a row is this plus a gap tall.
card_height: f32,
}
impl Metrics {
fn of(window_width: Pixels) -> Metrics {
let columns = columns(window_width);
let gap = gap(columns);
// The column the page centres, minus the scrollbar's lane.
let content = (window_width.as_f32() - 2. * PAGE_PADDING).min(MAX_WIDTH)
- Scrollbar::width().as_f32();
let card_width = (content - gap * (columns - 1) as f32) / columns as f32;
Metrics {
columns,
gap,
card_height: (card_width / ASPECT).max(0.),
}
}
}
pub struct CardGrid {
/// All the cards, A to Z; rebuilt only when the statistics change.
cards: Rc<[Card]>,
/// Indices into `cards`, after the filter and the search.
shown: Vec<usize>,
scroll: UniformListScrollHandle,
/// Guard the light rendering in tests.
#[cfg(test)]
pub(crate) renders: usize,
#[cfg(test)]
pub(crate) card_builds: usize,
#[cfg(test)]
pub(crate) set_cards_calls: usize,
}
impl CardGrid {
pub fn new() -> CardGrid {
CardGrid {
cards: Rc::new([]),
shown: Vec::new(),
scroll: UniformListScrollHandle::new(),
#[cfg(test)]
renders: 0,
#[cfg(test)]
card_builds: 0,
#[cfg(test)]
set_cards_calls: 0,
}
}
/// Replaces the cards; the grid is drawn again only when they differ (reloads keep coming
/// during an import). `set_shown` follows, as the indices belong to the cards.
pub fn set_cards(&mut self, cards: Rc<[Card]>, cx: &mut Context<Self>) {
#[cfg(test)]
{
self.set_cards_calls += 1;
}
if *self.cards != *cards {
self.cards = cards;
cx.notify();
}
}
pub fn set_shown(&mut self, shown: Vec<usize>, cx: &mut Context<Self>) {
if self.shown != shown {
self.shown = shown;
cx.notify();
}
}
#[cfg(test)]
pub(crate) fn names(&self) -> Vec<SharedString> {
self.shown
.iter()
.filter_map(|&ix| self.cards.get(ix))
.map(|card| card.name.clone())
.collect()
}
/// How far the grid is scrolled down.
#[cfg(test)]
pub(crate) fn scrolled(&self) -> Pixels {
-self.scroll.0.borrow().base_handle.offset().y
}
fn row(&mut self, ix: usize, metrics: Metrics, cx: &mut Context<Self>) -> AnyElement {
let start = ix * metrics.columns;
let row = div()
.h_flex()
.items_start()
.w_full()
.h(px(metrics.card_height + metrics.gap))
.pb(px(metrics.gap))
.pr(Scrollbar::width())
.gap(px(metrics.gap));
let slots = (start..start + metrics.columns).map(|slot| {
// A stale index shows nothing rather than panicking.
match self.shown.get(slot).and_then(|&c| self.cards.get(c)) {
Some(data) => {
#[cfg(test)]
{
self.card_builds += 1;
}
card(data, cx).into_any_element()
}
// The last row keeps every card the same width.
None => div().flex_1().min_w_0().into_any_element(),
}
});
row.children(slots.collect::<Vec<_>>()).into_any_element()
}
}
impl Render for CardGrid {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
#[cfg(test)]
{
self.renders += 1;
}
if self.shown.is_empty() {
let muted = cx.theme().muted_foreground;
return div()
.w_full()
.debug_selector(|| "no-champion".into())
.py(px(30.))
.text_color(muted)
.child("Aucun champion trouvé")
.into_any_element();
}
let metrics = Metrics::of(window.viewport_size().width);
let rows = self.shown.len().div_ceil(metrics.columns);
div()
.relative()
.size_full()
.child(
uniform_list(
"cards",
rows,
cx.processor(move |grid, range: Range<usize>, _, cx| {
range
.map(|ix| grid.row(ix, metrics, cx))
.collect::<Vec<_>>()
}),
)
.size_full()
.track_scroll(&self.scroll),
)
.child(
div()
.absolute()
.top_0()
.right_0()
.bottom_0()
.w(Scrollbar::width())
.child(Scrollbar::vertical(&self.scroll)),
)
.into_any_element()
}
}
#[cfg(test)]
mod tests {
use gpui_kit::px;
// Explicit imports: a glob of `gpui_kit` would shadow the built-in `#[test]`.
use super::{Metrics, columns};
#[test]
fn the_columns_follow_the_mockups_breakpoints() {
for (width, expected) in [
(2560., 6),
(1280., 6),
(1000., 6),
(999., 4),
(720., 4),
(719., 3),
(480., 3),
(479., 2),
(300., 2),
] {
assert_eq!(columns(px(width)), expected, "{width} px");
}
}
#[test]
fn the_cards_fill_the_column_the_page_centres() {
use gpui_kit::component::scroll::Scrollbar;
for (window, column) in [(1280., 1256.), (3000., 1264.), (800., 776.), (500., 476.)] {
let metrics = Metrics::of(px(window));
let card_width = metrics.card_height * super::ASPECT;
let row = card_width * metrics.columns as f32
+ metrics.gap * (metrics.columns - 1) as f32
+ Scrollbar::width().as_f32();
assert!(
(row - column).abs() < 0.01,
"{window} px: {row} vs {column}"
);
}
}
}