Lay the animation foundation and veil the switch between tabs (#86)

Follow the Windows « Show animations » setting at start and on its
change, keep the strongest animation asked for while « Défi » is not
visible until it shows again, and switch tabs under a veil of the
page's background instead of fading or sliding the screen, which would
draw the cached views again at every frame.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WYQB8paBcLk67XoAUYARBK
This commit is contained in:
nessarandClaude Opus 5.5 committed 2026-11-21 18:51:33 +01:00
1 parent 0f860fda15
commit 2ede37c521
5 files changed
+754 -18

No files matched your search

+3
View File
@@ -6,6 +6,7 @@ mod card_grid;
mod card_images;
mod card_order;
mod champion_table;
mod motion;
mod page;
#[cfg(feature = "profile")]
mod profile;
@@ -163,6 +164,7 @@ impl RootView {
fn show_page(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.form.update(cx, |form, cx| form.reset(window, cx));
self.screen = Screen::Page;
self.page.update(cx, |page, cx| page.uncovered(window, cx));
self.focus.focus(window, cx);
cx.notify();
}
@@ -320,6 +322,7 @@ fn main() {
.reduce(|update, seasons| format!("{update}\n{seasons}"));
gpui_kit::open_window(options, cx, |window, cx| {
single_instance::accept_relaunches(window);
motion::follow_windows_setting(window, cx);
cx.new(|cx| RootView::new(setup, window, cx))
})
.map(drop)
+290
View File
@@ -0,0 +1,290 @@
//! The one place that decides whether the app animates, and when (#86).
use std::time::{Duration, Instant};
use gpui_kit::base::apply_system_reduce_motion;
use gpui_kit::base::async_util::{Sender, unbounded};
use gpui_kit::{App, Window};
use windows::Win32::Foundation::{HWND, LPARAM, LRESULT, WPARAM};
use windows::Win32::UI::Shell::{DefSubclassProc, RemoveWindowSubclass, SetWindowSubclass};
use windows::Win32::UI::WindowsAndMessaging::{
SPI_SETCLIENTAREAANIMATION, WM_NCDESTROY, WM_SETTINGCHANGE,
};
use crate::tray::window_hwnd;
/// How long the screen leaving takes to go under the veil.
const FADE_OUT: Duration = Duration::from_millis(130);
/// How long the screen arriving takes to come out of it.
const FADE_IN: Duration = Duration::from_millis(280);
/// What the end of a followed Match brought its champion, from the weakest to the strongest:
/// the animation it plays on « Défi » is in proportion (#89).
#[cfg_attr(
not(test),
expect(dead_code, reason = "the end of a followed Match asks for it (#89)")
)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub(crate) enum MatchEnd {
/// Nothing new: the tick blinks.
Nothing,
/// A new Mark: the tick steps up.
Mark,
/// The champion's first top 1: the comet.
Completion,
}
/// Animations asked for while « Défi » is not in front of the player: only the strongest is
/// kept, and it plays once « Défi » shows again.
pub(crate) struct Deferred<A> {
waiting: Option<A>,
}
impl<A: Ord> Deferred<A> {
pub fn new() -> Self {
Deferred { waiting: None }
}
/// Gives back the animation to play now when « Défi » is visible, else keeps it if it is the
/// strongest waiting.
pub fn request(&mut self, animation: A, visible: bool) -> Option<A> {
if visible {
return Some(animation);
}
if self
.waiting
.as_ref()
.is_none_or(|waiting| animation > *waiting)
{
self.waiting = Some(animation);
}
None
}
/// « Défi » shows again: the animation waiting, if any, to play now.
pub fn shown(&mut self) -> Option<A> {
self.waiting.take()
}
/// A champ select starts: what waited is no longer news.
pub fn abandon(&mut self) {
self.waiting = None;
}
}
/// The screen leaving « Défi » or « Statistiques » goes under a veil of the page's background,
/// then the one arriving comes out of it. A veil rather than the content's own opacity or
/// position: either is part of a cached view's key, so fading or sliding the content would draw
/// the grid, the ring and the charts again at each frame and build the cards again (rule #1).
pub(crate) struct Fade {
phase: FadePhase,
/// The veil's opacity when the phase started: a switch during a transition starts again from
/// where the veil is.
from: f32,
start: Instant,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum FadePhase {
/// The screen leaving goes under the veil.
Out,
/// The screen arriving comes out of it.
In,
}
impl Fade {
pub fn out(from: f32, start: Instant) -> Self {
Fade {
phase: FadePhase::Out,
from,
start,
}
}
pub fn fade_in(from: f32, start: Instant) -> Self {
Fade {
phase: FadePhase::In,
from,
start,
}
}
pub fn phase(&self) -> FadePhase {
self.phase
}
/// The veil's opacity, 0 to 1.
pub fn veil(&self, now: Instant) -> f32 {
let progress = self.progress(now);
match self.phase {
FadePhase::Out => self.from + (1. - self.from) * ease_in(progress),
FadePhase::In => self.from * (1. - ease_out(progress)),
}
}
pub fn finished(&self, now: Instant) -> bool {
self.progress(now) >= 1.
}
fn duration(&self) -> Duration {
match self.phase {
FadePhase::Out => FADE_OUT,
FadePhase::In => FADE_IN,
}
}
fn progress(&self, now: Instant) -> f32 {
let elapsed = now.saturating_duration_since(self.start);
(elapsed.as_secs_f32() / self.duration().as_secs_f32()).min(1.)
}
}
/// Close to CSS's `ease-in`.
fn ease_in(t: f32) -> f32 {
t * t
}
/// Close to the reference's `cubic-bezier(.2, .8, .2, 1)`.
fn ease_out(t: f32) -> f32 {
1. - (1. - t).powi(3)
}
/// Follows the Windows « Show animations » setting while the app runs. gpui-base reads it into
/// `App::reduce_motion` at start (`gpui_kit::init`) but not on its change, which Windows
/// broadcasts to every top-level window: the window's own messages are watched for it, with no
/// polling.
pub(crate) fn follow_windows_setting(window: &Window, cx: &mut App) {
let Some(hwnd) = window_hwnd(window) else {
return;
};
let (tx, rx) = unbounded::<()>();
let data = Box::into_raw(Box::new(tx));
// The subclass owns the sender until WM_NCDESTROY. This window belongs to the UI thread.
if !unsafe { SetWindowSubclass(hwnd, Some(setting_proc), SETTING_SUBCLASS, data as usize) }
.as_bool()
{
drop(unsafe { Box::from_raw(data) });
return;
}
cx.spawn(async move |cx| {
while rx.recv().await.is_ok() {
cx.update(apply_system_reduce_motion);
}
})
.detach();
}
/// The tray's subclass is 0.
const SETTING_SUBCLASS: usize = 1;
unsafe extern "system" fn setting_proc(
hwnd: HWND,
message: u32,
wparam: WPARAM,
lparam: LPARAM,
_id: usize,
data: usize,
) -> LRESULT {
let sender = data as *mut Sender<()>;
if animation_setting_changed(message, wparam.0) {
// Applied outside the window procedure, by the task above.
let _ = unsafe { &*sender }.try_send(());
} else if message == WM_NCDESTROY {
unsafe {
let _ = RemoveWindowSubclass(hwnd, Some(setting_proc), SETTING_SUBCLASS);
drop(Box::from_raw(sender));
}
}
unsafe { DefSubclassProc(hwnd, message, wparam, lparam) }
}
/// Whether a window message says the Windows « Show animations » setting changed.
fn animation_setting_changed(message: u32, wparam: usize) -> bool {
message == WM_SETTINGCHANGE && wparam == SPI_SETCLIENTAREAANIMATION.0 as usize
}
#[cfg(test)]
mod tests {
use std::time::{Duration, Instant};
use windows::Win32::UI::WindowsAndMessaging::{
SPI_SETCLIENTAREAANIMATION, SPI_SETMOUSESPEED, WM_SETTINGCHANGE, WM_SIZE,
};
use super::{Deferred, Fade, animation_setting_changed};
#[test]
fn an_animation_asked_for_while_visible_plays_at_once() {
let mut deferred = Deferred::new();
assert_eq!(deferred.request(1, true), Some(1));
}
#[test]
fn of_the_animations_asked_for_while_hidden_only_the_strongest_plays_once_shown() {
let mut deferred = Deferred::new();
assert_eq!(deferred.request(1, false), None);
assert_eq!(deferred.request(3, false), None);
assert_eq!(deferred.request(2, false), None);
assert_eq!(deferred.shown(), Some(3));
// It plays once.
assert_eq!(deferred.shown(), None);
}
#[test]
fn a_champ_select_that_starts_abandons_the_animations_waiting() {
let mut deferred = Deferred::new();
deferred.request(2, false);
deferred.abandon();
assert_eq!(deferred.shown(), None);
}
fn at(start: Instant, ms: u64) -> Instant {
start + Duration::from_millis(ms)
}
#[test]
fn the_veil_covers_the_screen_leaving_in_130_ms_and_uncovers_the_one_arriving_in_280_ms() {
let start = Instant::now();
let out = Fade::out(0., start);
assert_eq!(out.veil(start), 0.);
assert!(!out.finished(at(start, 129)));
assert!(out.finished(at(start, 130)));
assert_eq!(out.veil(at(start, 130)), 1.);
// Slow at first, as the reference's ease-in.
assert!(out.veil(at(start, 65)) < 0.5);
let arrival = Fade::fade_in(1., start);
assert_eq!(arrival.veil(start), 1.);
assert!(!arrival.finished(at(start, 279)));
assert!(arrival.finished(at(start, 280)));
assert_eq!(arrival.veil(at(start, 280)), 0.);
// Fast at first, then settling.
assert!(arrival.veil(at(start, 140)) < 0.5);
}
#[test]
fn a_switch_during_a_transition_starts_from_where_the_veil_is() {
let start = Instant::now();
assert_eq!(Fade::out(0.4, start).veil(start), 0.4);
assert_eq!(Fade::fade_in(0.4, start).veil(start), 0.4);
assert_eq!(Fade::fade_in(0.4, start).veil(at(start, 280)), 0.);
}
#[test]
fn only_a_change_of_the_animations_setting_is_read_again() {
assert!(animation_setting_changed(
WM_SETTINGCHANGE,
SPI_SETCLIENTAREAANIMATION.0 as usize
));
// The theme (no parameter) or another setting.
assert!(!animation_setting_changed(WM_SETTINGCHANGE, 0));
assert!(!animation_setting_changed(
WM_SETTINGCHANGE,
SPI_SETMOUSESPEED.0 as usize
));
assert!(!animation_setting_changed(
WM_SIZE,
SPI_SETCLIENTAREAANIMATION.0 as usize
));
}
}
+456 -15
View File
@@ -30,6 +30,7 @@ use crate::card::Card;
use crate::card_grid::{CardGrid, CardHovered};
use crate::card_order::card_order;
use crate::champion_table::ChampionTable;
use crate::motion::{Deferred, Fade, FadePhase, MatchEnd};
use crate::ring::{self, Ring, Tick};
use crate::text::{CLIENT_ABSENT, official_differs, sync_line};
use crate::worker::{
@@ -154,8 +155,22 @@ pub struct Page {
crashed: bool,
last_reload: Instant,
current_season: Option<String>,
/// Not persisted: the app opens on the Challenge.
/// Not persisted: the app opens on the Challenge. The navigation's: the screen shown follows
/// it at the end of the transition.
tab: Tab,
/// The tab whose screen is drawn.
shown_tab: Tab,
/// The transition between the tabs, while it runs.
fade: Option<Fade>,
/// A frame of the transition is asked for.
fade_frame: bool,
/// « Paramètres » stands in for the page.
covered: bool,
/// The animation asked for while « Défi » was not visible.
deferred: Deferred<MatchEnd>,
/// The animations played, in order.
#[cfg(test)]
played: Vec<MatchEnd>,
/// The Season of the Statistics tab.
season: SeasonScope,
format: Option<Format>,
@@ -295,6 +310,15 @@ impl Page {
let subscriptions = vec![
Self::light_on_hover(&home_grid, cx),
Self::light_on_hover(&anvil_grid, cx),
// Minimised or hidden in the notification area, the window draws nothing: a
// transition ends at once, and an animation waits for it to show again.
cx.observe_window_visibility(window, |this, visibility, window, cx| {
if visibility.is_visible() {
this.challenge_shown(window, cx);
} else {
this.end_fade(window, cx);
}
}),
cx.subscribe(&search_input, |this, input, event: &InputEvent, cx| {
if matches!(event, InputEvent::Change) {
this.search = ChampionSearch::new(&input.read(cx).value());
@@ -407,6 +431,13 @@ impl Page {
last_reload: Instant::now(),
current_season,
tab: Tab::Challenge,
shown_tab: Tab::Challenge,
fade: None,
fade_frame: false,
covered: false,
deferred: Deferred::new(),
#[cfg(test)]
played: Vec::new(),
season,
format: None,
filter: StateFilter::All,
@@ -468,6 +499,8 @@ impl Page {
/// grids' portraits and the banner's splash, in memory and in the window's atlas. They load
/// again when it is drawn.
pub fn hidden(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.covered = true;
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));
}
@@ -475,6 +508,56 @@ impl Page {
.update(cx, |art, cx| art.release(window, cx));
}
/// The page is back in front of « Paramètres ».
pub fn uncovered(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.covered = false;
self.challenge_shown(window, cx);
}
/// Plays an animation of « Défi » now if it is visible, else keeps the strongest until it
/// is. With the Windows « Show animations » setting off, the end state is all there is.
#[cfg_attr(
not(test),
expect(dead_code, reason = "the end of a followed Match asks for it (#89)")
)]
pub(crate) fn animate(&mut self, end: MatchEnd, window: &mut Window, cx: &mut Context<Self>) {
if cx.reduce_motion() {
return;
}
if let Some(end) = self.deferred.request(end, self.challenge_visible(window)) {
self.play(end);
}
}
/// Whether « Défi » is in front of the player. A window merely covered by another cannot
/// be told on Windows (GPUI's `WindowVisibility`): visible then means shown and not
/// minimised.
fn challenge_visible(&self, window: &Window) -> bool {
window.is_visible()
&& !self.covered
&& self.tab == Tab::Challenge
&& self.shown_tab == Tab::Challenge
&& self.fade.is_none()
&& !self.in_champ_select()
}
/// « Défi » may have become visible: the animation waiting plays.
fn challenge_shown(&mut self, window: &Window, cx: &mut Context<Self>) {
if self.challenge_visible(window)
&& !cx.reduce_motion()
&& let Some(end) = self.deferred.shown()
{
self.play(end);
}
}
/// Draws the end of a followed Match: #89.
fn play(&mut self, end: MatchEnd) {
#[cfg(test)]
self.played.push(end);
let _ = end;
}
/// Starts a refresh, or queues one behind the refresh already running.
pub fn request_refresh(&mut self, cx: &mut Context<Self>) {
if self.refresh.request() {
@@ -588,25 +671,84 @@ impl Page {
}
if self.tab != tab {
self.tab = tab;
if tab == Tab::Statistics {
// The home grid is not shown: its portraits are given back, and come back
// with the grid.
self.home_grid
.update(cx, |grid, cx| grid.release_images(window, cx));
// Nor is the banner: its illustration too.
self.banner_art
.update(cx, |art, cx| art.release(window, cx));
}
// The tab left was not kept up to date.
self.refresh_table(cx);
if self.home_cards_stale {
self.prepare_home_cards(cx);
}
self.refresh_home(cx);
if cx.reduce_motion() || !window.is_visible() {
// Nothing to see: the end state at once.
self.end_fade(window, cx);
} else {
// From where the veil is: a switch during a transition does not wait for it.
let now = cx.background_executor().now();
let veil = self.veil(cx);
self.fade = Some(if self.shown_tab == tab {
Fade::fade_in(veil, now)
} else {
Fade::out(veil, now)
});
self.next_fade_frame(window, cx);
}
cx.notify();
}
}
/// Draws the screen of the tab chosen.
fn show_tab(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if self.shown_tab == self.tab {
return;
}
self.shown_tab = self.tab;
if self.shown_tab == Tab::Statistics {
// The home grid is not shown: its portraits are given back, and come back with the
// grid.
self.home_grid
.update(cx, |grid, cx| grid.release_images(window, cx));
// Nor is the banner: its illustration too.
self.banner_art
.update(cx, |art, cx| art.release(window, cx));
}
}
/// Shows the end of the transition, if one runs, at once.
fn end_fade(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.fade = None;
self.show_tab(window, cx);
self.challenge_shown(window, cx);
cx.notify();
}
/// Asks for the transition's next frame, once.
fn next_fade_frame(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if !self.fade_frame {
self.fade_frame = true;
cx.on_next_frame(window, |page, window, cx| page.fade_frame(window, cx));
}
}
/// One frame of the transition: the screen arriving replaces the one leaving under the full
/// veil, and no frame is asked for once the veil is lifted.
fn fade_frame(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.fade_frame = false;
let Some(fade) = &self.fade else {
return;
};
let now = cx.background_executor().now();
if fade.finished(now) {
match fade.phase() {
FadePhase::Out => {
self.show_tab(window, cx);
self.fade = Some(Fade::fade_in(1., now));
}
FadePhase::In => return self.end_fade(window, cx),
}
}
self.next_fade_frame(window, cx);
cx.notify();
}
fn apply(&mut self, msg: Msg, window: &mut Window, cx: &mut Context<Self>) {
match msg {
Msg::Progress(progress) => {
@@ -629,6 +771,10 @@ impl Page {
if self.in_champ_select() {
// The champ select is about the Challenge, whatever tab was open.
self.tab = Tab::Challenge;
self.fade = None;
self.show_tab(window, cx);
// What waited is no longer news.
self.deferred.abandon();
// The home grid gives way to the champ select's sections.
self.home_grid
.update(cx, |grid, cx| grid.release_images(window, cx));
@@ -636,6 +782,8 @@ impl Page {
self.reset_home_view(window, cx);
self.anvil_grid
.update(cx, |grid, cx| grid.release_images(window, cx));
// An animation asked for during the champ select plays on the home.
self.challenge_shown(window, cx);
}
}
if switch {
@@ -1360,6 +1508,13 @@ impl Page {
.collect()
}
/// The veil's opacity over the screen shown, 0 at rest.
fn veil(&self, cx: &App) -> f32 {
self.fade
.as_ref()
.map_or(0., |fade| fade.veil(cx.background_executor().now()))
}
#[cfg(test)]
pub(crate) fn champion_sort(&self) -> Option<ChampionSort> {
self.champion_sort
@@ -1435,10 +1590,24 @@ impl Render for Page {
.when(shown("banners"), |page| {
page.child(div().v_flex().gap_1().children(self.banners()))
})
.children(match self.tab {
Tab::Challenge => self.home_content(window, cx),
Tab::Statistics => self.statistics_content(cx),
})
.child(
// The screen under the header, which does not move: the veil of the transition
// covers it alone.
div()
.relative()
.v_flex()
.flex_1()
.min_h_0()
.gap_3()
.children(match self.shown_tab {
Tab::Challenge => self.home_content(window, cx),
Tab::Statistics => self.statistics_content(cx),
})
.when(self.fade.is_some(), |screen| {
let veil = cx.theme().background.opacity(self.veil(cx));
screen.child(div().absolute().inset_0().bg(veil))
}),
)
}
}
@@ -1448,6 +1617,7 @@ mod tests {
use std::path::Path;
use std::rc::Rc;
use std::sync::Arc;
use std::time::Duration;
use arena_tracker_core::{
Account, ChallengeSummary, Champion, ChampionChallenge, ChampionRow, ChampionSort,
@@ -1458,16 +1628,25 @@ mod tests {
use gpui_kit::{
AppContext as _, Bounds, Entity, Focusable as _, KeyDownEvent, KeyUpEvent, Keystroke,
Modifiers, ScrollDelta, ScrollEvent, TestAppContext, VisualTestContext, WindowBounds,
WindowOptions, point, px, size,
WindowOptions, WindowVisibility, point, px, size,
};
use super::{ChampionSortChosen, Page, SelectEvent, Tab};
use crate::card::ASPECT;
use crate::card_grid::{CardGrid, row_height};
use crate::champion_table::tests::row;
use crate::motion::MatchEnd;
/// Switches to a tab, and lets its transition end.
fn switch_tab(page: &Entity<Page>, cx: &mut VisualTestContext, tab: Tab) {
cx.update(|window, cx| page.update(cx, |page, cx| page.set_tab(tab, window, cx)));
for _ in 0..10 {
if page.read_with(cx, |page, _| page.fade.is_none()) {
return;
}
frame_after(cx, 50);
}
panic!("the transition did not end");
}
fn open_page(cx: &mut TestAppContext) -> (Entity<Page>, VisualTestContext) {
@@ -2677,6 +2856,268 @@ mod tests {
assert_eq!(page.read_with(cx, |page, _| page.tab), Tab::Statistics);
}
/// 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));
cx.update(|window, cx| {
window.simulate_next_frame(cx);
window.draw(cx).clear(cx);
});
}
fn veil(page: &Entity<Page>, cx: &mut VisualTestContext) -> f32 {
page.read_with(cx, |page, cx| page.veil(cx))
}
fn shown_tab(page: &Entity<Page>, cx: &mut VisualTestContext) -> Tab {
page.read_with(cx, |page, _| page.shown_tab)
}
/// Asks for a tab, without waiting for its transition.
fn start_switch(page: &Entity<Page>, cx: &mut VisualTestContext, tab: Tab) {
cx.update(|window, cx| page.update(cx, |page, cx| page.set_tab(tab, window, cx)));
draw(cx);
}
#[gpui::test]
fn switching_tabs_veils_the_screen_leaving_then_unveils_the_one_arriving(
cx: &mut TestAppContext,
) {
let (page, cx) = open_page_in_season(cx);
let cx = &mut { cx };
show(&page, cx, statistics(0, 5, champions(5)));
draw(cx);
start_switch(&page, cx, Tab::Statistics);
// The navigation follows at once; the screen leaving stays, going under the veil.
assert_eq!(page.read_with(cx, |page, _| page.tab), Tab::Statistics);
assert_eq!(shown_tab(&page, cx), Tab::Challenge);
frame_after(cx, 65);
let halfway = veil(&page, cx);
assert!(halfway > 0. && halfway < 1., "{halfway}");
assert_eq!(shown_tab(&page, cx), Tab::Challenge);
// After 130 ms the other screen is under a full veil, which lifts in 280 ms.
frame_after(cx, 70);
assert_eq!(shown_tab(&page, cx), Tab::Statistics);
assert!(veil(&page, cx) > 0.9);
frame_after(cx, 280);
assert_eq!(veil(&page, cx), 0.);
assert_at_rest(cx);
}
#[gpui::test]
fn without_animations_or_with_the_window_hidden_the_other_screen_shows_at_once(
cx: &mut TestAppContext,
) {
let (page, cx) = open_page_in_season(cx);
let cx = &mut { cx };
show(&page, cx, statistics(0, 5, champions(5)));
draw(cx);
// The Windows « Show animations » setting is off.
cx.update(|_, cx| cx.set_reduce_motion(true));
start_switch(&page, cx, Tab::Statistics);
assert_eq!(shown_tab(&page, cx), Tab::Statistics);
assert!(page.read_with(cx, |page, _| page.fade.is_none()));
assert_at_rest(cx);
cx.update(|_, cx| cx.set_reduce_motion(false));
cx.simulate_visibility_change(WindowVisibility::Hidden);
start_switch(&page, cx, Tab::Challenge);
assert_eq!(shown_tab(&page, cx), Tab::Challenge);
assert!(page.read_with(cx, |page, _| page.fade.is_none()));
assert_at_rest(cx);
}
#[gpui::test]
fn a_window_hidden_during_a_transition_ends_it(cx: &mut TestAppContext) {
let (page, cx) = open_page_in_season(cx);
let cx = &mut { cx };
show(&page, cx, statistics(0, 5, champions(5)));
draw(cx);
start_switch(&page, cx, Tab::Statistics);
frame_after(cx, 30);
cx.simulate_visibility_change(WindowVisibility::Hidden);
assert_eq!(shown_tab(&page, cx), Tab::Statistics);
assert!(page.read_with(cx, |page, _| page.fade.is_none()));
assert_at_rest(cx);
}
#[gpui::test]
fn a_switch_back_during_a_transition_starts_from_where_the_veil_is(cx: &mut TestAppContext) {
let (page, cx) = open_page_in_season(cx);
let cx = &mut { cx };
show(&page, cx, statistics(0, 5, champions(5)));
draw(cx);
start_switch(&page, cx, Tab::Statistics);
frame_after(cx, 100);
let before = veil(&page, cx);
assert!(before > 0.);
// « Défi » is still the screen shown: it comes back out of the veil, without a jump.
start_switch(&page, cx, Tab::Challenge);
assert_eq!(veil(&page, cx), before);
frame_after(cx, 280);
assert_eq!(shown_tab(&page, cx), Tab::Challenge);
assert_eq!(veil(&page, cx), 0.);
}
// The transition only lays a veil over the screen: under it, the cached views are drawn
// from their cache, and nothing is computed again (rule #1).
#[gpui::test]
fn a_transition_neither_builds_the_cards_again_nor_recomputes_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, ring) =
page.read_with(cx, |page, _| (page.home_grid.clone(), page.ring.clone()));
let generation = page.read_with(cx, |page, _| page.stats_generation);
let built = |cx: &mut VisualTestContext| {
let ring = ring.read_with(cx, |ring, _| ring.renders);
grid.read_with(cx, |grid, _| {
(grid.renders, grid.card_builds, grid.set_cards_calls, ring)
})
};
let home = built(cx);
// « Défi » leaves under the veil without its grid or its ring being drawn again.
start_switch(&page, cx, Tab::Statistics);
for _ in 0..5 {
frame_after(cx, 30);
}
assert_eq!(shown_tab(&page, cx), Tab::Statistics);
assert_eq!(built(cx), home, "the cards were built again");
// (The charts arriving play their own short entry, as after any switch.)
for _ in 0..8 {
frame_after(cx, 40);
}
assert_eq!(veil(&page, cx), 0.);
// Back to « Défi »: the grid and the ring are drawn once, then not again.
start_switch(&page, cx, Tab::Challenge);
for _ in 0..5 {
frame_after(cx, 30);
}
assert_eq!(shown_tab(&page, cx), Tab::Challenge);
let back = built(cx);
for _ in 0..8 {
frame_after(cx, 40);
}
assert_eq!(veil(&page, cx), 0.);
assert_eq!(built(cx), back, "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 animate(page: &Entity<Page>, cx: &mut VisualTestContext, end: MatchEnd) {
cx.update(|window, cx| page.update(cx, |page, cx| page.animate(end, window, cx)));
}
fn played(page: &Entity<Page>, cx: &mut VisualTestContext) -> Vec<MatchEnd> {
page.read_with(cx, |page, _| page.played.clone())
}
#[gpui::test]
fn an_animation_waits_for_the_challenge_to_show_and_only_the_strongest_plays(
cx: &mut TestAppContext,
) {
use MatchEnd::{Completion, Mark, Nothing};
let (page, cx) = open_page_in_season(cx);
let cx = &mut { cx };
show(&page, cx, statistics(0, 5, champions(5)));
draw(cx);
animate(&page, cx, Mark);
assert_eq!(played(&page, cx), [Mark]);
// Minimised, or hidden in the notification area.
cx.simulate_visibility_change(WindowVisibility::Hidden);
for end in [Nothing, Completion, Mark] {
animate(&page, cx, end);
}
assert_eq!(played(&page, cx), [Mark]);
cx.simulate_visibility_change(WindowVisibility::Visible);
assert_eq!(played(&page, cx), [Mark, Completion]);
// Under « Statistiques »: it plays once « Défi » is back, at the end of the transition.
switch_tab(&page, cx, Tab::Statistics);
animate(&page, cx, Nothing);
start_switch(&page, cx, Tab::Challenge);
frame_after(cx, 200);
assert_eq!(played(&page, cx), [Mark, Completion]);
switch_tab(&page, cx, Tab::Challenge);
assert_eq!(played(&page, cx), [Mark, Completion, Nothing]);
// Behind « Paramètres ».
cx.update(|window, cx| page.update(cx, |page, cx| page.hidden(window, cx)));
animate(&page, cx, Mark);
assert_eq!(played(&page, cx).len(), 3);
cx.update(|window, cx| page.update(cx, |page, cx| page.uncovered(window, cx)));
assert_eq!(played(&page, cx), [Mark, Completion, Nothing, Mark]);
}
#[gpui::test]
fn a_champ_select_that_starts_abandons_the_animation_waiting(cx: &mut TestAppContext) {
use crate::worker::Msg;
use arena_tracker_core::ChampSelect;
let (page, cx) = open_page_in_season(cx);
let cx = &mut { cx };
switch_tab(&page, cx, Tab::Statistics);
animate(&page, cx, MatchEnd::Completion);
let champ_select = ChampSelect::new(Some(39), [39].into(), Some(vec![39]));
cx.update(|window, cx| {
page.update(cx, |page, cx| {
page.apply(
Msg::ChampSelect(Some(("Alice#EUW".into(), champ_select))),
window,
cx,
)
})
});
cx.update(|window, cx| {
page.update(cx, |page, cx| {
page.apply(Msg::ChampSelect(None), window, cx)
})
});
draw(cx);
assert_eq!(played(&page, cx), []);
}
#[gpui::test]
fn without_animations_nothing_waits_nor_plays(cx: &mut TestAppContext) {
let (page, cx) = open_page_in_season(cx);
let cx = &mut { cx };
cx.update(|_, cx| cx.set_reduce_motion(true));
animate(&page, cx, MatchEnd::Mark);
cx.simulate_visibility_change(WindowVisibility::Hidden);
animate(&page, cx, MatchEnd::Completion);
cx.simulate_visibility_change(WindowVisibility::Visible);
assert_eq!(played(&page, cx), []);
}
/// Nothing is drawn at rest: once gpui-kit's own short animations are over (the filters'
/// pill arrives with « Statistiques »), no frame is asked for.
fn assert_at_rest(cx: &mut VisualTestContext) {
frame_after(cx, 1000);
let asked: Vec<usize> = (0..3)
.map(|_| {
cx.executor().advance_clock(Duration::from_millis(100));
let asked = cx.update(|window, cx| window.simulate_next_frame(cx));
draw(cx);
asked
})
.collect();
assert_eq!(asked, [0; 3]);
}
// The Arena champ select of #13, replayed into the page (#74). The capture is the one the
// core replays: local cell 16, ally 17; the Anvil champions are 233, 64, 5, 16 and 68 and
// the client only offers 103, 12, 16, 233, 25 and 50.