Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
terminal: Fix terminal jitter on resize (#56715)
Bing Wangdf377660607f
diff --git a/crates/terminal/src/alacritty.rs b/crates/terminal/src/alacritty.rsindex 5766d8b8fde..6a8e9d0dd2f 100644--- a/crates/terminal/src/alacritty.rs+++ b/crates/terminal/src/alacritty.rs@@ -821,6 +821,14 @@ pub(super) fn make_content(term: &Term<ZedListener>, last_content: &Content) ->None};+ let bottom_line = term.screen_lines() as i32 - 1 - content.display_offset as i32;+ let bottom_row_occupied = content.cursor.point.line.0 >= bottom_line+ || cells+ .iter()+ .rev()+ .take_while(|cell| cell.point.line >= bottom_line)+ .any(|cell| cell.cell.character() != ' ');+Content {cells,mode: terminal_modes_from_alacritty(content.mode),@@ -835,6 +843,7 @@ pub(super) fn make_content(term: &Term<ZedListener>, last_content: &Content) ->last_hovered_word: last_content.last_hovered_word.clone(),scrolled_to_top: content.display_offset == term.history_size(),scrolled_to_bottom: content.display_offset == 0,+ bottom_row_occupied,}}diff --git a/crates/terminal/src/terminal.rs b/crates/terminal/src/terminal.rsindex 796593fe45d..8d0521d3975 100644--- a/crates/terminal/src/terminal.rs+++ b/crates/terminal/src/terminal.rs@@ -498,6 +498,7 @@ pub struct Content {pub last_hovered_word: Option<HoveredWord>,pub scrolled_to_top: bool,pub scrolled_to_bottom: bool,+ pub bottom_row_occupied: bool,}#[derive(Debug, Clone, Eq, PartialEq)]@@ -524,6 +525,7 @@ impl Default for Content {last_hovered_word: None,scrolled_to_top: false,scrolled_to_bottom: false,+ bottom_row_occupied: false,}}}diff --git a/crates/terminal_view/src/terminal_element.rs b/crates/terminal_view/src/terminal_element.rsindex b74d28593d0..84c5f986617 100644--- a/crates/terminal_view/src/terminal_element.rs+++ b/crates/terminal_view/src/terminal_element.rs@@ -1033,6 +1033,10 @@ impl Element for TerminalElement {origin.x += gutter;if matches!(self.terminal_view.read(cx).mode, TerminalMode::Standalone) {+ let should_anchor_to_bottom = {+ let content = self.terminal.read(cx).last_content();+ content.scrolled_to_bottom && content.bottom_row_occupied+ };let scale_factor = window.scale_factor();let line_height_pixels = px(line_height);let line_height_device_px = (f32::from(line_height_pixels) * scale_factor)@@ -1054,7 +1058,7 @@ impl Element for TerminalElement {let padding = px(padding_device_px as f32 / scale_factor.max(1.0));size.height = snapped_height;- if self.terminal.read(cx).scrolled_to_bottom() {+ if should_anchor_to_bottom {origin.y += padding;}}diff --git a/crates/terminal_view/src/terminal_view.rs b/crates/terminal_view/src/terminal_view.rsindex a323da81b50..1e1c4b78190 100644--- a/crates/terminal_view/src/terminal_view.rs+++ b/crates/terminal_view/src/terminal_view.rs@@ -3053,6 +3053,72 @@ mod tests {});}+ async fn draw_standalone_terminal(+ output: &[u8],+ cx: &mut TestAppContext,+ ) -> (gpui::Bounds<Pixels>, gpui::Size<Pixels>) {+ let (project, workspace) = init_test(cx).await;+ let terminal = cx.new(|cx| {+ terminal::TerminalBuilder::new_display_only(+ CursorShape::default(),+ terminal::terminal_settings::AlternateScroll::On,+ None,+ 0,+ cx.background_executor(),+ PathStyle::local(),+ )+ .subscribe(cx)+ });+ terminal.update(cx, |terminal, cx| {+ terminal.write_output(output, cx);+ });++ let (terminal_view, cx) = cx.add_window_view(|window, cx| {+ TerminalView::new(+ terminal.clone(),+ workspace.downgrade(),+ None,+ project.downgrade(),+ window,+ cx,+ )+ });++ let draw_size = gpui::size(px(400.), px(201.));+ cx.simulate_resize(draw_size);+ cx.draw(gpui::Point::default(), draw_size, |_, _| {+ terminal_view.clone().into_any_element()+ });+ cx.run_until_parked();+ cx.draw(gpui::Point::default(), draw_size, |_, _| {+ terminal_view.clone().into_any_element()+ });++ let bounds = terminal.read_with(cx, |terminal, _| {+ terminal.last_content().terminal_bounds.bounds+ });+ (bounds, draw_size)+ }++ #[gpui::test]+ async fn test_short_standalone_terminal_stays_top_anchored_on_resize(cx: &mut TestAppContext) {+ let (bounds, _) = draw_standalone_terminal(b"$ ", cx).await;+ assert_eq!(bounds.origin.y, px(0.));+ }++ #[gpui::test]+ async fn test_full_standalone_terminal_stays_bottom_anchored_on_resize(+ cx: &mut TestAppContext,+ ) {+ let (bounds, draw_size) = draw_standalone_terminal(+ b"one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten\n",+ cx,+ )+ .await;+ assert!(bounds.origin.y > px(0.));+ assert_eq!(bounds.bottom(), draw_size.height);+ }+#[gpui::test]async fn test_tab_content_shows_terminal_title_when_custom_title_directly_set_empty(cx: &mut TestAppContext,