From 7015377ecd699f6e8727824d5aab6032988827d1 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Tue, 24 Nov 2026 18:33:45 +0000 Subject: [PATCH] Keep terminal pet animations in step with task status (#63729) ## Why The terminal pet could settle into its idle animation while a task was still running or waiting for approval. Its running notification also expired after three minutes, even if the task was still active. ## What changed Use the terminal program status to select the pet's reaction on redraw. Loop the running, waiting, and failed animations while their notifications are active. Play the review reaction three times before returning to idle animation, and clear the reaction when the program status returns to idle. Apply the same playback rules to custom task animations. Schedule animation redraws only when the pet can be drawn and the sprite can change. A pet that cannot fit or is covered by the shortcut HUD requests no animation frames. ## Testing Extend tests to check sprite selection through approval, completion, failure, and acknowledgment, along with custom animation playback and redraw scheduling when the pet is hidden. GitOrigin-RevId: 09ef2a8558190eb2f63f5c286b4271838f228f67 --- codex-rs/tui/src/app/pets.rs | 12 +- codex-rs/tui/src/chatwidget.rs | 4 +- codex-rs/tui/src/chatwidget/pets.rs | 30 ++++- codex-rs/tui/src/chatwidget/pets_tests.rs | 5 +- .../src/chatwidget/tests/program_status.rs | 66 ++++++----- .../src/chatwidget/tests/status_and_layout.rs | 60 ++++++---- codex-rs/tui/src/chatwidget/tool_requests.rs | 32 ----- codex-rs/tui/src/pets/ambient.rs | 72 ++++++++++-- codex-rs/tui/src/pets/model.rs | 111 ++++++++---------- 9 files changed, 228 insertions(+), 164 deletions(-) diff --git a/codex-rs/tui/src/app/pets.rs b/codex-rs/tui/src/app/pets.rs index 1e8a40133f..00733e710b 100644 --- a/codex-rs/tui/src/app/pets.rs +++ b/codex-rs/tui/src/app/pets.rs @@ -11,13 +11,11 @@ impl App { ) -> Result<()> { if self.chat_widget.ambient_pet_image_enabled() { let ambient_pet_area = self.conversation_area(screen_size.into()); - let request = self - .chat_widget - .ambient_pet_draw(ambient_pet_area, rendered_area.bottom()) - .filter(|pet| { - tui.shortcut_hud_area - .is_none_or(|hud| !hud.intersects(pet.area())) - }); + let request = self.chat_widget.ambient_pet_draw( + ambient_pet_area, + rendered_area.bottom(), + tui.shortcut_hud_area, + ); if let Err(err) = tui.draw_ambient_pet_image(request) { self.handle_ambient_pet_image_render_error(tui, err)?; } diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 25abc0ba75..e7bc70379c 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -1234,9 +1234,7 @@ impl ChatWidget { self.bottom_pane.pre_draw_tick(); self.flush_realtime_transcript_history(); self.refresh_realtime_microphone_level(); - if let Some(pet) = self.ambient_pet.as_ref() { - pet.schedule_next_frame(); - } + self.tick_ambient_pet(); self.refresh_goal_status_indicator_for_time_tick(); if self.terminal_title_shows_action_required() != self.last_terminal_title_requires_action { self.refresh_terminal_title(); diff --git a/codex-rs/tui/src/chatwidget/pets.rs b/codex-rs/tui/src/chatwidget/pets.rs index a46b013bf9..2b6e2acb8e 100644 --- a/codex-rs/tui/src/chatwidget/pets.rs +++ b/codex-rs/tui/src/chatwidget/pets.rs @@ -79,6 +79,25 @@ pub(super) fn start_configured_pet_load_if_needed( } impl ChatWidget { + pub(super) fn tick_ambient_pet(&mut self) { + use crate::pets::PetNotificationKind; + use crate::terminal_program_status::ProgramStatus; + + if self.ambient_pet.is_none() { + return; + } + let kind = match self.desired_program_status() { + ProgramStatus::Idle => None, + ProgramStatus::Working => Some(PetNotificationKind::Running), + ProgramStatus::Blocked(_) => Some(PetNotificationKind::Waiting), + ProgramStatus::Done => Some(PetNotificationKind::Review), + ProgramStatus::Error => Some(PetNotificationKind::Failed), + }; + if let Some(pet) = &mut self.ambient_pet { + pet.sync_notification(kind); + } + } + pub(super) fn set_ambient_pet_notification( &mut self, kind: crate::pets::PetNotificationKind, @@ -104,6 +123,7 @@ impl ChatWidget { &self, area: Rect, composer_bottom_y: u16, + shortcut_hud_area: Option, ) -> Option { if !self.bottom_pane.no_modal_or_popup_active() { return None; @@ -113,9 +133,13 @@ impl ChatWidget { TuiPetAnchor::Composer => composer_bottom_y, TuiPetAnchor::ScreenBottom => area.bottom(), }; - self.ambient_pet - .as_ref()? - .draw_request(area, anchor_bottom_y) + let pet = self.ambient_pet.as_ref()?; + let draw = pet.draw_request(area, anchor_bottom_y)?; + if shortcut_hud_area.is_some_and(|hud| hud.intersects(draw.area())) { + return None; + } + pet.schedule_next_frame(); + Some(draw) } pub(super) fn ambient_pet_wrap_reserved_cols(&self) -> u16 { diff --git a/codex-rs/tui/src/chatwidget/pets_tests.rs b/codex-rs/tui/src/chatwidget/pets_tests.rs index 61ac065692..c5509e5dcf 100644 --- a/codex-rs/tui/src/chatwidget/pets_tests.rs +++ b/codex-rs/tui/src/chatwidget/pets_tests.rs @@ -87,7 +87,10 @@ async fn configured_pet_loads_are_scoped_across_widget_replacements() { let area = ratatui::layout::Rect::new( /*x*/ 0, /*y*/ 0, /*width*/ 80, /*height*/ 24, ); - assert!(chat.ambient_pet_draw(area, area.bottom()).is_some()); + assert!( + chat.ambient_pet_draw(area, area.bottom(), /*shortcut_hud_area*/ None) + .is_some() + ); } #[tokio::test] diff --git a/codex-rs/tui/src/chatwidget/tests/program_status.rs b/codex-rs/tui/src/chatwidget/tests/program_status.rs index a79994155c..cf9ded358d 100644 --- a/codex-rs/tui/src/chatwidget/tests/program_status.rs +++ b/codex-rs/tui/src/chatwidget/tests/program_status.rs @@ -3,15 +3,30 @@ use super::*; use crate::terminal_program_status::ProgramStatus; -use crate::terminal_program_status::ProgramStatusKind; use pretty_assertions::assert_eq; #[tokio::test] async fn program_status_tracks_live_results_and_resolved_approvals() { let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; - let mut statuses = vec![chat.desired_program_status()]; + chat.install_test_ambient_pet_for_tests(/*animations_enabled*/ false); + let status = |chat: &mut ChatWidget| { + chat.pre_draw_tick(); + let area = ratatui::layout::Rect::new( + /*x*/ 0, /*y*/ 0, /*width*/ 80, /*height*/ 24, + ); + // Inspect the selected sprite even when an approval modal covers it. + let draw = chat + .ambient_pet + .as_ref() + .unwrap() + .draw_request(area, area.bottom()) + .unwrap(); + let status = chat.desired_program_status(); + format!("{status:?} {}", draw.frame.display()) + }; + let mut statuses = vec![status(&mut chat)]; handle_turn_started(&mut chat, "status-turn"); - statuses.push(chat.desired_program_status()); + statuses.push(status(&mut chat)); handle_exec_approval_request( &mut chat, "status-approval", @@ -31,18 +46,18 @@ async fn program_status_tracks_live_results_and_resolved_approvals() { available_decisions: None, }, ); - statuses.push(chat.desired_program_status()); + statuses.push(status(&mut chat)); chat.handle_key_event(KeyEvent::new(KeyCode::Char('y'), KeyModifiers::NONE)); - statuses.push(chat.desired_program_status()); + statuses.push(status(&mut chat)); handle_turn_completed(&mut chat, "status-turn", /*duration_ms*/ None); - statuses.push(chat.desired_program_status()); - chat.pre_draw_tick(); - statuses.push(chat.desired_program_status()); + statuses.push(status(&mut chat)); + // A second draw must leave the unread result in place. + statuses.push(status(&mut chat)); chat.acknowledge_program_status(); - statuses.push(chat.desired_program_status()); + statuses.push(status(&mut chat)); handle_turn_started(&mut chat, "cancelled-turn"); handle_turn_interrupted(&mut chat, "cancelled-turn"); - statuses.push(chat.desired_program_status()); + statuses.push(status(&mut chat)); handle_turn_started(&mut chat, "failed-turn"); chat.handle_server_notification( ServerNotification::TurnCompleted(TurnCompletedNotification { @@ -56,24 +71,21 @@ async fn program_status_tracks_live_results_and_resolved_approvals() { }), /*replay_kind*/ None, ); - statuses.push(chat.desired_program_status()); + statuses.push(status(&mut chat)); chat.acknowledge_program_status(); - statuses.push(chat.desired_program_status()); - assert_eq!( - statuses, - [ - ProgramStatus::Idle, - ProgramStatus::Working, - ProgramStatus::Blocked(ProgramStatusKind::Permission), - ProgramStatus::Working, - ProgramStatus::Done, - ProgramStatus::Done, - ProgramStatus::Idle, - ProgramStatus::Idle, - ProgramStatus::Error, - ProgramStatus::Idle, - ] - ); + statuses.push(status(&mut chat)); + insta::assert_snapshot!(statuses.join("\n"), @r" + Idle frame-0.png + Working frame-56.png + Blocked(Permission) frame-48.png + Working frame-56.png + Done frame-64.png + Done frame-64.png + Idle frame-0.png + Idle frame-0.png + Error frame-40.png + Idle frame-0.png + "); } #[tokio::test] diff --git a/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs b/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs index 4f815b4217..db54f8d687 100644 --- a/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs +++ b/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs @@ -2828,7 +2828,7 @@ async fn ambient_pet_stays_hidden_until_a_pet_is_selected() { /*x*/ 0, /*y*/ 0, /*width*/ 60, /*height*/ 20, ); let draw = chat - .ambient_pet_draw(area, area.bottom()) + .ambient_pet_draw(area, area.bottom(), /*shortcut_hud_area*/ None) .expect("ambient pet draw request"); assert_eq!(draw.x, 51); assert_eq!(draw.y, 14); @@ -2842,7 +2842,7 @@ async fn ambient_pet_stays_hidden_until_a_pet_is_selected() { handle_turn_started(&mut chat, "turn-1"); handle_agent_reasoning_delta(&mut chat, "**Thinking**"); let draw_with_status = chat - .ambient_pet_draw(area, area.bottom()) + .ambient_pet_draw(area, area.bottom(), /*shortcut_hud_area*/ None) .expect("ambient pet draw request with status"); assert_eq!(draw_with_status.y, draw.y); assert_eq!( @@ -2865,13 +2865,21 @@ async fn ambient_pet_screen_bottom_anchor_uses_terminal_bottom() { ); let composer_bottom_y = 20; let default_draw = chat - .ambient_pet_draw(terminal_area, composer_bottom_y) + .ambient_pet_draw( + terminal_area, + composer_bottom_y, + /*shortcut_hud_area*/ None, + ) .expect("composer-anchored pet draw request"); assert_eq!(default_draw.y, 14); chat.local_settings.tui.pet_anchor = TuiPetAnchor::ScreenBottom; let screen_bottom_draw = chat - .ambient_pet_draw(terminal_area, composer_bottom_y) + .ambient_pet_draw( + terminal_area, + composer_bottom_y, + /*shortcut_hud_area*/ None, + ) .expect("screen-bottom anchored pet draw request"); assert_eq!(screen_bottom_draw.y, 18); } @@ -3030,27 +3038,39 @@ async fn ambient_pet_draw_uses_terminal_screen_area_not_short_inline_viewport() use ratatui::layout::Rect; let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; - enable_test_ambient_pet(&mut chat); + let (tx, mut frames) = tokio::sync::broadcast::channel(1); + chat.ambient_pet = Some(crate::pets::test_ambient_pet( + FrameRequester::new(tx), + /*animations_enabled*/ true, + )); + tokio::time::pause(); + handle_turn_started(&mut chat, "pet-turn"); + chat.pre_draw_tick(); - assert!( - chat.ambient_pet_draw( - Rect::new( - /*x*/ 0, /*y*/ 21, /*width*/ 80, /*height*/ 3, - ), - /*composer_bottom_y*/ 24 - ) - .is_none(), - "a normal short inline viewport cannot fit the ambient pet" + let screen = Rect::new( + /*x*/ 0, /*y*/ 0, /*width*/ 80, /*height*/ 24, ); + let short = Rect::new( + /*x*/ 0, /*y*/ 21, /*width*/ 80, /*height*/ 3, + ); + assert!( + chat.ambient_pet_draw(short, screen.bottom(), /*shortcut_hud_area*/ None) + .is_none() + ); + assert!( + chat.ambient_pet_draw(screen, screen.bottom(), Some(screen)) + .is_none() + ); + let timeout = std::time::Duration::from_secs(1); + assert!(tokio::time::timeout(timeout, frames.recv()).await.is_err()); let draw = chat - .ambient_pet_draw( - Rect::new( - /*x*/ 0, /*y*/ 0, /*width*/ 80, /*height*/ 24, - ), - /*composer_bottom_y*/ 24, - ) + .ambient_pet_draw(screen, screen.bottom(), /*shortcut_hud_area*/ None) .expect("full terminal screen has room for the ambient pet"); + tokio::time::timeout(timeout, frames.recv()) + .await + .unwrap() + .unwrap(); assert_eq!(draw.x, 71); assert_eq!(draw.y, 18); } diff --git a/codex-rs/tui/src/chatwidget/tool_requests.rs b/codex-rs/tui/src/chatwidget/tool_requests.rs index 2c0fca1424..3f21cc8b19 100644 --- a/codex-rs/tui/src/chatwidget/tool_requests.rs +++ b/codex-rs/tui/src/chatwidget/tool_requests.rs @@ -307,10 +307,6 @@ impl ChatWidget { }); self.bottom_pane .push_approval_request(request, &self.config.features); - self.set_ambient_pet_notification( - crate::pets::PetNotificationKind::Waiting, - /*body*/ None, - ); self.request_redraw(); } @@ -328,10 +324,6 @@ impl ChatWidget { }); self.bottom_pane .push_approval_request(request, &self.config.features); - self.set_ambient_pet_notification( - crate::pets::PetNotificationKind::Waiting, - /*body*/ None, - ); self.request_redraw(); self.notify(Notification::EditApprovalRequested { cwd: self.config.cwd.to_path_buf(), @@ -410,20 +402,12 @@ impl ChatWidget { } } } - self.set_ambient_pet_notification( - crate::pets::PetNotificationKind::Waiting, - /*body*/ None, - ); self.request_redraw(); } pub(crate) fn push_approval_request(&mut self, request: ApprovalRequest) { self.bottom_pane .push_approval_request(request, &self.config.features); - self.set_ambient_pet_notification( - crate::pets::PetNotificationKind::Waiting, - /*body*/ None, - ); self.request_redraw(); } @@ -435,10 +419,6 @@ impl ChatWidget { // Inactive-thread prompts must leave the foreground stream and command activity intact. self.bottom_pane .push_user_verification_request(thread_id, request); - self.set_ambient_pet_notification( - crate::pets::PetNotificationKind::Waiting, - /*body*/ None, - ); self.request_redraw(); } @@ -448,10 +428,6 @@ impl ChatWidget { ) { self.bottom_pane .push_mcp_server_elicitation_request(request); - self.set_ambient_pet_notification( - crate::pets::PetNotificationKind::Waiting, - /*body*/ None, - ); self.request_redraw(); } @@ -466,10 +442,6 @@ impl ChatWidget { }; self.notify(Notification::PlanModePrompt { title }); self.bottom_pane.push_user_input_request(ev); - self.set_ambient_pet_notification( - crate::pets::PetNotificationKind::Waiting, - /*body*/ None, - ); self.request_redraw(); } @@ -485,10 +457,6 @@ impl ChatWidget { }); self.bottom_pane .push_approval_request(request, &self.config.features); - self.set_ambient_pet_notification( - crate::pets::PetNotificationKind::Waiting, - /*body*/ None, - ); self.request_redraw(); } } diff --git a/codex-rs/tui/src/pets/ambient.rs b/codex-rs/tui/src/pets/ambient.rs index 714f83b34b..a900460ff6 100644 --- a/codex-rs/tui/src/pets/ambient.rs +++ b/codex-rs/tui/src/pets/ambient.rs @@ -11,8 +11,6 @@ //! that does not overlap reserved bottom-pane space. It does not persist pet //! selection or decide when modal/popover UI should suppress the sprite. -#[cfg(test)] -use std::collections::HashMap; use std::path::PathBuf; use std::time::Duration; use std::time::Instant; @@ -38,7 +36,6 @@ const PET_TARGET_HEIGHT_PX: u16 = 75; const PET_COMPOSER_GAP_PX: u16 = 10; const TERMINAL_ROW_HEIGHT_PX: u16 = 15; -const RUNNING_LIFETIME: Duration = Duration::from_secs(3 * 60); const FAILED_LIFETIME: Duration = Duration::from_secs(60 * 60); const WAITING_LIFETIME: Duration = Duration::from_secs(24 * 60 * 60); const REVIEW_LIFETIME: Duration = Duration::from_secs(7 * 24 * 60 * 60); @@ -79,12 +76,12 @@ impl PetNotificationKind { } } - fn lifetime(self) -> Duration { + fn lifetime(self) -> Option { match self { - Self::Running => RUNNING_LIFETIME, - Self::Waiting => WAITING_LIFETIME, - Self::Review => REVIEW_LIFETIME, - Self::Failed => FAILED_LIFETIME, + Self::Running => None, + Self::Waiting => Some(WAITING_LIFETIME), + Self::Review => Some(REVIEW_LIFETIME), + Self::Failed => Some(FAILED_LIFETIME), } } } @@ -106,7 +103,9 @@ impl PetNotification { } fn is_expired(&self, now: Instant) -> bool { - now.saturating_duration_since(self.updated_at) >= self.kind.lifetime() + self.kind + .lifetime() + .is_some_and(|lifetime| now.saturating_duration_since(self.updated_at) >= lifetime) } } @@ -186,6 +185,14 @@ impl AmbientPet { self.animation_started_at = Instant::now(); } + /// A redraw must not restart playback or renew an expired notification. + pub(crate) fn sync_notification(&mut self, kind: Option) { + if self.notification.as_ref().map(|n| n.kind) != kind { + self.notification = kind.map(|kind| PetNotification::new(kind, /*body*/ None)); + self.animation_started_at = Instant::now(); + } + } + pub(crate) fn set_animations_enabled(&mut self, enabled: bool) { self.animations_enabled = enabled; } @@ -401,6 +408,12 @@ fn current_animation_frame(animation: &Animation, elapsed: Duration) -> Option(); + if elapsed_nanos >= prefix_nanos && loop_start == animation.frames.len() - 1 { + return Some(AnimationFrameTick { + sprite_index: animation.frames[loop_start].sprite_index, + delay: None, + }); + } let loop_nanos = animation.frames[loop_start..] .iter() .map(|frame| frame.duration.as_nanos()) @@ -468,10 +481,12 @@ pub(crate) fn test_ambient_pet( columns: 8, rows: 9, frame_count: 72, - animations: HashMap::from([("idle".to_string(), test_animation())]), + animations: super::model::with_default_animations(Default::default()), }, support: PetImageSupport::Supported(ImageProtocol::Kitty), - frames: vec![PathBuf::from("frame-0.png"), PathBuf::from("frame-1.png")], + frames: (0..72) + .map(|i| PathBuf::from(format!("frame-{i}.png"))) + .collect(), sixel_dir: PathBuf::new(), frame_requester, notification: None, @@ -503,6 +518,7 @@ fn test_animation() -> Animation { #[cfg(test)] mod tests { use super::*; + use pretty_assertions::assert_eq; #[test] fn notification_labels_match_codex_app_vocabulary() { @@ -512,9 +528,33 @@ mod tests { assert_eq!(PetNotificationKind::Failed.label(), "Blocked"); } + #[test] + fn active_pet_tracks_keep_playing_but_ready_settles_into_idle() { + let mut pet = test_ambient_pet( + FrameRequester::test_dummy(), + /*animations_enabled*/ true, + ); + let elapsed = Duration::from_secs(181); + for (kind, row) in [ + (PetNotificationKind::Running, 7), + (PetNotificationKind::Waiting, 6), + (PetNotificationKind::Failed, 5), + (PetNotificationKind::Review, 0), + ] { + pet.sync_notification(Some(kind)); + let started = pet.animation_started_at; + pet.sync_notification(Some(kind)); + assert_eq!(pet.animation_started_at, started); + let notification = pet.visible_notification(started + elapsed).unwrap(); + let animation = &pet.pet.animations[notification.kind.animation_name()]; + let frame = current_animation_frame(animation, elapsed).unwrap(); + assert_eq!(frame.sprite_index / 8, row); + } + } + #[test] fn animation_frame_uses_per_frame_duration() { - let animation = test_animation(); + let mut animation = test_animation(); assert_eq!( current_animation_frame(&animation, Duration::from_millis(/*millis*/ 15)), @@ -523,6 +563,14 @@ mod tests { delay: Some(Duration::from_millis(/*millis*/ 5)), }) ); + animation.loop_start = Some(1); + assert_eq!( + current_animation_frame(&animation, Duration::from_millis(/*millis*/ 10)), + Some(AnimationFrameTick { + sprite_index: 1, + delay: None, + }) + ); } #[test] diff --git a/codex-rs/tui/src/pets/model.rs b/codex-rs/tui/src/pets/model.rs index aa02731ab7..5066a82463 100644 --- a/codex-rs/tui/src/pets/model.rs +++ b/codex-rs/tui/src/pets/model.rs @@ -178,7 +178,7 @@ fn load_builtin_pet(pet: catalog::BuiltinPet, codex_home: Option<&Path>) -> Resu columns: catalog::DEFAULT_FRAME_COLUMNS, rows: catalog::DEFAULT_FRAME_ROWS, frame_count: default_frame_count(), - animations: default_animations(), + animations: with_default_animations(HashMap::new()), }) } @@ -389,12 +389,7 @@ fn load_animations( specs: HashMap, frame_count: usize, ) -> Result> { - let mut animations = default_animations(); - if specs.is_empty() { - validate_animation_indices(&animations, frame_count)?; - return Ok(animations); - } - + let mut animations = HashMap::new(); for (name, spec) in specs { if spec.frames.is_empty() { bail!("animation {name} must include at least one frame"); @@ -444,9 +439,7 @@ fn load_animations( ); } - animations - .entry("idle".to_string()) - .or_insert_with(idle_animation); + let animations = with_default_animations(animations); validate_animation_indices(&animations, frame_count)?; Ok(animations) } @@ -481,9 +474,11 @@ fn default_frame_count() -> usize { (catalog::DEFAULT_FRAME_COLUMNS * catalog::DEFAULT_FRAME_ROWS) as usize } -fn default_animations() -> HashMap { - [ - ("idle", idle_animation()), +pub(super) fn with_default_animations( + mut animations: HashMap, +) -> HashMap { + let idle = animations.remove("idle").unwrap_or_else(idle_animation); + for (name, default) in [ ( "running-right", app_state_animation( @@ -575,10 +570,19 @@ fn default_animations() -> HashMap { /*final_frame_duration_ms*/ 240, ), ), - ] - .into_iter() - .map(|(name, animation)| (name.to_string(), animation)) - .collect() + ] { + let custom = animations.contains_key(name); + let animation = animations.entry(name.to_string()).or_insert(default); + // Task state controls playback even for custom one-shot tracks. + match name { + "running" | "waiting" | "failed" => animation.loop_start = Some(0), + "review" => settle_into_idle(animation, &idle.frames), + _ if !custom => settle_into_idle(animation, &idle_animation().frames), + _ => {} + } + } + animations.insert("idle".to_string(), idle); + animations } fn idle_animation() -> Animation { @@ -611,24 +615,26 @@ fn app_state_animation( }), }) .collect::>(); - let primary_frame_count = primary_frames.len() * 3; - let frames = primary_frames - .iter() - .chain(primary_frames.iter()) - .chain(primary_frames.iter()) - .cloned() - .chain(idle_animation().frames) - .collect(); Animation { - frames, - loop_start: Some(primary_frame_count), + frames: primary_frames, + loop_start: Some(/*loop_start*/ 0), fallback: "idle".to_string(), } } +/// Desktop plays a short reaction three times, then loops the idle frames. +fn settle_into_idle(animation: &mut Animation, idle: &[AnimationFrame]) { + let count = animation.frames.len(); + animation.frames.extend_from_within(..count); + animation.frames.extend_from_within(..count); + animation.loop_start = Some(animation.frames.len()); + animation.frames.extend_from_slice(idle); +} + #[cfg(test)] mod tests { use super::*; + use pretty_assertions::assert_eq; fn write_minimal_pet() -> tempfile::TempDir { write_pet_manifest( @@ -679,7 +685,7 @@ mod tests { #[test] fn app_idle_animation_uses_calm_loop() { - let animations = default_animations(); + let animations = with_default_animations(HashMap::new()); let idle = &animations["idle"]; assert_eq!(sprite_indices(idle), vec![0, 1, 2, 3, 4, 5]); @@ -687,29 +693,9 @@ mod tests { assert_eq!(idle.loop_start, Some(/*loop_start*/ 0)); } - #[test] - fn app_running_animation_repeats_then_settles_into_idle() { - let animations = default_animations(); - let running = &animations["running"]; - let primary = vec![56, 57, 58, 59, 60, 61]; - - assert_eq!(sprite_indices(running)[0..6], primary); - assert_eq!(sprite_indices(running)[6..12], primary); - assert_eq!(sprite_indices(running)[12..18], primary); - assert_eq!( - sprite_indices(running)[18..], - sprite_indices(&animations["idle"]) - ); - assert_eq!( - durations_ms(running)[0..6], - vec![120, 120, 120, 120, 120, 220] - ); - assert_eq!(running.loop_start, Some(/*loop_start*/ 18)); - } - #[test] fn app_notification_states_use_expected_rows() { - let animations = default_animations(); + let animations = with_default_animations(HashMap::new()); assert_eq!( sprite_indices(&animations["waiting"])[0..6], @@ -726,20 +712,27 @@ mod tests { } #[test] - fn custom_animation_specs_keep_manifest_fps_and_loop_shape() { + fn custom_task_tracks_follow_state_and_other_tracks_honor_manifest() { let animations = load_animations( - HashMap::from([( - "custom".to_string(), - AnimationSpec { - frames: vec![1, 2], - fps: Some(/*fps*/ 2.0), - loop_animation: Some(/*loop_animation*/ false), - fallback: "idle".to_string(), - }, - )]), + ["custom", "running", "review"] + .into_iter() + .map(|name| { + ( + name.to_string(), + AnimationSpec { + frames: vec![1, 2], + fps: Some(/*fps*/ 2.0), + loop_animation: Some(/*loop_animation*/ false), + fallback: "idle".to_string(), + }, + ) + }) + .collect(), default_frame_count(), ) .unwrap(); + assert_eq!(animations["running"].loop_start, Some(0)); + assert_eq!(animations["review"].loop_start, Some(6)); let custom = &animations["custom"]; assert_eq!(sprite_indices(custom), vec![1, 2]);