Merge pull request #454 from ludovic111/fix/title-toast-slide

World backup toast slides in and out with its sounds (#384)
This commit is contained in:
Ludovic Marie
2026-10-16 07:37:05 +02:00
committed by GitHub
6 changed files with 123 additions and 60 deletions
+3
View File
@@ -3665,6 +3665,9 @@ impl Game<'_> {
}
if let Some(title) = &mut self.title {
title.poll_backup();
if let Some(sound) = title.update_toast(self.settings.notification_display_time) {
self.audio.play(sound, None, 1.0, 1.0);
}
}
if let Some(title) = &self.title {
if horizon_client::ui_theme::horizon() {
+1 -45
View File
@@ -7,6 +7,7 @@ use horizon_client::{
hud::HudBatch,
items::ItemStack,
text::{self, Style, Text},
toasts::Slide,
};
use serde_json::Value;
use std::{
@@ -68,51 +69,6 @@ impl Toast {
(fully_for as f64 / (5000.0 * multiplier / n).max(1.0) % n) as usize
}
}
/// `ToastManager.ToastInstance`: slides a toast in and out over 600 ms,
/// eased by the square, and counts how long it has been fully shown.
#[derive(Default)]
struct Slide {
/// When it first updated; the times below are milliseconds since.
origin: Option<Instant>,
begun: bool,
/// `animationStartTime`.
start: i64,
/// `becameFullyVisibleAt`.
fully_at: i64,
show: bool,
/// `visiblePortion`: 0 off screen, 1 fully in.
portion: f32,
/// `fullyVisibleFor`.
fully_for: i64,
}
impl Slide {
const MS: i64 = 600;
/// `ToastInstance.update` at `now`; `wants_show` is the toast's own
/// update. The visibility it switched to (the sound to play), and whether
/// it has finished sliding out.
fn update(&mut self, now: i64, wants_show: impl FnOnce(i64) -> bool) -> (Option<bool>, bool) {
let was = self.show;
if !self.begun {
self.begun = true;
self.start = now;
self.show = true;
}
if self.show && now - self.start <= Self::MS {
self.fully_at = now;
}
self.fully_for = now - self.fully_at;
let progress = ((now - self.start) as f32 / Self::MS as f32).clamp(0.0, 1.0);
let progress = progress * progress;
self.portion = if self.show { progress } else { 1.0 - progress };
let wanted = wants_show(self.fully_for);
if wanted != self.show {
self.start = now - ((1.0 - self.portion) * Self::MS as f32) as i64;
self.show = wanted;
}
let finished = !self.show && now - self.start > Self::MS;
((self.show != was).then_some(self.show), finished)
}
}
#[derive(Default)]
pub(super) struct AdvState {
pub(super) tree: Arc<Tree>,
+1
View File
@@ -167,6 +167,7 @@ pub mod text;
pub mod tick_chunk_membership;
pub mod title;
pub mod title_edition;
pub mod toasts;
pub mod ui;
pub mod ui_theme;
pub mod vanillagen;
+28 -15
View File
@@ -136,7 +136,8 @@ pub struct Title {
message_lines: Cell<usize>,
backup: Option<mpsc::Receiver<Result<u64, String>>>,
backup_shown: Cell<bool>,
toast: Option<(std::time::Instant, BackupToast)>,
/// EditWorldScreen's `SystemToast` (`WORLD_BACKUP`) and its slide.
toast: Option<(crate::toasts::Slide, BackupToast)>,
icons: RefCell<std::collections::HashMap<PathBuf, (std::time::SystemTime, Option<Vec<u8>>)>>,
splash: Option<String>,
/// The mode last drawn and when it appeared (Horizon's screen fades).
@@ -424,9 +425,28 @@ impl Title {
Ok(bytes) => BackupToast::Created { folder, bytes },
Err(error) => BackupToast::Failed(error),
};
self.toast = Some((std::time::Instant::now(), toast));
self.toast = Some((crate::toasts::Slide::default(), toast));
}
}
/// `ToastManager.update` for the system toast: slides it in, keeps it
/// for 5 s times the notification display time, slides it out. The
/// sound of a new visibility (`Toast.Visibility`), if any.
pub fn update_toast(&mut self, multiplier: f32) -> Option<&'static str> {
let (slide, _) = self.toast.as_mut()?;
// SystemToast.update: shown until displayTime (5000 ms) has passed.
let time = 5000.0 * f64::from(multiplier);
let (sound, finished) = slide.update_now(|fully| (fully as f64) < time);
if finished {
self.toast = None;
}
sound.map(|show| {
if show {
"minecraft:ui.toast.in"
} else {
"minecraft:ui.toast.out"
}
})
}
fn start_backup(&mut self, world: WorldEntry) {
let (send, receive) = mpsc::channel();
let saves = self.saves.clone();
@@ -1123,25 +1143,18 @@ impl Title {
if let Mode::Multiplayer(screen) = &self.mode {
screen.draw_tooltip(atlas, batch, cursor);
}
if let Some((at, toast)) = &self.toast
&& at.elapsed().as_secs() < 5
{
if let Some((slide, toast)) = &self.toast {
let (title, message) = toast.text();
let width = HudBatch::text_width(atlas, &title)
.max(HudBatch::text_width(atlas, &message))
.ceil()
.max(160.0)
+ 30.0;
toast_background(atlas, batch, w - width, width);
batch.text(
atlas,
&title,
w - width + 18.0,
7.0,
rgba(255, 255, 0, 255),
false,
);
batch.text(atlas, &message, w - width + 18.0, 19.0, u32::MAX, false);
// Toast.xPos: in from the right edge by the visible portion.
let x = w - width * slide.portion;
toast_background(atlas, batch, x, width);
batch.text(atlas, &title, x + 18.0, 7.0, rgba(255, 255, 0, 255), false);
batch.text(atlas, &message, x + 18.0, 19.0, u32::MAX, false);
}
}
/// The action and whether to play `AbstractWidget.playButtonClickSound`.
+32
View File
@@ -373,6 +373,38 @@ fn stopped_backup_worker_returns_to_the_list_with_an_error_toast() {
);
}
#[test]
fn backup_toast_slides_in_and_out_with_its_sounds() {
let mut f = Fixture::new();
f.world("World", "World");
f.select();
f.click("Edit");
f.click("Make Backup");
f.title.poll_backup();
f.finish_backup();
assert_eq!(f.title.update_toast(1.0), Some("minecraft:ui.toast.in"));
assert_eq!(f.title.update_toast(1.0), None);
let age = |f: &mut Fixture, ms: u64| {
let (slide, _) = f.title.toast.as_mut().unwrap();
let origin = slide.origin.as_mut().unwrap();
*origin -= std::time::Duration::from_millis(ms);
};
// Half way in: (300 / 600)² of its width shows.
age(&mut f, 300);
assert_eq!(f.title.update_toast(1.0), None);
let portion = f.title.toast.as_ref().unwrap().0.portion;
assert!((portion - 0.25).abs() < 0.02, "{portion}");
// Shown in full for the display time, then out.
age(&mut f, 300);
f.title.update_toast(1.0);
age(&mut f, 5100);
assert_eq!(f.title.update_toast(1.0), Some("minecraft:ui.toast.out"));
assert!(f.title.toast.is_some());
age(&mut f, 700);
assert_eq!(f.title.update_toast(1.0), None);
assert!(f.title.toast.is_none());
}
#[test]
fn failed_operations_show_error_and_leave_world_intact() {
let mut f = Fixture::new();
+58
View File
@@ -0,0 +1,58 @@
//! `ToastManager`'s slide, shared by the game's toasts and the title
//! screens' system toasts.
use std::time::Instant;
/// `ToastManager.ToastInstance`: slides a toast in and out over 600 ms,
/// eased by the square, and counts how long it has been fully shown.
#[derive(Default)]
pub struct Slide {
/// When it first updated; the times below are milliseconds since.
pub origin: Option<Instant>,
begun: bool,
/// `animationStartTime`.
start: i64,
/// `becameFullyVisibleAt`.
fully_at: i64,
show: bool,
/// `visiblePortion`: 0 off screen, 1 fully in.
pub portion: f32,
/// `fullyVisibleFor`.
pub fully_for: i64,
}
impl Slide {
pub const MS: i64 = 600;
/// `ToastInstance.update` at `now`; `wants_show` is the toast's own
/// update. The visibility it switched to (the sound to play), and whether
/// it has finished sliding out.
pub fn update(
&mut self,
now: i64,
wants_show: impl FnOnce(i64) -> bool,
) -> (Option<bool>, bool) {
let was = self.show;
if !self.begun {
self.begun = true;
self.start = now;
self.show = true;
}
if self.show && now - self.start <= Self::MS {
self.fully_at = now;
}
self.fully_for = now - self.fully_at;
let progress = ((now - self.start) as f32 / Self::MS as f32).clamp(0.0, 1.0);
let progress = progress * progress;
self.portion = if self.show { progress } else { 1.0 - progress };
let wanted = wants_show(self.fully_for);
if wanted != self.show {
self.start = now - ((1.0 - self.portion) * Self::MS as f32) as i64;
self.show = wanted;
}
let finished = !self.show && now - self.start > Self::MS;
((self.show != was).then_some(self.show), finished)
}
/// `update` at the time since the first one.
pub fn update_now(&mut self, wants_show: impl FnOnce(i64) -> bool) -> (Option<bool>, bool) {
let origin = *self.origin.get_or_insert_with(Instant::now);
self.update(origin.elapsed().as_millis() as i64, wants_show)
}
}