Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Commit

Permalink
Mouse coordinates synchronized with the cursor
Browse files Browse the repository at this point in the history
Signed-off-by: Robert Vojta <[email protected]>
  • Loading branch information
zrzka authored Oct 6, 2019
1 parent 34d8e78 commit 73477c9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
- Removed unsafe `static mut`
- Documentation update
- Remove all references to the crossterm book
- Mouse coordinates synchronized with the cursor (breaking)
- Upper/left reported as `(0, 0)`

## Windows only

Expand Down
15 changes: 10 additions & 5 deletions src/input/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,11 @@ where
let mut next = || iter.next().unwrap();

let cb = next() as i8 - 32;
// (1, 1) are the coords for upper left.
let cx = next().saturating_sub(32) as u16;
let cy = next().saturating_sub(32) as u16;
// See http://www.xfree86.org/current/ctlseqs.html#Mouse%20Tracking
// The upper left character position on the terminal is denoted as 1,1.
// Subtract 1 to keep it synced with cursor
let cx = next().saturating_sub(32) as u16 - 1;
let cy = next().saturating_sub(32) as u16 - 1;

InputEvent::Mouse(match cb & 0b11 {
0 => {
Expand Down Expand Up @@ -455,8 +457,11 @@ where
let nums = &mut str_buf.split(';');

let cb = nums.next().unwrap().parse::<u16>().unwrap();
let cx = nums.next().unwrap().parse::<u16>().unwrap();
let cy = nums.next().unwrap().parse::<u16>().unwrap();
// See http://www.xfree86.org/current/ctlseqs.html#Mouse%20Tracking
// The upper left character position on the terminal is denoted as 1,1.
// Subtract 1 to keep it synced with cursor
let cx = nums.next().unwrap().parse::<u16>().unwrap() - 1;
let cy = nums.next().unwrap().parse::<u16>().unwrap() - 1;

match cb {
0..=2 | 64..=65 => {
Expand Down
5 changes: 3 additions & 2 deletions src/input/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,9 @@ fn parse_mouse_event_record(event: &MouseEvent) -> Option<crate::MouseEvent> {
// mimicks the behavior; additionally, in xterm, mouse move is only handled when a
// mouse button is held down (ie. mouse drag)

let xpos = event.mouse_position.x + 1;
let ypos = event.mouse_position.y + 1;
// Windows returns (0, 0) for upper/left
let xpos = event.mouse_position.x;
let ypos = event.mouse_position.y;

// TODO (@imdaveho): check if linux only provides coords for visible terminal window vs the total buffer

Expand Down

0 comments on commit 73477c9

Please sign in to comment.