Animate tab and pane transitions with Neovide window easing

This commit is contained in:
2026-11-17 01:24:47 -08:00
parent d7e9be973c
commit 7782c47b38
5 changed files with 250 additions and 28 deletions
+1
View File
@@ -12,6 +12,7 @@ mod render;
mod server;
mod session;
mod terminal;
mod transition;
use anyhow::{Result, bail};
use gpui::{prelude::*, *};
+156 -28
View File
@@ -12,6 +12,7 @@ use crate::{
Snapshot,
},
render::{FONT_SIZE, LINE_HEIGHT, RenderCell, Renderer},
transition::Slide,
};
use alacritty_terminal::{
term::{TermMode, color::Colors},
@@ -32,6 +33,9 @@ pub struct Workspace {
snapshot: Option<Arc<Snapshot>>,
panes: HashMap<u64, Entity<TerminalPane>>,
error: Option<String>,
outgoing: Vec<Entity<TerminalPane>>,
transition: Option<Slide>,
transition_frame_pending: bool,
_updates: Task<()>,
}
impl Workspace {
@@ -54,6 +58,33 @@ impl Workspace {
view.error = Some(error);
}
if let Some(snapshot) = update.snapshot {
if let Some(direction) = view
.snapshot
.as_ref()
.and_then(|old| scene_direction(old, &snapshot))
{
view.outgoing = view
.snapshot
.as_ref()
.unwrap()
.panes
.iter()
.filter_map(|data| view.panes.get(&data.id).cloned())
.collect();
for pane in &view.outgoing {
pane.update(cx, |pane, cx| {
pane.read_only = true;
pane.transitioning = true;
pane.rename = None;
pane.selecting = false;
pane.preedit.clear();
pane.blink_timer = None;
cx.notify();
});
}
view.panes.clear();
view.transition = Some(Slide::new(direction, Instant::now()));
}
let active = snapshot
.tabs
.iter()
@@ -87,14 +118,16 @@ impl Workspace {
let client = view.client.clone();
let frame = snapshot.clone();
let pane = cx.new(|cx| {
TerminalPane::new(
let mut pane = TerminalPane::new(
client,
frame,
index,
active == Some(data.id),
window,
cx,
)
);
pane.transitioning = view.transition.is_some();
pane
});
view.panes.insert(data.id, pane);
}
@@ -124,12 +157,37 @@ impl Workspace {
snapshot: None,
panes: HashMap::new(),
error: None,
outgoing: Vec::new(),
transition: None,
transition_frame_pending: false,
_updates: task,
}
}
}
impl Render for Workspace {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let now = Instant::now();
if self
.transition
.as_ref()
.is_some_and(|transition| transition.finished(now))
{
self.transition = None;
self.outgoing.clear();
for pane in self.panes.values() {
pane.update(cx, |pane, cx| {
pane.transitioning = false;
cx.notify();
});
}
}
if self.transition.is_some() && !self.transition_frame_pending {
self.transition_frame_pending = true;
cx.on_next_frame(window, |view, _, cx| {
view.transition_frame_pending = false;
cx.notify();
});
}
let tabs = self
.snapshot
.as_ref()
@@ -157,21 +215,29 @@ impl Render for Workspace {
},
move |window, _| window.remove_window(),
);
let (incoming_offset, outgoing_offset) = self
.transition
.as_ref()
.map(|transition| transition.offsets(now))
.unwrap_or(([0., 0.], [0., 0.]));
let mut body = div()
.flex()
.relative()
.flex_1()
.min_h_0()
.gap(px(6.))
.p(px(chrome::CONTENT_INSET))
.overflow_hidden()
.bg(rgb(chrome::APP_BACKGROUND));
if !self.outgoing.is_empty() {
body = body.child(scene(outgoing_offset).children(self.outgoing.iter().cloned()));
}
let mut incoming = scene(incoming_offset);
if let Some(snapshot) = &self.snapshot {
for data in &snapshot.panes {
if let Some(pane) = self.panes.get(&data.id) {
body = body.child(pane.clone());
incoming = incoming.child(pane.clone());
}
}
if snapshot.panes.is_empty() {
body = body.child(
incoming = incoming.child(
div()
.p_4()
.text_color(rgb(chrome::MUTED))
@@ -179,13 +245,14 @@ impl Render for Workspace {
);
}
} else {
body = body.child(
incoming = incoming.child(
div()
.p_4()
.text_color(rgb(chrome::MUTED))
.child("Connecting…"),
);
}
body = body.child(incoming);
let mut root = div()
.size_full()
.flex()
@@ -208,11 +275,50 @@ impl Render for Workspace {
}
}
/// Absolute positioning translates the scene without changing its layout size.
fn scene(offset: [f32; 2]) -> Div {
div()
.absolute()
.left(relative(offset[0]))
.top(relative(offset[1]))
.size_full()
.flex()
.gap(px(6.))
.p(px(chrome::CONTENT_INSET))
.bg(rgb(chrome::APP_BACKGROUND))
}
fn scene_direction(old: &Snapshot, next: &Snapshot) -> Option<[f32; 2]> {
if old.panes.is_empty() || next.panes.is_empty() {
return None;
}
if old.active_tab != next.active_tab {
let previous = old
.tabs
.iter()
.position(|tab| tab.id == old.active_tab)
.unwrap_or(0);
let next_index = next
.tabs
.iter()
.position(|tab| tab.id == next.active_tab)
.unwrap_or(0);
return Some([if next_index > previous { 1. } else { -1. }, 0.]);
}
next.panes
.iter()
.any(|pane| !old.panes.iter().any(|old| old.id == pane.id))
.then_some([1., 0.])
}
struct TerminalPane {
client: Client,
snapshot: Arc<Snapshot>,
index: usize,
active: bool,
// Frozen outgoing scenes are display-only; no PTY resize, focus or input.
read_only: bool,
transitioning: bool,
focus: FocusHandle,
font: Font,
bounds: Bounds<Pixels>,
@@ -260,6 +366,8 @@ impl TerminalPane {
snapshot,
index,
active,
read_only: false,
transitioning: false,
focus,
font,
bounds: Bounds::default(),
@@ -282,6 +390,9 @@ impl TerminalPane {
&self.snapshot.panes[self.index]
}
fn command(&self, command: ClientMessage) {
if self.read_only {
return;
}
if let Err(error) = self.client.send(command) {
log::warn!("{error:#}");
}
@@ -295,7 +406,7 @@ impl TerminalPane {
cx.notify();
}
fn send(&mut self, bytes: Vec<u8>, cx: &mut Context<Self>) {
if bytes.is_empty() {
if self.read_only || bytes.is_empty() {
return;
}
self.preedit.clear();
@@ -356,6 +467,9 @@ impl TerminalPane {
MouseButton::Left,
cx.listener(|pane, _, window, cx| {
cx.stop_propagation();
if pane.read_only {
return;
}
if pane.rename.is_none() {
window.focus(&pane.focus);
}
@@ -387,7 +501,7 @@ impl TerminalPane {
}
fn key_down(&mut self, event: &KeyDownEvent, window: &mut Window, cx: &mut Context<Self>) {
if self.rename.is_some() {
if self.read_only || self.rename.is_some() {
return;
}
let key = &event.keystroke;
@@ -497,6 +611,9 @@ impl TerminalPane {
)
}
fn mouse_down(&mut self, event: &MouseDownEvent, window: &mut Window, cx: &mut Context<Self>) {
if self.read_only {
return;
}
window.focus(&self.focus);
self.activity(cx);
let pane = self.data().id;
@@ -528,6 +645,9 @@ impl TerminalPane {
}
}
fn scroll(&mut self, event: &ScrollWheelEvent, _: &mut Window, cx: &mut Context<Self>) {
if self.read_only {
return;
}
self.scroll_remainder += match event.delta {
ScrollDelta::Lines(d) => d.y * 3.,
ScrollDelta::Pixels(d) => f32::from(d.y) / LINE_HEIGHT,
@@ -562,7 +682,7 @@ impl TerminalPane {
let lines = ((f32::from(bounds.size.height) - 2. * PADDING) / LINE_HEIGHT)
.floor()
.clamp(1., 500.) as u16;
if self.requested_size != Some((columns, lines)) {
if !self.read_only && self.requested_size != Some((columns, lines)) {
self.requested_size = Some((columns, lines));
self.command(ClientMessage::Resize {
pane: self.data().id,
@@ -572,7 +692,9 @@ impl TerminalPane {
cell_height: LINE_HEIGHT as u16,
});
}
if self.prepared_revision == Some(self.snapshot.revision) && self.bounds == bounds {
let prepare_rows = self.prepared_revision != Some(self.snapshot.revision)
|| self.bounds.size != bounds.size;
if !prepare_rows && self.bounds.origin == bounds.origin {
return;
}
let started = Instant::now();
@@ -604,6 +726,23 @@ impl TerminalPane {
px(LINE_HEIGHT),
),
);
let mut cursor_color = colors::resolve(Color::Named(NamedColor::Cursor), &palette);
cursor_color.a = 0.55;
self.cursor_bounds = cursor_bounds;
self.cursor.set_target(
cursor_bounds,
if hidden {
CursorShape::Hidden
} else {
cursor.shape.into()
},
cursor_color,
Instant::now(),
);
if !prepare_rows {
return;
}
let data = self.data();
let rows = data
.cells
.chunks(usize::from(data.columns))
@@ -628,19 +767,6 @@ impl TerminalPane {
.collect()
})
.collect();
let mut cursor_color = colors::resolve(Color::Named(NamedColor::Cursor), &palette);
cursor_color.a = 0.55;
self.cursor_bounds = cursor_bounds;
self.cursor.set_target(
cursor_bounds,
if hidden {
CursorShape::Hidden
} else {
cursor.shape.into()
},
cursor_color,
Instant::now(),
);
self.renderer
.prepare(rows, &self.font, self.cell_width, window);
self.prepared_revision = Some(self.snapshot.revision);
@@ -691,7 +817,7 @@ impl Render for TerminalPane {
move |bounds,(),window,cx| {
paint.update(cx,|pane,cx| {
let started=Instant::now();
if pane.rename.is_none() {
if !pane.read_only && pane.rename.is_none() {
window.handle_input(&pane.focus,ElementInputHandler::new(bounds,paint.clone()),cx);
}
let mut delay=None;
@@ -699,7 +825,9 @@ impl Render for TerminalPane {
let origin=bounds.origin+point(px(PADDING),px(PADDING));
let focused=window.is_window_active() && pane.active && pane.rename.is_none();
pane.renderer.paint_with_cursor(origin,window,cx,|window| {
delay=pane.cursor.paint(Instant::now(),focused,window);
if !pane.transitioning {
delay=pane.cursor.paint(Instant::now(),focused,window);
}
});
if !pane.preedit.is_empty() {
let run=TextRun{len:pane.preedit.len(),font:pane.font.clone(),color:rgb(chrome::TEXT).into(),background_color:Some(rgb(chrome::TERMINAL_BACKGROUND).into()),
+81
View File
@@ -0,0 +1,81 @@
//! Neovide's window-position easing, shared by horizontal and vertical views.
//!
//! Adapted from Neovide's renderer/animation_utils.rs (MIT). Its rendered_window
//! uses this curve with a default position_animation_length of 150 ms.
use std::time::{Duration, Instant};
pub const DURATION: Duration = Duration::from_millis(150);
/// Neovide's exact exponential ease-out, including its exact final endpoint.
pub fn ease_out_expo(t: f32) -> f32 {
if (t - 1.0).abs() < f32::EPSILON {
1.0
} else {
1.0 - 2.0f32.powf(-10.0 * t)
}
}
/// Direction is a unit vector: [1, 0] enters from the right; [0, -1] from above.
pub struct Slide {
started: Instant,
direction: [f32; 2],
}
impl Slide {
pub fn new(direction: [f32; 2], now: Instant) -> Self {
Self {
started: now,
direction,
}
}
pub fn finished(&self, now: Instant) -> bool {
now.saturating_duration_since(self.started) >= DURATION
}
/// Incoming and outgoing offsets expressed as fractions of the viewport.
pub fn offsets(&self, now: Instant) -> ([f32; 2], [f32; 2]) {
let t = (now.saturating_duration_since(self.started).as_secs_f32()
/ DURATION.as_secs_f32())
.clamp(0., 1.);
let progress = ease_out_expo(t);
(
self.direction.map(|axis| axis * (1. - progress)),
self.direction.map(|axis| -axis * progress),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn neovide_curve_is_monotonic_and_finishes_exactly() {
assert_eq!(ease_out_expo(0.), 0.);
assert_eq!(ease_out_expo(0.5), 0.96875);
assert_eq!(ease_out_expo(1.), 1.);
for step in 1..=100 {
assert!(ease_out_expo(step as f32 / 100.) >= ease_out_expo((step - 1) as f32 / 100.));
}
}
#[test]
fn horizontal_and_vertical_scenes_share_a_boundary_and_stop() {
let now = Instant::now();
for direction in [[1., 0.], [-1., 0.], [0., 1.], [0., -1.]] {
let slide = Slide::new(direction, now);
assert_eq!(slide.offsets(now), (direction, [0., 0.]));
let (incoming, outgoing) = slide.offsets(now + DURATION / 2);
for axis in 0..2 {
assert_eq!(incoming[axis] - outgoing[axis], direction[axis]);
}
assert_eq!(slide.offsets(now + DURATION).0, [0., 0.]);
assert!(slide.finished(now + DURATION));
assert_eq!(
slide.offsets(now + DURATION * 2),
slide.offsets(now + DURATION)
);
}
}
}