Preserve the TUI logo's aspect ratio across terminal cell sizes (#63445)

## Why

Mapping the logo to a fixed character grid can stretch it when terminal
cells have different pixel proportions.

## What changed

Use the terminal's pixel and grid dimensions to scale the logo uniformly
and keep it centered in the space reserved for it. Use the original grid
mapping when usable pixel dimensions are unavailable.

Refresh the cached dimensions after resize or resume in onboarding,
startup draft input, and the main TUI, even if the grid size is unchanged.

## Testing

Update the settled onboarding logo snapshot to cover a HiDPI terminal
with 11 × 25.5 logical pixels per cell.

GitOrigin-RevId: 3a8a504e00ba80ba0acc421a8b48f64c73e465aa
This commit is contained in:
Eric Traut
2026-11-23 21:41:05 +00:00
committed by copyberry
parent a66312f2e9
commit 7a1325c191
8 changed files with 79 additions and 29 deletions
+6
View File
@@ -1058,6 +1058,12 @@ impl App {
{
return Ok(AppRunControl::Continue);
}
if matches!(&event, TuiEvent::Resize(_) | TuiEvent::Resume) {
self.chat_widget
.empty_state_animation
.borrow_mut()
.invalidate_window_size();
}
// Resume arrives after suspension; retain the last painted phase across hidden owners.
if matches!(&event, TuiEvent::Resume) || !tui.is_owned_screen() || self.overlay.is_some() {
self.chat_widget
+16 -6
View File
@@ -18,6 +18,7 @@ use crossterm::event::MouseEventKind;
use ratatui::buffer::Buffer;
use ratatui::layout::Position;
use ratatui::layout::Rect;
use ratatui::layout::Size;
use ratatui::style::Color;
use crate::motion::MotionMode;
@@ -49,6 +50,7 @@ pub(crate) struct EmptyStateAnimation {
opacity: f32,
fade_from: f32,
renderer: Option<Renderer>,
measured_screen: Size,
stage: Option<Rect>,
replaying: bool,
}
@@ -60,6 +62,7 @@ impl EmptyStateAnimation {
pub(crate) fn start_fresh(&mut self) {
self.cancel_replay();
self.invalidate_window_size();
self.eligible = true;
self.spin_elapsed = Duration::ZERO;
self.last_frame = None;
@@ -114,6 +117,11 @@ impl EmptyStateAnimation {
self.last_frame = None;
}
/// A resize can change the pixel dimensions without changing the grid.
pub(crate) fn invalidate_window_size(&mut self) {
self.measured_screen = Size::default();
}
/// Paint the shared logo sequence inside a caller-owned, reserved rectangle.
/// The caller clears the stage and keeps its layout stable after motion finishes.
pub(crate) fn render_in(
@@ -242,12 +250,14 @@ impl EmptyStateAnimation {
};
let background = background.unwrap_or((15, 20, 37));
let light = Lighting::terminal(foreground.unwrap_or((210, 221, 235)), background);
let cells = self.renderer.get_or_insert_with(Renderer::default).frame(
area.width,
area.height,
phase,
&light,
);
let renderer = self.renderer.get_or_insert_default();
// Recheck on grid resize, even if the capped stage stays the same size.
// Test buffers have no associated terminal.
if !cfg!(test) && self.measured_screen != buffer.area.as_size() {
renderer.window_size = crossterm::terminal::window_size().ok();
self.measured_screen = buffer.area.as_size();
}
let cells = renderer.frame(area.width, area.height, phase, &light);
for (i, cell) in cells.iter().enumerate().filter(|(_, cell)| cell.dots != 0) {
let [_, r, g, b] = cell.rgb.to_be_bytes();
let target = &mut buffer[(
@@ -4,6 +4,8 @@
use std::f64::consts::TAU;
use crossterm::terminal::WindowSize;
use super::geometry::EXTENT;
use super::geometry::FIELDS;
use super::geometry::GRID;
@@ -30,6 +32,7 @@ pub(super) struct Cell {
}
pub(super) struct Renderer {
pub(super) window_size: Option<WindowSize>,
shape: Vec<f32>,
depth: Vec<f32>,
colors: Vec<u32>,
@@ -40,6 +43,7 @@ impl Default for Renderer {
fn default() -> Self {
let samples = usize::from(MAX_COLUMNS) * usize::from(MAX_ROWS) * 8;
Self {
window_size: None,
shape: vec![0.0; GRID * GRID],
depth: vec![f32::NEG_INFINITY; samples],
colors: vec![0; samples],
@@ -73,6 +77,24 @@ impl Renderer {
let dot_rows = rows * 4;
let dot_width = 800.0 / dot_columns as f64;
let dot_height = 550.0 / dot_rows as f64;
// Fit the reference canvas uniformly in pixels, keeping its center fixed.
// Without pixel dimensions, keep the original mapping onto the full dot grid.
let (mut scale_x, mut scale_y) = (206.0, 206.0);
if let Some(size) = &self.window_size
&& size.columns > 0
&& size.rows > 0
&& size.width > size.columns
&& size.height > size.rows
{
// A HiDPI terminal can report fractional cell sizes in logical pixels.
let width = f64::from(size.width) / f64::from(size.columns);
let height = f64::from(size.height) / f64::from(size.rows);
let pixels_per_x = columns as f64 * width / 800.0;
let pixels_per_y = rows as f64 * height / 550.0;
let fit = pixels_per_x.min(pixels_per_y);
scale_x *= fit / pixels_per_x;
scale_y *= fit / pixels_per_y;
}
self.depth.fill(f32::NEG_INFINITY);
for (i, value) in self.shape.iter_mut().enumerate() {
*value =
@@ -96,8 +118,8 @@ impl Renderer {
let x3 = x1 * cz - y2 * sz;
let y3 = x1 * sz + y2 * cz;
let perspective = 4.4 / (4.4 - z2);
let column = ((400.0 + x3 * 206.0 * perspective) / dot_width).floor() as isize;
let row = ((275.0 + y3 * 206.0 * perspective) / dot_height).floor() as isize;
let column = ((400.0 + x3 * scale_x * perspective) / dot_width).floor() as isize;
let row = ((275.0 + y3 * scale_y * perspective) / dot_height).floor() as isize;
if column < 0 || column >= dot_columns as isize || row < 0 || row >= dot_rows as isize {
return;
}
@@ -447,6 +447,14 @@ fn onboarding_keeps_full_color_after_budget_and_pause() {
);
let start = Instant::now();
let mut animation = EmptyStateAnimation::default();
// At 2x density, 22 × 51 physical pixels per cell are 11 × 25.5 logical pixels.
animation.renderer.get_or_insert_default().window_size =
Some(crossterm::terminal::WindowSize {
columns: 80,
rows: 40,
width: 880,
height: 1020,
});
animation.start_fresh();
let mut buffer = Buffer::empty(area);
animation.render_in_at(area, &mut buffer, Presentation::Animated, start);
@@ -833,10 +833,7 @@ async fn run_onboarding_screen_inner(
}
for step in &onboarding_screen.steps {
if let Step::Welcome(widget) = step {
if matches!(&event, TuiEvent::Resume) {
widget.set_presentation(crate::empty_state_animation::Presentation::Hidden);
}
widget.set_focused(tui.is_terminal_focused());
widget.handle_draw_event(&event, tui.is_terminal_focused());
}
}
if !did_full_clear_after_success
+9 -2
View File
@@ -22,6 +22,7 @@ use crate::onboarding::keys;
use crate::onboarding::onboarding_screen::KeyboardHandler;
use crate::onboarding::onboarding_screen::StepStateProvider;
use crate::tui::FrameRequester;
use crate::tui::TuiEvent;
use super::onboarding_screen::StepState;
@@ -86,7 +87,13 @@ impl WelcomeWidget {
self.presentation.set(presentation);
}
pub(crate) fn set_focused(&self, focused: bool) {
pub(super) fn handle_draw_event(&self, event: &TuiEvent, focused: bool) {
if matches!(event, TuiEvent::Resize(_) | TuiEvent::Resume) {
self.animation.borrow_mut().invalidate_window_size();
}
if matches!(event, TuiEvent::Resume) {
self.set_presentation(Presentation::Hidden);
}
self.focused.set(focused);
}
}
@@ -175,7 +182,7 @@ mod tests {
let welcome_row = row_containing(&buf, "Welcome");
assert_eq!(welcome_row, Some(ANIMATION_HEIGHT + 1));
widget.set_focused(/*focused*/ false);
widget.handle_draw_event(&TuiEvent::FocusLost, /*focused*/ false);
(&widget).render_ref(area, &mut buf);
assert_eq!(row_containing(&buf, "Welcome"), welcome_row);
}
@@ -1,18 +1,15 @@
---
source: tui/src/empty_state_animation_tests.rs
expression: "buffer.content.chunks(usize::from(area.width)).map(|row|\nrow.iter().map(|cell|\ncell.symbol()).collect::<String>().trim_end().to_owned()).collect::<Vec<_>>().join(\"\\n\")"
expression: "buffer.content.chunks(usize::from(area.width)).map(|row|\nrow.iter().map(ratatui::buffer::Cell::symbol).collect::<String>().trim_end().to_owned()).collect::<Vec<_>>().join(\"\\n\")"
---
⣠⣴⣶⣿⣿⣷⣶⣤⣀⣀⣀⣀⣀
⢠⣾⣿⡿⠟⠛⠉⣻⣿⣿⣿⣿⣿⣿⣿⣿⣦⡀
⢀⣀⣿⣿⡟⢀⣠⣶⣿⣿⠿⠛⢉⣀⣀ ⠙⢿⣿⣿⣆
⢀⣾⣿⣿⣿⣿ ⢸⣿⡿⠋⢁⣤⣶⣿⣿⣿⣿⣦⣄⣻⣿⣿
⢠⣿⣿⡟⢹⣿⣿ ⢸⣿⣷⣾⣿⣿⣿⣯⣀⠉⠛⢿⣿⣿⣿⣿
⣼⣿⣿ ⢸⣿⣿ ⢸⣿⡿⠛⠁⠈⠛⢿⣿⣿⣦⣄⡈⠙⢿⣿⣷⡀
⢸⣿⣿⡀⠸⣿⣿⣶⣼⣿⡇ ⢸⣿⣟⠿⣿⣿⡆ ⢻⣿⣧
⢻⣿⣿⣄⡈⠙⠻⣿⣿⣷⣤⡀⢀⣤⣾⣿⣿ ⢸⣿⡇ ⣸⣿⣿
⣿⣿⣿⣿⣷⣦⣀⠉⣻⣿⣿⣿⡿⢿⣿⣿ ⢸⣿⡇⣠⣿⣿⡟
⢿⣿⣧⠉⠻⣿⣿⣿⣿⡿⠛⠉⣀⣼⣿⣿ ⣸⣿⣿⣿⣿⠟
⠘⢿⣿⣷⣄⡀⠉⠉⢁⣠⣴⣿⣿⡿⠟⠋⢀⣿⣿⡟⠋⠁
⠈⠛⢿⣿⣿⣿⣿⣿⣿⣿⣛⣉⣀⣀⣴⣿⣿⡟
⠈⠉⠉⠉⠙⠻⣿⣿⣿⣿⣿⣿⠿⠋
⠈⠉⠉
⢀⣠⣴⣶⣿⣿⣷⣶⣤⣀⣀⣀⣀⣀⡀
⣰⣿⣿⠟⠛⠉⣩⣭⣿⣿⣿⠿⠿⠿⣿⣿⣷⣄
⣀⣤⣶⣿⣿⠃⢠⣶⣿⡿⠟⠋⣁⣤⣶⣦⣄⡀⠙⣿⣿⣧
⢀⣼⣿⡿⢿⣿⣿ ⢸⣿⣇⣤⣶⣿⣿⣿⠛⠻⢿⣿⣷⣾⣿⣿
⣸⣿⣿⠁⢸⣿⣿ ⢸⣿⡿⠿⠋⠙⠿⢿⣿⣷⣤⣈⡙⠻⣿⣿⣦⡀
⢹⣿⣿⡄⠸⣿⣿⣶⣼⣿⡇ ⢸⣿⣟⠿⣿⣿⡆ ⢻⣿⣧
⠻⣿⣿⣦⣄⡉⠛⠿⣿⣷⣶⣄⣠⣴⣾⣿⣿ ⢸⣿⡇ ⣸⣿⣿
⣿⣿⡿⢿⣿⣷⣦⣤⣿⣿⣿⠿⠟⢹⣿⣿ ⢸⣿⣧⣾⣿⣿⠏
⠹⣿⣿⣄⡈⠙⠻⠿⠛⢉⣠⣴⣾⣿⣿⠟ ⣾⣿⣿⠿⠛⠁
⠙⠿⣿⣿⣷⣶⣶⣾⣿⣿⣟⠛⠉⢀⣠⣾⣿⡿⠁
⠉⠉⠉⠙⠙⠻⢿⣿⣿⣿⣿⣿⠿⠋
+3
View File
@@ -23,6 +23,9 @@ use crate::tui::TuiEvent;
impl StartupDraftPump {
pub(super) fn handle_event(&mut self, tui: &mut Tui, event: TuiEvent) -> io::Result<()> {
let screen_size = tui.screen_size_for_event(&event)?;
if matches!(&event, TuiEvent::Resize(_) | TuiEvent::Resume) {
self.blossom.get_mut().invalidate_window_size();
}
if tui.dismiss_shortcut_on_input(&event, &mut self.key_chord_matcher) {
return Ok(());
}