Animate tab and pane transitions with Neovide window easing
This commit is contained in:
@@ -149,6 +149,10 @@ Huayra, Attractor, and Sybil. Tabs retain violet accents; pane headers use teal,
|
|||||||
with a matching border around the active pane. The titlebar contains equal-width
|
with a matching border around the active pane. The titlebar contains equal-width
|
||||||
tabs and connection/window controls. `src/chrome.rs` records the theme sources.
|
tabs and connection/window controls. `src/chrome.rs` records the theme sources.
|
||||||
|
|
||||||
|
Tab changes and new panes slide with Neovide's 150 ms exponential window-motion
|
||||||
|
curve. Outgoing content remains visible during the transition; only the incoming
|
||||||
|
view accepts input and controls the terminal size.
|
||||||
|
|
||||||
Cursor motion adapts Neovide's four independently sprung corners: leading
|
Cursor motion adapts Neovide's four independently sprung corners: leading
|
||||||
corners move first and trailing corners stretch behind. The blink follows
|
corners move first and trailing corners stretch behind. The blink follows
|
||||||
buzzlocker: 500 ms solid after input, 500 ms cubic fade-out, then an immediate
|
buzzlocker: 500 ms solid after input, 500 ms cubic fade-out, then an immediate
|
||||||
|
|||||||
@@ -27,3 +27,11 @@ reused at the author's explicit request:
|
|||||||
|
|
||||||
It holds at full opacity for 500 ms after input, fades out with a 500 ms cubic
|
It holds at full opacity for 500 ms after input, fades out with a 500 ms cubic
|
||||||
ease-out curve, then returns to full opacity for 500 ms. It does not fade in.
|
ease-out curve, then returns to full opacity for 500 ms. It does not fade in.
|
||||||
|
|
||||||
|
## Neovide view transitions
|
||||||
|
|
||||||
|
`src/transition.rs` adapts Neovide's `ease_out_expo` window-position curve
|
||||||
|
from `src/renderer/animation_utils.rs`, used by `rendered_window.rs` with
|
||||||
|
`position_animation_length = 0.15` in `renderer/mod.rs`. This is the same
|
||||||
|
Neovide revision and MIT license recorded above. Horizontal tab and pane
|
||||||
|
transitions use this curve; text scrolling has a separate spring in Neovide.
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ mod render;
|
|||||||
mod server;
|
mod server;
|
||||||
mod session;
|
mod session;
|
||||||
mod terminal;
|
mod terminal;
|
||||||
|
mod transition;
|
||||||
|
|
||||||
use anyhow::{Result, bail};
|
use anyhow::{Result, bail};
|
||||||
use gpui::{prelude::*, *};
|
use gpui::{prelude::*, *};
|
||||||
|
|||||||
+156
-28
@@ -12,6 +12,7 @@ use crate::{
|
|||||||
Snapshot,
|
Snapshot,
|
||||||
},
|
},
|
||||||
render::{FONT_SIZE, LINE_HEIGHT, RenderCell, Renderer},
|
render::{FONT_SIZE, LINE_HEIGHT, RenderCell, Renderer},
|
||||||
|
transition::Slide,
|
||||||
};
|
};
|
||||||
use alacritty_terminal::{
|
use alacritty_terminal::{
|
||||||
term::{TermMode, color::Colors},
|
term::{TermMode, color::Colors},
|
||||||
@@ -32,6 +33,9 @@ pub struct Workspace {
|
|||||||
snapshot: Option<Arc<Snapshot>>,
|
snapshot: Option<Arc<Snapshot>>,
|
||||||
panes: HashMap<u64, Entity<TerminalPane>>,
|
panes: HashMap<u64, Entity<TerminalPane>>,
|
||||||
error: Option<String>,
|
error: Option<String>,
|
||||||
|
outgoing: Vec<Entity<TerminalPane>>,
|
||||||
|
transition: Option<Slide>,
|
||||||
|
transition_frame_pending: bool,
|
||||||
_updates: Task<()>,
|
_updates: Task<()>,
|
||||||
}
|
}
|
||||||
impl Workspace {
|
impl Workspace {
|
||||||
@@ -54,6 +58,33 @@ impl Workspace {
|
|||||||
view.error = Some(error);
|
view.error = Some(error);
|
||||||
}
|
}
|
||||||
if let Some(snapshot) = update.snapshot {
|
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
|
let active = snapshot
|
||||||
.tabs
|
.tabs
|
||||||
.iter()
|
.iter()
|
||||||
@@ -87,14 +118,16 @@ impl Workspace {
|
|||||||
let client = view.client.clone();
|
let client = view.client.clone();
|
||||||
let frame = snapshot.clone();
|
let frame = snapshot.clone();
|
||||||
let pane = cx.new(|cx| {
|
let pane = cx.new(|cx| {
|
||||||
TerminalPane::new(
|
let mut pane = TerminalPane::new(
|
||||||
client,
|
client,
|
||||||
frame,
|
frame,
|
||||||
index,
|
index,
|
||||||
active == Some(data.id),
|
active == Some(data.id),
|
||||||
window,
|
window,
|
||||||
cx,
|
cx,
|
||||||
)
|
);
|
||||||
|
pane.transitioning = view.transition.is_some();
|
||||||
|
pane
|
||||||
});
|
});
|
||||||
view.panes.insert(data.id, pane);
|
view.panes.insert(data.id, pane);
|
||||||
}
|
}
|
||||||
@@ -124,12 +157,37 @@ impl Workspace {
|
|||||||
snapshot: None,
|
snapshot: None,
|
||||||
panes: HashMap::new(),
|
panes: HashMap::new(),
|
||||||
error: None,
|
error: None,
|
||||||
|
outgoing: Vec::new(),
|
||||||
|
transition: None,
|
||||||
|
transition_frame_pending: false,
|
||||||
_updates: task,
|
_updates: task,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Render for Workspace {
|
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
|
let tabs = self
|
||||||
.snapshot
|
.snapshot
|
||||||
.as_ref()
|
.as_ref()
|
||||||
@@ -157,21 +215,29 @@ impl Render for Workspace {
|
|||||||
},
|
},
|
||||||
move |window, _| window.remove_window(),
|
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()
|
let mut body = div()
|
||||||
.flex()
|
.relative()
|
||||||
.flex_1()
|
.flex_1()
|
||||||
.min_h_0()
|
.min_h_0()
|
||||||
.gap(px(6.))
|
.overflow_hidden()
|
||||||
.p(px(chrome::CONTENT_INSET))
|
|
||||||
.bg(rgb(chrome::APP_BACKGROUND));
|
.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 {
|
if let Some(snapshot) = &self.snapshot {
|
||||||
for data in &snapshot.panes {
|
for data in &snapshot.panes {
|
||||||
if let Some(pane) = self.panes.get(&data.id) {
|
if let Some(pane) = self.panes.get(&data.id) {
|
||||||
body = body.child(pane.clone());
|
incoming = incoming.child(pane.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if snapshot.panes.is_empty() {
|
if snapshot.panes.is_empty() {
|
||||||
body = body.child(
|
incoming = incoming.child(
|
||||||
div()
|
div()
|
||||||
.p_4()
|
.p_4()
|
||||||
.text_color(rgb(chrome::MUTED))
|
.text_color(rgb(chrome::MUTED))
|
||||||
@@ -179,13 +245,14 @@ impl Render for Workspace {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
body = body.child(
|
incoming = incoming.child(
|
||||||
div()
|
div()
|
||||||
.p_4()
|
.p_4()
|
||||||
.text_color(rgb(chrome::MUTED))
|
.text_color(rgb(chrome::MUTED))
|
||||||
.child("Connecting…"),
|
.child("Connecting…"),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
body = body.child(incoming);
|
||||||
let mut root = div()
|
let mut root = div()
|
||||||
.size_full()
|
.size_full()
|
||||||
.flex()
|
.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 {
|
struct TerminalPane {
|
||||||
client: Client,
|
client: Client,
|
||||||
snapshot: Arc<Snapshot>,
|
snapshot: Arc<Snapshot>,
|
||||||
index: usize,
|
index: usize,
|
||||||
active: bool,
|
active: bool,
|
||||||
|
// Frozen outgoing scenes are display-only; no PTY resize, focus or input.
|
||||||
|
read_only: bool,
|
||||||
|
transitioning: bool,
|
||||||
focus: FocusHandle,
|
focus: FocusHandle,
|
||||||
font: Font,
|
font: Font,
|
||||||
bounds: Bounds<Pixels>,
|
bounds: Bounds<Pixels>,
|
||||||
@@ -260,6 +366,8 @@ impl TerminalPane {
|
|||||||
snapshot,
|
snapshot,
|
||||||
index,
|
index,
|
||||||
active,
|
active,
|
||||||
|
read_only: false,
|
||||||
|
transitioning: false,
|
||||||
focus,
|
focus,
|
||||||
font,
|
font,
|
||||||
bounds: Bounds::default(),
|
bounds: Bounds::default(),
|
||||||
@@ -282,6 +390,9 @@ impl TerminalPane {
|
|||||||
&self.snapshot.panes[self.index]
|
&self.snapshot.panes[self.index]
|
||||||
}
|
}
|
||||||
fn command(&self, command: ClientMessage) {
|
fn command(&self, command: ClientMessage) {
|
||||||
|
if self.read_only {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if let Err(error) = self.client.send(command) {
|
if let Err(error) = self.client.send(command) {
|
||||||
log::warn!("{error:#}");
|
log::warn!("{error:#}");
|
||||||
}
|
}
|
||||||
@@ -295,7 +406,7 @@ impl TerminalPane {
|
|||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
fn send(&mut self, bytes: Vec<u8>, cx: &mut Context<Self>) {
|
fn send(&mut self, bytes: Vec<u8>, cx: &mut Context<Self>) {
|
||||||
if bytes.is_empty() {
|
if self.read_only || bytes.is_empty() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.preedit.clear();
|
self.preedit.clear();
|
||||||
@@ -356,6 +467,9 @@ impl TerminalPane {
|
|||||||
MouseButton::Left,
|
MouseButton::Left,
|
||||||
cx.listener(|pane, _, window, cx| {
|
cx.listener(|pane, _, window, cx| {
|
||||||
cx.stop_propagation();
|
cx.stop_propagation();
|
||||||
|
if pane.read_only {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if pane.rename.is_none() {
|
if pane.rename.is_none() {
|
||||||
window.focus(&pane.focus);
|
window.focus(&pane.focus);
|
||||||
}
|
}
|
||||||
@@ -387,7 +501,7 @@ impl TerminalPane {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn key_down(&mut self, event: &KeyDownEvent, window: &mut Window, cx: &mut Context<Self>) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
let key = &event.keystroke;
|
let key = &event.keystroke;
|
||||||
@@ -497,6 +611,9 @@ impl TerminalPane {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
fn mouse_down(&mut self, event: &MouseDownEvent, window: &mut Window, cx: &mut Context<Self>) {
|
fn mouse_down(&mut self, event: &MouseDownEvent, window: &mut Window, cx: &mut Context<Self>) {
|
||||||
|
if self.read_only {
|
||||||
|
return;
|
||||||
|
}
|
||||||
window.focus(&self.focus);
|
window.focus(&self.focus);
|
||||||
self.activity(cx);
|
self.activity(cx);
|
||||||
let pane = self.data().id;
|
let pane = self.data().id;
|
||||||
@@ -528,6 +645,9 @@ impl TerminalPane {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn scroll(&mut self, event: &ScrollWheelEvent, _: &mut Window, cx: &mut Context<Self>) {
|
fn scroll(&mut self, event: &ScrollWheelEvent, _: &mut Window, cx: &mut Context<Self>) {
|
||||||
|
if self.read_only {
|
||||||
|
return;
|
||||||
|
}
|
||||||
self.scroll_remainder += match event.delta {
|
self.scroll_remainder += match event.delta {
|
||||||
ScrollDelta::Lines(d) => d.y * 3.,
|
ScrollDelta::Lines(d) => d.y * 3.,
|
||||||
ScrollDelta::Pixels(d) => f32::from(d.y) / LINE_HEIGHT,
|
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)
|
let lines = ((f32::from(bounds.size.height) - 2. * PADDING) / LINE_HEIGHT)
|
||||||
.floor()
|
.floor()
|
||||||
.clamp(1., 500.) as u16;
|
.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.requested_size = Some((columns, lines));
|
||||||
self.command(ClientMessage::Resize {
|
self.command(ClientMessage::Resize {
|
||||||
pane: self.data().id,
|
pane: self.data().id,
|
||||||
@@ -572,7 +692,9 @@ impl TerminalPane {
|
|||||||
cell_height: LINE_HEIGHT as u16,
|
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;
|
return;
|
||||||
}
|
}
|
||||||
let started = Instant::now();
|
let started = Instant::now();
|
||||||
@@ -604,6 +726,23 @@ impl TerminalPane {
|
|||||||
px(LINE_HEIGHT),
|
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
|
let rows = data
|
||||||
.cells
|
.cells
|
||||||
.chunks(usize::from(data.columns))
|
.chunks(usize::from(data.columns))
|
||||||
@@ -628,19 +767,6 @@ impl TerminalPane {
|
|||||||
.collect()
|
.collect()
|
||||||
})
|
})
|
||||||
.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
|
self.renderer
|
||||||
.prepare(rows, &self.font, self.cell_width, window);
|
.prepare(rows, &self.font, self.cell_width, window);
|
||||||
self.prepared_revision = Some(self.snapshot.revision);
|
self.prepared_revision = Some(self.snapshot.revision);
|
||||||
@@ -691,7 +817,7 @@ impl Render for TerminalPane {
|
|||||||
move |bounds,(),window,cx| {
|
move |bounds,(),window,cx| {
|
||||||
paint.update(cx,|pane,cx| {
|
paint.update(cx,|pane,cx| {
|
||||||
let started=Instant::now();
|
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);
|
window.handle_input(&pane.focus,ElementInputHandler::new(bounds,paint.clone()),cx);
|
||||||
}
|
}
|
||||||
let mut delay=None;
|
let mut delay=None;
|
||||||
@@ -699,7 +825,9 @@ impl Render for TerminalPane {
|
|||||||
let origin=bounds.origin+point(px(PADDING),px(PADDING));
|
let origin=bounds.origin+point(px(PADDING),px(PADDING));
|
||||||
let focused=window.is_window_active() && pane.active && pane.rename.is_none();
|
let focused=window.is_window_active() && pane.active && pane.rename.is_none();
|
||||||
pane.renderer.paint_with_cursor(origin,window,cx,|window| {
|
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() {
|
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()),
|
let run=TextRun{len:pane.preedit.len(),font:pane.font.clone(),color:rgb(chrome::TEXT).into(),background_color:Some(rgb(chrome::TERMINAL_BACKGROUND).into()),
|
||||||
|
|||||||
@@ -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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user