Animer le survol des cartes au-dessus d’une grille en cache (#88) #97
No files matched your search
+174
-41
@@ -10,7 +10,7 @@ use gpui_kit::prelude::FluentBuilder as _;
|
||||
use gpui_kit::*;
|
||||
|
||||
use crate::text::{ordinal, thousands};
|
||||
use crate::theme::{LOGO_GOLD, ON_GOLD, surface};
|
||||
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;
|
||||
@@ -114,14 +114,54 @@ pub struct Card {
|
||||
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>,
|
||||
/// 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 {
|
||||
/// 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,
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
@@ -205,36 +245,80 @@ fn sheet_lines(row: &ChampionRow) -> Vec<(&'static str, SharedString)> {
|
||||
lines
|
||||
}
|
||||
|
||||
/// One card, as wide as its parent gives it, `ASPECT` wide over tall. Hovering only changes the
|
||||
/// 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> {
|
||||
/// 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}"))
|
||||
}
|
||||
|
||||
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 (border, ring, background) = (theme.border, theme.ring, surface(theme));
|
||||
let greyed = card.greyed();
|
||||
let id = card.id;
|
||||
div()
|
||||
.id(("card", id))
|
||||
.relative()
|
||||
.flex_1()
|
||||
.min_w_0()
|
||||
.aspect_ratio(ASPECT)
|
||||
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(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}"))
|
||||
.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()
|
||||
.inset_0()
|
||||
.size_full()
|
||||
.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)
|
||||
@@ -251,28 +335,77 @@ pub fn card(card: &Card, state: CardState<'_>, cx: &App) -> Stateful<Div> {
|
||||
})
|
||||
.child(shade())
|
||||
.when(greyed, |el| el.child(reasons(id, &card.reasons)))
|
||||
.child(caption(card))
|
||||
.when(state.sheet, |el| el.child(sheet(card)))
|
||||
.when(progress < 1., |el| {
|
||||
let slide = if pose.is_some_and(|pose| pose.moves) {
|
||||
CAPTION_SLIDE * progress
|
||||
} else {
|
||||
0.
|
||||
};
|
||||
el.child(
|
||||
caption(card)
|
||||
.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))
|
||||
}))
|
||||
}
|
||||
|
||||
/// 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;
|
||||
/// 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()
|
||||
.inset_0()
|
||||
.rounded(px(INNER_RADIUS))
|
||||
.top(px(-width))
|
||||
.left(px(-width))
|
||||
.right(px(-width))
|
||||
.bottom(px(-width))
|
||||
.rounded(px(RADIUS + width))
|
||||
.border(px(width))
|
||||
.border_color(background)
|
||||
}
|
||||
|
||||
/// Over the lower half of the card, as the mockup's `.sheet`: the name, then a line per figure,
|
||||
/// on a shade that darkens from its top. Only a lifted card builds it.
|
||||
fn sheet(card: &Card) -> Div {
|
||||
let id = card.id;
|
||||
let clear = rgba(SHEET_BG & 0xffffff00);
|
||||
div()
|
||||
.absolute()
|
||||
.left_0()
|
||||
.right_0()
|
||||
.rounded_b(px(INNER_RADIUS))
|
||||
.v_flex()
|
||||
.justify_center()
|
||||
.p(px(12.))
|
||||
.bg(rgba(SHEET_BG))
|
||||
.justify_end()
|
||||
.pt(px(44.))
|
||||
.px(px(12.))
|
||||
.pb(px(12.))
|
||||
.bg(linear_gradient(
|
||||
180.,
|
||||
linear_color_stop(clear, 0.),
|
||||
linear_color_stop(rgba(SHEET_BG), 0.34),
|
||||
))
|
||||
.text_size(px(12.))
|
||||
.text_color(rgb(SHEET_LABEL))
|
||||
.debug_selector(|| format!("sheet-{id}"))
|
||||
.child(
|
||||
div()
|
||||
.mb(px(10.))
|
||||
.mb(px(6.))
|
||||
.text_size(px(17.))
|
||||
.font_weight(FontWeight::SEMIBOLD)
|
||||
.text_color(white())
|
||||
@@ -283,7 +416,7 @@ fn sheet(card: &Card) -> Div {
|
||||
.h_flex()
|
||||
.justify_between()
|
||||
.gap(px(4.))
|
||||
.py(px(2.))
|
||||
.py(px(1.5))
|
||||
.debug_selector(|| format!("sheet-{id}-{label}"))
|
||||
.child(*label)
|
||||
.child(
|
||||
@@ -328,7 +461,7 @@ fn caption(card: &Card) -> Div {
|
||||
.absolute()
|
||||
.left(px(12.))
|
||||
.right(px(8.))
|
||||
.bottom(px(12.))
|
||||
.bottom(px(CAPTION_BOTTOM))
|
||||
.v_flex()
|
||||
.child(
|
||||
div()
|
||||
|
||||
+148
-63
@@ -1,7 +1,10 @@
|
||||
//! The card grid: all the cards a list shows, in rows of the same count, only the visible rows
|
||||
//! built. It is its own view because it owns the scrolling and the hover of its cards, and the
|
||||
//! portraits they show (`card_images`).
|
||||
//! portraits they show (`card_images`). Its cards never change on hover or focus: the card under
|
||||
//! the pointer or the keyboard is drawn lifted over the grid (`card_lift`), from where the grid
|
||||
//! last placed it.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::ops::Range;
|
||||
use std::rc::Rc;
|
||||
@@ -10,7 +13,7 @@ use gpui_kit::component::scroll::Scrollbar;
|
||||
use gpui_kit::component::{ActiveTheme as _, StyledExt as _};
|
||||
use gpui_kit::*;
|
||||
|
||||
use crate::card::{ASPECT, Card, CardState, card};
|
||||
use crate::card::{ASPECT, Card, card};
|
||||
use crate::card_images::{self, CardImages};
|
||||
use crate::page::PAGE_PADDING;
|
||||
|
||||
@@ -31,11 +34,14 @@ pub fn init(cx: &mut App) {
|
||||
/// The widest content column of the home, as in the mockup.
|
||||
pub const MAX_WIDTH: f32 = 1264.;
|
||||
|
||||
/// The most cards a row holds.
|
||||
const MAX_COLUMNS: usize = 6;
|
||||
|
||||
/// How many cards fit a row of a window `width` wide, with the mockup's breakpoints: its
|
||||
/// `max-width` rules apply up to and including 1000, 720 and 480 px.
|
||||
fn columns(width: Pixels) -> usize {
|
||||
match width.as_f32() {
|
||||
w if w > 1000. => 6,
|
||||
w if w > 1000. => MAX_COLUMNS,
|
||||
w if w > 720. => 4,
|
||||
w if w > 480. => 3,
|
||||
_ => 2,
|
||||
@@ -84,12 +90,33 @@ pub fn row_height(window_width: Pixels) -> Pixels {
|
||||
px(Metrics::of(window_width).row_height())
|
||||
}
|
||||
|
||||
/// The card under the pointer changed: its id, or none.
|
||||
/// The card under the pointer, or the one the keyboard is on, changed: its id, or none.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct CardHovered(pub Option<u32>);
|
||||
pub struct CardHovered {
|
||||
pub id: Option<u32>,
|
||||
/// The keyboard brought it: the pointer moving puts it down.
|
||||
pub by_keyboard: bool,
|
||||
}
|
||||
|
||||
/// What makes one card reachable with the keyboard: its handle, and the subscriptions that draw
|
||||
/// the grid again when the focus comes or goes (a cached view is not drawn by the focus alone).
|
||||
/// None of the grid's cards stays lifted, at once: a wheel turned over them, or they are no
|
||||
/// longer shown.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct PutDown;
|
||||
|
||||
/// Where the grid last drew a card, in the window, the part of the window it showed of it, and
|
||||
/// its index in the grid's cards.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Placed {
|
||||
pub bounds: Bounds<Pixels>,
|
||||
pub visible: Bounds<Pixels>,
|
||||
pub ix: usize,
|
||||
}
|
||||
|
||||
/// The cards the grid drew last, by id: filled as its rows are laid out, read between its draws.
|
||||
pub type Placements = Rc<RefCell<HashMap<u32, Placed>>>;
|
||||
|
||||
/// What makes one card reachable with the keyboard: its handle, and the subscriptions that lift
|
||||
/// it while the keyboard is on it.
|
||||
struct CardFocus {
|
||||
handle: FocusHandle,
|
||||
_subscriptions: [Subscription; 2],
|
||||
@@ -102,8 +129,10 @@ pub struct CardGrid {
|
||||
/// Indices into `cards`, after the filter and the search.
|
||||
shown: Vec<usize>,
|
||||
scroll: UniformListScrollHandle,
|
||||
/// The card under the pointer, which shows its sheet.
|
||||
/// The card under the pointer or the keyboard, which is lifted.
|
||||
hovered: Option<u32>,
|
||||
/// Where the visible cards were drawn last.
|
||||
placed: Placements,
|
||||
/// How far the list was scrolled when last drawn: a wheel that leaves it there (at either
|
||||
/// end) moves no card from under the pointer.
|
||||
drawn_scroll: Pixels,
|
||||
@@ -126,6 +155,7 @@ pub struct CardGrid {
|
||||
}
|
||||
|
||||
impl EventEmitter<CardHovered> for CardGrid {}
|
||||
impl EventEmitter<PutDown> for CardGrid {}
|
||||
|
||||
impl CardGrid {
|
||||
pub fn new() -> CardGrid {
|
||||
@@ -134,6 +164,7 @@ impl CardGrid {
|
||||
shown: Vec::new(),
|
||||
scroll: UniformListScrollHandle::new(),
|
||||
hovered: None,
|
||||
placed: Placements::default(),
|
||||
drawn_scroll: Pixels::ZERO,
|
||||
focus: HashMap::new(),
|
||||
focus_stale: false,
|
||||
@@ -184,20 +215,27 @@ impl CardGrid {
|
||||
self.focus_stale = false;
|
||||
let mut previous = std::mem::take(&mut self.focus);
|
||||
for card in self.cards.iter() {
|
||||
let focus = previous.remove(&card.id).unwrap_or_else(|| {
|
||||
let id = card.id;
|
||||
let focus = previous.remove(&id).unwrap_or_else(|| {
|
||||
let handle = cx.focus_handle().tab_stop(true);
|
||||
let gained = cx.on_focus_in(&handle, window, |_, _, cx| cx.notify());
|
||||
let lost = cx.on_focus_out(&handle, window, |_, _, _, cx| cx.notify());
|
||||
// As `:focus-visible`: a card focused by a click is already under the pointer.
|
||||
let gained = cx.on_focus_in(&handle, window, move |grid, window, cx| {
|
||||
if window.last_input_was_keyboard() {
|
||||
grid.hover_by(Some(id), true, cx);
|
||||
}
|
||||
});
|
||||
let lost = cx.on_focus_out(&handle, window, move |grid, _, _, cx| {
|
||||
grid.set_hovered(id, false, cx);
|
||||
});
|
||||
CardFocus {
|
||||
handle,
|
||||
_subscriptions: [gained, lost],
|
||||
}
|
||||
});
|
||||
self.focus.insert(card.id, focus);
|
||||
self.focus.insert(id, focus);
|
||||
}
|
||||
}
|
||||
|
||||
/// Only a change of card draws the grid again.
|
||||
fn set_hovered(&mut self, id: u32, hovered: bool, cx: &mut Context<Self>) {
|
||||
let next = match (hovered, self.hovered) {
|
||||
(true, _) => Some(id),
|
||||
@@ -207,16 +245,27 @@ impl CardGrid {
|
||||
self.hover(next, cx);
|
||||
}
|
||||
|
||||
/// The one change of the card under the pointer: it draws the grid again and is told
|
||||
/// (the ring lights that card's tick). True when it changed.
|
||||
fn hover(&mut self, next: Option<u32>, cx: &mut Context<Self>) -> bool {
|
||||
if next == self.hovered {
|
||||
return false;
|
||||
/// The one change of the card under the pointer or the keyboard: it is told (the ring
|
||||
/// lights that card's tick, the card is lifted over the grid), and the grid is not drawn
|
||||
/// again.
|
||||
fn hover(&mut self, next: Option<u32>, cx: &mut Context<Self>) {
|
||||
self.hover_by(next, false, cx);
|
||||
}
|
||||
|
||||
fn hover_by(&mut self, next: Option<u32>, by_keyboard: bool, cx: &mut Context<Self>) {
|
||||
if next != self.hovered {
|
||||
self.hovered = next;
|
||||
cx.emit(CardHovered {
|
||||
id: next,
|
||||
by_keyboard,
|
||||
});
|
||||
}
|
||||
self.hovered = next;
|
||||
cx.emit(CardHovered(next));
|
||||
cx.notify();
|
||||
true
|
||||
}
|
||||
|
||||
/// The pointer moved while the keyboard had lifted a card, or the pointer went to another
|
||||
/// grid: none of its cards is under the pointer or the keyboard any more.
|
||||
pub fn put_down(&mut self, cx: &mut Context<Self>) {
|
||||
self.hover(None, cx);
|
||||
}
|
||||
|
||||
/// The card under the pointer is no longer shown (filtered out, or gone with new cards):
|
||||
@@ -227,6 +276,8 @@ impl CardGrid {
|
||||
};
|
||||
if self.hovered.is_some_and(|id| !shown(id)) {
|
||||
self.hover(None, cx);
|
||||
// It would settle back where it is no longer drawn.
|
||||
cx.emit(PutDown);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -277,6 +328,8 @@ impl CardGrid {
|
||||
pub fn release_images(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
// Nor is any of its cards under the pointer: the ring's tick goes out.
|
||||
self.hover(None, cx);
|
||||
cx.emit(PutDown);
|
||||
self.placed.borrow_mut().clear();
|
||||
let released = self
|
||||
.images
|
||||
.as_ref()
|
||||
@@ -314,60 +367,89 @@ impl CardGrid {
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn images(&self) -> Option<&Entity<CardImages>> {
|
||||
/// The portraits its cards show, for a card drawn over it.
|
||||
pub fn images(&self) -> Option<&Entity<CardImages>> {
|
||||
self.images.as_ref()
|
||||
}
|
||||
|
||||
/// Where its visible cards were drawn last.
|
||||
pub fn placed(&self) -> &Placements {
|
||||
&self.placed
|
||||
}
|
||||
|
||||
/// All its cards, which `Placed::ix` indexes.
|
||||
pub fn cards(&self) -> &Rc<[Card]> {
|
||||
&self.cards
|
||||
}
|
||||
|
||||
/// A grid as tall as its rows: the hand of a champ select.
|
||||
pub fn is_hand(&self) -> bool {
|
||||
self.fit_content
|
||||
}
|
||||
|
||||
/// How far the grid is scrolled down.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn scrolled(&self) -> Pixels {
|
||||
-self.scroll.0.borrow().base_handle.offset().y
|
||||
}
|
||||
|
||||
fn row(
|
||||
&mut self,
|
||||
ix: usize,
|
||||
metrics: Metrics,
|
||||
window: &Window,
|
||||
cx: &mut Context<Self>,
|
||||
) -> AnyElement {
|
||||
fn row(&mut self, ix: usize, metrics: Metrics, cx: &mut Context<Self>) -> AnyElement {
|
||||
let start = ix * metrics.columns;
|
||||
let row = div()
|
||||
// The id and index of the card in each slot, for the row to say where it placed them.
|
||||
let mut placing = [None; MAX_COLUMNS];
|
||||
let mut slots = Vec::with_capacity(metrics.columns);
|
||||
for (column, slot) in (start..start + metrics.columns).enumerate() {
|
||||
// A stale index shows nothing rather than panicking.
|
||||
let shown = self.shown.get(slot).copied();
|
||||
match shown.and_then(|c| Some((c, self.cards.get(c)?))) {
|
||||
Some((c, data)) => {
|
||||
#[cfg(test)]
|
||||
{
|
||||
self.card_builds += 1;
|
||||
}
|
||||
let id = data.id;
|
||||
placing[column] = Some((id, c));
|
||||
let focus = self.focus.get(&id).map(|focus| &focus.handle);
|
||||
slots.push(
|
||||
card(data, focus, cx)
|
||||
.on_hover(cx.listener(move |grid, hovered: &bool, _, cx| {
|
||||
grid.set_hovered(id, *hovered, cx)
|
||||
}))
|
||||
.into_any_element(),
|
||||
);
|
||||
}
|
||||
// The last row keeps every card the same width.
|
||||
None => slots.push(div().flex_1().min_w_0().into_any_element()),
|
||||
}
|
||||
}
|
||||
let placed = self.placed.clone();
|
||||
div()
|
||||
.h_flex()
|
||||
.items_start()
|
||||
.w_full()
|
||||
.h(px(metrics.card_height + metrics.gap))
|
||||
.pb(px(metrics.gap))
|
||||
.pr(Scrollbar::width())
|
||||
.gap(px(metrics.gap));
|
||||
let slots = (start..start + metrics.columns).map(|slot| {
|
||||
// A stale index shows nothing rather than panicking.
|
||||
match self.shown.get(slot).and_then(|&c| self.cards.get(c)) {
|
||||
Some(data) => {
|
||||
#[cfg(test)]
|
||||
{
|
||||
self.card_builds += 1;
|
||||
.gap(px(metrics.gap))
|
||||
// Only a row painted is laid out here, not one `uniform_list` merely measures.
|
||||
.on_children_prepainted(move |bounds, window, _| {
|
||||
let visible = window.content_mask().bounds;
|
||||
let mut placed = placed.borrow_mut();
|
||||
for (slot, bounds) in placing.iter().zip(bounds) {
|
||||
if let Some((id, ix)) = *slot {
|
||||
placed.insert(
|
||||
id,
|
||||
Placed {
|
||||
bounds,
|
||||
visible,
|
||||
ix,
|
||||
},
|
||||
);
|
||||
}
|
||||
let focus = self.focus.get(&data.id).map(|focus| &focus.handle);
|
||||
// The sheet shows on the card under the pointer and on the one the
|
||||
// keyboard is on, as `:focus-visible` does.
|
||||
let sheet = self.hovered == Some(data.id)
|
||||
|| focus.is_some_and(|handle| {
|
||||
handle.is_focused(window) && window.last_input_was_keyboard()
|
||||
});
|
||||
let id = data.id;
|
||||
card(data, CardState { sheet, focus }, cx)
|
||||
.on_hover(cx.listener(move |grid, hovered: &bool, _, cx| {
|
||||
grid.set_hovered(id, *hovered, cx)
|
||||
}))
|
||||
.into_any_element()
|
||||
}
|
||||
// The last row keeps every card the same width.
|
||||
None => div().flex_1().min_w_0().into_any_element(),
|
||||
}
|
||||
});
|
||||
row.children(slots.collect::<Vec<_>>()).into_any_element()
|
||||
})
|
||||
.children(slots)
|
||||
.into_any_element()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -390,6 +472,9 @@ impl CardGrid {
|
||||
if moved {
|
||||
grid.hover(None, cx);
|
||||
}
|
||||
// Even at either end of the list, no card stays lifted after a wheel; one that
|
||||
// stays under the pointer keeps its tick lit.
|
||||
cx.emit(PutDown);
|
||||
}))
|
||||
.on_action(cx.listener(|grid, _: &NextCard, window, cx| grid.move_focus(1, window, cx)))
|
||||
.on_action(
|
||||
@@ -408,6 +493,8 @@ impl Render for CardGrid {
|
||||
self.sync_focus(window, cx);
|
||||
}
|
||||
self.drawn_scroll = self.scrolled_to();
|
||||
// Only the rows drawn now place their cards.
|
||||
self.placed.borrow_mut().clear();
|
||||
let images = (self.images)
|
||||
.get_or_insert_with(|| CardImages::new(cx))
|
||||
.clone();
|
||||
@@ -426,9 +513,7 @@ impl Render for CardGrid {
|
||||
let rows = self.shown.len().div_ceil(metrics.columns);
|
||||
if self.fit_content {
|
||||
// The same rows and cards (sheet, hover, keyboard focus), all built, as a column.
|
||||
let rows: Vec<_> = (0..rows)
|
||||
.map(|ix| self.row(ix, metrics, window, cx))
|
||||
.collect();
|
||||
let rows: Vec<_> = (0..rows).map(|ix| self.row(ix, metrics, cx)).collect();
|
||||
return self
|
||||
.with_cards_behaviour(div().v_flex().w_full(), images, cx)
|
||||
.children(rows)
|
||||
@@ -440,9 +525,9 @@ impl Render for CardGrid {
|
||||
uniform_list(
|
||||
"cards",
|
||||
rows,
|
||||
cx.processor(move |grid, range: Range<usize>, window, cx| {
|
||||
cx.processor(move |grid, range: Range<usize>, _, cx| {
|
||||
range
|
||||
.map(|ix| grid.row(ix, metrics, window, cx))
|
||||
.map(|ix| grid.row(ix, metrics, cx))
|
||||
.collect::<Vec<_>>()
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -0,0 +1,457 @@
|
||||
//! The lift of the card under the pointer or the keyboard (#88): which cards are lifted, and how
|
||||
//! far, in each grid. A card rises when the pointer or the keyboard comes to it and settles back
|
||||
//! when it leaves, each time from where it is; in the hand, a veil lays on the other cards.
|
||||
|
||||
use std::rc::Rc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use gpui_kit::component::ActiveTheme as _;
|
||||
use gpui_kit::*;
|
||||
|
||||
use crate::card::{CARD_RADIUS, Card, Pose, lifted, veil_color};
|
||||
use crate::card_grid::{Placed, Placements};
|
||||
use crate::card_images::CardImages;
|
||||
use crate::motion::ease_out;
|
||||
|
||||
/// How long a card takes to rise all the way, or to settle back.
|
||||
const RISE: Duration = Duration::from_millis(350);
|
||||
/// How long the hand's veil takes to lay on its cards, or to lift.
|
||||
const VEIL: Duration = Duration::from_millis(300);
|
||||
|
||||
/// A value moving to `to`, from where it was when asked to.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct Tween {
|
||||
from: f32,
|
||||
to: f32,
|
||||
start: Instant,
|
||||
duration: Duration,
|
||||
}
|
||||
|
||||
impl Tween {
|
||||
fn value(&self, now: Instant) -> f32 {
|
||||
self.from + (self.to - self.from) * ease_out(self.progress(now))
|
||||
}
|
||||
|
||||
fn progress(&self, now: Instant) -> f32 {
|
||||
if self.duration.is_zero() {
|
||||
return 1.;
|
||||
}
|
||||
let elapsed = now.saturating_duration_since(self.start);
|
||||
(elapsed.as_secs_f32() / self.duration.as_secs_f32()).min(1.)
|
||||
}
|
||||
|
||||
fn done(&self, now: Instant) -> bool {
|
||||
self.progress(now) >= 1.
|
||||
}
|
||||
|
||||
/// Towards `to` from where it is now, over the part of `full` that is left to go.
|
||||
fn toward(&self, to: f32, full: Duration, now: Instant) -> Tween {
|
||||
let from = self.value(now);
|
||||
Tween {
|
||||
from,
|
||||
to,
|
||||
start: now,
|
||||
duration: full.mul_f32((to - from).abs()),
|
||||
}
|
||||
}
|
||||
|
||||
fn rest(value: f32, now: Instant) -> Tween {
|
||||
Tween {
|
||||
from: value,
|
||||
to: value,
|
||||
start: now,
|
||||
duration: Duration::ZERO,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A card lifted, or settling back, over grid `G`.
|
||||
struct Entry<G> {
|
||||
grid: G,
|
||||
id: u32,
|
||||
lift: Tween,
|
||||
}
|
||||
|
||||
/// A card as it is drawn now: `progress` is 0 at rest and 1 lifted all the way.
|
||||
pub(crate) struct Lifted {
|
||||
pub id: u32,
|
||||
pub progress: f32,
|
||||
}
|
||||
|
||||
/// Which cards are lifted, by id: what they show is the grid's card as it is when drawn.
|
||||
pub(crate) struct Lifts<G> {
|
||||
/// In paint order: the card rising last.
|
||||
cards: Vec<Entry<G>>,
|
||||
/// The veil of each hand, while it is not lifted.
|
||||
veils: Vec<(G, Tween)>,
|
||||
}
|
||||
|
||||
impl<G: Copy + PartialEq> Lifts<G> {
|
||||
pub fn new() -> Self {
|
||||
Lifts {
|
||||
cards: Vec::new(),
|
||||
veils: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The card under the pointer or the keyboard in `grid` is now `id`, or none: it rises,
|
||||
/// and the one it replaces in that grid settles back.
|
||||
/// In a hand (`veiled`), a veil lays on its other cards while one is lifted.
|
||||
pub fn hover(&mut self, grid: G, id: Option<u32>, veiled: bool, now: Instant) {
|
||||
if veiled {
|
||||
let to = if id.is_some() { 1. } else { 0. };
|
||||
match self.veils.iter_mut().find(|(hand, _)| *hand == grid) {
|
||||
Some((_, veil)) if veil.to != to => *veil = veil.toward(to, VEIL, now),
|
||||
Some(_) => {}
|
||||
None => self
|
||||
.veils
|
||||
.push((grid, Tween::rest(0., now).toward(to, VEIL, now))),
|
||||
}
|
||||
}
|
||||
for entry in self.cards.iter_mut() {
|
||||
if entry.grid == grid && Some(entry.id) != id && entry.lift.to != 0. {
|
||||
entry.lift = entry.lift.toward(0., RISE, now);
|
||||
}
|
||||
}
|
||||
if let Some(id) = id {
|
||||
// A card settling back rises again from where it is, drawn over the others.
|
||||
let settling =
|
||||
(self.cards.iter()).position(|entry| entry.grid == grid && entry.id == id);
|
||||
let lift = match settling.map(|ix| self.cards.remove(ix)) {
|
||||
Some(entry) if entry.lift.to == 1. => entry.lift,
|
||||
Some(entry) => entry.lift.toward(1., RISE, now),
|
||||
None => Tween::rest(0., now).toward(1., RISE, now),
|
||||
};
|
||||
self.cards.push(Entry { grid, id, lift });
|
||||
}
|
||||
}
|
||||
|
||||
/// The cards of `grid` moved under a still pointer (a wheel): none stays lifted, nor does
|
||||
/// its veil, at once.
|
||||
pub fn drop_grid(&mut self, grid: G) {
|
||||
self.cards.retain(|entry| entry.grid != grid);
|
||||
self.veils.retain(|(hand, _)| *hand != grid);
|
||||
}
|
||||
|
||||
/// No card lifted any more, at once: the grids are no longer shown.
|
||||
pub fn clear(&mut self) {
|
||||
self.cards.clear();
|
||||
self.veils.clear();
|
||||
}
|
||||
|
||||
/// Every card and veil where it was going, at once: nothing to animate.
|
||||
pub fn finish(&mut self, now: Instant) {
|
||||
for entry in self.cards.iter_mut() {
|
||||
entry.lift = Tween::rest(entry.lift.to, now);
|
||||
}
|
||||
for (_, veil) in self.veils.iter_mut() {
|
||||
*veil = Tween::rest(veil.to, now);
|
||||
}
|
||||
self.tidy(now);
|
||||
}
|
||||
|
||||
/// Forgets the cards back at rest: they are the grid's own again.
|
||||
pub fn tidy(&mut self, now: Instant) {
|
||||
self.cards
|
||||
.retain(|entry| !(entry.lift.to == 0. && entry.lift.done(now)));
|
||||
self.veils
|
||||
.retain(|(_, veil)| !(veil.to == 0. && veil.done(now)));
|
||||
}
|
||||
|
||||
/// Whether a card still moves.
|
||||
pub fn animating(&self, now: Instant) -> bool {
|
||||
self.cards.iter().any(|entry| !entry.lift.done(now))
|
||||
|| self.veils.iter().any(|(_, veil)| !veil.done(now))
|
||||
}
|
||||
|
||||
/// How far the veil lays on the cards of `grid`, 0 to 1.
|
||||
pub fn veil(&self, grid: G, now: Instant) -> f32 {
|
||||
(self.veils.iter())
|
||||
.find(|(hand, _)| *hand == grid)
|
||||
.map_or(0., |(_, veil)| veil.value(now))
|
||||
}
|
||||
|
||||
/// The cards of `grid` to draw over it, in paint order.
|
||||
pub fn lifted(&self, grid: G, now: Instant) -> impl Iterator<Item = Lifted> + '_ {
|
||||
self.cards
|
||||
.iter()
|
||||
.filter(move |entry| entry.grid == grid)
|
||||
.map(move |entry| Lifted {
|
||||
id: entry.id,
|
||||
progress: entry.lift.value(now),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Draws the lifted cards of a grid over it, `lifted` in paint order with how far each is, and
|
||||
/// the hand's `veil` over the others, where the grid last placed them and as `cards` holds them
|
||||
/// now: the grid is not drawn again, and the pointer still meets its cards. Painted after the
|
||||
/// rest of the page, so a card rises over what is above it; a card the grid shows only in part
|
||||
/// stays cut where it is.
|
||||
pub(crate) fn layer(
|
||||
cards: Rc<[Card]>,
|
||||
placed: Placements,
|
||||
images: Option<Entity<CardImages>>,
|
||||
lifted_cards: Vec<Lifted>,
|
||||
veil: f32,
|
||||
moves: bool,
|
||||
) -> AnyElement {
|
||||
deferred(
|
||||
canvas(
|
||||
move |_, window, cx| {
|
||||
let placed = placed.borrow();
|
||||
let background = cx.theme().background;
|
||||
let mut layers = Vec::new();
|
||||
if veil > 0. {
|
||||
for (id, place) in placed.iter() {
|
||||
if !lifted_cards.iter().any(|lifted| lifted.id == *id) {
|
||||
layers.push(Layer::Quad(place.bounds, veil_color(veil), place.visible));
|
||||
}
|
||||
}
|
||||
}
|
||||
for &Lifted { id, progress } in &lifted_cards {
|
||||
let place = placed.get(&id);
|
||||
let card = place.and_then(|place| cards.get(place.ix));
|
||||
let (Some(place), Some(card)) = (place, card) else {
|
||||
continue;
|
||||
};
|
||||
let pose = Pose {
|
||||
progress,
|
||||
// Settling back, it goes under the veil of the cards at rest.
|
||||
veil: veil * (1. - progress),
|
||||
moves,
|
||||
};
|
||||
// Its place in the grid, where the card at rest is not to be seen.
|
||||
layers.push(Layer::Quad(place.bounds, background, place.visible));
|
||||
let mask = cut(place).map(|bounds| ContentMask { bounds });
|
||||
let mut element = self::lifted(card, pose, place.bounds.size, cx);
|
||||
if let Some(images) = &images {
|
||||
element = element.image_cache(images.clone());
|
||||
}
|
||||
let mut element = element.into_any_element();
|
||||
let origin = place.bounds.origin - point(px(0.), px(pose.rise(card)));
|
||||
window.with_content_mask(mask, |window| {
|
||||
element.prepaint_as_root(origin, place.bounds.size.into(), window, cx)
|
||||
});
|
||||
layers.push(Layer::Card(element, mask));
|
||||
}
|
||||
layers
|
||||
},
|
||||
|_, layers, window, cx| {
|
||||
for layer in layers {
|
||||
match layer {
|
||||
Layer::Quad(bounds, color, visible) => {
|
||||
let mask = ContentMask { bounds: visible };
|
||||
window.with_content_mask(Some(mask), |window| {
|
||||
window.paint_quad(fill(bounds, color).corner_radii(px(CARD_RADIUS)))
|
||||
});
|
||||
}
|
||||
Layer::Card(mut element, mask) => {
|
||||
window.with_content_mask(mask, |window| element.paint(window, cx));
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
.absolute(),
|
||||
)
|
||||
.into_any_element()
|
||||
}
|
||||
|
||||
/// Where a lifted card is cut: nowhere when its grid shows it whole, so it rises out of a
|
||||
/// scrolling list, else where the grid cuts it. A fraction of a pixel of layout or scrolling
|
||||
/// does not cut a card flush with the list's edge.
|
||||
fn cut(place: &Placed) -> Option<Bounds<Pixels>> {
|
||||
const SLACK: f32 = 1.;
|
||||
let (card, shown) = (place.bounds, place.visible);
|
||||
let whole = card.top().as_f32() >= shown.top().as_f32() - SLACK
|
||||
&& card.bottom().as_f32() <= shown.bottom().as_f32() + SLACK
|
||||
&& card.left().as_f32() >= shown.left().as_f32() - SLACK
|
||||
&& card.right().as_f32() <= shown.right().as_f32() + SLACK;
|
||||
(!whole).then_some(shown)
|
||||
}
|
||||
|
||||
/// What the layer paints, in order.
|
||||
enum Layer {
|
||||
/// A rounded quad over a card's place, cut to what the grid shows of it.
|
||||
Quad(Bounds<Pixels>, Hsla, Bounds<Pixels>),
|
||||
Card(AnyElement, Option<ContentMask<Pixels>>),
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use gpui_kit::{Bounds, point, px, size};
|
||||
|
||||
use super::{Lifts, cut};
|
||||
use crate::card_grid::Placed;
|
||||
|
||||
const GRID: u32 = 1;
|
||||
|
||||
fn at(start: Instant, ms: u64) -> Instant {
|
||||
start + Duration::from_millis(ms)
|
||||
}
|
||||
|
||||
fn on(id: u32) -> Option<u32> {
|
||||
Some(id)
|
||||
}
|
||||
|
||||
fn progress(lifts: &Lifts<u32>, id: u32, now: Instant) -> Option<f32> {
|
||||
lifts
|
||||
.lifted(GRID, now)
|
||||
.find(|lifted| lifted.id == id)
|
||||
.map(|lifted| lifted.progress)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_hovered_card_rises_in_350_ms_and_settles_back_in_as_long_once_left() {
|
||||
let start = Instant::now();
|
||||
let mut lifts = Lifts::new();
|
||||
lifts.hover(GRID, on(7), false, start);
|
||||
assert_eq!(progress(&lifts, 7, start), Some(0.));
|
||||
let half = progress(&lifts, 7, at(start, 175)).unwrap();
|
||||
// Fast at first, then settling, as the reference's cubic-bezier(.2, .8, .2, 1).
|
||||
assert!(half > 0.5 && half < 1., "{half}");
|
||||
assert_eq!(progress(&lifts, 7, at(start, 350)), Some(1.));
|
||||
assert!(lifts.animating(at(start, 349)));
|
||||
assert!(!lifts.animating(at(start, 350)));
|
||||
|
||||
lifts.hover(GRID, None, false, at(start, 400));
|
||||
assert_eq!(progress(&lifts, 7, at(start, 400)), Some(1.));
|
||||
assert!(progress(&lifts, 7, at(start, 600)).unwrap() > 0.);
|
||||
assert_eq!(progress(&lifts, 7, at(start, 750)), Some(0.));
|
||||
// Back at rest, the card is the grid's own again.
|
||||
lifts.tidy(at(start, 750));
|
||||
assert_eq!(progress(&lifts, 7, at(start, 750)), None);
|
||||
assert!(!lifts.animating(at(start, 750)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_card_left_or_taken_back_during_its_animation_starts_from_where_it_is() {
|
||||
let start = Instant::now();
|
||||
let mut lifts = Lifts::new();
|
||||
lifts.hover(GRID, on(7), false, start);
|
||||
let midway = progress(&lifts, 7, at(start, 100)).unwrap();
|
||||
|
||||
// Left on its way up: it settles from there, with no jump, and sooner than from the top.
|
||||
lifts.hover(GRID, None, false, at(start, 100));
|
||||
assert_eq!(progress(&lifts, 7, at(start, 100)), Some(midway));
|
||||
let back = at(start, 100 + (350. * midway).ceil() as u64);
|
||||
assert_eq!(progress(&lifts, 7, back), Some(0.));
|
||||
|
||||
// Taken back on its way down: it rises again from there, still one card.
|
||||
lifts.hover(GRID, on(7), false, at(start, 200));
|
||||
lifts.hover(GRID, None, false, at(start, 200));
|
||||
lifts.hover(GRID, on(7), false, at(start, 200));
|
||||
let low = progress(&lifts, 7, at(start, 200)).unwrap();
|
||||
assert!(low > 0. && low < midway, "{low}");
|
||||
assert_eq!(lifts.lifted(GRID, at(start, 200)).count(), 1);
|
||||
assert_eq!(progress(&lifts, 7, at(start, 550)), Some(1.));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_card_rising_is_drawn_over_the_one_settling_and_each_grid_keeps_its_own() {
|
||||
let start = Instant::now();
|
||||
let mut lifts = Lifts::new();
|
||||
lifts.hover(GRID, on(7), false, start);
|
||||
lifts.hover(GRID, on(8), false, at(start, 350));
|
||||
lifts.hover(GRID, on(7), false, at(start, 400));
|
||||
let order: Vec<_> = (lifts.lifted(GRID, at(start, 400)))
|
||||
.map(|lifted| lifted.id)
|
||||
.collect();
|
||||
assert_eq!(order, [8, 7]);
|
||||
|
||||
// Another grid saying none of its cards is under the pointer leaves this one's.
|
||||
lifts.hover(2, None, false, at(start, 400));
|
||||
lifts.hover(2, on(9), false, at(start, 400));
|
||||
assert_eq!(progress(&lifts, 7, at(start, 750)), Some(1.));
|
||||
assert_eq!(lifts.lifted(2, at(start, 400)).count(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn in_the_hand_a_veil_lays_on_the_other_cards_while_one_is_lifted() {
|
||||
let start = Instant::now();
|
||||
let mut lifts = Lifts::new();
|
||||
lifts.hover(GRID, on(7), true, start);
|
||||
assert_eq!(lifts.veil(GRID, start), 0.);
|
||||
assert!(lifts.veil(GRID, at(start, 150)) > 0.);
|
||||
assert_eq!(lifts.veil(GRID, at(start, 300)), 1.);
|
||||
assert!(lifts.animating(at(start, 340)));
|
||||
|
||||
// From card to card, it stays.
|
||||
lifts.hover(GRID, on(8), true, at(start, 400));
|
||||
assert_eq!(lifts.veil(GRID, at(start, 500)), 1.);
|
||||
|
||||
lifts.hover(GRID, None, true, at(start, 800));
|
||||
assert!(lifts.veil(GRID, at(start, 900)) > 0.);
|
||||
assert_eq!(lifts.veil(GRID, at(start, 1100)), 0.);
|
||||
|
||||
// Elsewhere than in the hand, none.
|
||||
lifts.hover(2, on(9), false, at(start, 1200));
|
||||
assert_eq!(lifts.veil(2, at(start, 1600)), 0.);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_wheel_over_a_grid_drops_its_cards_at_once() {
|
||||
let start = Instant::now();
|
||||
let mut lifts = Lifts::new();
|
||||
lifts.hover(GRID, on(7), true, start);
|
||||
lifts.hover(2, on(9), false, start);
|
||||
lifts.drop_grid(GRID);
|
||||
assert_eq!(lifts.lifted(GRID, start).count(), 0);
|
||||
assert_eq!(lifts.veil(GRID, start), 0.);
|
||||
assert_eq!(lifts.lifted(2, start).count(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn finishing_shows_the_end_state_at_once_and_nothing_moves_any_more() {
|
||||
let start = Instant::now();
|
||||
let mut lifts = Lifts::new();
|
||||
lifts.hover(GRID, on(7), true, start);
|
||||
lifts.hover(GRID, on(8), true, at(start, 100));
|
||||
lifts.finish(at(start, 100));
|
||||
assert!(!lifts.animating(at(start, 100)));
|
||||
assert_eq!(progress(&lifts, 8, at(start, 100)), Some(1.));
|
||||
// The card left is back at rest, and the grid's own again.
|
||||
assert_eq!(progress(&lifts, 7, at(start, 100)), None);
|
||||
assert_eq!(lifts.veil(GRID, at(start, 100)), 1.);
|
||||
|
||||
lifts.clear();
|
||||
assert_eq!(lifts.lifted(GRID, start).count(), 0);
|
||||
assert_eq!(lifts.veil(GRID, start), 0.);
|
||||
}
|
||||
|
||||
fn placed(top: f32, list_top: f32) -> Placed {
|
||||
Placed {
|
||||
bounds: Bounds::new(point(px(12.), px(top)), size(px(240.), px(350.))),
|
||||
visible: Bounds::new(point(px(12.), px(list_top)), size(px(1256.), px(480.))),
|
||||
ix: 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_card_the_list_shows_whole_rises_out_of_it() {
|
||||
// The first row, flush with the list's top edge, give or take a fraction of a pixel of
|
||||
// layout or scrolling.
|
||||
assert_eq!(cut(&placed(583., 583.)), None);
|
||||
assert_eq!(cut(&placed(582.7, 583.)), None);
|
||||
assert_eq!(cut(&placed(583.3, 583.)), None);
|
||||
// A column of a fractional width: the bounds' intersection with the list differs from
|
||||
// the card by a rounding error, which cut the first row's lifted card (#88).
|
||||
let column = Placed {
|
||||
bounds: Bounds::new(point(px(533.3333), px(583.)), size(px(240.66666), px(357.))),
|
||||
..placed(583., 583.)
|
||||
};
|
||||
assert_ne!(column.visible.intersect(&column.bounds), column.bounds);
|
||||
assert_eq!(cut(&column), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_card_the_list_shows_in_part_stays_cut_where_it_is() {
|
||||
let half_scrolled = placed(400., 583.);
|
||||
assert_eq!(cut(&half_scrolled), Some(half_scrolled.visible));
|
||||
let below = placed(900., 583.);
|
||||
assert_eq!(cut(&below), Some(below.visible));
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ mod banner_art;
|
||||
mod card;
|
||||
mod card_grid;
|
||||
mod card_images;
|
||||
mod card_lift;
|
||||
mod card_order;
|
||||
mod champion_table;
|
||||
mod motion;
|
||||
|
||||
@@ -147,7 +147,7 @@ fn ease_in(t: f32) -> f32 {
|
||||
}
|
||||
|
||||
/// Close to the reference's `cubic-bezier(.2, .8, .2, 1)`.
|
||||
fn ease_out(t: f32) -> f32 {
|
||||
pub(crate) fn ease_out(t: f32) -> f32 {
|
||||
1. - (1. - t).powi(3)
|
||||
}
|
||||
|
||||
|
||||
+411
-38
@@ -27,7 +27,8 @@ mod statistics;
|
||||
|
||||
use crate::banner_art::BannerArt;
|
||||
use crate::card::Card;
|
||||
use crate::card_grid::{CardGrid, CardHovered};
|
||||
use crate::card_grid::{CardGrid, CardHovered, PutDown};
|
||||
use crate::card_lift::Lifts;
|
||||
use crate::card_order::card_order;
|
||||
use crate::champion_table::ChampionTable;
|
||||
use crate::motion::{AnimationSetting, Deferred, Fade, FadePhase, MatchEnd};
|
||||
@@ -164,6 +165,11 @@ pub struct Page {
|
||||
fade: Option<Fade>,
|
||||
/// A frame of the transition is asked for.
|
||||
frame_asked: bool,
|
||||
/// The cards lifted over their grid, by grid: the one under the pointer or the keyboard, and
|
||||
/// those settling back (#88).
|
||||
lifts: Lifts<EntityId>,
|
||||
/// The grid whose card the keyboard lifted: the pointer moving puts it down.
|
||||
lifted_by_keyboard: Option<WeakEntity<CardGrid>>,
|
||||
/// « Paramètres » stands in for the page.
|
||||
covered: bool,
|
||||
/// The animation asked for while « Défi » was not visible.
|
||||
@@ -243,9 +249,9 @@ pub struct Page {
|
||||
others_grid: Entity<CardGrid>,
|
||||
/// The section's header button, which the keyboard reaches.
|
||||
others_focus: FocusHandle,
|
||||
/// Lights the ring's tick of the card hovered in « Autres champions », whose grid is made
|
||||
/// again at each fold.
|
||||
others_hover: Subscription,
|
||||
/// Lights the ring's tick of the card hovered in « Autres champions », and lifts it; its grid
|
||||
/// is made again at each fold.
|
||||
others_hover: [Subscription; 2],
|
||||
_subscriptions: Vec<Subscription>,
|
||||
_listener: Task<()>,
|
||||
}
|
||||
@@ -307,13 +313,16 @@ impl Page {
|
||||
let banner_art = BannerArt::new(cx);
|
||||
let others_hover = Self::light_on_hover(&others_grid, cx);
|
||||
|
||||
let subscriptions = vec![
|
||||
Self::light_on_hover(&home_grid, cx),
|
||||
Self::light_on_hover(&anvil_grid, cx),
|
||||
// The Windows setting turned off: a transition running ends, and nothing waits.
|
||||
let mut subscriptions = Vec::new();
|
||||
subscriptions.extend(Self::light_on_hover(&home_grid, cx));
|
||||
subscriptions.extend(Self::light_on_hover(&anvil_grid, cx));
|
||||
subscriptions.extend([
|
||||
// The Windows setting turned off: a transition running ends, a card lifting is
|
||||
// lifted, and nothing waits.
|
||||
cx.observe_global_in::<AnimationSetting>(window, |this, window, cx| {
|
||||
if cx.reduce_motion() {
|
||||
this.deferred.abandon();
|
||||
this.lifts.finish(cx.background_executor().now());
|
||||
this.end_fade(window, cx);
|
||||
}
|
||||
}),
|
||||
@@ -323,6 +332,7 @@ impl Page {
|
||||
if visibility.is_visible() {
|
||||
this.play_waiting(window, cx);
|
||||
} else {
|
||||
this.lifts.finish(cx.background_executor().now());
|
||||
this.end_fade(window, cx);
|
||||
}
|
||||
}),
|
||||
@@ -403,7 +413,7 @@ impl Page {
|
||||
this.recompute(window, cx);
|
||||
},
|
||||
),
|
||||
];
|
||||
]);
|
||||
|
||||
// gpui-kit's async channel wakes the page on sends, with no timer or extra dependency.
|
||||
let listener = cx.spawn_in(window, async move |this, cx| {
|
||||
@@ -441,6 +451,8 @@ impl Page {
|
||||
shown_tab: Tab::Challenge,
|
||||
fade: None,
|
||||
frame_asked: false,
|
||||
lifts: Lifts::new(),
|
||||
lifted_by_keyboard: None,
|
||||
covered: false,
|
||||
deferred: Deferred::new(),
|
||||
#[cfg(test)]
|
||||
@@ -507,6 +519,7 @@ impl Page {
|
||||
/// again when it is drawn.
|
||||
pub fn hidden(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
self.covered = true;
|
||||
self.lifts.clear();
|
||||
self.end_fade(window, cx);
|
||||
for grid in [&self.home_grid, &self.anvil_grid, &self.others_grid] {
|
||||
grid.update(cx, |grid, cx| grid.release_images(window, cx));
|
||||
@@ -680,6 +693,8 @@ impl Page {
|
||||
}
|
||||
if self.tab != tab {
|
||||
self.tab = tab;
|
||||
// The grids leave under the veil: no card stays lifted over it.
|
||||
self.lifts.clear();
|
||||
if cx.reduce_motion() || !window.is_visible() {
|
||||
// Nothing to see: the end state at once.
|
||||
self.end_fade(window, cx);
|
||||
@@ -1178,11 +1193,60 @@ impl Page {
|
||||
self.ring.update(cx, |ring, cx| ring.set_ticks(ticks, cx));
|
||||
}
|
||||
|
||||
/// Lights the ring's tick of the card under the pointer in `grid`.
|
||||
fn light_on_hover(grid: &Entity<CardGrid>, cx: &mut Context<Self>) -> Subscription {
|
||||
cx.subscribe(grid, |this, grid, CardHovered(id): &CardHovered, cx| {
|
||||
this.light(grid.entity_id(), *id, cx);
|
||||
})
|
||||
/// Lifts the card under the pointer or the keyboard in `grid`, and lights its ring's tick.
|
||||
/// A wheel over the grid drops its cards at once.
|
||||
fn light_on_hover(grid: &Entity<CardGrid>, cx: &mut Context<Self>) -> [Subscription; 2] {
|
||||
[
|
||||
cx.subscribe(grid, |this, grid, hovered: &CardHovered, cx| {
|
||||
let CardHovered { id, by_keyboard } = *hovered;
|
||||
if by_keyboard && id.is_some() {
|
||||
this.lifted_by_keyboard = Some(grid.downgrade());
|
||||
} else if (this.lifted_by_keyboard.as_ref())
|
||||
.is_some_and(|lifted| lifted.entity_id() == grid.entity_id())
|
||||
{
|
||||
this.lifted_by_keyboard = None;
|
||||
}
|
||||
this.lift(&grid, id, cx);
|
||||
this.light(grid.entity_id(), id, cx);
|
||||
}),
|
||||
cx.subscribe(grid, |this, grid, _: &PutDown, cx| {
|
||||
this.lifts.drop_grid(grid.entity_id());
|
||||
cx.notify();
|
||||
}),
|
||||
]
|
||||
}
|
||||
|
||||
/// The card under the pointer or the keyboard in `grid` is now `id`: it rises over the grid,
|
||||
/// and the one it replaces settles back. With the Windows « Show animations » setting off,
|
||||
/// each is where it goes at once.
|
||||
fn lift(&mut self, grid: &Entity<CardGrid>, id: Option<u32>, cx: &mut Context<Self>) {
|
||||
let now = cx.background_executor().now();
|
||||
let hand = grid.read(cx).is_hand();
|
||||
self.lifts.hover(grid.entity_id(), id, hand, now);
|
||||
if cx.reduce_motion() {
|
||||
self.lifts.finish(now);
|
||||
}
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// The cards lifted over `grid`, if any: drawn after it, as long as it is.
|
||||
pub(super) fn lift_layer(&self, grid: &Entity<CardGrid>, cx: &App) -> Option<AnyElement> {
|
||||
let now = cx.background_executor().now();
|
||||
let id = grid.entity_id();
|
||||
let lifted: Vec<_> = self.lifts.lifted(id, now).collect();
|
||||
let veil = self.lifts.veil(id, now);
|
||||
if lifted.is_empty() && veil == 0. {
|
||||
return None;
|
||||
}
|
||||
let grid = grid.read(cx);
|
||||
Some(crate::card_lift::layer(
|
||||
grid.cards().clone(),
|
||||
grid.placed().clone(),
|
||||
grid.images().cloned(),
|
||||
lifted,
|
||||
veil,
|
||||
!cx.reduce_motion(),
|
||||
))
|
||||
}
|
||||
|
||||
/// The card under the pointer in `grid` changed to `id`. The tick follows the grid that lit
|
||||
@@ -1215,6 +1279,7 @@ impl Page {
|
||||
let replaced = self.others_grid.entity_id();
|
||||
self.others_grid = cx.new(|_| CardGrid::new());
|
||||
self.others_hover = Self::light_on_hover(&self.others_grid, cx);
|
||||
self.lifts.drop_grid(replaced);
|
||||
self.light(replaced, None, cx);
|
||||
}
|
||||
|
||||
@@ -1537,11 +1602,23 @@ impl Page {
|
||||
|
||||
impl Render for Page {
|
||||
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
// A card lifting or settling asks for the next frame, and none once they are still.
|
||||
let now = cx.background_executor().now();
|
||||
self.lifts.tidy(now);
|
||||
if self.lifts.animating(now) {
|
||||
window.request_animation_frame();
|
||||
}
|
||||
let muted = cx.theme().muted_foreground;
|
||||
let busy = self.busy();
|
||||
let status_line = sync_line(busy, self.progress, self.crashed, self.stored);
|
||||
|
||||
div()
|
||||
// The pointer takes over from the keyboard: the card it lifted goes down.
|
||||
.on_mouse_move(cx.listener(|page, _: &MouseMoveEvent, _, cx| {
|
||||
if let Some(grid) = page.lifted_by_keyboard.take().and_then(|g| g.upgrade()) {
|
||||
grid.update(cx, |grid, cx| grid.put_down(cx));
|
||||
}
|
||||
}))
|
||||
.v_flex()
|
||||
.size_full()
|
||||
.gap_3()
|
||||
@@ -2541,10 +2618,8 @@ mod tests {
|
||||
assert_eq!(grid.read_with(cx, |grid, _| grid.renders), renders + 1);
|
||||
}
|
||||
|
||||
// Hovering a card or scrolling the grid notifies the grid view, which GPUI draws again along
|
||||
// with its parent, the page (it is not a cached view). What the test proves is that this
|
||||
// costs no statistics and no cards: the statistics are not computed again and the cards are
|
||||
// not built again.
|
||||
// Hovering a card draws it lifted over the grid, which is not drawn again; scrolling the grid
|
||||
// draws it again. Neither computes the statistics again nor builds the cards again.
|
||||
#[gpui::test]
|
||||
fn scrolling_or_hovering_the_grid_does_not_recompute_the_statistics(cx: &mut TestAppContext) {
|
||||
let (page, cx) = open_page_in_season(cx);
|
||||
@@ -2557,18 +2632,15 @@ mod tests {
|
||||
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))
|
||||
});
|
||||
let hovers = count_hovers(&grid, cx);
|
||||
for id in ["card-1", "card-2"] {
|
||||
let card = center(cx, id);
|
||||
cx.simulate_mouse_move(card, None, Modifiers::none());
|
||||
draw(cx);
|
||||
}
|
||||
settle(cx);
|
||||
assert!(hovers.get() > 0, "no card was hovered");
|
||||
assert!(grid.read_with(cx, |grid, _| grid.renders) > renders);
|
||||
assert_eq!(grid.read_with(cx, |grid, _| grid.renders), renders);
|
||||
|
||||
let over = center(cx, "card-1");
|
||||
cx.simulate_event(ScrollEvent {
|
||||
@@ -2591,6 +2663,19 @@ mod tests {
|
||||
assert!(!page.read_with(cx, |page, _| page.stats_job.running()));
|
||||
}
|
||||
|
||||
/// How many times the card under the pointer or the keyboard changes in `grid`.
|
||||
fn count_hovers(grid: &Entity<CardGrid>, cx: &mut VisualTestContext) -> Rc<Cell<usize>> {
|
||||
let hovers = Rc::new(Cell::new(0));
|
||||
let counted = hovers.clone();
|
||||
cx.update(|_, cx| {
|
||||
cx.subscribe(grid, move |_, _: &crate::card_grid::CardHovered, _| {
|
||||
counted.set(counted.get() + 1)
|
||||
})
|
||||
.detach()
|
||||
});
|
||||
hovers
|
||||
}
|
||||
|
||||
fn center(cx: &mut VisualTestContext, id: &'static str) -> gpui_kit::Point<gpui_kit::Pixels> {
|
||||
cx.debug_bounds(id).unwrap().center()
|
||||
}
|
||||
@@ -2633,7 +2718,9 @@ mod tests {
|
||||
)
|
||||
}
|
||||
|
||||
/// The cards showing their sheet once their lift has played.
|
||||
fn sheet_ids(cx: &mut VisualTestContext, count: u32) -> Vec<u32> {
|
||||
settle(cx);
|
||||
(1..=count).filter(|&id| sheet(cx, id).is_some()).collect()
|
||||
}
|
||||
|
||||
@@ -2667,6 +2754,192 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
/// How far the lifted card `id` stands above its place in the grid, if it is drawn lifted.
|
||||
fn rise(cx: &mut VisualTestContext, id: u32) -> Option<f32> {
|
||||
let lifted = cx.debug_bounds(Box::leak(format!("lifted-{id}").into_boxed_str()))?;
|
||||
let place = cx
|
||||
.debug_bounds(Box::leak(format!("card-{id}").into_boxed_str()))
|
||||
.unwrap();
|
||||
assert_eq!(lifted.size, place.size, "the lifted card changed size");
|
||||
Some((place.origin.y - lifted.origin.y).as_f32())
|
||||
}
|
||||
|
||||
fn hover(cx: &mut VisualTestContext, id: &'static str) {
|
||||
let at = center(cx, id);
|
||||
cx.simulate_mouse_move(at, None, Modifiers::none());
|
||||
draw(cx);
|
||||
}
|
||||
|
||||
// The card under the pointer rises over the grid, from where the grid placed it, and settles
|
||||
// back once left: the grid stays cached through it all, no card is built and no statistics
|
||||
// are computed, and no frame is asked for once the cards are still.
|
||||
#[gpui::test]
|
||||
fn hovering_a_card_lifts_it_over_the_grid_which_is_not_drawn_again(cx: &mut TestAppContext) {
|
||||
let (page, cx) = open_page_in_season(cx);
|
||||
let cx = &mut { cx };
|
||||
show(&page, cx, statistics(1, 170, sheet_champions(170)));
|
||||
draw(cx);
|
||||
settle(cx);
|
||||
let grid = page.read_with(cx, |page, _| page.home_grid.clone());
|
||||
let generation = page.read_with(cx, |page, _| page.stats_generation);
|
||||
let built = |cx: &mut VisualTestContext| {
|
||||
grid.read_with(cx, |grid, _| {
|
||||
(grid.renders, grid.card_builds, grid.set_cards_calls)
|
||||
})
|
||||
};
|
||||
let at_rest = built(cx);
|
||||
|
||||
hover(cx, "card-2");
|
||||
assert_eq!(rise(cx, 2), Some(0.), "it jumped");
|
||||
frame_after(cx, 100);
|
||||
let rising = rise(cx, 2).expect("the card is not lifted");
|
||||
assert!(rising > 0. && rising < 10., "{rising}");
|
||||
// Its sheet slides up while it rises.
|
||||
assert!(cx.debug_bounds("sheet-2").is_some());
|
||||
settle(cx);
|
||||
assert_eq!(rise(cx, 2), Some(10.));
|
||||
|
||||
// To the next card: it rises while the one left settles back.
|
||||
hover(cx, "card-3");
|
||||
frame_after(cx, 100);
|
||||
assert!(rise(cx, 2).is_some_and(|rise| rise > 0. && rise < 10.));
|
||||
assert!(rise(cx, 3).is_some_and(|rise| rise > 0. && rise < 10.));
|
||||
settle(cx);
|
||||
assert_eq!(rise(cx, 2), None, "the card left stayed lifted");
|
||||
assert_eq!(rise(cx, 3), Some(10.));
|
||||
|
||||
cx.simulate_mouse_move(point(px(5.), px(5.)), None, Modifiers::none());
|
||||
draw(cx);
|
||||
settle(cx);
|
||||
assert_eq!(rise(cx, 3), None);
|
||||
frame_after(cx, 16);
|
||||
let asked = cx.update(|window, cx| window.simulate_next_frame(cx));
|
||||
assert_eq!(asked, 0, "a frame is asked for at rest");
|
||||
|
||||
assert_eq!(built(cx), at_rest, "the grid was drawn again");
|
||||
assert_eq!(
|
||||
page.read_with(cx, |page, _| page.stats_generation),
|
||||
generation
|
||||
);
|
||||
assert!(!page.read_with(cx, |page, _| page.stats_job.running()));
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_greyed_card_rises_less_with_the_same_sheet(cx: &mut TestAppContext) {
|
||||
let (page, cx) = open_page_in_season(cx);
|
||||
let cx = &mut { cx };
|
||||
show(&page, cx, statistics(1, 12, sheet_champions(12)));
|
||||
draw(cx);
|
||||
// The first champion is Completed: greyed.
|
||||
hover(cx, "card-1");
|
||||
assert_eq!(sheet_ids(cx, 12), [1]);
|
||||
assert_eq!(sheet(cx, 1), Some(true));
|
||||
assert_eq!(rise(cx, 1), Some(4.));
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn without_animations_the_sheet_shows_at_once_and_the_card_stays_put(cx: &mut TestAppContext) {
|
||||
let (page, cx) = open_page_in_season(cx);
|
||||
let cx = &mut { cx };
|
||||
show(&page, cx, statistics(1, 12, sheet_champions(12)));
|
||||
draw(cx);
|
||||
settle(cx);
|
||||
animations(cx, false);
|
||||
draw(cx);
|
||||
|
||||
hover(cx, "card-2");
|
||||
assert_eq!(rise(cx, 2), Some(0.));
|
||||
assert!(cx.debug_bounds("sheet-2").is_some(), "the sheet waits");
|
||||
let asked = cx.update(|window, cx| window.simulate_next_frame(cx));
|
||||
assert_eq!(asked, 0, "a frame is asked for");
|
||||
|
||||
// Left, it is gone at once.
|
||||
hover(cx, "card-3");
|
||||
assert_eq!(rise(cx, 2), None);
|
||||
assert_eq!(rise(cx, 3), Some(0.));
|
||||
}
|
||||
|
||||
// A lifted card shows the grid's card as it is now: a first top 1 meanwhile greys it and
|
||||
// dates its sheet.
|
||||
#[gpui::test]
|
||||
fn a_lifted_card_shows_its_figures_as_they_change(cx: &mut TestAppContext) {
|
||||
let (page, cx) = open_page_in_season(cx);
|
||||
let cx = &mut { cx };
|
||||
show(&page, cx, statistics(1, 12, sheet_champions(12)));
|
||||
draw(cx);
|
||||
hover(cx, "card-2");
|
||||
assert_eq!(sheet_ids(cx, 12), [2]);
|
||||
assert_eq!(sheet(cx, 2), Some(false));
|
||||
assert_eq!(rise(cx, 2), Some(10.));
|
||||
|
||||
let mut rows = sheet_champions(12);
|
||||
rows[1].state = Some(ChampionState::Completed);
|
||||
rows[1].challenge = rows[0].challenge;
|
||||
show(&page, cx, statistics(2, 12, rows));
|
||||
draw(cx);
|
||||
assert_eq!(sheet_ids(cx, 12), [2]);
|
||||
assert_eq!(sheet(cx, 2), Some(true));
|
||||
assert_eq!(rise(cx, 2), Some(4.));
|
||||
}
|
||||
|
||||
// A card the keyboard lifted goes down as soon as the pointer moves.
|
||||
#[gpui::test]
|
||||
fn the_pointer_takes_over_from_the_keyboard(cx: &mut TestAppContext) {
|
||||
let (page, cx) = open_page_in_season(cx);
|
||||
let cx = &mut { cx };
|
||||
show(&page, cx, statistics(1, 12, sheet_champions(12)));
|
||||
draw(cx);
|
||||
focus_sort(&page, cx);
|
||||
press(cx, "tab");
|
||||
assert_eq!(sheet_ids(cx, 12), [1]);
|
||||
cx.simulate_mouse_move(point(px(5.), px(5.)), None, Modifiers::none());
|
||||
draw(cx);
|
||||
assert!(
|
||||
sheet_ids(cx, 12).is_empty(),
|
||||
"the keyboard's card stayed lifted"
|
||||
);
|
||||
}
|
||||
|
||||
// A wheel at the top of the list moves no card: the card is put down all the same, while
|
||||
// its tick stays lit as it is still under the pointer.
|
||||
#[gpui::test]
|
||||
fn a_wheel_puts_the_card_down_even_at_the_end_of_the_list(cx: &mut TestAppContext) {
|
||||
let (page, cx) = open_page_in_season(cx);
|
||||
let cx = &mut { cx };
|
||||
show(&page, cx, statistics(1, 170, sheet_champions(170)));
|
||||
draw(cx);
|
||||
hover(cx, "card-2");
|
||||
settle(cx);
|
||||
assert_eq!(rise(cx, 2), Some(10.));
|
||||
let at = center(cx, "card-2");
|
||||
cx.simulate_event(ScrollEvent {
|
||||
position: at,
|
||||
delta: ScrollDelta::Pixels(point(px(0.), px(400.))),
|
||||
..Default::default()
|
||||
});
|
||||
draw(cx);
|
||||
assert_eq!(rise(cx, 2), None);
|
||||
assert_eq!(page.read_with(cx, |p, cx| p.ring.read(cx).lit()), Some(2));
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn turning_animations_off_puts_a_rising_card_where_it_goes(cx: &mut TestAppContext) {
|
||||
let (page, cx) = open_page_in_season(cx);
|
||||
let cx = &mut { cx };
|
||||
show(&page, cx, statistics(1, 12, sheet_champions(12)));
|
||||
draw(cx);
|
||||
hover(cx, "card-2");
|
||||
frame_after(cx, 50);
|
||||
animations(cx, false);
|
||||
draw(cx);
|
||||
assert_eq!(rise(cx, 2), Some(0.));
|
||||
assert!(cx.debug_bounds("sheet-2").is_some());
|
||||
// The frame asked for before is the last.
|
||||
frame_after(cx, 16);
|
||||
let asked = cx.update(|window, cx| window.simulate_next_frame(cx));
|
||||
assert_eq!(asked, 0, "a frame is asked for");
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn scrolling_the_grid_hides_the_sheet_of_the_card_that_was_under_the_pointer(
|
||||
cx: &mut TestAppContext,
|
||||
@@ -2686,13 +2959,20 @@ mod tests {
|
||||
..Default::default()
|
||||
});
|
||||
draw(cx);
|
||||
// At once, with no card settling back from where it was.
|
||||
assert!(
|
||||
cx.debug_bounds("lifted-1").is_none(),
|
||||
"the card stayed lifted"
|
||||
);
|
||||
assert!(sheet_ids(cx, 170).is_empty(), "the sheet stuck to its card");
|
||||
}
|
||||
|
||||
/// The focus on the sort selector, the last stop of the home's tools before the cards.
|
||||
/// The focus on the sort selector, the last stop of the home's tools before the cards, in
|
||||
/// the window in front: the focus moves only there.
|
||||
fn focus_sort(page: &Entity<Page>, cx: &mut VisualTestContext) {
|
||||
let select = page.read_with(cx, |p, _| p.sort_select.clone());
|
||||
cx.update(|window, cx| {
|
||||
window.activate_window();
|
||||
let handle = select.read(cx).focus_handle(cx);
|
||||
window.focus(&handle, cx)
|
||||
});
|
||||
@@ -2779,8 +3059,9 @@ mod tests {
|
||||
assert!(sheet_ids(cx, 3).is_empty(), "the focus stayed in the grid");
|
||||
}
|
||||
|
||||
// Hovering or tabbing over the cards notifies the grid view (GPUI draws its parent page again
|
||||
// too): the cards are not built again and no statistics are computed.
|
||||
// Hovering the cards does not draw the grid again; tabbing over them does (GPUI draws the
|
||||
// whole window when the focus moves). Neither builds the cards again nor computes any
|
||||
// statistics.
|
||||
#[gpui::test]
|
||||
fn showing_a_sheet_does_not_rebuild_the_cards_nor_recompute_the_statistics(
|
||||
cx: &mut TestAppContext,
|
||||
@@ -2789,6 +3070,8 @@ mod tests {
|
||||
let cx = &mut { cx };
|
||||
show(&page, cx, statistics(1, 170, sheet_champions(170)));
|
||||
draw(cx);
|
||||
// The Completed card's crown is loaded, which draws the grid again.
|
||||
settle(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);
|
||||
@@ -2799,14 +3082,13 @@ mod tests {
|
||||
cx.simulate_mouse_move(card, None, Modifiers::none());
|
||||
draw(cx);
|
||||
}
|
||||
let hovered_renders = grid.read_with(cx, |grid, _| grid.renders);
|
||||
assert!(hovered_renders > renders, "the grid was not drawn");
|
||||
settle(cx);
|
||||
assert_eq!(grid.read_with(cx, |grid, _| grid.renders), renders);
|
||||
|
||||
focus_sort(&page, cx);
|
||||
for _ in 0..10 {
|
||||
press(cx, "tab");
|
||||
}
|
||||
assert!(grid.read_with(cx, |grid, _| grid.renders) > hovered_renders);
|
||||
// The key presses ended the pointer's hover: the focused card alone shows its sheet.
|
||||
assert_eq!(sheet_ids(cx, 170), [10]);
|
||||
|
||||
@@ -2862,6 +3144,11 @@ mod tests {
|
||||
assert_eq!(page.read_with(cx, |page, _| page.tab), Tab::Statistics);
|
||||
}
|
||||
|
||||
/// Lets the cards lifting or settling back reach where they go.
|
||||
fn settle(cx: &mut VisualTestContext) {
|
||||
frame_after(cx, 400);
|
||||
}
|
||||
|
||||
/// Moves the test clock on, then delivers the next frame as the display would.
|
||||
fn frame_after(cx: &mut VisualTestContext, ms: u64) {
|
||||
cx.executor().advance_clock(Duration::from_millis(ms));
|
||||
@@ -3975,8 +4262,97 @@ mod tests {
|
||||
assert_eq!(ids(&anvil_cards(&page, &mut window)), [233]);
|
||||
}
|
||||
|
||||
// As the home grid's test: hovering the cards of « Autres champions » or scrolling it notifies
|
||||
// the grid, and costs no statistics and no cards. (The capture's catalog is too short for
|
||||
// In the hand alone, a veil lays on the other cards while one is lifted; the hand is not
|
||||
// drawn again.
|
||||
#[gpui::test]
|
||||
async fn in_the_hand_a_veil_lays_on_the_other_cards(cx: &mut TestAppContext) {
|
||||
let (page, mut window) = page_in_champ_select(cx, &frames()[0]).await;
|
||||
let cx = &mut window;
|
||||
settle(cx);
|
||||
let hand = page.read_with(cx, |p, _| p.anvil_grid.clone());
|
||||
let renders = hand.read_with(cx, |grid, _| grid.renders);
|
||||
let id = ids(&anvil_cards(&page, cx))[0];
|
||||
let veil = |cx: &mut VisualTestContext, grid: &Entity<CardGrid>| {
|
||||
page.read_with(cx, |p, cx| {
|
||||
p.lifts
|
||||
.veil(grid.entity_id(), cx.background_executor().now())
|
||||
})
|
||||
};
|
||||
hover(cx, Box::leak(format!("card-{id}").into_boxed_str()));
|
||||
settle(cx);
|
||||
assert_eq!(rise(cx, id), Some(10.));
|
||||
assert_eq!(veil(cx, &hand), 1.);
|
||||
assert_eq!(hand.read_with(cx, |grid, _| grid.renders), renders);
|
||||
|
||||
cx.simulate_mouse_move(point(px(5.), px(5.)), None, Modifiers::none());
|
||||
draw(cx);
|
||||
settle(cx);
|
||||
assert_eq!(veil(cx, &hand), 0.);
|
||||
|
||||
// « Autres champions » has none.
|
||||
click_others(cx);
|
||||
scroll_champ_select_down(cx);
|
||||
let others = page.read_with(cx, |p, _| p.others_grid.clone());
|
||||
hover(cx, "card-103");
|
||||
settle(cx);
|
||||
assert_eq!(rise(cx, 103), Some(10.));
|
||||
assert_eq!(veil(cx, &others), 0.);
|
||||
}
|
||||
|
||||
// One card at most is lifted: the pointer going from the hand to « Autres champions » puts
|
||||
// the hand's card down and lifts its veil.
|
||||
#[gpui::test]
|
||||
async fn one_card_is_lifted_at_a_time_across_the_grids(cx: &mut TestAppContext) {
|
||||
let (page, mut window) = page_in_champ_select(cx, &frames()[0]).await;
|
||||
let cx = &mut window;
|
||||
click_others(cx);
|
||||
scroll_champ_select_down(cx);
|
||||
settle(cx);
|
||||
let hand = page.read_with(cx, |p, _| p.anvil_grid.clone());
|
||||
let id = ids(&anvil_cards(&page, cx))[0];
|
||||
let veil = |cx: &mut VisualTestContext| {
|
||||
page.read_with(cx, |p, cx| {
|
||||
p.lifts
|
||||
.veil(hand.entity_id(), cx.background_executor().now())
|
||||
})
|
||||
};
|
||||
hover(cx, Box::leak(format!("card-{id}").into_boxed_str()));
|
||||
settle(cx);
|
||||
assert_eq!(rise(cx, id), Some(10.));
|
||||
hover(cx, "card-103");
|
||||
settle(cx);
|
||||
assert_eq!(rise(cx, 103), Some(10.));
|
||||
assert_eq!(rise(cx, id), None, "the hand's card stayed lifted");
|
||||
assert_eq!(veil(cx), 0.);
|
||||
}
|
||||
|
||||
// The hand gives way when the champ select ends: its lifted card goes at once, and no frame
|
||||
// is asked for a card no longer shown.
|
||||
#[gpui::test]
|
||||
async fn a_grid_no_longer_shown_asks_for_no_frame(cx: &mut TestAppContext) {
|
||||
let (page, mut window) = page_in_champ_select(cx, &frames()[0]).await;
|
||||
let cx = &mut window;
|
||||
settle(cx);
|
||||
let id = ids(&anvil_cards(&page, cx))[0];
|
||||
hover(cx, Box::leak(format!("card-{id}").into_boxed_str()));
|
||||
settle(cx);
|
||||
leave(&page, cx);
|
||||
draw(cx);
|
||||
let lifting = page.read_with(cx, |p, cx| {
|
||||
let now = cx.background_executor().now();
|
||||
(
|
||||
p.lifts.animating(now),
|
||||
p.lifts.veil(p.anvil_grid.entity_id(), now),
|
||||
)
|
||||
});
|
||||
assert_eq!(lifting, (false, 0.));
|
||||
frame_after(cx, 16);
|
||||
let asked = cx.update(|window, cx| window.simulate_next_frame(cx));
|
||||
assert_eq!(asked, 0, "a frame is asked for");
|
||||
}
|
||||
|
||||
// As the home grid's test: hovering the cards of « Autres champions » does not draw its grid
|
||||
// again, and neither it nor scrolling costs statistics or cards. (The capture's catalog is too short for
|
||||
// the list to scroll: the wheel still reaches the grid, which is what clears its hover.)
|
||||
#[gpui::test]
|
||||
async fn hovering_or_scrolling_others_does_not_recompute_the_statistics(
|
||||
@@ -3992,18 +4368,15 @@ mod tests {
|
||||
let renders = grid.read_with(cx, |grid, _| grid.renders);
|
||||
assert!(set_cards > 0 && renders > 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))
|
||||
});
|
||||
let hovers = count_hovers(&grid, cx);
|
||||
for id in ["card-103", "card-12", "card-103"] {
|
||||
let card = center(cx, id);
|
||||
cx.simulate_mouse_move(card, None, Modifiers::none());
|
||||
draw(cx);
|
||||
}
|
||||
settle(cx);
|
||||
assert!(hovers.get() > 0, "no card was hovered");
|
||||
assert!(grid.read_with(cx, |grid, _| grid.renders) > renders);
|
||||
assert_eq!(grid.read_with(cx, |grid, _| grid.renders), renders);
|
||||
|
||||
let over = center(cx, "card-103");
|
||||
cx.simulate_event(ScrollEvent {
|
||||
|
||||
@@ -127,13 +127,14 @@ impl Page {
|
||||
column = column.child(self.home_heading(cx));
|
||||
}
|
||||
if shown("cards") {
|
||||
// Cached: only a change to the cards, the filter, the search or a hover
|
||||
// or scroll over them draws the grid again. Its size comes from this
|
||||
// style.
|
||||
column =
|
||||
column.child(self.home_grid.clone().cached(
|
||||
// Cached: only a change to the cards, the filter, the search or a
|
||||
// scroll over them draws the grid again; the card hovered is drawn
|
||||
// lifted over it. Its size comes from this style.
|
||||
column = column
|
||||
.child(self.home_grid.clone().cached(
|
||||
StyleRefinement::default().w_full().flex_1().min_h(px(240.)),
|
||||
));
|
||||
))
|
||||
.children(self.lift_layer(&self.home_grid, cx));
|
||||
}
|
||||
}
|
||||
// Without statistics the heading would count « 0 » and the grid would say no
|
||||
@@ -237,6 +238,7 @@ impl Page {
|
||||
.into_any_element(),
|
||||
None => self.anvil_grid.clone().into_any_element(),
|
||||
})
|
||||
.children(self.lift_layer(&self.anvil_grid, cx))
|
||||
.into_any_element(),
|
||||
),
|
||||
})
|
||||
@@ -321,6 +323,7 @@ impl Page {
|
||||
.min_h(row_height(window_width)),
|
||||
),
|
||||
)
|
||||
.children(self.lift_layer(&self.others_grid, cx))
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,15 @@ pub fn gold_ink(theme: &Theme) -> Hsla {
|
||||
theme.link
|
||||
}
|
||||
|
||||
/// The gold glow around a card lifted under the pointer.
|
||||
pub fn gold_glow(theme: &Theme) -> Hsla {
|
||||
if theme.mode.is_dark() {
|
||||
Hsla::from(rgb(LOGO_GOLD)).opacity(0.55)
|
||||
} else {
|
||||
Hsla::from(rgb(LIGHT_GOLD)).opacity(0.45)
|
||||
}
|
||||
}
|
||||
|
||||
/// The pale gold of a played champion's tick on the ring.
|
||||
pub fn ring_played(theme: &Theme) -> Hsla {
|
||||
if theme.mode.is_dark() {
|
||||
|
||||
+2
-2
@@ -29,7 +29,7 @@ Les trois marques portent sur le Défi d’un Compte pour une Saison, tous Forma
|
||||
|
||||
Une seule Partie peut donner les trois marques. Une 2ᵉ place en Arena 3×6 donne la coche « Victoire » et laisse le champion dans « Restants ».
|
||||
|
||||
Au survol ou au focus clavier, la fiche du champion apparaît en glissant sur le bas de la carte et affiche ses Parties, ses Victoires, son meilleur Placement, ses Tentatives et ses points de maîtrise. Pour un champion validé, elle affiche aussi **la date du premier top 1 de la saison**. Un top 1 ultérieur conserve cette date. Les Tentatives s’arrêtent à la Partie de validation incluse.
|
||||
Au survol ou au focus clavier, la fiche du champion apparaît en glissant sur la moitié basse de la carte et affiche ses Parties, ses Victoires, son meilleur Placement, ses Tentatives et ses points de maîtrise. Pour un champion validé, elle affiche aussi **la date du premier top 1 de la saison**. Un top 1 ultérieur conserve cette date. Les Tentatives s’arrêtent à la Partie de validation incluse.
|
||||
|
||||
### Pendant la sélection Arena
|
||||
|
||||
@@ -59,7 +59,7 @@ Cette section décrit l’identité « L’Anneau ». [La page de référence](p
|
||||
- Fin d’une Partie suivie en direct. Un top 1 déclenche la comète, la bannière et « Validé ». Une nouvelle Marque fait monter la graduation d’un cran et remplit la coche. Sinon, la graduation clignote et la ligne « Dernière Partie » apparaît en fondu. L’animation est différée jusqu’à ce que « Défi » soit visible, elle ne joue que la plus forte des Parties terminées entre-temps, et elle est abandonnée si une sélection commence. Les Parties importées ou lues dans l’historique n’animent rien.
|
||||
- Entrée en sélection : balayage, titre, cartes distribuées. Sortie : les cartes se retirent.
|
||||
- Transitions entre « Défi » et « Statistiques » : l’écran sortant passe sous un voile de la couleur du fond (≈ 130 ms), puis l’entrant en sort (≈ 280 ms). La barre du haut ne bouge pas. Changer d’onglet pendant une transition repart de là où en est le voile. Les sélecteurs de Saison et de Format sont sur la ligne du titre « Statistiques », sous le voile : la barre du haut, commune aux deux onglets, garde sa hauteur. Pas de glissement : déplacer l’écran redessinerait à chaque image la grille, l’anneau et les graphiques, qui sont en cache.
|
||||
- Survol : le même sur toutes les cartes. Le voile sur les cartes voisines ne s’applique que dans la main de la sélection.
|
||||
- Survol et focus clavier : le même sur toutes les cartes (grille de l’accueil, main de la sélection, « Autres champions »), en ≈ 350 ms. Une carte jouable monte de 10 px avec une ombre dorée ; une carte grisée monte de 4 px, sans ombre dorée. L’illustration zoome à 107 % dans la carte, qui garde sa taille, pendant que le nom et les coches s’effacent et que la fiche glisse depuis le bas. Quitter une carte en cours d’animation repart de là où elle en est. Une seule carte est levée à la fois : une carte levée au clavier redescend dès que le pointeur bouge. Une molette la repose aussitôt, même en haut ou en bas de la liste. Le voile sur les cartes voisines ne s’applique que dans la main de la sélection. Réglage Windows coupé : la fiche apparaît sans glisser et la carte ne monte pas.
|
||||
- Limites de GPUI, sans le modifier (ADR-0004) : pas de rotation, pas d’espacement des lettres animé, une montée de la carte au lieu d’un grossissement, un voile au lieu d’un filtre ou d’un fondu de l’écran entier.
|
||||
|
||||
### Page « Statistiques »
|
||||
|
||||
@@ -48,6 +48,7 @@ The identity (ring, banner, animations; `docs/interface.md`) follows the same ru
|
||||
- **Animations are triggered by an event** (end of a followed Match, champ select entering or leaving, card hover, tab change) and **draw nothing at rest**. No timer or loop keeps a frame going once an animation is over, and none runs while the window is hidden or minimised. They follow the Windows « Show animations » setting.
|
||||
- **The foundation** (`crates/app/src/motion.rs`, with the page saying when « Défi » is visible, #86) decides whether the app animates, and when. The Windows setting reaches GPUI's `App::reduce_motion`: gpui-base reads it at start, and the window's own `WM_SETTINGCHANGE` for `SPI_SETCLIENTAREAANIMATION` reads it again, with no polling. When it is off, a tab switch shows its end state at once, and an animation asked for is not played; turning it off ends a transition running and drops the animation waiting, as soon as Windows says so (`AnimationSetting`). An animation asked for while « Défi » is not visible (window minimised or hidden in the notification area, « Statistiques », « Paramètres » or a champ select in front) waits in `Deferred`: only the strongest is kept, it plays once when « Défi » shows again, and a champ select that starts abandons it.
|
||||
- **Tab transitions lay a veil, never move the screen.** A cached view's cache key holds its bounds and its opacity, so fading or sliding the screen itself would draw the grid, the ring and the charts again at every frame and build the cards again. The veil, of the page's background, covers the screen leaving (130 ms) then lifts off the one arriving (280 ms). Each frame is asked for by the transition itself (`on_next_frame`), and none once the veil is lifted. A window hidden during a transition ends it at once. The champion table is a cached view too, laid out by the space the page gives it, so the veil's frames do not draw it. The screen arriving is prepared (its rows copied and sorted, its cards built) only when it is shown, so a switch undone under the veil prepares nothing. Structural tests check that a transition draws neither the grid, the ring nor the table again, builds no card, recomputes no statistics, and asks for no frame at rest. The charts arriving play gpui-kit's own short entry, as after any switch.
|
||||
- **A card hovered is drawn over its grid, which stays cached** (#88). The cards of a grid never change on hover or focus: the page draws the card under the pointer or the keyboard (and those settling back) lifted over the grid, in a deferred layer painted after the page, from where the grid last placed it (`card_lift.rs`). The grid records its visible cards' bounds and indices while it lays out its rows, so drawing them over it reads no layout, searches no card and copies none: the lifted card is the grid's card as it is at each frame. The layer covers each lifted card's place with the page's background, and in the hand lays the veil on the other cards. Each frame is asked for by the page's render while a card moves (`request_animation_frame`), and none once they are still. A wheel drops the grid's lifted cards at once, even at either end of the list, and so does a grid no longer shown (the home under a champ select, the hand after it) or a card filtered out, so no frame is asked for a card no longer drawn; a tab switch or « Paramètres » drops them all, and a hidden window or the setting turned off shows their end state at once. Structural tests check that hovering a card, through its whole animation, draws none of the grid, builds no card and computes no statistics, and that no frame is asked for at rest. A zoomed illustration overflows the card and is cut square by GPUI's rectangular clip: a frame of the page's background rounds its corners again, which nicks the gold glow by a few pixels at each corner.
|
||||
- **Two splashes decoded at most**: the one shown and the one replacing it. All splashes are downloaded in the background, at low priority, after the card portraits, and kept on disk.
|
||||
- During champ select, the Anvil grid is cached at the height of its card rows. The capture replay checks that a splash fade builds no cards and that traversing the hand and « Autres champions » keeps at most two decoded splashes. The latest hovered card takes over when the current fade finishes.
|
||||
- **The ring is a cached view**, laid out from its style alone. A structural test guards it: hovering a card, scrolling the grid or an animation on another part does not render the ring; only a change of what it shows (the champions' state, their order, the selection) or its own animation does. Its gain is checked at the next release benchmark.
|
||||
Reference in new issue
Block a user