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

Calculate relative y mouse event cursor position #11

Merged
merged 6 commits into from
Oct 21, 2019
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/input/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use winapi::um::{

use crossterm_winapi::{
ButtonState, Console, ConsoleMode, EventFlags, Handle, InputEventType, KeyEventRecord,
MouseEvent,
MouseEvent, ScreenBuffer,
};
use lazy_static::lazy_static;

Expand Down Expand Up @@ -390,7 +390,7 @@ fn read_input_events() -> Result<(u32, Vec<InputEvent>)> {
}

fn handle_mouse_event(mouse_event: MouseEvent) -> Result<Option<InputEvent>> {
if let Some(event) = parse_mouse_event_record(&mouse_event) {
if let Ok(Some(event)) = parse_mouse_event_record(&mouse_event) {
return Ok(Some(InputEvent::Mouse(event)));
}
Ok(None)
Expand Down Expand Up @@ -530,19 +530,22 @@ fn parse_key_event_record(key_event: &KeyEventRecord) -> Option<KeyEvent> {
}
}

fn parse_mouse_event_record(event: &MouseEvent) -> Option<crate::MouseEvent> {
fn parse_mouse_event_record(event: &MouseEvent) -> Result<Option<crate::MouseEvent>> {
// NOTE (@imdaveho): xterm emulation takes the digits of the coords and passes them
// individually as bytes into a buffer; the below cxbs and cybs replicates that and
// mimicks the behavior; additionally, in xterm, mouse move is only handled when a
// mouse button is held down (ie. mouse drag)

// Windows returns (0, 0) for upper/left
let window_size = ScreenBuffer::current()?.info()?.terminal_window();

let xpos = event.mouse_position.x;
let ypos = event.mouse_position.y;
let mut ypos = event.mouse_position.y;

// TODO (@imdaveho): check if linux only provides coords for visible terminal window vs the total buffer
// The y position in with a mouse event is not relative to the window but absolute to screen buffer.
// This means that when the mouse cursor is at top left it will be x: 0, y: 2295 (e.g. y = number of cells counting from the absolute buffer height) instead of relative x: 0, y: 0 to the window.
ypos = ypos - window_size.top;

match event.event_flags {
Ok(match event.event_flags {
EventFlags::PressOrRelease => {
// Single click
match event.button_state {
Expand Down Expand Up @@ -604,5 +607,5 @@ fn parse_mouse_event_record(event: &MouseEvent) -> Option<crate::MouseEvent> {
EventFlags::DoubleClick => None, // NOTE (@imdaveho): double click not supported by unix terminals
EventFlags::MouseHwheeled => None, // NOTE (@imdaveho): horizontal scroll not supported by unix terminals
// TODO: Handle Ctrl + Mouse, Alt + Mouse, etc.
}
})
}