Animate tab and pane transitions with Neovide window easing
This commit is contained in:
+156
-28
@@ -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()),
|
||||
|
||||
Reference in New Issue
Block a user