Files
codex/codex-rs/tui/src/footer_selection_tests.rs
T
Eric Traut 988e85bd55 Honor tmux mouse changes while the TUI is in alternate screen (#63195)
## Why

tmux doesn't notify panes when its `mouse` option changes. Codex read
the option on screen entry, so toggling it while an alternate screen
was open could leave mouse capture out of step with the tmux setting.

## What changed

Poll the containing tmux session's `mouse` option in the background
while the alternate screen is active. Redraw on changes, even when the
TUI is idle, and reapply mouse capture for the current screen, including
nested screens such as `/resume`.

When tmux turns mouse capture off, discard queued mouse events and end
active drags and pending clicks while preserving selected text.

## Testing

Cover idle redraws on mouse changes, stale and failed probes, capture
updates across fullscreen and overlay modes, and late releases after
capture ends.

GitOrigin-RevId: a502a5e78bbc887adfe080ba2da4dbe8910f2420
2026-11-22 21:20:40 +00:00

182 lines
7.2 KiB
Rust

//! Exercise the rendered-cell adapter; gesture and clipboard state machines have shared tests.
use super::*;
use crossterm::event::KeyCode;
use crossterm::event::KeyEvent;
use crossterm::event::KeyModifiers;
use crossterm::event::MouseButton::Left;
use crossterm::event::MouseEventKind::Down;
use crossterm::event::MouseEventKind::Drag;
use crossterm::event::MouseEventKind::Up;
use pretty_assertions::assert_eq;
use ratatui::style::Stylize;
use ratatui::widgets::Widget;
fn mouse(kind: MouseEventKind, column: u16, row: u16) -> TuiEvent {
TuiEvent::Mouse(MouseEvent {
kind,
column,
row,
modifiers: KeyModifiers::NONE,
})
}
#[test]
fn rendered_selection_preserves_unicode_links_and_the_selected_revision() {
let area = Rect::new(
/*x*/ 0, /*y*/ 2, /*width*/ 30, /*height*/ 1,
);
let mut buffer = Buffer::empty(area);
Line::from(vec![
" ".into(),
"café 界 👩‍💻".cyan().underlined(),
" · 20%…".dim(),
])
.render(area, &mut buffer);
crate::terminal_hyperlinks::mark_underlined_hyperlink(&mut buffer, area, "https://example.com");
let original = buffer.clone();
let mut selection = RenderedSelection::default();
selection.areas.clear();
selection.register(area, /*invalidate*/ false);
selection.render(&mut buffer);
for event in [
mouse(Down(Left), /*column*/ 2, /*row*/ 2),
mouse(Drag(Left), /*column*/ 12, /*row*/ 2),
mouse(Up(Left), /*column*/ 12, /*row*/ 2),
] {
selection.handle_event(&event, &buffer);
}
assert_eq!(
selection.view.selected_text(&selection.cells).as_deref(),
Some("café 界 👩‍💻")
);
buffer.reset();
Line::from(" changed · 40%").render(area, &mut buffer);
selection.areas.clear();
selection.register(area, /*invalidate*/ false);
selection.render(&mut buffer);
for position in area.positions() {
assert_eq!(buffer[position].symbol(), original[position].symbol());
}
insta::assert_snapshot!(format!("{buffer:?}"));
// A new warning must replace the held revision even while the turn keeps running.
selection.register(area, /*invalidate*/ true);
assert!(!selection.view.has_selection_range());
}
#[test]
fn release_copy_and_cross_surface_drags_use_only_visible_text() {
let status = Rect::new(
/*x*/ 0, /*y*/ 0, /*width*/ 8, /*height*/ 1,
);
let footer = Rect { y: 1, ..status };
let mut buffer = Buffer::empty(status.union(footer));
"branch…".render(status, &mut buffer);
"? shortcuts".render(footer, &mut buffer);
let mut selection = RenderedSelection::default();
selection.view.copy_on_select = true;
selection.register(status, /*invalidate*/ false);
selection.register(footer, /*invalidate*/ false);
// Registration retains geometry only: select the final painted cells, not earlier content.
"commit…".render(status, &mut buffer);
let padding = (status.right() - 1, status.y);
buffer[padding].bg = ratatui::style::Color::DarkGray;
selection.handle_event(&mouse(Down(Left), /*column*/ 0, /*row*/ 0), &buffer);
selection.handle_event(&mouse(Drag(Left), /*column*/ 19, /*row*/ 1), &buffer);
let Some(RegionAction::Selection(ViewAction::CopyOnSelect(text))) =
selection.handle_event(&mouse(Up(Left), /*column*/ 19, /*row*/ 1), &buffer)
else {
panic!("release must copy the selected status row");
};
assert_eq!(text, "commit…");
selection.render(&mut buffer);
assert_eq!(buffer[padding].bg, ratatui::style::Color::DarkGray);
assert!(selection.view.has_selection_range());
// Collapsing the range keeps keyboard ownership, including Enter, with the footer.
for _ in text.chars() {
selection.handle_event(&TuiEvent::Key(KeyCode::Left.into()), &buffer);
}
assert!(!selection.view.has_selection_range());
assert!(matches!(
selection.handle_event(&TuiEvent::Key(KeyCode::Enter.into()), &buffer),
Some(RegionAction::Selection(ViewAction::Changed))
));
selection.handle_event(&TuiEvent::Key(KeyCode::Right.into()), &buffer);
assert_eq!(
selection.view.selected_text(&selection.cells).as_deref(),
Some("c")
);
// Invalidating another visible region must not release this selection.
selection.register(footer, /*invalidate*/ true);
assert!(selection.view.has_active_interaction());
// Find belongs to the app, not a hidden search inside the footer.
assert!(
selection
.handle_event(
&TuiEvent::Key(KeyEvent::new(KeyCode::Char('f'), KeyModifiers::CONTROL)),
&buffer
)
.is_none()
);
assert!(!selection.view.has_active_interaction());
// Resize invalidates both the highlight and its pointer anchor.
selection.handle_event(&mouse(Down(Left), /*column*/ 0, /*row*/ 1), &buffer);
selection.handle_event(&mouse(Drag(Left), /*column*/ 3, /*row*/ 1), &buffer);
selection.handle_event(
&TuiEvent::Resize(ratatui::layout::Size::new(
/*width*/ 10, /*height*/ 2,
)),
&buffer,
);
assert!(!selection.view.has_active_interaction());
}
#[test]
fn stationary_clicks_are_deferred_but_dragging_back_does_not_activate() {
let area = Rect::new(
/*x*/ 0, /*y*/ 0, /*width*/ 20, /*height*/ 1,
);
let mut buffer = Buffer::empty(area);
"⚠ 2 warnings".render(area, &mut buffer);
let mut selection = RenderedSelection::default();
selection.register(area, /*invalidate*/ false);
assert!(matches!(
selection.handle_event(&mouse(Down(Left), /*column*/ 2, /*row*/ 0), &buffer),
Some(RegionAction::Selection(_))
));
assert!(matches!(
selection.handle_event(&mouse(Up(Left), /*column*/ 2, /*row*/ 0), &buffer),
Some(RegionAction::Click(_))
));
selection.handle_event(&mouse(Down(Left), /*column*/ 5, /*row*/ 0), &buffer);
selection.handle_event(&mouse(Drag(Left), /*column*/ 7, /*row*/ 0), &buffer);
assert!(!matches!(
selection.handle_event(&mouse(Up(Left), /*column*/ 5, /*row*/ 0), &buffer),
Some(RegionAction::Click(_))
));
// Losing capture cancels a deferred click, even if its release arrives late.
selection.handle_event(&mouse(Down(Left), /*column*/ 8, /*row*/ 0), &buffer);
selection.end_drag();
assert!(
selection
.handle_event(&mouse(Up(Left), /*column*/ 8, /*row*/ 0), &buffer)
.is_none()
);
selection.handle_event(&mouse(Down(Left), /*column*/ 0, /*row*/ 0), &buffer);
selection.handle_event(&mouse(Drag(Left), /*column*/ 3, /*row*/ 0), &buffer);
assert!(selection.view.has_selection_range());
let selected = selection.view.selected_text(&selection.cells);
selection.end_drag();
selection.handle_event(&mouse(Up(Left), /*column*/ 9, /*row*/ 0), &buffer);
assert_eq!(selection.view.selected_text(&selection.cells), selected);
// A stationary click after a selection releases the frozen revision.
selection.handle_event(&mouse(Down(Left), /*column*/ 10, /*row*/ 0), &buffer);
assert!(matches!(
selection.handle_event(&mouse(Up(Left), /*column*/ 10, /*row*/ 0), &buffer),
Some(RegionAction::Click(_))
));
assert!(!selection.view.has_active_interaction());
}