From b695d7cbc0d29540993642c23b3d5798cbf9f7dc Mon Sep 17 00:00:00 2001 From: Ogulcan Celik Date: Wed, 3 Jun 2026 02:03:14 +0300 Subject: fix: forward any-motion pane mouse events refs #419 --- src/pane/input.rs | 26 ++++++++++++++++++++ src/pane/terminal.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 91 insertions(+), 2 deletions(-) (limited to 'src/pane') diff --git a/src/pane/input.rs b/src/pane/input.rs index 262e036d..6906e77d 100644 --- a/src/pane/input.rs +++ b/src/pane/input.rs @@ -58,6 +58,14 @@ pub(super) fn ghostty_mouse_encoder_for_terminal( ) -> Option { let mut encoder = crate::ghostty::MouseEncoder::new().ok()?; encoder.set_from_terminal(terminal); + if terminal + .mode_get(crate::ghostty::MODE_MOUSE_SGR_PIXELS) + .ok()? + { + // Herdr receives host mouse positions in terminal cells. Downgrade + // SGR-pixels to normal SGR so forwarded coordinates stay cell-local. + encoder.set_format(crate::ghostty::MOUSE_FORMAT_SGR); + } let cols = terminal.cols().ok()? as u32; let rows = terminal.rows().ok()? as u32; encoder.set_size(cols, rows, 1, 1); @@ -121,6 +129,24 @@ pub(super) fn ghostty_mouse_event_from_button_kind( Some(event) } +pub(super) fn ghostty_mouse_event_from_motion_kind( + kind: crossterm::event::MouseEventKind, + column: u16, + row: u16, + modifiers: crossterm::event::KeyModifiers, +) -> Option { + if kind != crossterm::event::MouseEventKind::Moved { + return None; + } + + let mut event = crate::ghostty::MouseEvent::new().ok()?; + event.set_action(crate::ghostty::MOUSE_ACTION_MOTION); + event.clear_button(); + event.set_mods(ghostty_mods_from_key_modifiers(modifiers)); + event.set_position(column as f32, row as f32); + Some(event) +} + pub(super) fn ghostty_mouse_event_from_wheel_kind( kind: crossterm::event::MouseEventKind, column: u16, diff --git a/src/pane/terminal.rs b/src/pane/terminal.rs index be1200e6..0a67f11a 100644 --- a/src/pane/terminal.rs +++ b/src/pane/terminal.rs @@ -16,8 +16,8 @@ use crate::protocol::CellData; use super::{ input::{ ghostty_key_event_from_terminal_key, ghostty_mouse_encoder_for_terminal, - ghostty_mouse_event_from_button_kind, ghostty_mouse_event_from_wheel_kind, - ghostty_prefers_herdr_text_encoding, + ghostty_mouse_event_from_button_kind, ghostty_mouse_event_from_motion_kind, + ghostty_mouse_event_from_wheel_kind, ghostty_prefers_herdr_text_encoding, }, kitty_keyboard::KittyKeyboardTracker, osc::{ @@ -283,6 +283,17 @@ impl PaneTerminal { .encode_mouse_button(kind, column, row, modifiers) } + pub fn encode_mouse_motion( + &self, + kind: crossterm::event::MouseEventKind, + column: u16, + row: u16, + modifiers: crossterm::event::KeyModifiers, + ) -> Option> { + self.ghostty + .encode_mouse_motion(kind, column, row, modifiers) + } + pub fn encode_mouse_wheel( &self, kind: crossterm::event::MouseEventKind, @@ -887,6 +898,24 @@ impl GhosttyPaneTerminal { encoder.encode(&event).ok() } + pub fn encode_mouse_motion( + &self, + kind: crossterm::event::MouseEventKind, + column: u16, + row: u16, + modifiers: crossterm::event::KeyModifiers, + ) -> Option> { + let Ok(core) = self.core.lock() else { + return None; + }; + if !core.terminal.mode_get(MODE_MOUSE_ANY_MOTION).ok()? { + return None; + } + let mut encoder = ghostty_mouse_encoder_for_terminal(&core.terminal)?; + let event = ghostty_mouse_event_from_motion_kind(kind, column, row, modifiers)?; + encoder.encode(&event).ok() + } + pub fn encode_mouse_wheel( &self, kind: crossterm::event::MouseEventKind, @@ -2350,6 +2379,40 @@ mod tests { assert_eq!(encoded.as_deref(), Some(&b"\x1b[<36;5;7M"[..])); } + #[test] + fn ghostty_mouse_moved_encoding_uses_any_motion_state() { + let (tx, _rx) = mpsc::channel(4); + let mut terminal = crate::ghostty::Terminal::new(80, 24, 0).unwrap(); + terminal.write(b"\x1b[?1003h\x1b[?1006h"); + let pane = GhosttyPaneTerminal::new(terminal, tx).unwrap(); + + let encoded = pane.encode_mouse_motion( + crossterm::event::MouseEventKind::Moved, + 4, + 6, + crossterm::event::KeyModifiers::empty(), + ); + + assert_eq!(encoded.as_deref(), Some(&b"\x1b[<35;5;7M"[..])); + } + + #[test] + fn ghostty_mouse_sgr_pixels_downgrades_to_cell_coordinates() { + let (tx, _rx) = mpsc::channel(4); + let mut terminal = crate::ghostty::Terminal::new(80, 24, 0).unwrap(); + terminal.write(b"\x1b[?1003h\x1b[?1006h\x1b[?1016h"); + let pane = GhosttyPaneTerminal::new(terminal, tx).unwrap(); + + let encoded = pane.encode_mouse_motion( + crossterm::event::MouseEventKind::Moved, + 4, + 6, + crossterm::event::KeyModifiers::empty(), + ); + + assert_eq!(encoded.as_deref(), Some(&b"\x1b[<35;5;7M"[..])); + } + #[test] fn ghostty_normalize_buffer_symbol_prefers_grapheme_width_when_metadata_disagrees() { const WIDE_GRAPHEME: &str = "🙂"; -- cgit v1.2.3