diff --git a/src/tui/event.rs b/src/tui/event.rs index 3c8d12a..68e4e49 100644 --- a/src/tui/event.rs +++ b/src/tui/event.rs @@ -105,9 +105,6 @@ impl App { let input_changed = self.handle_events().wrap_err("handle events failed")?; let overlay_count = self.overlay_count(); let overlay_closed = overlay_count < drawn_overlay_count; - let overlay_repainted = - (input_changed || redraw_requested || overlay_count != drawn_overlay_count) - && (overlay_count > 0 || drawn_overlay_count > 0); let continuously_animated = spinner_active || error_buffer::has_errors(); let safety_refresh = last_draw.elapsed() >= Duration::from_secs(1); if input_changed @@ -120,11 +117,7 @@ impl App { terminal.draw(|frame| { self.render_frame(frame); image_skips = terminal_image_skips(frame.buffer_mut()); - if terminal_images_need_redraw( - &drawn_image_skips, - &image_skips, - overlay_repainted, - ) { + if terminal_image_cells_changed(&drawn_image_skips, &image_skips) { image_redraw_marker = !image_redraw_marker; } mark_terminal_images(frame.buffer_mut(), image_redraw_marker); @@ -657,14 +650,6 @@ fn terminal_image_cells_changed(previous: &[bool], current: &[bool]) -> bool { !previous.is_empty() && previous != current } -fn terminal_images_need_redraw( - previous: &[bool], - current: &[bool], - overlay_repainted: bool, -) -> bool { - overlay_repainted || terminal_image_cells_changed(previous, current) -} - fn editor_runs_in_terminal(editor: &str) -> bool { let editor_name = std::path::Path::new(editor) .file_stem() diff --git a/src/tui/tests/event.rs b/src/tui/tests/event.rs index 532cce1..b9e3b71 100644 --- a/src/tui/tests/event.rs +++ b/src/tui/tests/event.rs @@ -100,13 +100,8 @@ fn terminal_image_cells_change_when_an_overlay_opens_or_closes() { &[true, false, false] )); assert!(!terminal_image_cells_changed(&[], &[true])); -} - -#[test] -fn repainting_an_overlay_redraws_images_even_when_its_area_is_unchanged() { - assert!(terminal_images_need_redraw( + assert!(!terminal_image_cells_changed( &[true, false, true], &[true, false, true], - true, )); } diff --git a/src/tui/tests/widgets/popups/base.rs b/src/tui/tests/widgets/popups/base.rs new file mode 100644 index 0000000..860e942 --- /dev/null +++ b/src/tui/tests/widgets/popups/base.rs @@ -0,0 +1,27 @@ +use std::num::NonZeroU16; + +use super::*; + +#[test] +fn popup_cells_covering_terminal_images_are_always_redrawn() { + let area = Rect::new(0, 0, 4, 3); + let mut buffer = Buffer::empty(area); + buffer[(1, 1)].set_diff_option(CellDiffOption::Skip); + buffer[(2, 1)] + .set_symbol("\x1b_Gimage\x1b\\") + .set_diff_option(CellDiffOption::ForcedWidth(NonZeroU16::new(1).unwrap())); + let popup = PopupFrame { + title: Line::from("Popup"), + border_color: Color::White, + bg: Some(Color::Black), + keybinds: None, + search_line: None, + content: Box::new(|_, _| {}), + }; + + popup.render(area, &mut buffer); + + assert_eq!(buffer[(1, 1)].diff_option, CellDiffOption::AlwaysUpdate); + assert_eq!(buffer[(2, 1)].diff_option, CellDiffOption::AlwaysUpdate); + assert_eq!(buffer[(0, 1)].diff_option, CellDiffOption::None); +} diff --git a/src/tui/widgets/popups/base.rs b/src/tui/widgets/popups/base.rs index bb9f044..4c95563 100644 --- a/src/tui/widgets/popups/base.rs +++ b/src/tui/widgets/popups/base.rs @@ -3,7 +3,7 @@ // so each popup type only worries about its inner area. use ratatui::{ - buffer::Buffer, + buffer::{Buffer, CellDiffOption}, layout::{Alignment, Rect}, style::{Color, Style}, text::{Line, Span}, @@ -25,6 +25,18 @@ pub struct PopupFrame<'a> { impl<'a> Widget for PopupFrame<'a> { fn render(self, area: Rect, buf: &mut Buffer) { + let covered_image_cells = area + .rows() + .flat_map(|row| row.columns()) + .filter(|position| { + buf.cell(*position).is_some_and(|cell| { + matches!(cell.diff_option, CellDiffOption::Skip) + || matches!(cell.diff_option, CellDiffOption::ForcedWidth(_)) + && cell.symbol().contains('\x1b') + }) + }) + .collect::>(); + // clear first so the popup doesn't layer on top of whatever was underneath Clear.render(area, buf); @@ -48,6 +60,19 @@ impl<'a> Widget for PopupFrame<'a> { let inner = block.inner(area); block.render(area, buf); (self.content)(inner, buf); + + // External image protocols can repaint unchanged terminal cells. Ratatui's + // AlwaysUpdate option keeps only the popup cells covering an image above it. + for position in covered_image_cells { + if let Some(cell) = buf.cell_mut(position) + && !matches!( + cell.diff_option, + CellDiffOption::Skip | CellDiffOption::ForcedWidth(_) + ) + { + cell.set_diff_option(CellDiffOption::AlwaysUpdate); + } + } } } @@ -65,3 +90,7 @@ pub fn render_summary(rows: &[(&str, &str)], area: Rect, buf: &mut Buffer) { .collect::>(); Paragraph::new(lines).render(area, buf); } + +#[cfg(test)] +#[path = "../../tests/widgets/popups/base.rs"] +mod tests;