feat(tui): preserve browser takeover input

This commit is contained in:
Felipe Coury
2026-07-06 10:24:40 -03:00
parent 6dcc65fba1
commit 4cb937a1e7
12 changed files with 212 additions and 53 deletions
@@ -61,22 +61,26 @@ fn drag_lifecycle_clamps_and_preserves_the_preferred_ratio() {
kind: MousePrimaryEventKind::Drag,
column: 80,
row: 4,
modifiers: crossterm::event::KeyModifiers::NONE,
}));
assert!(!state.handle_mouse(MousePrimaryEvent {
kind: MousePrimaryEventKind::Press,
column: 20,
row: 4,
modifiers: crossterm::event::KeyModifiers::NONE,
}));
assert!(state.handle_mouse(MousePrimaryEvent {
kind: MousePrimaryEventKind::Press,
column: 70,
row: 4,
modifiers: crossterm::event::KeyModifiers::NONE,
}));
assert!(state.handle_mouse(MousePrimaryEvent {
kind: MousePrimaryEventKind::Drag,
column: 10,
row: 4,
modifiers: crossterm::event::KeyModifiers::NONE,
}));
assert_eq!(
split_widths(OwnedScreenLayout::new(
@@ -91,6 +95,7 @@ fn drag_lifecycle_clamps_and_preserves_the_preferred_ratio() {
kind: MousePrimaryEventKind::Release,
column: 70,
row: 4,
modifiers: crossterm::event::KeyModifiers::NONE,
}));
assert_eq!(state.preference(), initial_preference);
@@ -98,16 +103,19 @@ fn drag_lifecycle_clamps_and_preserves_the_preferred_ratio() {
kind: MousePrimaryEventKind::Press,
column: 70,
row: 4,
modifiers: crossterm::event::KeyModifiers::NONE,
}));
assert!(state.handle_mouse(MousePrimaryEvent {
kind: MousePrimaryEventKind::Drag,
column: 80,
row: 4,
modifiers: crossterm::event::KeyModifiers::NONE,
}));
assert!(state.handle_mouse(MousePrimaryEvent {
kind: MousePrimaryEventKind::Release,
column: 80,
row: u16::MAX,
modifiers: crossterm::event::KeyModifiers::NONE,
}));
assert_eq!(
split_widths(OwnedScreenLayout::new(
@@ -122,6 +130,7 @@ fn drag_lifecycle_clamps_and_preserves_the_preferred_ratio() {
kind: MousePrimaryEventKind::Drag,
column: 70,
row: 4,
modifiers: crossterm::event::KeyModifiers::NONE,
}));
let preference = state.preference();
@@ -140,11 +149,13 @@ fn drag_lifecycle_clamps_and_preserves_the_preferred_ratio() {
kind: MousePrimaryEventKind::Press,
column: 58,
row: 4,
modifiers: crossterm::event::KeyModifiers::NONE,
}));
assert!(state.handle_mouse(MousePrimaryEvent {
kind: MousePrimaryEventKind::Release,
column: 58,
row: 4,
modifiers: crossterm::event::KeyModifiers::NONE,
}));
assert_eq!(state.preference(), preference);
@@ -152,6 +163,7 @@ fn drag_lifecycle_clamps_and_preserves_the_preferred_ratio() {
kind: MousePrimaryEventKind::Press,
column: 58,
row: 4,
modifiers: crossterm::event::KeyModifiers::NONE,
}));
let narrow = Rect::new(
/*x*/ 10, /*y*/ 2, /*width*/ 82, /*height*/ 20,
@@ -190,5 +202,6 @@ fn drag_lifecycle_clamps_and_preserves_the_preferred_ratio() {
kind: MousePrimaryEventKind::Press,
column: 51,
row: 4,
modifiers: crossterm::event::KeyModifiers::NONE,
}));
}
+14 -1
View File
@@ -188,7 +188,12 @@ fn is_following_bottom(app: &App, slot: PaneSlot) -> bool {
}
fn primary_event(kind: MousePrimaryEventKind, column: u16, row: u16) -> MousePrimaryEvent {
MousePrimaryEvent { kind, column, row }
MousePrimaryEvent {
kind,
column,
row,
modifiers: KeyModifiers::NONE,
}
}
fn primary_press(column: u16, row: u16) -> MousePrimaryEvent {
@@ -1495,6 +1500,7 @@ async fn mouse_wheel_routes_by_pointer_without_changing_focus() {
direction: MouseScrollDirection::Up,
column: 2,
row: 2,
modifiers: KeyModifiers::NONE,
},
));
assert_eq!(app.chat_widget.focused_slot(), PaneSlot::Parent);
@@ -1507,6 +1513,7 @@ async fn mouse_wheel_routes_by_pointer_without_changing_focus() {
direction: MouseScrollDirection::Up,
column: 44,
row: 2,
modifiers: KeyModifiers::NONE,
},
));
assert_eq!(app.chat_widget.focused_slot(), PaneSlot::Parent);
@@ -1556,6 +1563,7 @@ async fn mouse_wheel_routes_to_the_pane_with_an_active_selection() {
direction: MouseScrollDirection::Up,
column: side_area.x,
row: side_area.y,
modifiers: KeyModifiers::NONE,
},
));
@@ -1594,6 +1602,7 @@ async fn narrow_side_clears_parent_hit_area_before_wheel_routing() {
direction: MouseScrollDirection::Up,
column: side_area.x,
row: side_area.y,
modifiers: KeyModifiers::NONE,
},
));
assert!(is_following_bottom(&app, PaneSlot::Parent));
@@ -1618,6 +1627,7 @@ async fn resizing_between_split_and_focused_only_preserves_pane_state() {
direction: MouseScrollDirection::Up,
column: 2,
row: 2,
modifiers: KeyModifiers::NONE,
}));
assert!(app.chat_widget.focus(PaneSlot::Side));
@@ -1801,6 +1811,7 @@ async fn mouse_wheel_scrolls_transcript_without_changing_draft() {
direction: MouseScrollDirection::Up,
column: 2,
row: 2,
modifiers: KeyModifiers::NONE,
}));
terminal
.draw(|frame| {
@@ -1823,12 +1834,14 @@ async fn mouse_wheel_scrolls_transcript_without_changing_draft() {
direction: MouseScrollDirection::Up,
column: 2,
row: 7,
modifiers: KeyModifiers::NONE,
}));
assert!(screen.handle_mouse_scroll(MouseScrollEvent {
direction: MouseScrollDirection::Down,
column: 2,
row: 2,
modifiers: KeyModifiers::NONE,
}));
terminal
.draw(|frame| {
+1
View File
@@ -99,6 +99,7 @@ pub(crate) async fn run_cwd_selection_prompt(
TuiEvent::Paste(_)
| TuiEvent::MouseScroll(_)
| TuiEvent::MousePrimary(_)
| TuiEvent::MouseOther(_)
| TuiEvent::FocusLost => {}
TuiEvent::Draw | TuiEvent::Resize => {
tui.draw(u16::MAX, |frame| {
@@ -102,6 +102,7 @@ pub(crate) async fn run_external_agent_config_migration_prompt(
TuiEvent::Paste(_)
| TuiEvent::MouseScroll(_)
| TuiEvent::MousePrimary(_)
| TuiEvent::MouseOther(_)
| TuiEvent::FocusLost => {}
TuiEvent::Draw | TuiEvent::Resize => {
let _ = tui.draw(u16::MAX, |frame| {
+1
View File
@@ -155,6 +155,7 @@ pub(crate) async fn run_model_migration_prompt(
TuiEvent::Paste(_)
| TuiEvent::MouseScroll(_)
| TuiEvent::MousePrimary(_)
| TuiEvent::MouseOther(_)
| TuiEvent::FocusLost => {}
TuiEvent::Draw | TuiEvent::Resize => {
let _ = alt.tui.draw(u16::MAX, |frame| {
@@ -509,7 +509,10 @@ pub(crate) async fn run_onboarding_app(
TuiEvent::Paste(text) => {
onboarding_screen.handle_paste(text);
}
TuiEvent::MouseScroll(_) | TuiEvent::MousePrimary(_) | TuiEvent::FocusLost => {}
TuiEvent::MouseScroll(_)
| TuiEvent::MousePrimary(_)
| TuiEvent::MouseOther(_)
| TuiEvent::FocusLost => {}
TuiEvent::Draw | TuiEvent::Resize => {
if !did_full_clear_after_success
&& onboarding_screen.steps.iter().any(|step| {
+4 -1
View File
@@ -475,7 +475,10 @@ async fn run_session_picker_with_loader(
TuiEvent::Paste(pasted) => {
state.handle_paste(pasted);
}
TuiEvent::MouseScroll(_) | TuiEvent::MousePrimary(_) | TuiEvent::FocusLost => {}
TuiEvent::MouseScroll(_)
| TuiEvent::MousePrimary(_)
| TuiEvent::MouseOther(_)
| TuiEvent::FocusLost => {}
TuiEvent::Draw | TuiEvent::Resize => {
if let Ok(size) = alt.tui.terminal.size() {
let list_height =
+1
View File
@@ -169,6 +169,7 @@ async fn run_startup_hooks_review_app(
TuiEvent::Paste(_)
| TuiEvent::MouseScroll(_)
| TuiEvent::MousePrimary(_)
| TuiEvent::MouseOther(_)
| TuiEvent::FocusLost => {}
TuiEvent::Draw | TuiEvent::Resize => draw_view(tui, &view)?,
}
+36 -9
View File
@@ -22,15 +22,7 @@ pub(crate) fn browser_key_input(event: KeyEvent) -> Option<BrowserKeyInput> {
let modifiers = browser_modifiers(event.modifiers);
let (key, code, text) = match event.code {
KeyCode::Char(character) => {
let code = if character.is_ascii_alphabetic() {
format!("Key{}", character.to_ascii_uppercase())
} else if character.is_ascii_digit() {
format!("Digit{character}")
} else if character == ' ' {
"Space".to_string()
} else {
character.to_string()
};
let code = browser_key_code(character);
let text = (!modifiers.control && !modifiers.alt && !modifiers.meta)
.then(|| character.to_string());
(character.to_string(), code, text)
@@ -70,6 +62,41 @@ pub(crate) fn browser_key_input(event: KeyEvent) -> Option<BrowserKeyInput> {
})
}
fn browser_key_code(character: char) -> String {
if character.is_ascii_alphabetic() {
return format!("Key{}", character.to_ascii_uppercase());
}
if character.is_ascii_digit() {
return format!("Digit{character}");
}
let code = match character {
' ' => "Space",
'!' => "Digit1",
'@' => "Digit2",
'#' => "Digit3",
'$' => "Digit4",
'%' => "Digit5",
'^' => "Digit6",
'&' => "Digit7",
'*' => "Digit8",
'(' => "Digit9",
')' => "Digit0",
'-' | '_' => "Minus",
'=' | '+' => "Equal",
'[' | '{' => "BracketLeft",
']' | '}' => "BracketRight",
'\\' | '|' => "Backslash",
';' | ':' => "Semicolon",
'\'' | '"' => "Quote",
',' | '<' => "Comma",
'.' | '>' => "Period",
'/' | '?' => "Slash",
'`' | '~' => "Backquote",
_ => return character.to_string(),
};
code.to_string()
}
/// Converts a crossterm mouse event into coordinates relative to `viewport`.
///
/// Events outside the browser viewport are ignored so surrounding panel chrome retains ownership
+52
View File
@@ -21,6 +21,10 @@ use crossterm::event::DisableFocusChange;
use crossterm::event::EnableBracketedPaste;
use crossterm::event::EnableFocusChange;
use crossterm::event::KeyEvent;
use crossterm::event::KeyModifiers;
use crossterm::event::MouseButton;
use crossterm::event::MouseEvent;
use crossterm::event::MouseEventKind;
#[cfg(not(unix))]
use crossterm::terminal::supports_keyboard_enhancement;
use ratatui::backend::Backend;
@@ -521,6 +525,10 @@ pub enum TuiEvent {
MouseScroll(MouseScrollEvent),
/// A primary mouse-button lifecycle event with terminal coordinates.
MousePrimary(MousePrimaryEvent),
/// A mouse event not represented by the conversation-focused primary and vertical-scroll
/// variants. Browser takeover uses this for pointer motion, secondary buttons, and horizontal
/// scrolling without broadening conversation input handling.
MouseOther(MouseEvent),
/// Notification that the terminal stopped receiving keyboard and mouse input.
FocusLost,
/// A terminal size notification that should be handled as resize-sensitive draw work.
@@ -537,6 +545,22 @@ pub struct MouseScrollEvent {
pub direction: MouseScrollDirection,
pub column: u16,
pub row: u16,
pub modifiers: KeyModifiers,
}
impl From<MouseScrollEvent> for MouseEvent {
fn from(event: MouseScrollEvent) -> Self {
let kind = match event.direction {
MouseScrollDirection::Up => MouseEventKind::ScrollUp,
MouseScrollDirection::Down => MouseEventKind::ScrollDown,
};
Self {
kind,
column: event.column,
row: event.row,
modifiers: event.modifiers,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@@ -544,6 +568,23 @@ pub struct MousePrimaryEvent {
pub kind: MousePrimaryEventKind,
pub column: u16,
pub row: u16,
pub modifiers: KeyModifiers,
}
impl From<MousePrimaryEvent> for MouseEvent {
fn from(event: MousePrimaryEvent) -> Self {
let kind = match event.kind {
MousePrimaryEventKind::Press => MouseEventKind::Down(MouseButton::Left),
MousePrimaryEventKind::Drag => MouseEventKind::Drag(MouseButton::Left),
MousePrimaryEventKind::Release => MouseEventKind::Up(MouseButton::Left),
};
Self {
kind,
column: event.column,
row: event.row,
modifiers: event.modifiers,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@@ -572,6 +613,8 @@ pub struct Tui {
suspend_context: SuspendContext,
// True when terminal/tab is focused; updated internally from crossterm events
terminal_focused: Arc<AtomicBool>,
// Browser takeover needs mouse events that the conversation UI intentionally ignores.
raw_mouse_events: Arc<AtomicBool>,
enhanced_keys_supported: bool,
notification_backend: Option<DesktopNotificationBackend>,
notification_condition: NotificationCondition,
@@ -624,6 +667,7 @@ impl Tui {
#[cfg(unix)]
suspend_context: SuspendContext::new(),
terminal_focused: Arc::new(AtomicBool::new(true)),
raw_mouse_events: Arc::new(AtomicBool::new(false)),
enhanced_keys_supported,
notification_backend: Some(detect_backend(NotificationMethod::default())),
notification_condition: NotificationCondition::default(),
@@ -659,6 +703,12 @@ impl Tui {
self.enhanced_keys_supported
}
/// Preserve pointer motion, secondary buttons, and horizontal scrolling for browser takeover.
pub(crate) fn set_raw_mouse_events(&self, raw_mouse_events: bool) {
self.raw_mouse_events
.store(raw_mouse_events, Ordering::Relaxed);
}
pub fn is_alt_screen_active(&self) -> bool {
self.screen_session.is_active()
}
@@ -753,6 +803,7 @@ impl Tui {
self.event_broker.clone(),
self.draw_tx.subscribe(),
self.terminal_focused.clone(),
self.raw_mouse_events.clone(),
self.suspend_context.clone(),
self.screen_session.clone(),
);
@@ -761,6 +812,7 @@ impl Tui {
self.event_broker.clone(),
self.draw_tx.subscribe(),
self.terminal_focused.clone(),
self.raw_mouse_events.clone(),
);
Box::pin(stream)
}
+84 -41
View File
@@ -150,6 +150,7 @@ pub struct TuiEventStream<S: EventSource + Default + Unpin = CrosstermEventSourc
draw_stream: BroadcastStream<()>,
resume_stream: WatchStream<()>,
terminal_focused: Arc<AtomicBool>,
raw_mouse_events: Arc<AtomicBool>,
poll_draw_first: bool,
#[cfg(unix)]
suspend_context: crate::tui::job_control::SuspendContext,
@@ -162,6 +163,7 @@ impl<S: EventSource + Default + Unpin> TuiEventStream<S> {
broker: Arc<EventBroker<S>>,
draw_rx: broadcast::Receiver<()>,
terminal_focused: Arc<AtomicBool>,
raw_mouse_events: Arc<AtomicBool>,
#[cfg(unix)] suspend_context: crate::tui::job_control::SuspendContext,
#[cfg(unix)] screen_session: ScreenSession,
) -> Self {
@@ -171,6 +173,7 @@ impl<S: EventSource + Default + Unpin> TuiEventStream<S> {
draw_stream: BroadcastStream::new(draw_rx),
resume_stream,
terminal_focused,
raw_mouse_events,
poll_draw_first: false,
#[cfg(unix)]
suspend_context,
@@ -271,17 +274,20 @@ impl<S: EventSource + Default + Unpin> TuiEventStream<S> {
direction: MouseScrollDirection::Up,
column: mouse_event.column,
row: mouse_event.row,
modifiers: mouse_event.modifiers,
})),
MouseEventKind::ScrollDown => Some(TuiEvent::MouseScroll(MouseScrollEvent {
direction: MouseScrollDirection::Down,
column: mouse_event.column,
row: mouse_event.row,
modifiers: mouse_event.modifiers,
})),
MouseEventKind::Down(crossterm::event::MouseButton::Left) => {
Some(TuiEvent::MousePrimary(MousePrimaryEvent {
kind: MousePrimaryEventKind::Press,
column: mouse_event.column,
row: mouse_event.row,
modifiers: mouse_event.modifiers,
}))
}
MouseEventKind::Drag(crossterm::event::MouseButton::Left) => {
@@ -289,6 +295,7 @@ impl<S: EventSource + Default + Unpin> TuiEventStream<S> {
kind: MousePrimaryEventKind::Drag,
column: mouse_event.column,
row: mouse_event.row,
modifiers: mouse_event.modifiers,
}))
}
MouseEventKind::Up(crossterm::event::MouseButton::Left) => {
@@ -296,6 +303,7 @@ impl<S: EventSource + Default + Unpin> TuiEventStream<S> {
kind: MousePrimaryEventKind::Release,
column: mouse_event.column,
row: mouse_event.row,
modifiers: mouse_event.modifiers,
}))
}
MouseEventKind::Down(_)
@@ -303,7 +311,10 @@ impl<S: EventSource + Default + Unpin> TuiEventStream<S> {
| MouseEventKind::Drag(_)
| MouseEventKind::Moved
| MouseEventKind::ScrollLeft
| MouseEventKind::ScrollRight => None,
| MouseEventKind::ScrollRight => self
.raw_mouse_events
.load(Ordering::Relaxed)
.then_some(TuiEvent::MouseOther(mouse_event)),
},
Event::FocusGained => {
self.terminal_focused.store(true, Ordering::Relaxed);
@@ -422,6 +433,7 @@ mod tests {
broker,
draw_rx,
terminal_focused,
Arc::new(AtomicBool::new(false)),
#[cfg(unix)]
crate::tui::job_control::SuspendContext::new(),
#[cfg(unix)]
@@ -543,6 +555,7 @@ mod tests {
kind: MousePrimaryEventKind::Press,
column: 3,
row: 4,
modifiers: KeyModifiers::NONE,
}))
));
assert!(matches!(
@@ -551,6 +564,7 @@ mod tests {
kind: MousePrimaryEventKind::Drag,
column: 5,
row: 6,
modifiers: KeyModifiers::NONE,
}))
));
assert!(matches!(
@@ -559,59 +573,86 @@ mod tests {
kind: MousePrimaryEventKind::Release,
column: 7,
row: 8,
modifiers: KeyModifiers::NONE,
}))
));
}
#[tokio::test(flavor = "current_thread")]
async fn vertical_mouse_wheel_maps_to_scroll_and_ignored_events_yield_to_draws() {
async fn non_conversation_mouse_events_are_preserved() {
let (broker, handle, _draw_tx, draw_rx, terminal_focused) = setup();
let raw_mouse_events = Arc::new(AtomicBool::new(true));
let mut stream = TuiEventStream::new(
broker,
draw_rx,
terminal_focused,
raw_mouse_events,
#[cfg(unix)]
crate::tui::job_control::SuspendContext::new(),
#[cfg(unix)]
ScreenSession::new(),
);
let events = [
MouseEvent {
kind: MouseEventKind::Down(MouseButton::Right),
column: 3,
row: 4,
modifiers: KeyModifiers::ALT,
},
MouseEvent {
kind: MouseEventKind::Moved,
column: 5,
row: 6,
modifiers: KeyModifiers::NONE,
},
MouseEvent {
kind: MouseEventKind::ScrollLeft,
column: 7,
row: 8,
modifiers: KeyModifiers::SHIFT,
},
];
for event in events {
handle.send(Ok(Event::Mouse(event)));
}
for expected in events {
assert!(matches!(
stream.next().await,
Some(TuiEvent::MouseOther(actual)) if actual == expected
));
}
}
#[tokio::test(flavor = "current_thread")]
async fn non_conversation_mouse_events_are_dropped_by_default() {
let (broker, handle, _draw_tx, draw_rx, terminal_focused) = setup();
let mut stream = make_stream(broker, draw_rx, terminal_focused);
handle.send(Ok(Event::Mouse(MouseEvent {
kind: MouseEventKind::Moved,
column: 5,
row: 6,
modifiers: KeyModifiers::NONE,
})));
let expected_key = KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE);
handle.send(Ok(Event::Key(expected_key)));
assert!(matches!(stream.next().await, Some(TuiEvent::Key(key)) if key == expected_key));
}
#[tokio::test(flavor = "current_thread")]
async fn vertical_mouse_wheel_maps_to_scroll() {
let (broker, handle, draw_tx, draw_rx, terminal_focused) = setup();
let mut stream = make_stream(broker, draw_rx, terminal_focused);
stream.poll_draw_first = false;
handle.send(Ok(Event::Mouse(MouseEvent {
kind: MouseEventKind::Down(MouseButton::Right),
column: 3,
row: 4,
modifiers: KeyModifiers::NONE,
})));
handle.send(Ok(Event::Mouse(MouseEvent {
kind: MouseEventKind::Down(MouseButton::Middle),
column: 3,
row: 4,
modifiers: KeyModifiers::NONE,
})));
handle.send(Ok(Event::Mouse(MouseEvent {
kind: MouseEventKind::Drag(MouseButton::Right),
column: 3,
row: 4,
modifiers: KeyModifiers::NONE,
})));
handle.send(Ok(Event::Mouse(MouseEvent {
kind: MouseEventKind::Up(MouseButton::Middle),
column: 3,
row: 4,
modifiers: KeyModifiers::NONE,
})));
handle.send(Ok(Event::Mouse(MouseEvent {
kind: MouseEventKind::ScrollLeft,
column: 4,
row: 4,
modifiers: KeyModifiers::NONE,
})));
for column in 0..=MAX_SKIPPED_EVENTS_PER_POLL {
handle.send(Ok(Event::Mouse(MouseEvent {
kind: MouseEventKind::Moved,
column: column as u16,
row: 0,
modifiers: KeyModifiers::NONE,
})));
}
handle.send(Ok(Event::Mouse(MouseEvent {
kind: MouseEventKind::ScrollUp,
column: 5,
row: 6,
modifiers: KeyModifiers::NONE,
modifiers: KeyModifiers::ALT,
})));
handle.send(Ok(Event::Mouse(MouseEvent {
kind: MouseEventKind::ScrollDown,
@@ -631,6 +672,7 @@ mod tests {
direction: MouseScrollDirection::Up,
column: 5,
row: 6,
modifiers: KeyModifiers::ALT,
}))
));
assert!(matches!(
@@ -639,6 +681,7 @@ mod tests {
direction: MouseScrollDirection::Down,
column: 7,
row: 8,
modifiers: KeyModifiers::NONE,
}))
));
}
+1
View File
@@ -61,6 +61,7 @@ pub(crate) async fn run_update_prompt_if_needed(
TuiEvent::Paste(_)
| TuiEvent::MouseScroll(_)
| TuiEvent::MousePrimary(_)
| TuiEvent::MouseOther(_)
| TuiEvent::FocusLost => {}
TuiEvent::Draw | TuiEvent::Resize => {
tui.draw(u16::MAX, |frame| {