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
+294
-22
@@ -1,6 +1,7 @@
|
||||
//! The page and its two tabs: the shared state, the header row with its navigation, the banners and
|
||||
//! the champ select view. `home` and `statistics` render what is under the banners.
|
||||
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
||||
|
||||
@@ -22,6 +23,8 @@ use gpui_kit::*;
|
||||
mod home;
|
||||
mod statistics;
|
||||
|
||||
use crate::card::Card;
|
||||
use crate::card_grid::CardGrid;
|
||||
use crate::champion_table::ChampionTable;
|
||||
use crate::text::{CLIENT_ABSENT, official_differs, sync_line};
|
||||
use crate::worker::{
|
||||
@@ -29,6 +32,8 @@ use crate::worker::{
|
||||
};
|
||||
use statistics::ChartsRow;
|
||||
|
||||
/// The page's padding on every side: the card grid derives its width from it.
|
||||
pub(crate) const PAGE_PADDING: f32 = 12.;
|
||||
const ALL_SEASONS: &str = "Toutes les Saisons";
|
||||
const ALL_FORMATS: &str = "Tous les Formats";
|
||||
/// While an import runs, the page is recomputed at most this often.
|
||||
@@ -168,8 +173,10 @@ pub struct Page {
|
||||
format_names: Vec<SharedString>,
|
||||
charts: Entity<ChartsRow>,
|
||||
table: Entity<ChampionTable>,
|
||||
/// Temporary: the cards replace it (#72).
|
||||
home_table: Entity<ChampionTable>,
|
||||
/// The home's champions, one card each.
|
||||
home_grid: Entity<CardGrid>,
|
||||
/// How many cards the grid shows, prepared when the filter or search change.
|
||||
home_shown_count: SharedString,
|
||||
_subscriptions: Vec<Subscription>,
|
||||
_listener: Task<()>,
|
||||
}
|
||||
@@ -211,7 +218,7 @@ impl Page {
|
||||
let charts = cx.new(|_| ChartsRow::new());
|
||||
let home_search_input = cx.new(|cx| InputState::new(window, cx).placeholder("Rechercher"));
|
||||
let table = cx.new(|_| ChampionTable::new());
|
||||
let home_table = cx.new(|_| ChampionTable::new());
|
||||
let home_grid = cx.new(|_| CardGrid::new());
|
||||
|
||||
let subscriptions = vec![
|
||||
cx.subscribe(&search_input, |this, input, event: &InputEvent, cx| {
|
||||
@@ -333,7 +340,8 @@ impl Page {
|
||||
format_names,
|
||||
charts,
|
||||
table,
|
||||
home_table,
|
||||
home_grid,
|
||||
home_shown_count: SharedString::default(),
|
||||
_subscriptions: subscriptions,
|
||||
_listener: listener,
|
||||
};
|
||||
@@ -747,6 +755,7 @@ impl Page {
|
||||
return;
|
||||
}
|
||||
self.prepare_home_labels();
|
||||
self.prepare_home_cards(cx);
|
||||
let charts = self.stats.as_ref().map(|s| &s.charts);
|
||||
let in_season = self.in_season();
|
||||
self.charts
|
||||
@@ -778,6 +787,16 @@ impl Page {
|
||||
self.home_fraction = fraction;
|
||||
}
|
||||
|
||||
/// One card per champion, in the statistics' order (A to Z), for when the statistics change.
|
||||
fn prepare_home_cards(&mut self, cx: &mut Context<Self>) {
|
||||
let cards: Rc<[Card]> = self
|
||||
.home_statistics()
|
||||
.map(|s| s.rows.iter().map(Card::from_row).collect())
|
||||
.unwrap_or_default();
|
||||
self.home_grid
|
||||
.update(cx, |grid, cx| grid.set_cards(cards, cx));
|
||||
}
|
||||
|
||||
fn in_champ_select(&self) -> bool {
|
||||
self.champ_select.is_some()
|
||||
}
|
||||
@@ -865,21 +884,24 @@ impl Page {
|
||||
});
|
||||
}
|
||||
|
||||
/// Rebuilds the home's rows from its own filter and search, with the total untouched; while
|
||||
/// the home is not shown, the next switch does.
|
||||
/// Recomputes which cards the home shows from its own filter and search, with the total
|
||||
/// untouched; while the home is not shown, the next switch does.
|
||||
fn refresh_home(&mut self, cx: &mut Context<Self>) {
|
||||
if self.tab != Tab::Challenge || self.in_champ_select() {
|
||||
return;
|
||||
}
|
||||
// The cards are the rows, in the same order: a card's index is its row's.
|
||||
let rows = self.home_statistics().into_iter().flat_map(|s| &s.rows);
|
||||
let rows = rows
|
||||
.filter(|row| self.home_filter.keeps(row) && self.home_search.matches(&row.champion))
|
||||
.cloned()
|
||||
let shown: Vec<usize> = rows
|
||||
.enumerate()
|
||||
.filter(|(_, row)| {
|
||||
self.home_filter.keeps(row) && self.home_search.matches(&row.champion)
|
||||
})
|
||||
.map(|(ix, _)| ix)
|
||||
.collect();
|
||||
self.home_table.update(cx, |table, cx| {
|
||||
table.set_rows(rows, true);
|
||||
cx.notify();
|
||||
});
|
||||
self.home_shown_count = shown.len().to_string().into();
|
||||
self.home_grid
|
||||
.update(cx, |grid, cx| grid.set_shown(shown, cx));
|
||||
}
|
||||
|
||||
fn champ_select_view(&self, cx: &App) -> Div {
|
||||
@@ -1044,7 +1066,7 @@ impl Render for Page {
|
||||
.v_flex()
|
||||
.size_full()
|
||||
.gap_3()
|
||||
.p_3()
|
||||
.p(px(PAGE_PADDING))
|
||||
.when(shown("header"), |page| {
|
||||
page.child(
|
||||
// The selectors give way first (down to their minimum width), then the status
|
||||
@@ -1111,17 +1133,20 @@ mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use arena_tracker_core::{
|
||||
Account, ChallengeSummary, Champion, ChampionRow, ChampionState, Charts, Date, Format,
|
||||
MatchHistorySource, RiotApi, Season, SeasonScope, SourceError, StateFilter, Statistics,
|
||||
Tracker,
|
||||
Account, ChallengeSummary, Champion, ChampionChallenge, ChampionRow, ChampionState, Charts,
|
||||
Date, Format, MatchHistorySource, RiotApi, Season, SeasonScope, SourceError, StateFilter,
|
||||
Statistics, Tracker,
|
||||
};
|
||||
use gpui_kit::base::async_util::unbounded;
|
||||
use gpui_kit::{
|
||||
AppContext as _, Bounds, Entity, KeyDownEvent, KeyUpEvent, Keystroke, Modifiers,
|
||||
TestAppContext, VisualTestContext, WindowBounds, WindowOptions, point, px, size,
|
||||
ScrollDelta, ScrollEvent, TestAppContext, VisualTestContext, WindowBounds, WindowOptions,
|
||||
point, px, size,
|
||||
};
|
||||
|
||||
use super::{Page, Tab};
|
||||
use crate::card::ASPECT;
|
||||
use crate::card_grid::CardGrid;
|
||||
use crate::champion_table::tests::row;
|
||||
|
||||
fn open_page(cx: &mut TestAppContext) -> (Entity<Page>, VisualTestContext) {
|
||||
@@ -1532,7 +1557,7 @@ mod tests {
|
||||
assert_eq!(page.home_season, None);
|
||||
assert!(page.home_statistics().is_none());
|
||||
assert!(page.home_count.is_empty());
|
||||
assert!(page.home_table.read(cx).names().is_empty());
|
||||
assert!(page.home_grid.read(cx).names().is_empty());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1561,11 +1586,11 @@ mod tests {
|
||||
),
|
||||
);
|
||||
cx.update(|window, cx| window.draw(cx).clear(cx));
|
||||
let (input, table) = page.read_with(cx, |p, _| {
|
||||
(p.home_search_input.clone(), p.home_table.clone())
|
||||
let (input, grid) = page.read_with(cx, |p, _| {
|
||||
(p.home_search_input.clone(), p.home_grid.clone())
|
||||
});
|
||||
let names = |cx: &mut VisualTestContext| {
|
||||
let mut names = table.read_with(cx, |table, _| table.names());
|
||||
let mut names = grid_names(&grid, cx);
|
||||
names.sort();
|
||||
names
|
||||
};
|
||||
@@ -1681,6 +1706,253 @@ mod tests {
|
||||
assert!(charts.read_with(cx, |charts, _| charts.renders) > 0);
|
||||
}
|
||||
|
||||
fn grid_names(grid: &Entity<CardGrid>, cx: &mut VisualTestContext) -> Vec<String> {
|
||||
grid.read_with(cx, |grid, _| {
|
||||
grid.names().iter().map(|name| name.to_string()).collect()
|
||||
})
|
||||
}
|
||||
|
||||
fn draw(cx: &mut VisualTestContext) {
|
||||
cx.update(|window, cx| window.draw(cx).clear(cx));
|
||||
}
|
||||
|
||||
/// Never played champions with distinct ids, in A to Z order.
|
||||
fn champions(count: u32) -> Vec<ChampionRow> {
|
||||
(1..=count)
|
||||
.map(|id| ChampionRow {
|
||||
champion: Champion {
|
||||
id,
|
||||
key: format!("Champion{id}"),
|
||||
name: format!("Champion {id:03}"),
|
||||
},
|
||||
state: Some(ChampionState::NeverPlayed),
|
||||
challenge: Some(ChampionChallenge::default()),
|
||||
..row("")
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn the_grid_follows_the_alias_search_and_the_filters(cx: &mut TestAppContext) {
|
||||
let (page, cx) = open_page_in_season(cx);
|
||||
let cx = &mut { cx };
|
||||
let champion = |id, key: &str, name: &str, state, matches, completed: bool| ChampionRow {
|
||||
champion: Champion {
|
||||
id,
|
||||
key: key.into(),
|
||||
name: name.into(),
|
||||
},
|
||||
state: Some(state),
|
||||
matches,
|
||||
challenge: Some(ChampionChallenge {
|
||||
matches,
|
||||
completed_on: completed.then_some(Date {
|
||||
year: 2026,
|
||||
month: 3,
|
||||
day: 2,
|
||||
}),
|
||||
..Default::default()
|
||||
}),
|
||||
..row(name)
|
||||
};
|
||||
// As the statistics give them: A to Z, the accented name where French collation puts it.
|
||||
show(
|
||||
&page,
|
||||
cx,
|
||||
statistics(
|
||||
1,
|
||||
4,
|
||||
vec![
|
||||
champion(86, "Garen", "Garen", ChampionState::NeverPlayed, 0, false),
|
||||
champion(107, "Rengar", "Rengar", ChampionState::InProgress, 5, false),
|
||||
champion(
|
||||
4,
|
||||
"TwistedFate",
|
||||
"Twisted Fate",
|
||||
ChampionState::Completed,
|
||||
3,
|
||||
true,
|
||||
),
|
||||
champion(238, "Zed", "Éd", ChampionState::NeverPlayed, 0, false),
|
||||
],
|
||||
),
|
||||
);
|
||||
draw(cx);
|
||||
let (input, grid) = page.read_with(cx, |p, _| {
|
||||
(p.home_search_input.clone(), p.home_grid.clone())
|
||||
});
|
||||
let shown_count =
|
||||
|cx: &mut VisualTestContext| page.read_with(cx, |p, _| p.home_shown_count.to_string());
|
||||
// « Tous »: everything, never played and completed ones included, in the given order.
|
||||
assert_eq!(
|
||||
grid_names(&grid, cx),
|
||||
["Garen", "Rengar", "Twisted Fate", "Éd"]
|
||||
);
|
||||
assert_eq!(shown_count(cx), "4");
|
||||
draw(cx);
|
||||
assert!(cx.debug_bounds("no-champion").is_none());
|
||||
|
||||
cx.update(|window, cx| input.update(cx, |input, cx| input.focus(window, cx)));
|
||||
cx.simulate_input("TF");
|
||||
assert_eq!(grid_names(&grid, cx), ["Twisted Fate"]);
|
||||
assert_eq!(shown_count(cx), "1");
|
||||
|
||||
cx.update(|window, cx| input.update(cx, |input, cx| input.clean(window, cx)));
|
||||
assert_eq!(grid_names(&grid, cx).len(), 4);
|
||||
cx.simulate_input("ren");
|
||||
page.update(cx, |p, cx| {
|
||||
p.home_filter = StateFilter::Remaining;
|
||||
p.refresh_home(cx);
|
||||
});
|
||||
assert_eq!(grid_names(&grid, cx), ["Garen", "Rengar"]);
|
||||
|
||||
// Nothing completed matches: the grid says so.
|
||||
page.update(cx, |p, cx| {
|
||||
p.home_filter = StateFilter::Completed;
|
||||
p.refresh_home(cx);
|
||||
});
|
||||
assert!(grid_names(&grid, cx).is_empty());
|
||||
assert_eq!(shown_count(cx), "0");
|
||||
draw(cx);
|
||||
assert!(cx.debug_bounds("no-champion").is_some());
|
||||
assert!(cx.debug_bounds("card-4").is_none());
|
||||
|
||||
// « Validés » alone brings the completed card back.
|
||||
cx.update(|window, cx| input.update(cx, |input, cx| input.clean(window, cx)));
|
||||
assert_eq!(grid_names(&grid, cx), ["Twisted Fate"]);
|
||||
draw(cx);
|
||||
assert!(cx.debug_bounds("card-4").is_some());
|
||||
assert!(cx.debug_bounds("no-champion").is_none());
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_card_is_as_wide_as_the_mockups_and_a_card_without_a_portrait_renders(
|
||||
cx: &mut TestAppContext,
|
||||
) {
|
||||
let (page, cx) = open_page_in_season(cx);
|
||||
let cx = &mut { cx };
|
||||
show(&page, cx, statistics(0, 30, champions(30)));
|
||||
draw(cx);
|
||||
let first = cx
|
||||
.debug_bounds("card-1")
|
||||
.expect("the first card is not drawn");
|
||||
let second = cx.debug_bounds("card-2").unwrap();
|
||||
let next_row = cx.debug_bounds("card-7").unwrap();
|
||||
// 6 columns at 1280 px, as tall as the aspect says.
|
||||
assert_eq!(first.origin.y, second.origin.y);
|
||||
assert!(next_row.origin.x == first.origin.x);
|
||||
let (width, height) = (first.size.width.as_f32(), first.size.height.as_f32());
|
||||
assert!((height - width / ASPECT).abs() < 1., "{width} x {height}");
|
||||
// The first-row measure sizes every row: the next one starts a card and a gap lower.
|
||||
let step = (next_row.origin.y - first.origin.y).as_f32();
|
||||
assert!(
|
||||
(step - (height + 14.)).abs() < 1.,
|
||||
"step {step}, card {height}"
|
||||
);
|
||||
assert!(
|
||||
((second.origin.x - first.origin.x).as_f32() - width - 14.).abs() < 1.,
|
||||
"gap between columns"
|
||||
);
|
||||
// None of these cards has a portrait.
|
||||
page.read_with(cx, |page, cx| {
|
||||
assert!(
|
||||
page.home_grid
|
||||
.read(cx)
|
||||
.names()
|
||||
.contains(&"Champion 001".into())
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn only_the_visible_cards_are_built(cx: &mut TestAppContext) {
|
||||
let (page, cx) = open_page_in_season(cx);
|
||||
let cx = &mut { cx };
|
||||
show(&page, cx, statistics(0, 170, champions(170)));
|
||||
draw(cx);
|
||||
let grid = page.read_with(cx, |page, _| page.home_grid.clone());
|
||||
let built = grid.read_with(cx, |grid, _| grid.card_builds);
|
||||
// A few rows of 6 under 860 px, the first measured again: far from the 170 cards.
|
||||
assert!(built > 0);
|
||||
assert!(built <= 8 * 6, "{built} cards built for 170");
|
||||
}
|
||||
|
||||
// Hovering outside the grid draws the page again, not the cards: the grid is a cached view
|
||||
// (rule #1), like the charts row.
|
||||
#[gpui::test]
|
||||
fn hovering_outside_the_grid_does_not_render_the_cards(cx: &mut TestAppContext) {
|
||||
let (page, cx) = open_page_in_season(cx);
|
||||
let cx = &mut { cx };
|
||||
show(&page, cx, statistics(0, 170, champions(170)));
|
||||
draw(cx);
|
||||
let grid = page.read_with(cx, |page, _| page.home_grid.clone());
|
||||
let renders = grid.read_with(cx, |grid, _| grid.renders);
|
||||
assert!(renders > 0);
|
||||
|
||||
// A navigation button's hover style redraws the window.
|
||||
for _ in 0..2 {
|
||||
let button = center(cx, "tab-statistics");
|
||||
cx.simulate_mouse_move(button, None, Modifiers::none());
|
||||
draw(cx);
|
||||
cx.simulate_mouse_move(point(px(5.), px(5.)), None, Modifiers::none());
|
||||
draw(cx);
|
||||
}
|
||||
assert_eq!(grid.read_with(cx, |grid, _| grid.renders), renders);
|
||||
|
||||
// The count does move when the grid changes.
|
||||
grid.update(cx, |_, cx| cx.notify());
|
||||
draw(cx);
|
||||
assert_eq!(grid.read_with(cx, |grid, _| grid.renders), renders + 1);
|
||||
}
|
||||
|
||||
// Hovering a card or scrolling the grid draws the grid, and nothing upstream: the cards are
|
||||
// not built again and no statistics are computed.
|
||||
#[gpui::test]
|
||||
fn scrolling_or_hovering_the_grid_does_not_recompute_the_statistics(cx: &mut TestAppContext) {
|
||||
let (page, cx) = open_page_in_season(cx);
|
||||
let cx = &mut { cx };
|
||||
show(&page, cx, statistics(0, 170, champions(170)));
|
||||
draw(cx);
|
||||
let grid = page.read_with(cx, |page, _| page.home_grid.clone());
|
||||
let generation = page.read_with(cx, |page, _| page.stats_generation);
|
||||
let set_cards = grid.read_with(cx, |grid, _| grid.set_cards_calls);
|
||||
let renders = grid.read_with(cx, |grid, _| grid.renders);
|
||||
assert!(set_cards > 0);
|
||||
|
||||
let hovers = Rc::new(Cell::new(0));
|
||||
let _subscription = cx.update(|_, cx| {
|
||||
let hovers = hovers.clone();
|
||||
cx.observe(&grid, move |_, _| hovers.set(hovers.get() + 1))
|
||||
});
|
||||
for id in ["card-1", "card-2"] {
|
||||
let card = center(cx, id);
|
||||
cx.simulate_mouse_move(card, None, Modifiers::none());
|
||||
draw(cx);
|
||||
}
|
||||
assert!(hovers.get() > 0, "no card was hovered");
|
||||
assert!(grid.read_with(cx, |grid, _| grid.renders) > renders);
|
||||
|
||||
let over = center(cx, "card-1");
|
||||
cx.simulate_event(ScrollEvent {
|
||||
position: over,
|
||||
delta: ScrollDelta::Pixels(point(px(0.), px(-400.))),
|
||||
..Default::default()
|
||||
});
|
||||
draw(cx);
|
||||
assert!(grid.read_with(cx, |grid, _| grid.scrolled()) > px(0.));
|
||||
|
||||
assert_eq!(
|
||||
grid.read_with(cx, |grid, _| grid.set_cards_calls),
|
||||
set_cards,
|
||||
"the cards were built again"
|
||||
);
|
||||
assert_eq!(
|
||||
page.read_with(cx, |page, _| page.stats_generation),
|
||||
generation
|
||||
);
|
||||
assert!(!page.read_with(cx, |page, _| page.stats_job.running()));
|
||||
}
|
||||
|
||||
fn center(cx: &mut VisualTestContext, id: &'static str) -> gpui_kit::Point<gpui_kit::Pixels> {
|
||||
cx.debug_bounds(id).unwrap().center()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user