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:
1 parent
bd4de1ec05
commit
bd9f1c7c98
6 files changed
+927
-48
No files matched your search
@@ -0,0 +1,325 @@
|
||||
//! The champion card: the one representation of a champion, on the home today and in the Arena
|
||||
//! champ select later. A `Card` is built once when the statistics change; `card` only lays it out.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use arena_tracker_core::{ChampionRow, Marks};
|
||||
use gpui_kit::component::{ActiveTheme as _, Icon, IconName, StyledExt as _};
|
||||
use gpui_kit::prelude::FluentBuilder as _;
|
||||
use gpui_kit::*;
|
||||
|
||||
/// Width over height.
|
||||
pub const ASPECT: f32 = 0.68;
|
||||
const RADIUS: f32 = 10.;
|
||||
/// The border is inside the radius: the layers it holds are rounded one pixel less.
|
||||
const INNER_RADIUS: f32 = RADIUS - 1.;
|
||||
|
||||
// The caption and badges sit on the dark shade, in both themes: their colours are fixed.
|
||||
const ACCENT: u32 = 0xc4b3fc;
|
||||
const INK: u32 = 0x201732;
|
||||
const MARK_ON: u32 = 0xdfd4ff;
|
||||
const MARK_OFF: u32 = 0xcdccd4;
|
||||
const MARK_OFF_RING: u32 = 0xacaab7;
|
||||
const BADGE_BG: u32 = 0x202027ee;
|
||||
const BADGE_TEXT: u32 = 0xf0eff3;
|
||||
|
||||
/// Why a card is greyed out. Later tickets add the champ select's reasons (banned, taken by an
|
||||
/// ally, not offered).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Reason {
|
||||
/// At least one top 1 in the Season.
|
||||
Completed,
|
||||
}
|
||||
|
||||
impl Reason {
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
Reason::Completed => "Validé",
|
||||
}
|
||||
}
|
||||
|
||||
fn icon(self) -> IconName {
|
||||
match self {
|
||||
Reason::Completed => IconName::Check,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Card {
|
||||
pub id: u32,
|
||||
pub name: SharedString,
|
||||
/// The square icon today; the tall art later.
|
||||
pub portrait: Option<PathBuf>,
|
||||
pub marks: Marks,
|
||||
/// In display order. A card with any reason is greyed.
|
||||
pub reasons: Vec<Reason>,
|
||||
}
|
||||
|
||||
impl Card {
|
||||
/// A row without a Challenge (no Season) has no Marks.
|
||||
pub fn from_row(row: &ChampionRow) -> Card {
|
||||
let marks = row.challenge.map_or(
|
||||
Marks {
|
||||
played: false,
|
||||
won: false,
|
||||
completed: false,
|
||||
},
|
||||
|challenge| challenge.marks(),
|
||||
);
|
||||
Card {
|
||||
id: row.champion.id,
|
||||
name: row.champion.name.clone().into(),
|
||||
portrait: row.portrait.clone(),
|
||||
marks,
|
||||
reasons: if marks.completed {
|
||||
vec![Reason::Completed]
|
||||
} else {
|
||||
Vec::new()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn greyed(&self) -> bool {
|
||||
!self.reasons.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// One card, as wide as its parent gives it, `ASPECT` wide over tall. Hovering only changes the
|
||||
/// border colour.
|
||||
pub fn card(card: &Card, cx: &App) -> impl IntoElement {
|
||||
let theme = cx.theme();
|
||||
let (border, ring, background) = (theme.border, theme.ring, theme.muted);
|
||||
let greyed = card.greyed();
|
||||
let id = card.id;
|
||||
div()
|
||||
.id(("card", id))
|
||||
.relative()
|
||||
.flex_1()
|
||||
.min_w_0()
|
||||
.aspect_ratio(ASPECT)
|
||||
.overflow_hidden()
|
||||
.rounded(px(RADIUS))
|
||||
.border_1()
|
||||
.border_color(border)
|
||||
.bg(background)
|
||||
.hover(move |style| style.border_color(ring))
|
||||
.debug_selector(|| format!("card-{id}"))
|
||||
.children(card.portrait.as_ref().map(|path| {
|
||||
img(path.clone())
|
||||
.absolute()
|
||||
.inset_0()
|
||||
.size_full()
|
||||
.rounded(px(INNER_RADIUS))
|
||||
.object_fit(ObjectFit::Cover)
|
||||
.grayscale(greyed)
|
||||
}))
|
||||
// Stands for the mockup's `brightness(.72)`, which the image element cannot do.
|
||||
.when(greyed, |el| {
|
||||
el.child(
|
||||
div()
|
||||
.absolute()
|
||||
.inset_0()
|
||||
.rounded(px(INNER_RADIUS))
|
||||
.bg(hsla(0., 0., 0., 0.28)),
|
||||
)
|
||||
})
|
||||
.child(shade())
|
||||
.when(greyed, |el| el.child(reasons(&card.reasons)))
|
||||
.child(caption(card))
|
||||
}
|
||||
|
||||
/// Keeps the caption readable on any portrait.
|
||||
fn shade() -> Div {
|
||||
let clear = hsla(240. / 360., 0.3, 0.06, 0.);
|
||||
let dark = hsla(240. / 360., 0.3, 0.06, 0.95);
|
||||
div()
|
||||
.absolute()
|
||||
.inset_0()
|
||||
.rounded(px(INNER_RADIUS))
|
||||
.bg(linear_gradient(
|
||||
180.,
|
||||
linear_color_stop(clear, 0.5),
|
||||
linear_color_stop(dark, 0.92),
|
||||
))
|
||||
}
|
||||
|
||||
fn reasons(reasons: &[Reason]) -> Div {
|
||||
div()
|
||||
.absolute()
|
||||
.top(px(9.))
|
||||
.left(px(8.))
|
||||
.right(px(8.))
|
||||
.h_flex()
|
||||
.items_start()
|
||||
.flex_wrap()
|
||||
.gap(px(3.))
|
||||
.children(reasons.iter().map(|reason| {
|
||||
div()
|
||||
.h_flex()
|
||||
.items_center()
|
||||
.gap(px(4.))
|
||||
.px(px(6.))
|
||||
.py(px(3.))
|
||||
.rounded(px(4.))
|
||||
.bg(rgba(BADGE_BG))
|
||||
.text_color(rgb(BADGE_TEXT))
|
||||
.text_size(px(10.))
|
||||
.font_weight(FontWeight::SEMIBOLD)
|
||||
.child(Icon::new(reason.icon()).size(px(12.)))
|
||||
.child(reason.label())
|
||||
}))
|
||||
}
|
||||
|
||||
fn caption(card: &Card) -> Div {
|
||||
div()
|
||||
.absolute()
|
||||
.left(px(12.))
|
||||
.right(px(8.))
|
||||
.bottom(px(12.))
|
||||
.v_flex()
|
||||
.child(
|
||||
div()
|
||||
.text_size(px(18.))
|
||||
.font_weight(FontWeight::SEMIBOLD)
|
||||
.line_height(relative(1.18))
|
||||
.text_color(white())
|
||||
.child(card.name.clone()),
|
||||
)
|
||||
.child(
|
||||
div()
|
||||
.h_flex()
|
||||
.items_center()
|
||||
.flex_wrap()
|
||||
.mt(px(7.))
|
||||
.gap_x(px(10.))
|
||||
.gap_y(px(4.))
|
||||
.child(mark("Joué", card.marks.played))
|
||||
.child(mark("Victoire", card.marks.won)),
|
||||
)
|
||||
}
|
||||
|
||||
/// A dot, filled with a check when on, and its label.
|
||||
fn mark(label: &'static str, on: bool) -> Div {
|
||||
div()
|
||||
.h_flex()
|
||||
.items_center()
|
||||
.gap(px(4.))
|
||||
.text_size(px(10.))
|
||||
.font_weight(FontWeight::SEMIBOLD)
|
||||
.text_color(rgb(if on { MARK_ON } else { MARK_OFF }))
|
||||
.child(
|
||||
div()
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.size(px(11.))
|
||||
.rounded_full()
|
||||
.border_1()
|
||||
.border_color(rgb(if on { ACCENT } else { MARK_OFF_RING }))
|
||||
.when(on, |dot| {
|
||||
dot.bg(rgb(ACCENT))
|
||||
.text_color(rgb(INK))
|
||||
.child(Icon::new(IconName::Check).size(px(8.)))
|
||||
}),
|
||||
)
|
||||
.child(label)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use arena_tracker_core::{Champion, ChampionChallenge, ChampionRow, Date};
|
||||
|
||||
// Explicit imports: a glob of `gpui_kit` would shadow the built-in `#[test]`.
|
||||
use super::{Card, Reason};
|
||||
|
||||
fn row(challenge: Option<ChampionChallenge>) -> ChampionRow {
|
||||
ChampionRow {
|
||||
champion: Champion {
|
||||
id: 4,
|
||||
key: "TwistedFate".into(),
|
||||
name: "Twisted Fate".into(),
|
||||
},
|
||||
portrait: None,
|
||||
card_portrait: None,
|
||||
matches: 0,
|
||||
best_placement: None,
|
||||
top1: 0,
|
||||
wins: 0,
|
||||
average_placement: None,
|
||||
last_match_ms: None,
|
||||
attempts: None,
|
||||
state: None,
|
||||
challenge,
|
||||
}
|
||||
}
|
||||
|
||||
fn day() -> Date {
|
||||
Date {
|
||||
year: 2026,
|
||||
month: 3,
|
||||
day: 2,
|
||||
}
|
||||
}
|
||||
|
||||
fn marks(card: &Card) -> (bool, bool) {
|
||||
(card.marks.played, card.marks.won)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_champion_never_played_has_no_mark_and_keeps_its_colours() {
|
||||
let card = Card::from_row(&row(Some(ChampionChallenge::default())));
|
||||
assert_eq!(marks(&card), (false, false));
|
||||
assert!(card.reasons.is_empty());
|
||||
assert!(!card.greyed());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_champion_played_without_a_win_has_only_the_played_mark() {
|
||||
let challenge = ChampionChallenge {
|
||||
matches: 3,
|
||||
best_placement: Some(4),
|
||||
..Default::default()
|
||||
};
|
||||
let card = Card::from_row(&row(Some(challenge)));
|
||||
assert_eq!(marks(&card), (true, false));
|
||||
assert!(!card.greyed());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_champion_with_a_win_but_no_top_1_is_in_colour_with_both_marks() {
|
||||
let challenge = ChampionChallenge {
|
||||
matches: 2,
|
||||
wins: 1,
|
||||
best_placement: Some(2),
|
||||
completed_on: None,
|
||||
};
|
||||
let card = Card::from_row(&row(Some(challenge)));
|
||||
assert_eq!(marks(&card), (true, true));
|
||||
assert!(!card.marks.completed);
|
||||
assert!(!card.greyed());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_completed_champion_is_greyed_with_the_validated_reason() {
|
||||
let challenge = ChampionChallenge {
|
||||
matches: 4,
|
||||
wins: 2,
|
||||
best_placement: Some(1),
|
||||
completed_on: Some(day()),
|
||||
};
|
||||
let card = Card::from_row(&row(Some(challenge)));
|
||||
assert_eq!(marks(&card), (true, true));
|
||||
assert_eq!(card.reasons, [Reason::Completed]);
|
||||
assert_eq!(card.reasons[0].label(), "Validé");
|
||||
assert!(card.greyed());
|
||||
assert_eq!(card.name.as_ref(), "Twisted Fate");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_row_without_a_challenge_has_no_marks() {
|
||||
let card = Card::from_row(&row(None));
|
||||
assert_eq!(marks(&card), (false, false));
|
||||
assert!(!card.greyed());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user