mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Fade Astra composer stars and stabilize cursor redraws (#44879)
## What changed - Fade Astra stars over one second after 15 seconds from the first visible frame, or quickly on composer input, drafts, voice input, and popups. Disconnected key handling also dismisses the animation. - Preserve placeholder and draft text, including spaces. Stop scheduling animation frames when unfocused or finished, while allowing the idle deadline to elapse without terminal focus. - Hide the terminal cursor before repainting and cache its style to avoid redundant anchor repairs. Invalidate cursor state after screen transitions, external programs, resume, and caught-panic recovery. ## Testing Add regression tests and snapshots for fade timing, input dismissal, focus changes, text preservation, and frame scheduling. Add cursor redraw tests and a PTY test for cursor visibility and shape restoration after a caught panic. Update the reconnect test to wait until both the draft and notification are visible. GitOrigin-RevId: 0c9aaa13e6bde820f88e3757457063680d7921a3
This commit is contained in:
1 parent
7b491281c8
commit
33bdf976cc
20 files changed
+1097
-63
No files matched your search
@@ -45,6 +45,11 @@ impl ChatComposer {
|
||||
}
|
||||
|
||||
pub(crate) fn handle_disconnected_key(&mut self, key: KeyEvent) {
|
||||
if matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat)
|
||||
&& !matches!(key.code, KeyCode::Null)
|
||||
{
|
||||
self.interact_with_astra_sparkle();
|
||||
}
|
||||
self.cancel_history_search();
|
||||
self.attachments.clear_remote_image_selection();
|
||||
self.popups.active = ActivePopup::None;
|
||||
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
---
|
||||
source: tui/src/bottom_pane/chat_composer/sparkle_tests.rs
|
||||
assertion_line: 260
|
||||
expression: "format!(\"before timeout\\n{}\\n\\nfading\\n{}\\n\\nfinished\\n{}\",\nbuffer_text(&active), buffer_text(&fading), buffer_text(&finished),)"
|
||||
---
|
||||
before timeout
|
||||
⠁ ⠄ ⠈ ⠁ ⠐ ⠄ ⢀
|
||||
›⠁Ask Codex to do anything ⠈ ⠁ ⠈ ⠄ ⠂
|
||||
⠠⢀⠐ ⠄ ⠄ ⠠ ⠄⠂ ⡀ ⠈ ⠄
|
||||
⠄ ⠐
|
||||
⠈ ⠠ ⠠ ⢀⢀ ⠐ ⠂ ⠐ ⠐ ⡀
|
||||
⠄ ⠄ ⢀ ⢀ ⠠ ⠠ ⠄ ⡀⠂
|
||||
⠈ ⠁ ⠐ ⠐ ⢀
|
||||
? for shortcuts 100% context left
|
||||
|
||||
fading
|
||||
⠁ ⠄ ⠈ ⠁ ⠐ ⠄ ⢀
|
||||
›⠁Ask Codex to do anything ⠈ ⠁ ⠈ ⠄
|
||||
⠠⢀ ⠄⠂ ⡀ ⠈ ⠄
|
||||
⠄
|
||||
⠠ ⠠ ⢀⢀ ⠂ ⠐ ⡀
|
||||
⠄ ⢀ ⠠ ⠠ ⠄ ⡀⠂
|
||||
⠁ ⠐ ⠐ ⢀
|
||||
? for shortcuts 100% context left
|
||||
|
||||
finished
|
||||
|
||||
› Ask Codex to do anything
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
? for shortcuts 100% context left
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
---
|
||||
source: tui/src/bottom_pane/chat_composer/sparkle_tests.rs
|
||||
expression: buffer_text(&active)
|
||||
---
|
||||
⠈ ⢀ ⠈ ⠁ ⠐ ⠐ ⠐ ⠄ ⠈
|
||||
›⠁Ask Codex to do anything ⠈ ⠁ ⠁ ⠈
|
||||
⠠ ⠄ ⢀ ⠄ ⠄⠈ ⠄
|
||||
⠐ ⠄ ⡀ ⠠
|
||||
⠈ ⠠⠁ ⠠⠠ ⢀ ⠂
|
||||
⠄ ⠄ ⠐ ⠠ ⠂ ⡀
|
||||
⠁
|
||||
? for shortcuts 100% context left
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
---
|
||||
source: tui/src/bottom_pane/chat_composer/sparkle_tests.rs
|
||||
expression: "format!(\"idle\\n{}\\n\\nfading\\n{}\\n\\nfinished\\n{}\", buffer_text(&active),\nbuffer_text(&fading), buffer_text(&finished),)"
|
||||
---
|
||||
idle
|
||||
⢀ ⠁ ⢀
|
||||
› Ask Codex to do anything ⠈
|
||||
⠠⢀ ⠠ ⠄ ⠠
|
||||
? for shortcuts 100% context left
|
||||
|
||||
fading
|
||||
⠁ ⢀
|
||||
› Ask Codex to do anything ⠈
|
||||
⢀ ⠠ ⠄
|
||||
? for shortcuts 100% context left
|
||||
|
||||
finished
|
||||
|
||||
› Ask Codex to do anything
|
||||
|
||||
? for shortcuts 100% context left
|
||||
@@ -1,7 +1,10 @@
|
||||
//! Sparse Astra stars on the existing composer surface, fading with the terminal's colors.
|
||||
//! `enabled_foreground` owns eligibility, including late-arriving terminal colors.
|
||||
//! Rendering owns frame scheduling, so hidden composers do not keep animating.
|
||||
//! Astra stars throughout the untouched composer, using the terminal's colors.
|
||||
//! Selecting Astra starts one flourish; composer input fades it quickly, or 15 seconds after the
|
||||
//! first visible frame starts a smooth fade. The deadline keeps running while terminal focus is
|
||||
//! elsewhere. Only visible, eligible frames schedule more work, and the final frame clears the
|
||||
//! stars without taking over the terminal's native mouse actions.
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::sync::LazyLock;
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
@@ -9,6 +12,7 @@ use std::time::Instant;
|
||||
use codex_config::types::Tui;
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::buffer::CellDiffOption;
|
||||
use ratatui::layout::Position;
|
||||
use ratatui::layout::Rect;
|
||||
use ratatui::style::Color;
|
||||
use regex_lite::Regex;
|
||||
@@ -24,6 +28,10 @@ use crate::terminal_palette::effective_stdout_color_level;
|
||||
use crate::terminal_palette::rgb_color;
|
||||
|
||||
const FRAME_TICK: Duration = Duration::from_millis(150);
|
||||
const IDLE_TIMEOUT: Duration = Duration::from_secs(15);
|
||||
const IDLE_FADE: Duration = Duration::from_secs(1);
|
||||
const INTERACTION_FADE: Duration = Duration::from_millis(75);
|
||||
const FADE_FRAME_TICK: Duration = Duration::from_millis(25);
|
||||
const DOTS: [&str; 8] = ["⠁", "⠂", "⠄", "⠈", "⠐", "⠠", "⡀", "⢀"];
|
||||
static ASTRA_MODEL: LazyLock<Regex> = LazyLock::new(|| match Regex::new(r"(?i)\bastra\b") {
|
||||
Ok(regex) => regex,
|
||||
@@ -31,17 +39,36 @@ static ASTRA_MODEL: LazyLock<Regex> = LazyLock::new(|| match Regex::new(r"(?i)\b
|
||||
});
|
||||
|
||||
pub(super) struct Sparkle {
|
||||
model: String,
|
||||
whimsy: bool,
|
||||
animations: bool,
|
||||
started: Instant,
|
||||
phase: Cell<Phase>,
|
||||
terminal_focused: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum Phase {
|
||||
Waiting,
|
||||
Visible {
|
||||
since: Instant,
|
||||
elapsed: Duration,
|
||||
},
|
||||
Fading {
|
||||
started: Instant,
|
||||
elapsed: Duration,
|
||||
duration: Duration,
|
||||
start_visibility: f32,
|
||||
},
|
||||
Finished,
|
||||
}
|
||||
|
||||
struct SparkleFrame {
|
||||
elapsed: Duration,
|
||||
visibility: f32,
|
||||
next: Duration,
|
||||
}
|
||||
|
||||
impl Sparkle {
|
||||
fn enabled_foreground(&self) -> Option<(u8, u8, u8)> {
|
||||
if !self.whimsy
|
||||
|| !self.animations
|
||||
|| !ASTRA_MODEL.is_match(&self.model)
|
||||
if matches!(self.phase.get(), Phase::Finished)
|
||||
|| effective_stdout_color_level() != StdoutColorLevel::TrueColor
|
||||
{
|
||||
return None;
|
||||
@@ -49,35 +76,209 @@ impl Sparkle {
|
||||
// Read the cached palette here: Windows may populate it after widget creation.
|
||||
default_fg()
|
||||
}
|
||||
|
||||
fn interact(&self, now: Instant) {
|
||||
match self.phase.get() {
|
||||
Phase::Waiting => self.phase.set(Phase::Finished),
|
||||
Phase::Visible { since, elapsed } if now >= since + IDLE_TIMEOUT => {
|
||||
self.phase.set(Phase::Fading {
|
||||
started: since + IDLE_TIMEOUT,
|
||||
elapsed,
|
||||
duration: IDLE_FADE,
|
||||
start_visibility: 1.0,
|
||||
});
|
||||
self.interact(now);
|
||||
}
|
||||
Phase::Visible { elapsed, .. } => self.phase.set(Phase::Fading {
|
||||
started: now,
|
||||
elapsed,
|
||||
duration: INTERACTION_FADE,
|
||||
start_visibility: 1.0,
|
||||
}),
|
||||
Phase::Fading {
|
||||
started,
|
||||
elapsed,
|
||||
duration: IDLE_FADE,
|
||||
start_visibility,
|
||||
} => {
|
||||
let remaining = IDLE_FADE.saturating_sub(now.saturating_duration_since(started));
|
||||
if remaining.is_zero() {
|
||||
self.phase.set(Phase::Finished);
|
||||
} else {
|
||||
// Input during the timed fade finishes quickly without brightening the dots.
|
||||
self.phase.set(Phase::Fading {
|
||||
started: now,
|
||||
elapsed,
|
||||
duration: INTERACTION_FADE.min(remaining),
|
||||
start_visibility: start_visibility * remaining.as_secs_f32()
|
||||
/ IDLE_FADE.as_secs_f32(),
|
||||
});
|
||||
}
|
||||
}
|
||||
Phase::Fading { .. } | Phase::Finished => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn frame(&self, now: Instant) -> Option<SparkleFrame> {
|
||||
let elapsed = now.saturating_duration_since(self.started);
|
||||
match self.phase.get() {
|
||||
Phase::Waiting => {
|
||||
self.phase.set(Phase::Visible {
|
||||
since: now,
|
||||
elapsed,
|
||||
});
|
||||
Some(SparkleFrame {
|
||||
elapsed,
|
||||
visibility: 1.0,
|
||||
next: FRAME_TICK,
|
||||
})
|
||||
}
|
||||
Phase::Visible {
|
||||
since,
|
||||
elapsed: last_visible_elapsed,
|
||||
} => {
|
||||
let remaining_idle =
|
||||
IDLE_TIMEOUT.saturating_sub(now.saturating_duration_since(since));
|
||||
if remaining_idle.is_zero() {
|
||||
// Keep the last rendered dots in place, even if this frame arrives late.
|
||||
self.phase.set(Phase::Fading {
|
||||
started: since + IDLE_TIMEOUT,
|
||||
elapsed: last_visible_elapsed,
|
||||
duration: IDLE_FADE,
|
||||
start_visibility: 1.0,
|
||||
});
|
||||
return self.frame(now);
|
||||
}
|
||||
self.phase.set(Phase::Visible { since, elapsed });
|
||||
Some(SparkleFrame {
|
||||
elapsed,
|
||||
visibility: 1.0,
|
||||
next: FRAME_TICK.min(remaining_idle),
|
||||
})
|
||||
}
|
||||
Phase::Fading {
|
||||
started,
|
||||
elapsed,
|
||||
duration,
|
||||
start_visibility,
|
||||
} => {
|
||||
let remaining_fade =
|
||||
duration.saturating_sub(now.saturating_duration_since(started));
|
||||
if remaining_fade.is_zero() {
|
||||
self.phase.set(Phase::Finished);
|
||||
return None;
|
||||
}
|
||||
// Fade the last visible stars so neither timeout nor typing introduces dots.
|
||||
Some(SparkleFrame {
|
||||
elapsed,
|
||||
visibility: start_visibility * remaining_fade.as_secs_f32()
|
||||
/ duration.as_secs_f32(),
|
||||
next: FADE_FRAME_TICK.min(remaining_fade),
|
||||
})
|
||||
}
|
||||
Phase::Finished => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BottomPane {
|
||||
pub(crate) fn set_astra_sparkle(&mut self, model: &str, settings: &Tui) {
|
||||
let started = self
|
||||
.composer
|
||||
.astra_sparkle
|
||||
.as_ref()
|
||||
.map_or_else(Instant::now, |sparkle| sparkle.started);
|
||||
self.composer.astra_sparkle = Some(Sparkle {
|
||||
model: model.to_owned(),
|
||||
whimsy: settings.whimsy,
|
||||
animations: settings.animations,
|
||||
started,
|
||||
});
|
||||
let changed = if !settings.whimsy || !settings.animations || !ASTRA_MODEL.is_match(model) {
|
||||
self.composer.astra_sparkle.take().is_some()
|
||||
} else if self.composer.astra_sparkle.is_none() {
|
||||
self.composer.astra_sparkle = Some(Sparkle {
|
||||
started: Instant::now(),
|
||||
phase: Cell::new(Phase::Waiting),
|
||||
terminal_focused: true,
|
||||
});
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
if changed && let Some(requester) = &self.composer.frame_requester {
|
||||
requester.schedule_frame();
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn set_sparkle_terminal_focus(&mut self, focused: bool) {
|
||||
if let Some(sparkle) = &mut self.composer.astra_sparkle {
|
||||
sparkle.terminal_focused = focused;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ChatComposer {
|
||||
pub(super) fn render_sparkle(&self, area: Rect, cursor: Option<(u16, u16)>, buf: &mut Buffer) {
|
||||
if area.is_empty() || !matches!(self.popups.active, ActivePopup::None) {
|
||||
pub(super) fn interact_with_astra_sparkle(&self) {
|
||||
if let Some(sparkle) = &self.astra_sparkle {
|
||||
sparkle.interact(Instant::now());
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn render_sparkle(
|
||||
&self,
|
||||
area: Rect,
|
||||
textarea: Rect,
|
||||
cursor: Option<(u16, u16)>,
|
||||
buf: &mut Buffer,
|
||||
) {
|
||||
self.render_sparkle_at(area, textarea, cursor, Instant::now(), buf);
|
||||
}
|
||||
|
||||
fn render_sparkle_at(
|
||||
&self,
|
||||
area: Rect,
|
||||
textarea: Rect,
|
||||
cursor: Option<(u16, u16)>,
|
||||
now: Instant,
|
||||
buf: &mut Buffer,
|
||||
) {
|
||||
if (!self.is_empty()
|
||||
|| self.draft.paste_burst.is_active()
|
||||
|| !self.draft.input_enabled
|
||||
|| self.voice_strip.is_some()
|
||||
|| !matches!(self.popups.active, ActivePopup::None))
|
||||
&& let Some(sparkle) = &self.astra_sparkle
|
||||
{
|
||||
sparkle.interact(now);
|
||||
}
|
||||
if !self.has_focus || area.height < 3 || textarea.is_empty() {
|
||||
return;
|
||||
}
|
||||
if let Some(sparkle) = &self.astra_sparkle
|
||||
&& sparkle.terminal_focused
|
||||
&& let Some(foreground) = sparkle.enabled_foreground()
|
||||
&& let Some(frame) = sparkle.frame(now)
|
||||
{
|
||||
render_stars(area, cursor, sparkle.started.elapsed(), foreground, buf);
|
||||
let protected_area = if !self.is_empty() || self.draft.paste_burst.is_active() {
|
||||
Rect::new(area.x, textarea.y, area.width, textarea.height)
|
||||
} else {
|
||||
let placeholder = if self.draft.input_enabled {
|
||||
self.placeholder_text.as_str()
|
||||
} else {
|
||||
self.draft
|
||||
.input_disabled_placeholder
|
||||
.as_deref()
|
||||
.unwrap_or("Input disabled.")
|
||||
};
|
||||
// Spaces inside the placeholder are text too; the terminal can copy them.
|
||||
Rect::new(
|
||||
textarea.x,
|
||||
textarea.y,
|
||||
placeholder.width().min(usize::from(textarea.width)) as u16,
|
||||
/*height*/ 1,
|
||||
)
|
||||
};
|
||||
render_stars(
|
||||
area,
|
||||
cursor,
|
||||
Some(protected_area),
|
||||
frame.elapsed,
|
||||
foreground,
|
||||
frame.visibility,
|
||||
buf,
|
||||
);
|
||||
if let Some(requester) = &self.frame_requester {
|
||||
requester.schedule_frame_in(FRAME_TICK);
|
||||
requester.schedule_frame_in(frame.next);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -86,8 +287,10 @@ impl ChatComposer {
|
||||
fn render_stars(
|
||||
area: Rect,
|
||||
cursor: Option<(u16, u16)>,
|
||||
protected_area: Option<Rect>,
|
||||
elapsed: Duration,
|
||||
foreground: (u8, u8, u8),
|
||||
visibility: f32,
|
||||
buf: &mut Buffer,
|
||||
) {
|
||||
let time = elapsed.as_secs_f32();
|
||||
@@ -104,6 +307,7 @@ fn render_stars(
|
||||
}
|
||||
// Preserve the cursor, selections, and pixels from the effort bursts.
|
||||
if cursor == Some((x, y))
|
||||
|| protected_area.is_some_and(|protected| protected.contains(Position::new(x, y)))
|
||||
|| !cell.modifier.is_empty()
|
||||
|| cell.diff_option != CellDiffOption::None
|
||||
{
|
||||
@@ -122,7 +326,7 @@ fn render_stars(
|
||||
}
|
||||
let phase =
|
||||
(time / (4.0 + (hash % 31) as f32 / 10.0) + (hash % 997) as f32 / 997.0).fract();
|
||||
let brightness = (phase * std::f32::consts::PI).sin().powi(12) * 0.55;
|
||||
let brightness = (phase * std::f32::consts::PI).sin().powi(12) * 0.55 * visibility;
|
||||
if brightness < 0.04 {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -7,15 +7,66 @@ use codex_protocol::openai_models::ReasoningEffort;
|
||||
use pretty_assertions::assert_eq;
|
||||
use ratatui::style::Style;
|
||||
|
||||
const SPARKLE_PALETTE: DefaultColors = DefaultColors {
|
||||
fg: (230, 216, 255),
|
||||
bg: (36, 27, 53),
|
||||
};
|
||||
|
||||
fn new_test_sparkle() -> Sparkle {
|
||||
Sparkle {
|
||||
model: "astra".to_string(),
|
||||
whimsy: true,
|
||||
animations: true,
|
||||
started: Instant::now(),
|
||||
phase: Cell::new(Phase::Waiting),
|
||||
terminal_focused: true,
|
||||
}
|
||||
}
|
||||
|
||||
fn new_test_pane() -> BottomPane {
|
||||
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
BottomPane::new(crate::bottom_pane::BottomPaneParams {
|
||||
app_event_tx: crate::app_event_sender::AppEventSender::new(tx),
|
||||
frame_requester: crate::tui::FrameRequester::test_dummy(),
|
||||
has_input_focus: true,
|
||||
enhanced_keys_supported: false,
|
||||
placeholder_text: "Ask Codex to do anything".into(),
|
||||
disable_paste_burst: true,
|
||||
animations_enabled: true,
|
||||
skills: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn buffer_text(buffer: &Buffer) -> String {
|
||||
buffer
|
||||
.content
|
||||
.chunks(usize::from(buffer.area.width))
|
||||
.map(|row| {
|
||||
row.iter()
|
||||
.map(ratatui::buffer::Cell::symbol)
|
||||
.collect::<String>()
|
||||
.trim_end()
|
||||
.to_owned()
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
fn rendered_sparkle(
|
||||
composer: &ChatComposer,
|
||||
area: Rect,
|
||||
now: Instant,
|
||||
baseline: &Buffer,
|
||||
) -> Buffer {
|
||||
let [surface, _, textarea, _] = composer.layout_areas(area);
|
||||
let mut buffer = baseline.clone();
|
||||
composer.render_sparkle_at(
|
||||
surface,
|
||||
textarea,
|
||||
composer.cursor_pos(area),
|
||||
now,
|
||||
&mut buffer,
|
||||
);
|
||||
buffer
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sparkle_keeps_the_existing_composer_layout() {
|
||||
with_test_default_colors(
|
||||
@@ -38,8 +89,10 @@ fn sparkle_keeps_the_existing_composer_layout() {
|
||||
render_stars(
|
||||
surface,
|
||||
composer.cursor_pos(area),
|
||||
/*protected_area*/ None,
|
||||
Duration::ZERO,
|
||||
/*foreground*/ (230, 216, 255),
|
||||
/*visibility*/ 1.0,
|
||||
&mut buffer,
|
||||
);
|
||||
// Snapshot the composer layout without freezing the star distribution.
|
||||
@@ -59,6 +112,294 @@ fn sparkle_keeps_the_existing_composer_layout() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sparkle_covers_the_untouched_composer_and_fades_after_interaction() {
|
||||
with_test_default_colors(SPARKLE_PALETTE, || {
|
||||
let width = 40;
|
||||
let (mut composer, _rx) = new_test_composer();
|
||||
let area = Rect::new(
|
||||
/*x*/ 0,
|
||||
/*y*/ 0,
|
||||
width,
|
||||
composer.desired_height(width),
|
||||
);
|
||||
let [_, _, textarea, _] = composer.layout_areas(area);
|
||||
let mut baseline = Buffer::empty(area);
|
||||
composer.render(area, &mut baseline);
|
||||
let sparkle = new_test_sparkle();
|
||||
let started = sparkle.started;
|
||||
composer.astra_sparkle = Some(sparkle);
|
||||
|
||||
let idle = started + Duration::from_secs(63);
|
||||
let active = rendered_sparkle(&composer, area, idle, &baseline);
|
||||
let stars = active
|
||||
.content
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(index, cell)| DOTS.contains(&cell.symbol()).then_some(index))
|
||||
.collect::<Vec<_>>();
|
||||
let star_rows = stars
|
||||
.iter()
|
||||
.map(|index| active.pos_of(*index).1)
|
||||
.collect::<Vec<_>>();
|
||||
assert!(star_rows.iter().any(|y| *y < textarea.y));
|
||||
assert!(
|
||||
star_rows
|
||||
.iter()
|
||||
.any(|y| (textarea.y..textarea.bottom()).contains(y))
|
||||
);
|
||||
assert!(star_rows.iter().any(|y| *y >= textarea.bottom()));
|
||||
let cursor = composer.cursor_pos(area).unwrap();
|
||||
assert_eq!(active[cursor], baseline[cursor]);
|
||||
|
||||
composer.astra_sparkle.as_ref().unwrap().interact(idle);
|
||||
let fading = rendered_sparkle(&composer, area, idle + INTERACTION_FADE / 2, &baseline);
|
||||
assert!(fading.content.iter().enumerate().all(|(index, cell)| {
|
||||
!DOTS.contains(&cell.symbol()) || active.content[index].symbol() == cell.symbol()
|
||||
}));
|
||||
assert!(stars.iter().any(|index| {
|
||||
let cell = &fading.content[*index];
|
||||
DOTS.contains(&cell.symbol()) && cell.fg != active.content[*index].fg
|
||||
}));
|
||||
|
||||
let finished = rendered_sparkle(&composer, area, idle + INTERACTION_FADE, &baseline);
|
||||
assert_eq!(finished, baseline);
|
||||
insta::assert_snapshot!(
|
||||
format!("astra_sparkle_interaction_{width}"),
|
||||
format!(
|
||||
"idle\n{}\n\nfading\n{}\n\nfinished\n{}",
|
||||
buffer_text(&active),
|
||||
buffer_text(&fading),
|
||||
buffer_text(&finished),
|
||||
)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sparkle_auto_fades_fifteen_seconds_after_its_first_visible_frame() {
|
||||
with_test_default_colors(SPARKLE_PALETTE, || {
|
||||
let (mut composer, _rx) = new_test_composer();
|
||||
let area = Rect::new(
|
||||
/*x*/ 0, /*y*/ 0, /*width*/ 80, /*height*/ 8,
|
||||
);
|
||||
let mut baseline = Buffer::empty(area);
|
||||
composer.render(area, &mut baseline);
|
||||
composer.astra_sparkle = Some(new_test_sparkle());
|
||||
let first_frame = composer.astra_sparkle.as_ref().unwrap().started
|
||||
+ Duration::from_secs(/*secs*/ 63);
|
||||
let first = rendered_sparkle(&composer, area, first_frame, &baseline);
|
||||
assert_ne!(first, baseline);
|
||||
|
||||
let deadline = first_frame + IDLE_TIMEOUT;
|
||||
let just_before = deadline - Duration::from_millis(/*millis*/ 50);
|
||||
let active = rendered_sparkle(&composer, area, just_before, &baseline);
|
||||
assert_ne!(active, baseline);
|
||||
assert_eq!(
|
||||
composer
|
||||
.astra_sparkle
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.frame(just_before)
|
||||
.unwrap()
|
||||
.next,
|
||||
Duration::from_millis(/*millis*/ 50)
|
||||
);
|
||||
|
||||
let sparkle = composer.astra_sparkle.as_ref().unwrap();
|
||||
assert_eq!(
|
||||
[
|
||||
sparkle.frame(deadline).unwrap().visibility,
|
||||
sparkle.frame(deadline + IDLE_FADE / 4).unwrap().visibility,
|
||||
sparkle.frame(deadline + IDLE_FADE / 2).unwrap().visibility,
|
||||
],
|
||||
[1.0, 0.75, 0.5]
|
||||
);
|
||||
let fading = rendered_sparkle(&composer, area, deadline + IDLE_FADE / 2, &baseline);
|
||||
assert!(fading.content.iter().enumerate().all(|(index, cell)| {
|
||||
!DOTS.contains(&cell.symbol()) || active.content[index].symbol() == cell.symbol()
|
||||
}));
|
||||
let finished = rendered_sparkle(&composer, area, deadline + IDLE_FADE, &baseline);
|
||||
assert_eq!(finished, baseline);
|
||||
assert!(matches!(
|
||||
composer.astra_sparkle.as_ref().unwrap().phase.get(),
|
||||
Phase::Finished
|
||||
));
|
||||
insta::assert_snapshot!(
|
||||
"astra_idle_timeout",
|
||||
format!(
|
||||
"before timeout\n{}\n\nfading\n{}\n\nfinished\n{}",
|
||||
buffer_text(&active),
|
||||
buffer_text(&fading),
|
||||
buffer_text(&finished),
|
||||
)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sparkle_preserves_placeholder_spaces_during_idle_animation() {
|
||||
with_test_default_colors(SPARKLE_PALETTE, || {
|
||||
let (mut composer, _rx) = new_test_composer();
|
||||
let area = Rect::new(
|
||||
/*x*/ 0, /*y*/ 0, /*width*/ 80, /*height*/ 8,
|
||||
);
|
||||
let [_, _, textarea, _] = composer.layout_areas(area);
|
||||
let mut baseline = Buffer::empty(area);
|
||||
composer.render(area, &mut baseline);
|
||||
composer.astra_sparkle = Some(new_test_sparkle());
|
||||
let start = composer.astra_sparkle.as_ref().unwrap().started
|
||||
+ Duration::from_secs(/*secs*/ 63);
|
||||
let end = textarea.x + composer.placeholder_text.width() as u16;
|
||||
for second in 0..IDLE_TIMEOUT.as_secs() {
|
||||
let active = rendered_sparkle(
|
||||
&composer,
|
||||
area,
|
||||
start + Duration::from_secs(second),
|
||||
&baseline,
|
||||
);
|
||||
for x in textarea.x..end {
|
||||
assert_eq!(active[(x, textarea.y)], baseline[(x, textarea.y)]);
|
||||
}
|
||||
assert_ne!(active, baseline);
|
||||
if second == 2 {
|
||||
insta::assert_snapshot!("astra_placeholder_spaces", buffer_text(&active));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_deadline_elapses_while_terminal_focus_is_elsewhere() {
|
||||
with_test_default_colors(SPARKLE_PALETTE, || {
|
||||
let (mut composer, _rx) = new_test_composer();
|
||||
let area = Rect::new(
|
||||
/*x*/ 0, /*y*/ 0, /*width*/ 80, /*height*/ 8,
|
||||
);
|
||||
let mut baseline = Buffer::empty(area);
|
||||
composer.render(area, &mut baseline);
|
||||
composer.astra_sparkle = Some(new_test_sparkle());
|
||||
let first_frame = composer.astra_sparkle.as_ref().unwrap().started;
|
||||
let active = rendered_sparkle(&composer, area, first_frame, &baseline);
|
||||
assert_ne!(active, baseline);
|
||||
composer.astra_sparkle.as_mut().unwrap().terminal_focused = false;
|
||||
let after_deadline = first_frame + IDLE_TIMEOUT + IDLE_FADE;
|
||||
let hidden = rendered_sparkle(&composer, area, after_deadline, &baseline);
|
||||
assert_eq!(hidden, baseline);
|
||||
assert!(matches!(
|
||||
composer.astra_sparkle.as_ref().unwrap().phase.get(),
|
||||
Phase::Visible { .. }
|
||||
));
|
||||
composer.astra_sparkle.as_mut().unwrap().terminal_focused = true;
|
||||
assert_eq!(
|
||||
rendered_sparkle(&composer, area, after_deadline, &baseline),
|
||||
baseline
|
||||
);
|
||||
assert!(matches!(
|
||||
composer.astra_sparkle.as_ref().unwrap().phase.get(),
|
||||
Phase::Finished
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn input_during_the_idle_fade_finishes_quickly_without_brightening() {
|
||||
let sparkle = new_test_sparkle();
|
||||
let first_frame = sparkle.started;
|
||||
sparkle.frame(first_frame).unwrap();
|
||||
sparkle
|
||||
.frame(first_frame + IDLE_TIMEOUT - Duration::from_millis(/*millis*/ 50))
|
||||
.unwrap();
|
||||
let interaction = first_frame + IDLE_TIMEOUT + IDLE_FADE / 2;
|
||||
let before = sparkle.frame(interaction).unwrap().visibility;
|
||||
sparkle.interact(interaction);
|
||||
assert_eq!(sparkle.frame(interaction).unwrap().visibility, before);
|
||||
assert!(sparkle.frame(interaction + INTERACTION_FADE).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typing_and_paste_start_one_fade_without_decorating_the_draft() {
|
||||
use crossterm::event::KeyCode;
|
||||
use crossterm::event::KeyEvent;
|
||||
|
||||
with_test_default_colors(SPARKLE_PALETTE, || {
|
||||
for paste in [false, true] {
|
||||
let (mut composer, _rx) = new_test_composer();
|
||||
let area = Rect::new(
|
||||
/*x*/ 0, /*y*/ 0, /*width*/ 60, /*height*/ 8,
|
||||
);
|
||||
composer.astra_sparkle = Some(new_test_sparkle());
|
||||
let mut initial = Buffer::empty(area);
|
||||
composer.render(area, &mut initial);
|
||||
|
||||
if paste {
|
||||
composer.handle_paste("first line\n中文 words".to_owned());
|
||||
} else {
|
||||
composer.handle_key_event(KeyEvent::from(KeyCode::Char('a')));
|
||||
composer.handle_key_event(KeyEvent::from(KeyCode::Char(' ')));
|
||||
}
|
||||
let sparkle = composer.astra_sparkle.take().unwrap();
|
||||
let Phase::Fading { started, .. } = sparkle.phase.get() else {
|
||||
panic!("first input should start fading");
|
||||
};
|
||||
let [surface, _, textarea, _] = composer.layout_areas(area);
|
||||
let mut baseline = Buffer::empty(area);
|
||||
composer.render(area, &mut baseline);
|
||||
composer.astra_sparkle = Some(sparkle);
|
||||
|
||||
let fading =
|
||||
rendered_sparkle(&composer, area, started + INTERACTION_FADE / 2, &baseline);
|
||||
for y in textarea.y..textarea.bottom() {
|
||||
for x in surface.x..surface.right() {
|
||||
assert_eq!(fading[(x, y)], baseline[(x, y)]);
|
||||
}
|
||||
}
|
||||
assert_eq!(
|
||||
rendered_sparkle(&composer, area, started + INTERACTION_FADE, &baseline),
|
||||
baseline
|
||||
);
|
||||
composer.set_text_content(String::new(), Vec::new(), Vec::new());
|
||||
assert!(matches!(
|
||||
composer.astra_sparkle.as_ref().unwrap().phase.get(),
|
||||
Phase::Finished
|
||||
));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread", start_paused = true)]
|
||||
async fn sparkle_stops_scheduling_after_the_fade() {
|
||||
let (mut composer, _rx) = new_test_composer();
|
||||
let (draw_tx, mut draw_rx) = tokio::sync::broadcast::channel(/*capacity*/ 4);
|
||||
composer.set_frame_requester(crate::tui::FrameRequester::new(draw_tx));
|
||||
composer.astra_sparkle = Some(new_test_sparkle());
|
||||
let area = Rect::new(
|
||||
/*x*/ 0, /*y*/ 0, /*width*/ 60, /*height*/ 3,
|
||||
);
|
||||
let render = |composer: &ChatComposer| {
|
||||
with_test_default_colors(SPARKLE_PALETTE, || {
|
||||
composer.render(area, &mut Buffer::empty(area))
|
||||
});
|
||||
};
|
||||
render(&composer);
|
||||
assert!(
|
||||
tokio::time::timeout(FRAME_TICK * 2, draw_rx.recv())
|
||||
.await
|
||||
.is_ok()
|
||||
);
|
||||
composer
|
||||
.astra_sparkle
|
||||
.as_ref()
|
||||
.expect("sparkle")
|
||||
.interact(Instant::now() - INTERACTION_FADE);
|
||||
render(&composer);
|
||||
assert!(
|
||||
tokio::time::timeout(FRAME_TICK * 2, draw_rx.recv())
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sparkle_preserves_content_cursor_and_background() {
|
||||
let area = Rect::new(
|
||||
@@ -78,8 +419,10 @@ fn sparkle_preserves_content_cursor_and_background() {
|
||||
render_stars(
|
||||
area,
|
||||
Some((3, 0)),
|
||||
/*protected_area*/ None,
|
||||
FRAME_TICK * tick,
|
||||
/*foreground*/ (230, 216, 255),
|
||||
/*visibility*/ 1.0,
|
||||
&mut after,
|
||||
);
|
||||
assert_eq!(
|
||||
@@ -122,8 +465,10 @@ fn stars_fade_using_the_custom_terminal_foreground() {
|
||||
render_stars(
|
||||
area,
|
||||
/*cursor*/ None,
|
||||
/*protected_area*/ None,
|
||||
FRAME_TICK * tick,
|
||||
colors.fg,
|
||||
/*visibility*/ 1.0,
|
||||
&mut buffer,
|
||||
);
|
||||
for (index, cell) in buffer.content.iter().enumerate() {
|
||||
@@ -149,18 +494,48 @@ fn stars_fade_using_the_custom_terminal_foreground() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn model_changes_and_disable_setting_control_sparkle() {
|
||||
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
let mut pane = BottomPane::new(crate::bottom_pane::BottomPaneParams {
|
||||
app_event_tx: crate::app_event_sender::AppEventSender::new(tx),
|
||||
frame_requester: crate::tui::FrameRequester::test_dummy(),
|
||||
has_input_focus: true,
|
||||
enhanced_keys_supported: false,
|
||||
placeholder_text: String::new(),
|
||||
disable_paste_burst: true,
|
||||
animations_enabled: true,
|
||||
skills: None,
|
||||
fn offline_composer_keys_fade_stars() {
|
||||
use crossterm::event::KeyCode;
|
||||
use crossterm::event::KeyEvent;
|
||||
use crossterm::event::KeyEventKind;
|
||||
use crossterm::event::KeyModifiers;
|
||||
|
||||
with_test_default_colors(SPARKLE_PALETTE, || {
|
||||
for code in [KeyCode::Enter, KeyCode::Tab, KeyCode::Left] {
|
||||
let mut pane = new_test_pane();
|
||||
let area = Rect::new(
|
||||
/*x*/ 0, /*y*/ 0, /*width*/ 40, /*height*/ 6,
|
||||
);
|
||||
pane.composer.astra_sparkle = Some(new_test_sparkle());
|
||||
pane.composer.render(area, &mut Buffer::empty(area));
|
||||
pane.handle_disconnected_key(KeyEvent::new(KeyCode::Null, KeyModifiers::NONE));
|
||||
assert!(matches!(
|
||||
pane.composer.astra_sparkle.as_ref().unwrap().phase.get(),
|
||||
Phase::Visible { .. }
|
||||
));
|
||||
pane.handle_disconnected_key(KeyEvent::new_with_kind(
|
||||
code,
|
||||
KeyModifiers::NONE,
|
||||
KeyEventKind::Release,
|
||||
));
|
||||
assert!(matches!(
|
||||
pane.composer.astra_sparkle.as_ref().unwrap().phase.get(),
|
||||
Phase::Visible { .. }
|
||||
));
|
||||
|
||||
pane.handle_disconnected_key(KeyEvent::new(code, KeyModifiers::NONE));
|
||||
assert!(matches!(
|
||||
pane.composer.astra_sparkle.as_ref().unwrap().phase.get(),
|
||||
Phase::Fading { .. }
|
||||
));
|
||||
assert_eq!(pane.composer.current_text(), "");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn model_changes_and_disable_setting_control_sparkle() {
|
||||
let mut pane = new_test_pane();
|
||||
with_test_default_colors(
|
||||
DefaultColors {
|
||||
fg: (230, 216, 255),
|
||||
@@ -178,6 +553,8 @@ fn model_changes_and_disable_setting_control_sparkle() {
|
||||
.astra_sparkle
|
||||
.as_ref()
|
||||
.map(|sparkle| sparkle.started);
|
||||
pane.set_sparkle_terminal_focus(/*focused*/ false);
|
||||
pane.set_sparkle_terminal_focus(/*focused*/ true);
|
||||
for model in ["astra", "ASTRA-preview", "openai/astra-2026-09-01"] {
|
||||
pane.set_astra_sparkle(model, &settings);
|
||||
assert_eq!(
|
||||
@@ -195,6 +572,15 @@ fn model_changes_and_disable_setting_control_sparkle() {
|
||||
started
|
||||
);
|
||||
}
|
||||
pane.composer.interact_with_astra_sparkle();
|
||||
pane.set_astra_sparkle("gpt-6-astra", &settings);
|
||||
assert_eq!(
|
||||
pane.composer
|
||||
.astra_sparkle
|
||||
.as_ref()
|
||||
.and_then(Sparkle::enabled_foreground),
|
||||
None,
|
||||
);
|
||||
for model in [
|
||||
"gpt-5.6-sol",
|
||||
"astral",
|
||||
@@ -230,7 +616,7 @@ fn model_changes_and_disable_setting_control_sparkle() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sparkle_renders_with_effort_bursts_and_pauses_for_popups() {
|
||||
fn sparkle_yields_to_drafts_effort_bursts_and_popups() {
|
||||
with_test_default_colors(
|
||||
DefaultColors {
|
||||
fg: (230, 216, 255),
|
||||
@@ -266,7 +652,7 @@ fn sparkle_renders_with_effort_bursts_and_pauses_for_popups() {
|
||||
after
|
||||
.content
|
||||
.iter()
|
||||
.any(|cell| DOTS.contains(&cell.symbol()))
|
||||
.all(|cell| !DOTS.contains(&cell.symbol()))
|
||||
);
|
||||
assert_eq!(
|
||||
composer.cursor_pos(area).map(|p| after[p].clone()),
|
||||
@@ -279,7 +665,8 @@ fn sparkle_renders_with_effort_bursts_and_pauses_for_popups() {
|
||||
composer.sync_popups();
|
||||
assert!(!matches!(composer.popups.active, ActivePopup::None));
|
||||
let mut buffer = Buffer::empty(area);
|
||||
composer.render_sparkle(area, /*cursor*/ None, &mut buffer);
|
||||
let [surface, _, textarea, _] = composer.layout_areas(area);
|
||||
composer.render_sparkle(surface, textarea, /*cursor*/ None, &mut buffer);
|
||||
assert_eq!(buffer, Buffer::empty(area));
|
||||
},
|
||||
);
|
||||
@@ -292,8 +679,9 @@ fn sparkle_waits_for_terminal_colors() {
|
||||
let area = Rect::new(
|
||||
/*x*/ 0, /*y*/ 0, /*width*/ 80, /*height*/ 3,
|
||||
);
|
||||
let [surface, _, textarea, _] = composer.layout_areas(area);
|
||||
let mut buffer = Buffer::empty(area);
|
||||
composer.render_sparkle(area, /*cursor*/ None, &mut buffer);
|
||||
composer.render_sparkle(surface, textarea, /*cursor*/ None, &mut buffer);
|
||||
assert_eq!(buffer, Buffer::empty(area));
|
||||
with_test_default_colors(
|
||||
DefaultColors {
|
||||
@@ -302,7 +690,7 @@ fn sparkle_waits_for_terminal_colors() {
|
||||
},
|
||||
|| {
|
||||
buffer.set_style(area, Style::default().bg(rgb_color((36, 27, 53))));
|
||||
composer.render_sparkle(area, /*cursor*/ None, &mut buffer);
|
||||
composer.render_sparkle(surface, textarea, /*cursor*/ None, &mut buffer);
|
||||
assert!(
|
||||
buffer
|
||||
.content
|
||||
@@ -375,7 +763,7 @@ fn sparkle_preserves_voice_indicator_styles() {
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(content, expected, "sparkle frame {tick}");
|
||||
}
|
||||
assert!(saw_stars);
|
||||
assert!(!saw_stars);
|
||||
insta::assert_snapshot!(
|
||||
"astra_voice_composer",
|
||||
baseline
|
||||
|
||||
Reference in new issue
Block a user