End a transition when animations are turned off, read the screen drawn (#86)
From the review: the Windows setting turned off during a transition ends it and drops the animation waiting; the errors and the official count follow the screen drawn rather than the tab chosen; clearer names for the veil's phases and the frames it asks for. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WYQB8paBcLk67XoAUYARBK
This commit is contained in:
1 parent
2ede37c521
commit
d7f5e23aea
3 files changed
+61
-32
No files matched your search
@@ -1,4 +1,6 @@
|
||||
//! The one place that decides whether the app animates, and when (#86).
|
||||
//! What decides whether the app animates, and when (#86): the Windows setting followed into
|
||||
//! `App::reduce_motion`, the animations waiting for « Défi » to be visible, and the veil of the
|
||||
//! switch between tabs. The page says when « Défi » is visible.
|
||||
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
@@ -93,7 +95,7 @@ pub(crate) enum FadePhase {
|
||||
}
|
||||
|
||||
impl Fade {
|
||||
pub fn out(from: f32, start: Instant) -> Self {
|
||||
pub fn covering(from: f32, start: Instant) -> Self {
|
||||
Fade {
|
||||
phase: FadePhase::Out,
|
||||
from,
|
||||
@@ -101,7 +103,7 @@ impl Fade {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fade_in(from: f32, start: Instant) -> Self {
|
||||
pub fn uncovering(from: f32, start: Instant) -> Self {
|
||||
Fade {
|
||||
phase: FadePhase::In,
|
||||
from,
|
||||
@@ -245,7 +247,7 @@ mod tests {
|
||||
#[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);
|
||||
let out = Fade::covering(0., start);
|
||||
assert_eq!(out.veil(start), 0.);
|
||||
assert!(!out.finished(at(start, 129)));
|
||||
assert!(out.finished(at(start, 130)));
|
||||
@@ -253,7 +255,7 @@ mod tests {
|
||||
// Slow at first, as the reference's ease-in.
|
||||
assert!(out.veil(at(start, 65)) < 0.5);
|
||||
|
||||
let arrival = Fade::fade_in(1., start);
|
||||
let arrival = Fade::uncovering(1., start);
|
||||
assert_eq!(arrival.veil(start), 1.);
|
||||
assert!(!arrival.finished(at(start, 279)));
|
||||
assert!(arrival.finished(at(start, 280)));
|
||||
@@ -265,9 +267,9 @@ mod tests {
|
||||
#[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.);
|
||||
assert_eq!(Fade::covering(0.4, start).veil(start), 0.4);
|
||||
assert_eq!(Fade::uncovering(0.4, start).veil(start), 0.4);
|
||||
assert_eq!(Fade::uncovering(0.4, start).veil(at(start, 280)), 0.);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+50
-23
@@ -155,15 +155,15 @@ pub struct Page {
|
||||
crashed: bool,
|
||||
last_reload: Instant,
|
||||
current_season: Option<String>,
|
||||
/// Not persisted: the app opens on the Challenge. The navigation's: the screen shown follows
|
||||
/// it at the end of the transition.
|
||||
/// The tab the navigation shows as chosen. Not persisted: the app opens on the Challenge.
|
||||
/// The screen drawn (`shown_tab`) follows it once the screen leaving is under the veil.
|
||||
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,
|
||||
frame_asked: bool,
|
||||
/// « Paramètres » stands in for the page.
|
||||
covered: bool,
|
||||
/// The animation asked for while « Défi » was not visible.
|
||||
@@ -314,7 +314,7 @@ impl Page {
|
||||
// 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);
|
||||
this.play_waiting(window, cx);
|
||||
} else {
|
||||
this.end_fade(window, cx);
|
||||
}
|
||||
@@ -433,7 +433,7 @@ impl Page {
|
||||
tab: Tab::Challenge,
|
||||
shown_tab: Tab::Challenge,
|
||||
fade: None,
|
||||
fade_frame: false,
|
||||
frame_asked: false,
|
||||
covered: false,
|
||||
deferred: Deferred::new(),
|
||||
#[cfg(test)]
|
||||
@@ -511,7 +511,7 @@ impl Page {
|
||||
/// 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);
|
||||
self.play_waiting(window, cx);
|
||||
}
|
||||
|
||||
/// Plays an animation of « Défi » now if it is visible, else keeps the strongest until it
|
||||
@@ -522,6 +522,8 @@ impl Page {
|
||||
)]
|
||||
pub(crate) fn animate(&mut self, end: MatchEnd, window: &mut Window, cx: &mut Context<Self>) {
|
||||
if cx.reduce_motion() {
|
||||
// Nor does an older one wait for the setting to come back.
|
||||
self.deferred.abandon();
|
||||
return;
|
||||
}
|
||||
if let Some(end) = self.deferred.request(end, self.challenge_visible(window)) {
|
||||
@@ -541,8 +543,8 @@ impl Page {
|
||||
&& !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>) {
|
||||
/// « Défi » may have become visible: the animation waiting, if any, plays.
|
||||
fn play_waiting(&mut self, window: &Window, cx: &mut Context<Self>) {
|
||||
if self.challenge_visible(window)
|
||||
&& !cx.reduce_motion()
|
||||
&& let Some(end) = self.deferred.shown()
|
||||
@@ -611,7 +613,7 @@ impl Page {
|
||||
/// whether the page shows it as the home's Challenge or as the selected Season.
|
||||
fn official(&self) -> Option<&ProgressCheck> {
|
||||
let updated = matches!((self.displayed(), &self.status.updating), (Some(v), Some(u)) if v.puuid == u.puuid);
|
||||
let shown = match (self.tab, &self.season) {
|
||||
let shown = match (self.shown_tab, &self.season) {
|
||||
(Tab::Challenge, _) => self.home_season.as_deref(),
|
||||
(Tab::Statistics, SeasonScope::Season(season)) => Some(season.as_str()),
|
||||
(Tab::Statistics, SeasonScope::AllSeasons) => None,
|
||||
@@ -658,7 +660,7 @@ impl Page {
|
||||
|
||||
/// The tab's own statistics error: the home may have computed its own.
|
||||
fn error(&self) -> Option<&String> {
|
||||
match (self.tab, &self.home_scope) {
|
||||
match (self.shown_tab, &self.home_scope) {
|
||||
(Tab::Challenge, Some(_)) => self.home_error.as_ref(),
|
||||
_ => self.stats_error.as_ref(),
|
||||
}
|
||||
@@ -685,9 +687,9 @@ impl Page {
|
||||
let now = cx.background_executor().now();
|
||||
let veil = self.veil(cx);
|
||||
self.fade = Some(if self.shown_tab == tab {
|
||||
Fade::fade_in(veil, now)
|
||||
Fade::uncovering(veil, now)
|
||||
} else {
|
||||
Fade::out(veil, now)
|
||||
Fade::covering(veil, now)
|
||||
});
|
||||
self.next_fade_frame(window, cx);
|
||||
}
|
||||
@@ -716,14 +718,14 @@ impl Page {
|
||||
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);
|
||||
self.play_waiting(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;
|
||||
if !self.frame_asked {
|
||||
self.frame_asked = true;
|
||||
cx.on_next_frame(window, |page, window, cx| page.fade_frame(window, cx));
|
||||
}
|
||||
}
|
||||
@@ -731,16 +733,20 @@ impl Page {
|
||||
/// 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;
|
||||
self.frame_asked = false;
|
||||
let Some(fade) = &self.fade else {
|
||||
return;
|
||||
};
|
||||
// The Windows setting was turned off meanwhile: the end state at once.
|
||||
if cx.reduce_motion() {
|
||||
return self.end_fade(window, cx);
|
||||
}
|
||||
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));
|
||||
self.fade = Some(Fade::uncovering(1., now));
|
||||
}
|
||||
FadePhase::In => return self.end_fade(window, cx),
|
||||
}
|
||||
@@ -771,9 +777,8 @@ 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.
|
||||
// Nothing plays during a champ select: what waited is no longer news.
|
||||
self.end_fade(window, cx);
|
||||
self.deferred.abandon();
|
||||
// The home grid gives way to the champ select's sections.
|
||||
self.home_grid
|
||||
@@ -783,7 +788,7 @@ impl Page {
|
||||
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);
|
||||
self.play_waiting(window, cx);
|
||||
}
|
||||
}
|
||||
if switch {
|
||||
@@ -3095,12 +3100,34 @@ mod tests {
|
||||
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);
|
||||
// One waits while the window is hidden, then the setting is turned off.
|
||||
cx.simulate_visibility_change(WindowVisibility::Hidden);
|
||||
animate(&page, cx, MatchEnd::Completion);
|
||||
cx.update(|_, cx| cx.set_reduce_motion(true));
|
||||
animate(&page, cx, MatchEnd::Nothing);
|
||||
cx.simulate_visibility_change(WindowVisibility::Visible);
|
||||
animate(&page, cx, MatchEnd::Mark);
|
||||
assert_eq!(played(&page, cx), []);
|
||||
// Turned on again, it does not bring back the one that waited.
|
||||
cx.update(|_, cx| cx.set_reduce_motion(false));
|
||||
switch_tab(&page, cx, Tab::Statistics);
|
||||
switch_tab(&page, cx, Tab::Challenge);
|
||||
assert_eq!(played(&page, cx), []);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn the_setting_turned_off_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.update(|_, cx| cx.set_reduce_motion(true));
|
||||
frame_after(cx, 16);
|
||||
assert_eq!(shown_tab(&page, cx), Tab::Statistics);
|
||||
assert!(page.read_with(cx, |page, _| page.fade.is_none()));
|
||||
assert_at_rest(cx);
|
||||
}
|
||||
|
||||
/// Nothing is drawn at rest: once gpui-kit's own short animations are over (the filters'
|
||||
|
||||
Reference in new issue
Block a user