fix(tui): keep popups above terminal images
This commit is contained in:
1 parent
6e898b8c45
commit
992f15b082
4 files changed
+59
-23
No files matched your search
+1
-16
@@ -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()
|
||||
|
||||
@@ -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,
|
||||
));
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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::<Vec<_>>();
|
||||
|
||||
// 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::<Vec<_>>();
|
||||
Paragraph::new(lines).render(area, buf);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../../tests/widgets/popups/base.rs"]
|
||||
mod tests;
|
||||
Reference in new issue
Block a user