refactor: own pane mouse protocol enums outside vt100
This commit is contained in:
+7
-7
@@ -2591,7 +2591,7 @@ impl AppState {
|
||||
|
||||
#[cfg(test)]
|
||||
fn wheel_routing(input_state: crate::pane::InputState) -> WheelRouting {
|
||||
if input_state.mouse_protocol_mode != vt100::MouseProtocolMode::None {
|
||||
if input_state.mouse_protocol_mode.reporting_enabled() {
|
||||
WheelRouting::MouseReport
|
||||
} else if input_state.alternate_screen && input_state.mouse_alternate_scroll {
|
||||
WheelRouting::AlternateScroll
|
||||
@@ -3373,8 +3373,8 @@ mod tests {
|
||||
application_cursor: false,
|
||||
bracketed_paste: false,
|
||||
focus_reporting: false,
|
||||
mouse_protocol_mode: vt100::MouseProtocolMode::ButtonMotion,
|
||||
mouse_protocol_encoding: vt100::MouseProtocolEncoding::Sgr,
|
||||
mouse_protocol_mode: crate::input::MouseProtocolMode::ButtonMotion,
|
||||
mouse_protocol_encoding: crate::input::MouseProtocolEncoding::Sgr,
|
||||
mouse_alternate_scroll: true,
|
||||
};
|
||||
|
||||
@@ -3388,8 +3388,8 @@ mod tests {
|
||||
application_cursor: false,
|
||||
bracketed_paste: false,
|
||||
focus_reporting: false,
|
||||
mouse_protocol_mode: vt100::MouseProtocolMode::None,
|
||||
mouse_protocol_encoding: vt100::MouseProtocolEncoding::Default,
|
||||
mouse_protocol_mode: crate::input::MouseProtocolMode::None,
|
||||
mouse_protocol_encoding: crate::input::MouseProtocolEncoding::Default,
|
||||
mouse_alternate_scroll: true,
|
||||
};
|
||||
|
||||
@@ -3403,8 +3403,8 @@ mod tests {
|
||||
application_cursor: false,
|
||||
bracketed_paste: false,
|
||||
focus_reporting: false,
|
||||
mouse_protocol_mode: vt100::MouseProtocolMode::None,
|
||||
mouse_protocol_encoding: vt100::MouseProtocolEncoding::Default,
|
||||
mouse_protocol_mode: crate::input::MouseProtocolMode::None,
|
||||
mouse_protocol_encoding: crate::input::MouseProtocolEncoding::Default,
|
||||
mouse_alternate_scroll: true,
|
||||
};
|
||||
|
||||
|
||||
+8
-8
@@ -1,6 +1,6 @@
|
||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEventKind};
|
||||
|
||||
use super::{KeyboardProtocol, TerminalKey};
|
||||
use super::{KeyboardProtocol, MouseProtocolEncoding, TerminalKey};
|
||||
|
||||
const KITTY_FLAG_REPORT_EVENT_TYPES: u16 = 0b0000_0010;
|
||||
const KITTY_FLAG_REPORT_ALTERNATE_KEYS: u16 = 0b0000_0100;
|
||||
@@ -44,7 +44,7 @@ pub fn encode_mouse_scroll(
|
||||
column: u16,
|
||||
row: u16,
|
||||
modifiers: KeyModifiers,
|
||||
encoding: vt100::MouseProtocolEncoding,
|
||||
encoding: MouseProtocolEncoding,
|
||||
) -> Option<Vec<u8>> {
|
||||
let button = match kind {
|
||||
MouseEventKind::ScrollUp => 64u16,
|
||||
@@ -61,7 +61,7 @@ pub fn encode_mouse_button(
|
||||
column: u16,
|
||||
row: u16,
|
||||
modifiers: KeyModifiers,
|
||||
encoding: vt100::MouseProtocolEncoding,
|
||||
encoding: MouseProtocolEncoding,
|
||||
) -> Option<Vec<u8>> {
|
||||
let (button, release) = match kind {
|
||||
MouseEventKind::Down(MouseButton::Left) => (0u16, false),
|
||||
@@ -84,10 +84,10 @@ fn encode_mouse_cb(
|
||||
column: u16,
|
||||
row: u16,
|
||||
modifiers: KeyModifiers,
|
||||
encoding: vt100::MouseProtocolEncoding,
|
||||
encoding: MouseProtocolEncoding,
|
||||
) -> Option<Vec<u8>> {
|
||||
let mut cb = match (encoding, release) {
|
||||
(vt100::MouseProtocolEncoding::Sgr, true) => base_button,
|
||||
(MouseProtocolEncoding::Sgr, true) => base_button,
|
||||
(_, true) => 3,
|
||||
(_, false) => base_button,
|
||||
};
|
||||
@@ -105,20 +105,20 @@ fn encode_mouse_cb(
|
||||
let row = row as u32 + 1;
|
||||
|
||||
match encoding {
|
||||
vt100::MouseProtocolEncoding::Sgr => Some(
|
||||
MouseProtocolEncoding::Sgr => Some(
|
||||
format!(
|
||||
"\x1b[<{cb};{column};{row}{}",
|
||||
if release { 'm' } else { 'M' }
|
||||
)
|
||||
.into_bytes(),
|
||||
),
|
||||
vt100::MouseProtocolEncoding::Default => {
|
||||
MouseProtocolEncoding::Default => {
|
||||
let cb = u8::try_from(cb + 32).ok()?;
|
||||
let column = u8::try_from(column + 32).ok()?;
|
||||
let row = u8::try_from(row + 32).ok()?;
|
||||
Some(vec![0x1b, b'[', b'M', cb, column, row])
|
||||
}
|
||||
vt100::MouseProtocolEncoding::Utf8 => {
|
||||
MouseProtocolEncoding::Utf8 => {
|
||||
let mut bytes = Vec::with_capacity(16);
|
||||
bytes.extend_from_slice(b"\x1b[M");
|
||||
push_mouse_codepoint(&mut bytes, cb as u32 + 32)?;
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ pub use encode::encode_key;
|
||||
pub use encode::{
|
||||
encode_cursor_key, encode_mouse_button, encode_mouse_scroll, encode_terminal_key,
|
||||
};
|
||||
pub use model::{KeyboardProtocol, TerminalKey};
|
||||
pub use model::{KeyboardProtocol, MouseProtocolEncoding, MouseProtocolMode, TerminalKey};
|
||||
pub use parse::parse_terminal_key_sequence;
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -55,3 +55,25 @@ impl KeyboardProtocol {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum MouseProtocolMode {
|
||||
None,
|
||||
Press,
|
||||
PressRelease,
|
||||
ButtonMotion,
|
||||
AnyMotion,
|
||||
}
|
||||
|
||||
impl MouseProtocolMode {
|
||||
pub fn reporting_enabled(self) -> bool {
|
||||
self != Self::None
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum MouseProtocolEncoding {
|
||||
Default,
|
||||
Utf8,
|
||||
Sgr,
|
||||
}
|
||||
|
||||
+3
-3
@@ -2,7 +2,7 @@ use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, ModifierKeyCode};
|
||||
|
||||
use super::{
|
||||
encode_cursor_key, encode_key, encode_mouse_button, encode_mouse_scroll, encode_terminal_key,
|
||||
parse_terminal_key_sequence, KeyboardProtocol, TerminalKey,
|
||||
parse_terminal_key_sequence, KeyboardProtocol, MouseProtocolEncoding, TerminalKey,
|
||||
};
|
||||
|
||||
fn assert_terminal_key_eq(
|
||||
@@ -198,7 +198,7 @@ fn sgr_mouse_scroll_encodes_wheel_button_and_coordinates() {
|
||||
4,
|
||||
6,
|
||||
KeyModifiers::SHIFT,
|
||||
vt100::MouseProtocolEncoding::Sgr,
|
||||
MouseProtocolEncoding::Sgr,
|
||||
)
|
||||
.expect("mouse scroll should encode");
|
||||
|
||||
@@ -212,7 +212,7 @@ fn sgr_mouse_release_keeps_button_code() {
|
||||
11,
|
||||
9,
|
||||
KeyModifiers::empty(),
|
||||
vt100::MouseProtocolEncoding::Sgr,
|
||||
MouseProtocolEncoding::Sgr,
|
||||
)
|
||||
.expect("mouse release should encode");
|
||||
|
||||
|
||||
+30
-14
@@ -273,14 +273,14 @@ pub struct InputState {
|
||||
pub application_cursor: bool,
|
||||
pub bracketed_paste: bool,
|
||||
pub focus_reporting: bool,
|
||||
pub mouse_protocol_mode: vt100::MouseProtocolMode,
|
||||
pub mouse_protocol_encoding: vt100::MouseProtocolEncoding,
|
||||
pub mouse_protocol_mode: crate::input::MouseProtocolMode,
|
||||
pub mouse_protocol_encoding: crate::input::MouseProtocolEncoding,
|
||||
pub mouse_alternate_scroll: bool,
|
||||
}
|
||||
|
||||
impl InputState {
|
||||
pub fn mouse_reporting_enabled(self) -> bool {
|
||||
self.mouse_protocol_mode != vt100::MouseProtocolMode::None
|
||||
self.mouse_protocol_mode.reporting_enabled()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -604,8 +604,24 @@ impl VtPaneTerminal {
|
||||
application_cursor: screen.application_cursor(),
|
||||
bracketed_paste: screen.bracketed_paste(),
|
||||
focus_reporting: false,
|
||||
mouse_protocol_mode: screen.mouse_protocol_mode(),
|
||||
mouse_protocol_encoding: screen.mouse_protocol_encoding(),
|
||||
mouse_protocol_mode: match screen.mouse_protocol_mode() {
|
||||
vt100::MouseProtocolMode::None => crate::input::MouseProtocolMode::None,
|
||||
vt100::MouseProtocolMode::Press => crate::input::MouseProtocolMode::Press,
|
||||
vt100::MouseProtocolMode::PressRelease => {
|
||||
crate::input::MouseProtocolMode::PressRelease
|
||||
}
|
||||
vt100::MouseProtocolMode::ButtonMotion => {
|
||||
crate::input::MouseProtocolMode::ButtonMotion
|
||||
}
|
||||
vt100::MouseProtocolMode::AnyMotion => crate::input::MouseProtocolMode::AnyMotion,
|
||||
},
|
||||
mouse_protocol_encoding: match screen.mouse_protocol_encoding() {
|
||||
vt100::MouseProtocolEncoding::Default => {
|
||||
crate::input::MouseProtocolEncoding::Default
|
||||
}
|
||||
vt100::MouseProtocolEncoding::Utf8 => crate::input::MouseProtocolEncoding::Utf8,
|
||||
vt100::MouseProtocolEncoding::Sgr => crate::input::MouseProtocolEncoding::Sgr,
|
||||
},
|
||||
mouse_alternate_scroll: self.mouse_alternate_scroll.load(Ordering::Relaxed),
|
||||
})
|
||||
}
|
||||
@@ -821,22 +837,22 @@ impl GhosttyPaneTerminal {
|
||||
.mode_get(crate::ghostty::MODE_MOUSE_ALTERNATE_SCROLL)
|
||||
.ok()?;
|
||||
let mouse_protocol_mode = if core.terminal.mode_get(1003).ok()? {
|
||||
vt100::MouseProtocolMode::AnyMotion
|
||||
crate::input::MouseProtocolMode::AnyMotion
|
||||
} else if core.terminal.mode_get(1002).ok()? {
|
||||
vt100::MouseProtocolMode::ButtonMotion
|
||||
crate::input::MouseProtocolMode::ButtonMotion
|
||||
} else if core.terminal.mode_get(1000).ok()? {
|
||||
vt100::MouseProtocolMode::PressRelease
|
||||
crate::input::MouseProtocolMode::PressRelease
|
||||
} else if core.terminal.mode_get(9).ok()? {
|
||||
vt100::MouseProtocolMode::Press
|
||||
crate::input::MouseProtocolMode::Press
|
||||
} else {
|
||||
vt100::MouseProtocolMode::None
|
||||
crate::input::MouseProtocolMode::None
|
||||
};
|
||||
let mouse_protocol_encoding = if mouse_sgr {
|
||||
vt100::MouseProtocolEncoding::Sgr
|
||||
crate::input::MouseProtocolEncoding::Sgr
|
||||
} else if mouse_utf8 {
|
||||
vt100::MouseProtocolEncoding::Utf8
|
||||
crate::input::MouseProtocolEncoding::Utf8
|
||||
} else {
|
||||
vt100::MouseProtocolEncoding::Default
|
||||
crate::input::MouseProtocolEncoding::Default
|
||||
};
|
||||
Some(InputState {
|
||||
alternate_screen,
|
||||
@@ -2162,7 +2178,7 @@ impl PaneRuntime {
|
||||
row: u16,
|
||||
modifiers: crossterm::event::KeyModifiers,
|
||||
) -> Option<Vec<u8>> {
|
||||
if self.input_state()?.mouse_protocol_mode == vt100::MouseProtocolMode::None {
|
||||
if !self.input_state()?.mouse_protocol_mode.reporting_enabled() {
|
||||
return None;
|
||||
}
|
||||
self.terminal
|
||||
|
||||
Reference in New Issue
Block a user