Add support for Mouse Events
This commit is contained in:
parent
87bc923f0a
commit
8d7d074933
4 changed files with 404 additions and 130 deletions
197
src/mouse_reporter.rs
Normal file
197
src/mouse_reporter.rs
Normal file
|
|
@ -0,0 +1,197 @@
|
|||
use cosmic::{
|
||||
iced::mouse::{Event as MouseEvent, ScrollDelta},
|
||||
iced::{keyboard::Modifiers, mouse::Button, Event},
|
||||
};
|
||||
|
||||
use crate::terminal::Terminal;
|
||||
|
||||
const SCROLL_SPEED: u32 = 3;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MouseReporter {
|
||||
last_movment_x: Option<u32>,
|
||||
last_movment_y: Option<u32>,
|
||||
button: Option<Button>,
|
||||
}
|
||||
|
||||
impl MouseReporter {
|
||||
fn button_number(&self, button: Button) -> Option<u8> {
|
||||
match button {
|
||||
Button::Left => Some(0),
|
||||
Button::Middle => Some(1),
|
||||
Button::Right => Some(2),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
//Implemented according to
|
||||
//https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Mouse-Tracking
|
||||
pub fn normal_mouse_code(
|
||||
&mut self,
|
||||
event: Event,
|
||||
modifiers: &Modifiers,
|
||||
is_utf8: bool,
|
||||
x: u32,
|
||||
y: u32,
|
||||
) -> Option<Vec<u8>> {
|
||||
//Buttons are handle slightly different between normal and sgr
|
||||
//for normal/utf8 the button release is always reported as button 3
|
||||
let Some(mut button) = (match event {
|
||||
Event::Mouse(MouseEvent::ButtonPressed(b)) => {
|
||||
self.button = Some(b);
|
||||
self.button_number(b)
|
||||
}
|
||||
Event::Mouse(MouseEvent::ButtonReleased(_b)) => {
|
||||
self.button = None;
|
||||
Some(3)
|
||||
}
|
||||
Event::Mouse(MouseEvent::CursorMoved { .. }) => {
|
||||
//Button pressed is reported as button 32 + 0,1,2 and event code M
|
||||
//And only reported if a button is previously pressed
|
||||
if (self.last_movment_x, self.last_movment_y) == (Some(x), Some(y)) {
|
||||
return None;
|
||||
} else {
|
||||
self.last_movment_x = Some(x);
|
||||
self.last_movment_y = Some(y);
|
||||
}
|
||||
//It seems that we should add 32 to signal movement even for normal mode
|
||||
//On button-motion events, xterm adds 32 to the event code (the third
|
||||
//character, Cb).
|
||||
//For example, motion into cell x,y with button 1 down is reported as
|
||||
//CSI M @ CxCy ( @ = 32 + 0 (button 1) + 32 (motion indicator) ).
|
||||
self.button
|
||||
.and_then(|button| self.button_number(button))
|
||||
.map(|b| b + 32)
|
||||
}
|
||||
_ => None,
|
||||
}) else {
|
||||
return None;
|
||||
};
|
||||
|
||||
if modifiers.shift() {
|
||||
button += 4;
|
||||
}
|
||||
if modifiers.alt() {
|
||||
button += 8;
|
||||
}
|
||||
if modifiers.control() {
|
||||
button += 16;
|
||||
}
|
||||
|
||||
//Normal mode have a max of 223 (255 - 32), while utf8 extend this to 2015
|
||||
let max_point: usize = if is_utf8 { 2015 } else { 223 };
|
||||
if x as usize >= max_point || y as usize >= max_point {
|
||||
return None;
|
||||
}
|
||||
|
||||
let utf8_encode_and_append = |mut pos: u32, dest: &mut Vec<u8>| {
|
||||
pos += 1 + 32;
|
||||
let mut utf8 = [0; 2]; //This is large enough since we have a max of 2015
|
||||
dest.extend_from_slice(
|
||||
(char::from_u32(pos).unwrap()) //This unwrap and encode_utf8 is safe due to our
|
||||
//specific range, pos will max be 2047
|
||||
.encode_utf8(&mut utf8)
|
||||
.as_bytes(),
|
||||
);
|
||||
};
|
||||
|
||||
//SPEC: Likewise, Cb will be UTF-8 encoded, to reduce confusion with wheel mouse events.
|
||||
//Always, or only when the the cooardinates is used? No other terminal seems to do this a
|
||||
//all? Doing what they are doing for now.
|
||||
let mut buf: Vec<u8> = vec![b'\x1b', b'[', b'M', 32 + button];
|
||||
//Should we remove 32+button from previous line, and use this instead? Or only on >= 95
|
||||
//utf8_encode_and_append(32 + button, &mut buf);
|
||||
|
||||
//For utf8 spec say: For positions less than 95, the resulting output is identical under both modes.
|
||||
//But also: Under normal mouse mode, positions outside (160,94) result in byte pairs which can be interpreted as a single UTF-8
|
||||
if is_utf8 && x >= 95 {
|
||||
utf8_encode_and_append(x, &mut buf);
|
||||
} else {
|
||||
//SPEC: For positions less than 95, the resulting output is identical under both modes.
|
||||
buf.push(32 + 1 + x as u8);
|
||||
}
|
||||
|
||||
if is_utf8 && y >= 95 {
|
||||
utf8_encode_and_append(y, &mut buf);
|
||||
} else {
|
||||
//SPEC For positions less than 95, the resulting output is identical under both modes.
|
||||
buf.push(32 + 1 + y as u8);
|
||||
}
|
||||
Some(buf)
|
||||
}
|
||||
|
||||
pub fn sgr_mouse_code(
|
||||
&mut self,
|
||||
event: Event,
|
||||
modifiers: &Modifiers,
|
||||
x: u32,
|
||||
y: u32,
|
||||
) -> Option<Vec<u8>> {
|
||||
let Some((button_no, event_code)) = (match event {
|
||||
Event::Mouse(MouseEvent::ButtonPressed(button)) => {
|
||||
//Button pressed is reported as button 0,1,2 and event code M
|
||||
self.button = Some(button);
|
||||
Some((self.button_number(button), "M"))
|
||||
}
|
||||
Event::Mouse(MouseEvent::ButtonReleased(button)) => {
|
||||
//Button pressed is reported as button 0,1,2 and event code m
|
||||
self.button = None;
|
||||
Some((self.button_number(button), "m"))
|
||||
}
|
||||
Event::Mouse(MouseEvent::CursorMoved { .. }) => {
|
||||
//Button pressed is reported as button 32 + 0,1,2 and event code M
|
||||
//And only reported if a button is previously pressed
|
||||
if (self.last_movment_x, self.last_movment_y) == (Some(x), Some(y)) {
|
||||
return None;
|
||||
} else {
|
||||
self.last_movment_x = Some(x);
|
||||
self.last_movment_y = Some(y);
|
||||
}
|
||||
self.button
|
||||
.map(|button| (self.button_number(button).map(|b| b + 32), "M"))
|
||||
}
|
||||
_ => None,
|
||||
}) else {
|
||||
return None;
|
||||
};
|
||||
|
||||
if let Some(mut button_no) = button_no {
|
||||
if modifiers.shift() {
|
||||
button_no += 4;
|
||||
}
|
||||
if modifiers.alt() {
|
||||
button_no += 8;
|
||||
}
|
||||
if modifiers.control() {
|
||||
button_no += 16;
|
||||
}
|
||||
let term_code = format!("\x1b[<{};{};{}{}", button_no, x + 1, y + 1, event_code);
|
||||
Some(term_code.as_bytes().to_vec())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
//Emulate mouse wheel scroll with up/down arrows. Using mouse spec uses
|
||||
//scroll-back and scroll-forw actions, which moves whole windows like page up/page down.
|
||||
pub fn report_mouse_wheel_scroll(
|
||||
&self,
|
||||
terminal: &Terminal,
|
||||
term_cell_width: f32,
|
||||
term_cell_height: f32,
|
||||
delta: ScrollDelta,
|
||||
) {
|
||||
let (_delta_x, delta_y) = match delta {
|
||||
ScrollDelta::Lines { x, y } => (x, y),
|
||||
ScrollDelta::Pixels { x, y } => (x / term_cell_width, y / term_cell_height),
|
||||
};
|
||||
//Send delta_y * SCROLL_SPEED number of Up/Down arrows
|
||||
for _ in 0..(delta_y.abs() as u32 * SCROLL_SPEED) {
|
||||
if delta_y > 0.0 {
|
||||
terminal.input_no_scroll(b"\x1B[1;3A".as_slice())
|
||||
} else if delta_y < 0.0 {
|
||||
terminal.input_no_scroll(b"\x1B[1;3B".as_slice())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue