Files
arena-tracker/crates/app/src/card.rs
T
nessarandClaude Opus 5.5 f257c57b27
CI / core (pull_request) Successful in 1m50s
Show the Live Match in a tab of its own, « En partie » (#109)
- The core reads the champion played in the client's gameflow session
  (champion selections, else the teams, by the summoner's client PUUID) and
  follows the Live Match from one read to the next: it starts with an Arena
  game in progress and lasts through that game's end-of-game screen.
- The follower tells the page the Live Match on each change, before the champ
  select's end, so the end of a champ select that launched a game shows
  « En partie » instead of « Défi », without computing the statistics again.
- « En partie »: the banner with the champion's splash, the Season and the
  count, its tick of the ring lit and the stake (« Un top 1 validerait X :
  88 / 172 », or the day it was validated); « Champion en cours
  d’identification » until the client says it. Under it, the card's sheet and
  the champion's 5 latest Matches of the Season, read at low priority.
- A Live Match is a phase like the champ select: the signed-in Account and the
  current Season, the Account selector greyed, « Paramètres » closed, and a
  minimised or hidden window shown again without the focus, never raised.
- Its end-of-game block ends it and #129 decides the screen; without a block,
  the lobby shows « Défi ». A screen gone with its tab falls back to « Défi ».
- Tests with the simulated client: launch from the champ select, a client met
  in a game, the champion identified late, the end with and without a block,
  a game outside Arena. docs/interface.md and the guide are updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0192He3BEKJ9v922udzup4Qn
2026-11-25 17:58:01 +01:00

421 lines
14 KiB
Rust

//! The champion card: the one representation of a champion, on the home and in the Arena champ
//! select. A `Card` is built once when the statistics change; `card` only lays it out.
mod detail;
use detail::{caption, reasons, shade, sheet};
use std::path::Path;
use std::sync::{Arc, LazyLock};
use arena_tracker_core::{ChampionRow, Marks, Unpickable};
use gpui_kit::component::{ActiveTheme as _, Icon, IconName, StyledExt as _};
use gpui_kit::prelude::FluentBuilder as _;
use gpui_kit::*;
use crate::text::{ordinal, thousands};
use crate::theme::{LOGO_GOLD, ON_GOLD, gold_glow, surface};
/// Width over height: the ratio the stored portraits are cropped to.
pub const ASPECT: f32 = arena_tracker_core::CARD_ASPECT as f32;
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 MARK_ON: u32 = 0xffffff;
const MARK_OFF: u32 = 0xd3d4db;
const MARK_OFF_RING: u32 = 0xbfc1cd;
const BADGE_BG: u32 = 0x11152aee;
const BADGE_TEXT: u32 = 0xffa0a0;
// The sheet is dark in both themes, like the shade it replaces.
const SHEET_BG: u32 = 0x0d1021f5;
const SHEET_LABEL: u32 = 0xc5cbe0;
// The sheet's lines, in order.
const PLAYED: &str = "Parties";
const WINS: &str = "Victoires";
const BEST_PLACEMENT: &str = "Meilleur Placement";
const ATTEMPTS: &str = "Tentatives";
const MASTERY: &str = "Points de maîtrise";
const UNKNOWN: &str = "Inconnus";
/// Only on a Completed champion.
pub const FIRST_TOP_1: &str = "Premier top 1";
const NONE: &str = "—";
/// Why a card is greyed out, in the order the badges are shown.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Reason {
/// At least one top 1 in the Season.
Completed,
/// Banned in the champ select.
Banned,
/// Confirmed by an ally in the champ select.
TakenByAlly,
/// The champ select does not offer the champion.
NotOffered,
}
impl Reason {
pub fn label(self) -> &'static str {
match self {
Reason::Completed => "Validé",
Reason::Banned => "Banni",
Reason::TakenByAlly => "Pris par un allié",
Reason::NotOffered => "Indisponible",
}
}
fn badge(self, id: u32) -> Div {
let badge = div()
.h_flex()
.items_center()
.gap(px(4.))
.px(px(6.))
.py(px(3.))
.rounded(px(4.))
.text_size(px(10.))
.font_weight(FontWeight::SEMIBOLD)
.debug_selector(move || format!("reason-{id}-{}", self.label()));
let ordinary = |badge: Div, icon: IconName| {
badge
.bg(rgba(BADGE_BG))
.text_color(rgb(BADGE_TEXT))
.child(Icon::new(icon).size(px(12.)))
};
let badge = match self {
Reason::Completed => {
static CROWN: LazyLock<Arc<Image>> = LazyLock::new(|| {
let svg = include_str!("../assets/crown.svg")
.replace("currentColor", &format!("#{ON_GOLD:06x}"));
Arc::new(Image::from_bytes(ImageFormat::Svg, svg.into_bytes()))
});
badge
.bg(rgb(LOGO_GOLD))
.text_color(rgb(ON_GOLD))
.child(img(CROWN.clone()).size(px(12.)))
}
Reason::Banned => ordinary(badge, IconName::Ban),
Reason::TakenByAlly => ordinary(badge, IconName::User),
Reason::NotOffered => ordinary(badge, IconName::EyeOff),
};
badge.child(self.label())
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Card {
pub id: u32,
pub name: SharedString,
/// The tall portrait, else the square icon while the tall one is not downloaded yet.
/// Shared, so drawing the card clones a pointer rather than a path.
pub portrait: Option<Arc<Path>>,
pub marks: Marks,
/// In display order. A card with any reason is greyed.
pub reasons: Vec<Reason>,
/// The lines of the sheet shown on hover and focus, as label and value, prepared with the
/// card so showing it reads nothing.
pub sheet: Vec<(&'static str, SharedString)>,
}
/// How far a card is lifted over its grid (#88), drawn over it while the pointer or the keyboard
/// is on it: it rises, its illustration zooms in, its caption fades and its sheet slides up from
/// the bottom.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Pose {
/// The Marks the end of a followed Match has just given, filling in gold (#89).
pub mark_fill: Option<MarkFill>,
/// 0 at rest, 1 lifted all the way.
pub progress: f32,
/// The hand's veil on this card: 0 to 1.
pub veil: f32,
/// Off with the Windows « Show animations » setting: the sheet shows, the card stays put.
pub moves: bool,
}
/// A card's Marks before the end of a followed Match, and how far the gold of those it gained has
/// filled in: 0 to 1.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MarkFill {
pub before: Marks,
pub progress: f32,
}
impl MarkFill {
/// The opacity of a Mark's gold: filling in when just gained, full when it was already on.
fn opacity(fill: Option<MarkFill>, was_on: fn(&Marks) -> bool) -> f32 {
fill.filter(|f| !was_on(&f.before))
.map_or(1., |f| f.progress)
}
}
/// How high a card rises: less for a greyed one, which cannot be picked.
const RISE: f32 = 10.;
const GREYED_RISE: f32 = 4.;
/// How much the illustration zooms in, inside the card.
const ZOOM: f32 = 0.07;
/// How far the sheet slides up, and the caption down.
const SHEET_SLIDE: f32 = 14.;
const CAPTION_SLIDE: f32 = 6.;
const CAPTION_BOTTOM: f32 = 12.;
/// The hand's veil on the other cards, as the mockup's `rgba(9, 11, 24, .42)`.
const VEIL: u32 = 0x090b18;
const VEIL_OPACITY: f32 = 0.42;
impl Pose {
/// How many pixels the card is above its place.
pub fn rise(&self, card: &Card) -> f32 {
if !self.moves {
return 0.;
}
self.progress * if card.greyed() { GREYED_RISE } else { RISE }
}
fn zoom(&self) -> f32 {
if self.moves { self.progress * ZOOM } else { 0. }
}
}
/// The hand's veil on a card at rest in it, `veil` from 0 to 1.
pub fn veil_color(veil: f32) -> Hsla {
Hsla::from(rgb(VEIL)).opacity(VEIL_OPACITY * veil)
}
/// The radius of a card, for what is drawn over one.
pub const CARD_RADIUS: f32 = RADIUS;
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
.card_portrait
.as_deref()
.or(row.portrait.as_deref())
.map(Arc::from),
marks,
reasons: if marks.completed {
vec![Reason::Completed]
} else {
Vec::new()
},
sheet: sheet_lines(row),
}
}
/// Adds the champ select's reasons after the Season's. The player's own pick has none.
pub fn with_unpickable(mut self, unpickable: Unpickable) -> Card {
let Unpickable {
banned,
taken_by_ally,
not_offered,
} = unpickable;
for (applies, reason) in [
(banned, Reason::Banned),
(taken_by_ally, Reason::TakenByAlly),
(not_offered, Reason::NotOffered),
] {
if applies {
self.reasons.push(reason);
}
}
self
}
pub fn greyed(&self) -> bool {
!self.reasons.is_empty()
}
}
/// The sheet's figures for the Season, all Formats; « — » where there is none (no Season, or no
/// Match). The date is only there once the champion is Completed. « En partie » shows them too.
pub fn sheet_lines(row: &ChampionRow) -> Vec<(&'static str, SharedString)> {
let challenge = row.challenge;
let count = |n: Option<usize>| -> SharedString {
n.map_or_else(|| NONE.into(), |n| n.to_string().into())
};
let mut lines = vec![
(PLAYED, count(challenge.map(|c| c.matches))),
(WINS, count(challenge.map(|c| c.wins))),
(
BEST_PLACEMENT,
challenge
.and_then(|c| c.best_placement)
.map_or_else(|| NONE.into(), |placement| ordinal(placement).into()),
),
(ATTEMPTS, count(row.attempts)),
// Unknown is not zero: the points were never fetched for this Account.
(
MASTERY,
row.mastery_points
.map_or_else(|| UNKNOWN.into(), |points| thousands(points).into()),
),
];
if let Some(date) = challenge.and_then(|c| c.completed_on) {
lines.push((FIRST_TOP_1, date.to_string().into()));
}
lines
}
/// One card at rest, as wide as its parent gives it, `ASPECT` wide over tall. Nothing about it
/// changes on hover or focus, so a grid of them stays cached: the card under the pointer or the
/// keyboard is drawn again over it, lifted (`lifted`). A focus handle makes it a tab stop. The
/// caller adds its own listeners.
pub fn card(card: &Card, focus: Option<&FocusHandle>, cx: &App) -> Stateful<Div> {
let id = card.id;
let root = div().flex_1().min_w_0().aspect_ratio(ASPECT);
face(root, card, None, cx)
.id(("card", id))
.when_some(focus, |el, handle| el.track_focus(handle))
.debug_selector(|| format!("card-{id}"))
}
/// The card drawn over its grid in `pose`, `size` as its place there; the caller sets it
/// `pose.rise` above. It has no listener: the pointer still meets the card at rest under it.
pub fn lifted(card: &Card, pose: Pose, size: Size<Pixels>, cx: &App) -> Div {
let theme = cx.theme();
let progress = pose.progress;
let shadows = if !pose.moves || progress == 0. {
Vec::new()
} else if card.greyed() {
vec![drop_shadow(12., 24., -14., 0.6 * progress)]
} else {
vec![
drop_shadow(18., 34., -14., 0.65 * progress),
BoxShadow {
color: gold_glow(theme).opacity(progress),
offset: point(px(0.), px(0.)),
blur_radius: px(28.),
spread_radius: px(-8.),
inset: false,
},
]
};
let id = card.id;
face(div().w(size.width).h(size.height), card, Some(pose), cx)
.shadow(shadows)
.debug_selector(move || format!("lifted-{id}"))
}
/// The card at rest drawn over its grid, `size` as its place there, under the hand's `veil`
/// (0 to 1): the champ select's motion (#87) moves it while the grid stays put. It has no
/// listener.
pub fn moving(card: &Card, veil: f32, size: Size<Pixels>, cx: &App) -> Div {
let id = card.id;
let pose = (veil > 0.).then_some(Pose {
mark_fill: None,
progress: 0.,
veil,
moves: true,
});
face(div().w(size.width).h(size.height), card, pose, cx)
.debug_selector(move || format!("moving-{id}"))
}
fn drop_shadow(y: f32, blur: f32, spread: f32, opacity: f32) -> BoxShadow {
BoxShadow {
color: hsla(0., 0., 0., opacity),
offset: point(px(0.), px(y)),
blur_radius: px(blur),
spread_radius: px(spread),
inset: false,
}
}
/// What a card shows, at rest (`None`) or lifted, in `root`.
fn face(root: Div, card: &Card, pose: Option<Pose>, cx: &App) -> Div {
let theme = cx.theme();
let greyed = card.greyed();
let id = card.id;
let progress = pose.map_or(0., |pose| pose.progress);
let zoom = pose.map_or(0., |pose| pose.zoom());
// Towards the accent as the card rises, as the hover and focus border did.
let border = theme.border.blend(theme.ring.opacity(progress));
root.relative()
.overflow_hidden()
.rounded(px(RADIUS))
.border_1()
.border_color(border)
.bg(surface(theme))
.children(card.portrait.as_ref().map(|path| {
// Zoomed in, the illustration overflows the card on every side, and is cut square:
// `corners` rounds it again.
img(path.clone())
.absolute()
.left(relative(-zoom / 2.))
.top(relative(-zoom / 2.))
.w(relative(1. + zoom))
.h(relative(1. + zoom))
.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(id, &card.reasons)))
.when(progress < 1., |el| {
let slide = if pose.is_some_and(|pose| pose.moves) {
CAPTION_SLIDE * progress
} else {
0.
};
el.child(
caption(card, pose.and_then(|p| p.mark_fill))
.bottom(px(CAPTION_BOTTOM - slide))
.opacity(1. - progress),
)
})
.when(progress > 0., |el| {
let slide = if pose.is_some_and(|pose| pose.moves) {
SHEET_SLIDE * (1. - progress)
} else {
0.
};
el.child(sheet(card).bottom(px(-slide)).opacity(progress))
})
.when(zoom > 0., |el| el.child(corners(theme.background)))
.children(pose.filter(|pose| pose.veil > 0.).map(|pose| {
div()
.absolute()
.inset_0()
.rounded(px(INNER_RADIUS))
.bg(veil_color(pose.veil))
}))
}
/// What lies outside the card's rounded corners, in `background`: a frame whose inner edge is
/// the card's radius, of which only the corners fall inside the card.
fn corners(background: Hsla) -> Div {
let width = RADIUS;
div()
.absolute()
.top(px(-width))
.left(px(-width))
.right(px(-width))
.bottom(px(-width))
.rounded(px(RADIUS + width))
.border(px(width))
.border_color(background)
}
#[cfg(test)]
mod tests;