Show the tall portraits on the cards, decoded only while shown (#77)
The champions thread downloads the card portraits after the square ones and tells the page at most once a second while they arrive, plus once at the end, so each shows without a restart. A card shows its tall portrait, the square icon while it is missing, else its plain background. Each card grid reads its portraits through its own image cache: only the cards it paints are loaded, and what its previous render did not paint is dropped at its next render, from the GPU atlas too, and all of it with the grid. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f1e8808082
commit
815daf33cb
8 files changed
+707
-11
No files matched your search
@@ -2987,4 +2987,143 @@ mod tests {
|
||||
assert!(window.debug_bounds("home-tools").is_some());
|
||||
assert!(window.debug_bounds("anvil-row").is_none());
|
||||
}
|
||||
|
||||
// The tall card portraits (#77).
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::card_images::tests::{PORTRAIT_JPEG, TestDir};
|
||||
|
||||
/// Joueur's tracker on the catalog, with the card portraits of `keys` on disk under `dir`.
|
||||
fn tracker_with_portraits(dir: &Path, keys: &[&str]) -> Tracker {
|
||||
let portraits = dir.join("portraits");
|
||||
let tracker = Tracker::open(Path::new(":memory:"), Box::new(JoueurHistory))
|
||||
.unwrap()
|
||||
.with_seasons(vec![season("Saison", 2020)])
|
||||
.with_champions(Box::new(Catalog), portraits.clone());
|
||||
tracker.refresh_champions().unwrap();
|
||||
tracker.sync(JOUEUR, &mut |_| {}).unwrap();
|
||||
let cards = portraits.join("cards");
|
||||
std::fs::create_dir_all(&cards).unwrap();
|
||||
for key in keys {
|
||||
std::fs::write(cards.join(format!("{key}.jpg")), PORTRAIT_JPEG).unwrap();
|
||||
}
|
||||
tracker
|
||||
}
|
||||
|
||||
fn card_portrait(dir: &Path, key: &str) -> PathBuf {
|
||||
dir.join("portraits")
|
||||
.join("cards")
|
||||
.join(format!("{key}.jpg"))
|
||||
}
|
||||
|
||||
// The worker saves a portrait, then tells the page: the card shows it without a restart.
|
||||
#[gpui::test]
|
||||
async fn a_card_portrait_saved_while_the_app_runs_shows_once_the_worker_tells(
|
||||
cx: &mut TestAppContext,
|
||||
) {
|
||||
cx.executor().allow_parking();
|
||||
let dir = TestDir::new("portrait-arrives");
|
||||
let (page, mut window) =
|
||||
open_page_with_tracker(cx, tracker_with_portraits(dir.path(), &[]));
|
||||
let grid = page.read_with(&window, |p, _| p.home_grid.clone());
|
||||
let ahri = |grid: &CardGrid| {
|
||||
let cards = grid.shown_cards().into_iter();
|
||||
cards
|
||||
.filter(|card| card.id == 103)
|
||||
.map(|card| card.portrait)
|
||||
.next()
|
||||
};
|
||||
assert_eq!(grid.read_with(&window, |grid, _| ahri(grid)), Some(None));
|
||||
draw(&mut window);
|
||||
|
||||
let path = card_portrait(dir.path(), "Ahri");
|
||||
std::fs::write(&path, PORTRAIT_JPEG).unwrap();
|
||||
window
|
||||
.update(|w, cx| page.update(cx, |p, cx| p.apply(crate::worker::Msg::Champions, w, cx)));
|
||||
cx.condition(&grid, |grid, _| ahri(grid) == Some(Some(path.clone())))
|
||||
.await;
|
||||
|
||||
// Drawn, through the grid's own cache.
|
||||
draw(&mut window);
|
||||
window.run_until_parked();
|
||||
draw(&mut window);
|
||||
let images = grid.read_with(&window, |grid, _| grid.images().cloned());
|
||||
let images = images.expect("the grid was not drawn");
|
||||
assert_eq!(images.read_with(&window, |i, _| i.held()), [path]);
|
||||
assert_eq!(images.read_with(&window, |i, _| i.loaded()).len(), 1);
|
||||
}
|
||||
|
||||
// The Anvil row is a grid of its own, not cached, with no `uniform_list`: its portraits go
|
||||
// through its own cache too, never through the app-wide one.
|
||||
#[gpui::test]
|
||||
async fn the_anvil_row_reads_its_portraits_through_its_own_cache(cx: &mut TestAppContext) {
|
||||
cx.executor().allow_parking();
|
||||
let dir = TestDir::new("anvil-portraits");
|
||||
let keys = ["Briar", "LeeSin", "Rumble", "Soraka", "XinZhao"];
|
||||
let (page, mut window) =
|
||||
open_page_with_tracker(cx, tracker_with_portraits(dir.path(), &keys));
|
||||
send(&page, &mut window, &frames()[0]);
|
||||
cx.condition(&page, |p, _| p.stats.is_some()).await;
|
||||
draw(&mut window);
|
||||
window.run_until_parked();
|
||||
draw(&mut window);
|
||||
|
||||
let grid = page.read_with(&window, |p, _| p.anvil_grid.clone());
|
||||
let images = grid.read_with(&window, |grid, _| grid.images().cloned());
|
||||
let images = images.expect("the row was not drawn");
|
||||
let mut expected: Vec<_> = keys
|
||||
.iter()
|
||||
.map(|key| card_portrait(dir.path(), key))
|
||||
.collect();
|
||||
expected.sort();
|
||||
assert_eq!(images.read_with(&window, |i, _| i.held()), expected);
|
||||
let loaded = images.read_with(&window, |i, _| i.loaded());
|
||||
assert_eq!(loaded.len(), keys.len());
|
||||
window.update(|w, cx| {
|
||||
assert!(loaded.iter().all(|image| w.has_image_atlas_entry(image)));
|
||||
for path in &expected {
|
||||
let source = gpui_kit::ImageSource::from(path.clone());
|
||||
assert!(!source.is_asset_cached(cx), "{path:?}");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Folding « Autres champions » drops its grid, and with it its portraits, on the GPU too.
|
||||
#[gpui::test]
|
||||
async fn folding_others_frees_its_portraits(cx: &mut TestAppContext) {
|
||||
cx.executor().allow_parking();
|
||||
let dir = TestDir::new("others-portraits");
|
||||
let keys = ["Ahri", "Alistar", "Morgana", "Swain"];
|
||||
let (page, mut window) =
|
||||
open_page_with_tracker(cx, tracker_with_portraits(dir.path(), &keys));
|
||||
send(&page, &mut window, &frames()[0]);
|
||||
cx.condition(&page, |p, _| p.stats.is_some()).await;
|
||||
draw(&mut window);
|
||||
click_others(&mut window);
|
||||
window.run_until_parked();
|
||||
draw(&mut window);
|
||||
assert_eq!(ids(&others_cards(&page, &mut window)), OTHERS_A_TO_Z);
|
||||
|
||||
let grid = page.read_with(&window, |p, _| p.others_grid.clone());
|
||||
let images = grid.read_with(&window, |grid, _| grid.images().cloned());
|
||||
let images = images.expect("the section was not drawn");
|
||||
let loaded = images.read_with(&window, |i, _| i.loaded());
|
||||
assert_eq!(loaded.len(), keys.len());
|
||||
let in_atlas = |window: &mut VisualTestContext| {
|
||||
window.update(|w, _| {
|
||||
(loaded.iter())
|
||||
.filter(|image| w.has_image_atlas_entry(image))
|
||||
.count()
|
||||
})
|
||||
};
|
||||
assert_eq!(in_atlas(&mut window), keys.len());
|
||||
let weak = images.downgrade();
|
||||
drop((images, grid));
|
||||
|
||||
click_others(&mut window);
|
||||
assert!(!others_open(&page, &mut window));
|
||||
window.run_until_parked();
|
||||
assert!(weak.upgrade().is_none(), "the folded grid's cache is kept");
|
||||
assert_eq!(in_atlas(&mut window), 0);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user