diff options
| author | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-06-03 02:03:14 +0300 |
|---|---|---|
| committer | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-06-03 02:03:44 +0300 |
| commit | b695d7cbc0d29540993642c23b3d5798cbf9f7dc (patch) | |
| tree | d7243c891dca6c8991583edd7666ec9fe8cc6f94 /src/pane | |
| parent | daf636fef7fd68bcb1e30cfb155883c3b831513c (diff) | |
fix: forward any-motion pane mouse events
refs #419
Diffstat (limited to 'src/pane')
| -rw-r--r-- | src/pane/input.rs | 26 | ||||
| -rw-r--r-- | src/pane/terminal.rs | 67 |
2 files changed, 91 insertions, 2 deletions
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<crate::ghostty::MouseEncoder> { 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<crate::ghostty::MouseEvent> { + 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<Vec<u8>> { + 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<Vec<u8>> { + 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, @@ -2351,6 +2380,40 @@ mod tests { } #[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 = "🙂"; const VS16_GRAPHEME: &str = "⚠️"; |
