tui: implement mouse support#279
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Warning Review limit reached
More reviews will be available in 2 minutes and 45 seconds. Learn how PR review limits work. Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file). ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits. 🚦 How do rate limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (6)
📒 Files selected for processing (26)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request introduces comprehensive mouse support to the TUI, enabling mouse-based pane switching, event list scrolling and double-clicking, popup navigation, and interactive pane divider dragging. Key feedback focuses on robust mouse event handling: first, converting mouse coordinate encoding in pseudo_term.rs to return raw bytes (Vec<u8>) instead of String to prevent UTF-8 multi-byte corruption of traditional X10 escape sequences; second, utilizing non-blocking try_send instead of await when forwarding mouse events to the PTY to prevent UI freezes under rapid mouse movement; and third, restricting divider dragging to left-click drag events to avoid unexpected jumps if a mouse release event is missed.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| fn encode_default_mouse(event: &MouseEvent, col: u16, row: u16) -> Option<String> { | ||
| // Traditional encoding: ESC [ M Cb Cx Cy | ||
| // Cb = button + 32, Cx = col + 33, Cy = row + 33 | ||
| // Coordinates are limited to 222 (255 - 33) | ||
| if col > 222 || row > 222 { | ||
| return None; | ||
| } | ||
|
|
||
| let button: u8 = match event.kind { | ||
| MouseEventKind::Down(MouseButton::Left) => 0, | ||
| MouseEventKind::Down(MouseButton::Middle) => 1, | ||
| MouseEventKind::Down(MouseButton::Right) => 2, | ||
| MouseEventKind::Up(_) => 3, // Release is encoded as button 3 | ||
| MouseEventKind::Drag(MouseButton::Left) => 32, | ||
| MouseEventKind::Drag(MouseButton::Middle) => 33, | ||
| MouseEventKind::Drag(MouseButton::Right) => 34, | ||
| MouseEventKind::ScrollUp => 64, | ||
| MouseEventKind::ScrollDown => 65, | ||
| MouseEventKind::ScrollLeft => 66, | ||
| MouseEventKind::ScrollRight => 67, | ||
| MouseEventKind::Moved => 35, | ||
| }; | ||
|
|
||
| let mut button = button; | ||
| if event.modifiers.contains(KeyModifiers::SHIFT) { | ||
| button += 4; | ||
| } | ||
| if event.modifiers.contains(KeyModifiers::ALT) { | ||
| button += 8; | ||
| } | ||
| if event.modifiers.contains(KeyModifiers::CONTROL) { | ||
| button += 16; | ||
| } | ||
|
|
||
| let cb = (button + 32) as char; | ||
| let cx = (col as u8 + 33) as char; | ||
| let cy = (row as u8 + 33) as char; | ||
| Some(format!("\x1b[M{cb}{cx}{cy}")) | ||
| } | ||
|
|
There was a problem hiding this comment.
In encode_default_mouse, coordinates col and row are converted to char after adding 33. If col or row is greater than 94, the resulting value (col + 33) will be greater than 127. In Rust, formatting a char with a value greater than 127 into a String encodes it as a multi-byte UTF-8 sequence. However, the traditional X10 mouse protocol expects raw single-byte coordinates. This UTF-8 encoding will corrupt the escape sequence and cause the terminal to misinterpret the mouse position.\n\nTo fix this, both encode_default_mouse and encode_sgr_mouse should return Option<Vec<u8>> instead of Option<String>, allowing raw bytes to be sent directly.
fn encode_default_mouse(event: &MouseEvent, col: u16, row: u16) -> Option<Vec<u8>> {
if col > 222 || row > 222 {
return None;
}
let button: u8 = match event.kind {
MouseEventKind::Down(MouseButton::Left) => 0,
MouseEventKind::Down(MouseButton::Middle) => 1,
MouseEventKind::Down(MouseButton::Right) => 2,
MouseEventKind::Up(_) => 3,
MouseEventKind::Drag(MouseButton::Left) => 32,
MouseEventKind::Drag(MouseButton::Middle) => 33,
MouseEventKind::Drag(MouseButton::Right) => 34,
MouseEventKind::ScrollUp => 64,
MouseEventKind::ScrollDown => 65,
MouseEventKind::ScrollLeft => 66,
MouseEventKind::ScrollRight => 67,
MouseEventKind::Moved => 35,
};
let mut button = button;
if event.modifiers.contains(KeyModifiers::SHIFT) {
button += 4;
}
if event.modifiers.contains(KeyModifiers::ALT) {
button += 8;
}
if event.modifiers.contains(KeyModifiers::CONTROL) {
button += 16;
}
let cb = button + 32;
let cx = col as u8 + 33;
let cy = row as u8 + 33;
Some(vec![b'\\x1b', b'[', b'M', cb, cx, cy])
}| pub async fn handle_mouse_event(&self, event: &MouseEvent, col: u16, row: u16) { | ||
| let (mode, encoding) = { | ||
| let parser = self.parser.read().unwrap(); | ||
| let screen = parser.screen(); | ||
| ( | ||
| screen.mouse_protocol_mode(), | ||
| screen.mouse_protocol_encoding(), | ||
| ) | ||
| }; | ||
|
|
||
| // Only forward mouse events if the program running in the terminal | ||
| // has enabled mouse reporting (e.g. via DECSET 1000/1002/1003). | ||
| if mode == vt100::MouseProtocolMode::None { | ||
| return; | ||
| } | ||
|
|
||
| // Filter events based on the protocol mode | ||
| let dominated = match mode { | ||
| vt100::MouseProtocolMode::None => unreachable!(), | ||
| // X10: only button press | ||
| vt100::MouseProtocolMode::Press => matches!( | ||
| event.kind, | ||
| MouseEventKind::Down(_) | ||
| | MouseEventKind::ScrollUp | ||
| | MouseEventKind::ScrollDown | ||
| | MouseEventKind::ScrollLeft | ||
| | MouseEventKind::ScrollRight | ||
| ), | ||
| // VT200: button press and release | ||
| vt100::MouseProtocolMode::PressRelease => matches!( | ||
| event.kind, | ||
| MouseEventKind::Down(_) | ||
| | MouseEventKind::Up(_) | ||
| | MouseEventKind::ScrollUp | ||
| | MouseEventKind::ScrollDown | ||
| | MouseEventKind::ScrollLeft | ||
| | MouseEventKind::ScrollRight | ||
| ), | ||
| // Button press, release, and drag (motion with button held) | ||
| vt100::MouseProtocolMode::ButtonMotion => matches!( | ||
| event.kind, | ||
| MouseEventKind::Down(_) | ||
| | MouseEventKind::Up(_) | ||
| | MouseEventKind::Drag(_) | ||
| | MouseEventKind::ScrollUp | ||
| | MouseEventKind::ScrollDown | ||
| | MouseEventKind::ScrollLeft | ||
| | MouseEventKind::ScrollRight | ||
| ), | ||
| // Everything including plain motion | ||
| vt100::MouseProtocolMode::AnyMotion => true, | ||
| }; | ||
| if !dominated { | ||
| return; | ||
| } | ||
|
|
||
| let seq = match encoding { | ||
| vt100::MouseProtocolEncoding::Sgr => encode_sgr_mouse(event, col, row), | ||
| // Default and UTF-8 both use the traditional encoding | ||
| vt100::MouseProtocolEncoding::Default | vt100::MouseProtocolEncoding::Utf8 => { | ||
| encode_default_mouse(event, col, row) | ||
| } | ||
| }; | ||
| if let Some(seq) = seq { | ||
| self | ||
| .master_tx | ||
| .send(Bytes::from(seq.into_bytes())) | ||
| .await | ||
| .ok(); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Using self.master_tx.send(...).await inside handle_mouse_event can block the main TUI thread if the PTY writer channel buffer (size 32) fills up. Rapid mouse movements (especially in AnyMotion mode) can easily flood the channel. Blocking the main thread will freeze the entire user interface.\n\nSince dropping intermediate mouse motion/drag events is perfectly acceptable and much better than freezing the UI, we should use non-blocking try_send instead of awaiting send.
pub fn handle_mouse_event(&self, event: &MouseEvent, col: u16, row: u16) {
let (mode, encoding) = {
let parser = self.parser.read().unwrap();
let screen = parser.screen();
(
screen.mouse_protocol_mode(),
screen.mouse_protocol_encoding(),
)
};
if mode == vt100::MouseProtocolMode::None {
return;
}
let dominated = match mode {
vt100::MouseProtocolMode::None => unreachable!(),
vt100::MouseProtocolMode::Press => matches!(
event.kind,
MouseEventKind::Down(_)
| MouseEventKind::ScrollUp
| MouseEventKind::ScrollDown
| MouseEventKind::ScrollLeft
| MouseEventKind::ScrollRight
),
vt100::MouseProtocolMode::PressRelease => matches!(
event.kind,
MouseEventKind::Down(_)
| MouseEventKind::Up(_)
| MouseEventKind::ScrollUp
| MouseEventKind::ScrollDown
| MouseEventKind::ScrollLeft
| MouseEventKind::ScrollRight
),
vt100::MouseProtocolMode::ButtonMotion => matches!(
event.kind,
MouseEventKind::Down(_)
| MouseEventKind::Up(_)
| MouseEventKind::Drag(_)
| MouseEventKind::ScrollUp
| MouseEventKind::ScrollDown
| MouseEventKind::ScrollLeft
| MouseEventKind::ScrollRight
),
vt100::MouseProtocolMode::AnyMotion => true,
};
if !dominated {
return;
}
let seq = match encoding {
vt100::MouseProtocolEncoding::Sgr => encode_sgr_mouse(event, col, row),
vt100::MouseProtocolEncoding::Default | vt100::MouseProtocolEncoding::Utf8 => {
encode_default_mouse(event, col, row)
}
};
if let Some(seq) = seq {
self
.master_tx
.try_send(Bytes::from(seq))
.ok();
}
}| fn encode_sgr_mouse(event: &MouseEvent, col: u16, row: u16) -> Option<String> { | ||
| // SGR (1006) mouse encoding: ESC [ < button ; col ; row M/m | ||
| // button: 0 = left, 1 = middle, 2 = right, 64 = scroll up, 65 = scroll down | ||
| // +32 for motion events | ||
| // M = press/motion, m = release | ||
| let (button, is_release) = match event.kind { | ||
| MouseEventKind::Down(MouseButton::Left) => (0u8, false), | ||
| MouseEventKind::Down(MouseButton::Middle) => (1, false), | ||
| MouseEventKind::Down(MouseButton::Right) => (2, false), | ||
| MouseEventKind::Up(MouseButton::Left) => (0, true), | ||
| MouseEventKind::Up(MouseButton::Middle) => (1, true), | ||
| MouseEventKind::Up(MouseButton::Right) => (2, true), | ||
| MouseEventKind::Drag(MouseButton::Left) => (32, false), | ||
| MouseEventKind::Drag(MouseButton::Middle) => (33, false), | ||
| MouseEventKind::Drag(MouseButton::Right) => (34, false), | ||
| MouseEventKind::ScrollUp => (64, false), | ||
| MouseEventKind::ScrollDown => (65, false), | ||
| MouseEventKind::ScrollLeft => (66, false), | ||
| MouseEventKind::ScrollRight => (67, false), | ||
| MouseEventKind::Moved => (35, false), | ||
| }; | ||
|
|
||
| // Add modifier flags | ||
| let mut button = button; | ||
| if event.modifiers.contains(KeyModifiers::SHIFT) { | ||
| button += 4; | ||
| } | ||
| if event.modifiers.contains(KeyModifiers::ALT) { | ||
| button += 8; | ||
| } | ||
| if event.modifiers.contains(KeyModifiers::CONTROL) { | ||
| button += 16; | ||
| } | ||
|
|
||
| // SGR format: ESC [ < button ; col+1 ; row+1 M/m | ||
| let suffix = if is_release { 'm' } else { 'M' }; | ||
| Some(format!( | ||
| "\x1b[<{};{};{}{}", | ||
| button, | ||
| col + 1, | ||
| row + 1, | ||
| suffix | ||
| )) | ||
| } |
There was a problem hiding this comment.
To align with the updated signature of encode_default_mouse and avoid unnecessary string-to-byte conversions in the main event loop, encode_sgr_mouse should also return Option<Vec<u8>> instead of Option<String>.
fn encode_sgr_mouse(event: &MouseEvent, col: u16, row: u16) -> Option<Vec<u8>> {
// SGR (1006) mouse encoding: ESC [ < button ; col ; row M/m
// button: 0 = left, 1 = middle, 2 = right, 64 = scroll up, 65 = scroll down
// +32 for motion events
// M = press/motion, m = release
let (button, is_release) = match event.kind {
MouseEventKind::Down(MouseButton::Left) => (0u8, false),
MouseEventKind::Down(MouseButton::Middle) => (1, false),
MouseEventKind::Down(MouseButton::Right) => (2, false),
MouseEventKind::Up(MouseButton::Left) => (0, true),
MouseEventKind::Up(MouseButton::Middle) => (1, true),
MouseEventKind::Up(MouseButton::Right) => (2, true),
MouseEventKind::Drag(MouseButton::Left) => (32, false),
MouseEventKind::Drag(MouseButton::Middle) => (33, false),
MouseEventKind::Drag(MouseButton::Right) => (34, false),
MouseEventKind::ScrollUp => (64, false),
MouseEventKind::ScrollDown => (65, false),
MouseEventKind::ScrollLeft => (66, false),
MouseEventKind::ScrollRight => (67, false),
MouseEventKind::Moved => (35, false),
};
// Add modifier flags
let mut button = button;
if event.modifiers.contains(KeyModifiers::SHIFT) {
button += 4;
}
if event.modifiers.contains(KeyModifiers::ALT) {
button += 8;
}
if event.modifiers.contains(KeyModifiers::CONTROL) {
button += 16;
}
// SGR format: ESC [ < button ; col+1 ; row+1 M/m
let suffix = if is_release { 'm' } else { 'M' };
Some(format!(
"\\x1b[<{};{};{}{}",
button,
col + 1,
row + 1,
suffix
).into_bytes())
}| if self.term.is_some() { | ||
| if self.dragging_divider { | ||
| match me.kind { | ||
| MouseEventKind::Drag(MouseButton::Left) | MouseEventKind::Moved => { |
There was a problem hiding this comment.
Matching MouseEventKind::Moved when self.dragging_divider is true can cause the pane divider to unexpectedly jump and follow the mouse cursor even when the left mouse button is not held down. This typically happens if the Up event is missed (e.g., when the mouse is released outside the terminal window). Restricting this match to MouseEventKind::Drag(MouseButton::Left) makes the dragging behavior much more robust.
| MouseEventKind::Drag(MouseButton::Left) | MouseEventKind::Moved => { | |
| MouseEventKind::Drag(MouseButton::Left) => { |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #279 +/- ##
==========================================
- Coverage 82.85% 81.83% -1.02%
==========================================
Files 81 82 +1
Lines 20475 21257 +782
==========================================
+ Hits 16964 17396 +432
- Misses 3511 3861 +350 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
No description provided.