Show the champion's sheet on hover and keyboard focus (#73)
The sheet covers the card with the Season's Matches, Wins, best Placement and Attempts, and the date of the first top 1 for a Completed champion. Its lines are prepared in Card::from_row; only the shown cards build it. The grid keeps the hovered card and one focus handle per card id, and handles Tab and Shift-Tab itself, scrolling the next card's row into view. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bd9f1c7c98
commit
796fae11b7
5 files changed
+520
-8
No files matched your search
+180
-3
@@ -8,6 +8,8 @@ use gpui_kit::component::{ActiveTheme as _, Icon, IconName, StyledExt as _};
|
||||
use gpui_kit::prelude::FluentBuilder as _;
|
||||
use gpui_kit::*;
|
||||
|
||||
use crate::text::ordinal;
|
||||
|
||||
/// Width over height.
|
||||
pub const ASPECT: f32 = 0.68;
|
||||
const RADIUS: f32 = 10.;
|
||||
@@ -22,6 +24,18 @@ const MARK_OFF: u32 = 0xcdccd4;
|
||||
const MARK_OFF_RING: u32 = 0xacaab7;
|
||||
const BADGE_BG: u32 = 0x202027ee;
|
||||
const BADGE_TEXT: u32 = 0xf0eff3;
|
||||
// The sheet is dark in both themes, like the shade it replaces.
|
||||
const SHEET_BG: u32 = 0x16161ef5;
|
||||
const SHEET_LABEL: u32 = 0xb4b4c3;
|
||||
|
||||
// The sheet's lines, in order. Mastery Points will join them (one more line).
|
||||
const PLAYED: &str = "Parties";
|
||||
const WINS: &str = "Victoires";
|
||||
const BEST_PLACEMENT: &str = "Meilleur Placement";
|
||||
const ATTEMPTS: &str = "Tentatives";
|
||||
/// Only on a Completed champion.
|
||||
pub const FIRST_TOP_1: &str = "Premier top 1";
|
||||
const NONE: &str = "—";
|
||||
|
||||
/// Why a card is greyed out. Later tickets add the champ select's reasons (banned, taken by an
|
||||
/// ally, not offered).
|
||||
@@ -54,6 +68,17 @@ pub struct Card {
|
||||
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)>,
|
||||
}
|
||||
|
||||
/// What the grid knows of a card that the card does not: whether to show its sheet, and the
|
||||
/// focus handle that makes it reachable with the keyboard.
|
||||
#[derive(Clone, Copy, Default)]
|
||||
pub struct CardState<'a> {
|
||||
pub sheet: bool,
|
||||
pub focus: Option<&'a FocusHandle>,
|
||||
}
|
||||
|
||||
impl Card {
|
||||
@@ -77,6 +102,7 @@ impl Card {
|
||||
} else {
|
||||
Vec::new()
|
||||
},
|
||||
sheet: sheet_lines(row),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,9 +111,34 @@ impl Card {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
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)),
|
||||
];
|
||||
if let Some(date) = challenge.and_then(|c| c.completed_on) {
|
||||
lines.push((FIRST_TOP_1, date.to_string().into()));
|
||||
}
|
||||
lines
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
/// border colour; the sheet covers it when `state` says so, and a focus handle makes it a tab
|
||||
/// stop with an accent border while the keyboard is on it. The caller adds its own listeners.
|
||||
pub fn card(card: &Card, state: CardState<'_>, cx: &App) -> Stateful<Div> {
|
||||
let theme = cx.theme();
|
||||
let (border, ring, background) = (theme.border, theme.ring, theme.muted);
|
||||
let greyed = card.greyed();
|
||||
@@ -104,6 +155,10 @@ pub fn card(card: &Card, cx: &App) -> impl IntoElement {
|
||||
.border_color(border)
|
||||
.bg(background)
|
||||
.hover(move |style| style.border_color(ring))
|
||||
.when_some(state.focus, |el, handle| {
|
||||
el.track_focus(handle)
|
||||
.focus_visible(move |style| style.border_color(ring))
|
||||
})
|
||||
.debug_selector(|| format!("card-{id}"))
|
||||
.children(card.portrait.as_ref().map(|path| {
|
||||
img(path.clone())
|
||||
@@ -127,6 +182,47 @@ pub fn card(card: &Card, cx: &App) -> impl IntoElement {
|
||||
.child(shade())
|
||||
.when(greyed, |el| el.child(reasons(&card.reasons)))
|
||||
.child(caption(card))
|
||||
.when(state.sheet, |el| el.child(sheet(card)))
|
||||
}
|
||||
|
||||
/// Over the whole card, as the mockup's `.card-info`: the name, then a line per figure. Only the
|
||||
/// shown cards build it.
|
||||
fn sheet(card: &Card) -> Div {
|
||||
let id = card.id;
|
||||
div()
|
||||
.absolute()
|
||||
.inset_0()
|
||||
.rounded(px(INNER_RADIUS))
|
||||
.v_flex()
|
||||
.justify_center()
|
||||
.p(px(12.))
|
||||
.bg(rgba(SHEET_BG))
|
||||
.text_size(px(12.))
|
||||
.text_color(rgb(SHEET_LABEL))
|
||||
.debug_selector(|| format!("sheet-{id}"))
|
||||
.child(
|
||||
div()
|
||||
.mb(px(10.))
|
||||
.text_size(px(17.))
|
||||
.font_weight(FontWeight::SEMIBOLD)
|
||||
.text_color(white())
|
||||
.child(card.name.clone()),
|
||||
)
|
||||
.children(card.sheet.iter().map(|(label, value)| {
|
||||
div()
|
||||
.h_flex()
|
||||
.justify_between()
|
||||
.gap(px(4.))
|
||||
.py(px(2.))
|
||||
.debug_selector(|| format!("sheet-{id}-{label}"))
|
||||
.child(*label)
|
||||
.child(
|
||||
div()
|
||||
.font_weight(FontWeight::SEMIBOLD)
|
||||
.text_color(white())
|
||||
.child(value.clone()),
|
||||
)
|
||||
}))
|
||||
}
|
||||
|
||||
/// Keeps the caption readable on any portrait.
|
||||
@@ -231,7 +327,7 @@ 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};
|
||||
use super::{Card, FIRST_TOP_1, Reason};
|
||||
|
||||
fn row(challenge: Option<ChampionChallenge>) -> ChampionRow {
|
||||
ChampionRow {
|
||||
@@ -316,6 +412,87 @@ mod tests {
|
||||
assert_eq!(card.name.as_ref(), "Twisted Fate");
|
||||
}
|
||||
|
||||
fn lines(card: &Card) -> Vec<(&'static str, String)> {
|
||||
card.sheet
|
||||
.iter()
|
||||
.map(|(label, value)| (*label, value.to_string()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sheet_of_a_champion_never_played_is_empty_figures_without_a_date() {
|
||||
let mut row = row(Some(ChampionChallenge::default()));
|
||||
row.attempts = Some(0);
|
||||
assert_eq!(
|
||||
lines(&Card::from_row(&row)),
|
||||
[
|
||||
("Parties", "0".to_string()),
|
||||
("Victoires", "0".into()),
|
||||
("Meilleur Placement", "—".into()),
|
||||
("Tentatives", "0".into()),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sheet_of_a_remaining_champion_has_its_best_placement_and_no_date() {
|
||||
let challenge = ChampionChallenge {
|
||||
matches: 5,
|
||||
wins: 2,
|
||||
best_placement: Some(2),
|
||||
completed_on: None,
|
||||
};
|
||||
let mut row = row(Some(challenge));
|
||||
row.attempts = Some(5);
|
||||
let card = Card::from_row(&row);
|
||||
assert_eq!(
|
||||
lines(&card),
|
||||
[
|
||||
("Parties", "5".to_string()),
|
||||
("Victoires", "2".into()),
|
||||
("Meilleur Placement", "2ᵉ".into()),
|
||||
("Tentatives", "5".into()),
|
||||
]
|
||||
);
|
||||
assert!(card.sheet.iter().all(|(label, _)| *label != FIRST_TOP_1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sheet_of_a_completed_champion_has_the_date_of_its_first_top_1() {
|
||||
let challenge = ChampionChallenge {
|
||||
matches: 6,
|
||||
wins: 3,
|
||||
best_placement: Some(1),
|
||||
completed_on: Some(day()),
|
||||
};
|
||||
let mut row = row(Some(challenge));
|
||||
row.attempts = Some(4);
|
||||
assert_eq!(
|
||||
lines(&Card::from_row(&row)),
|
||||
[
|
||||
("Parties", "6".to_string()),
|
||||
("Victoires", "3".into()),
|
||||
("Meilleur Placement", "1ᵉʳ".into()),
|
||||
("Tentatives", "4".into()),
|
||||
("Premier top 1", "02/03/2026".into()),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sheet_of_a_row_without_a_season_has_dashes_and_no_date() {
|
||||
let card = Card::from_row(&row(None));
|
||||
assert_eq!(
|
||||
lines(&card),
|
||||
[
|
||||
("Parties", "—".to_string()),
|
||||
("Victoires", "—".into()),
|
||||
("Meilleur Placement", "—".into()),
|
||||
("Tentatives", "—".into()),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_row_without_a_challenge_has_no_marks() {
|
||||
let card = Card::from_row(&row(None));
|
||||
|
||||
Reference in new issue
Block a user