Fix wrong resize size return (#526)
This commit is contained in:
parent
744ec05f19
commit
e46decc2f3
@ -47,7 +47,7 @@ version = "0.3.8"
|
|||||||
features = ["winuser"]
|
features = ["winuser"]
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
crossterm_winapi = "0.6.2"
|
crossterm_winapi = "0.7.0"
|
||||||
|
|
||||||
#
|
#
|
||||||
# UNIX dependencies
|
# UNIX dependencies
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
use std::io::stdout;
|
use std::io::stdout;
|
||||||
|
|
||||||
|
use crossterm::event::poll;
|
||||||
use crossterm::{
|
use crossterm::{
|
||||||
cursor::position,
|
cursor::position,
|
||||||
event::{read, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
|
event::{read, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
|
||||||
@ -11,6 +12,7 @@ use crossterm::{
|
|||||||
terminal::{disable_raw_mode, enable_raw_mode},
|
terminal::{disable_raw_mode, enable_raw_mode},
|
||||||
Result,
|
Result,
|
||||||
};
|
};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
const HELP: &str = r#"Blocking read()
|
const HELP: &str = r#"Blocking read()
|
||||||
- Keyboard, mouse and terminal resize events enabled
|
- Keyboard, mouse and terminal resize events enabled
|
||||||
@ -29,6 +31,11 @@ fn print_events() -> Result<()> {
|
|||||||
println!("Cursor position: {:?}\r", position());
|
println!("Cursor position: {:?}\r", position());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Event::Resize(_, _) = event {
|
||||||
|
let (original_size, new_size) = flush_resize_events(event);
|
||||||
|
println!("Resize from: {:?}, to: {:?}", original_size, new_size);
|
||||||
|
}
|
||||||
|
|
||||||
if event == Event::Key(KeyCode::Esc.into()) {
|
if event == Event::Key(KeyCode::Esc.into()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -37,6 +44,27 @@ fn print_events() -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Resize events can occur in batches.
|
||||||
|
// With a simple loop they can be flushed.
|
||||||
|
// This function will keep the first and last resize event.
|
||||||
|
fn flush_resize_events(event: Event) -> ((u16, u16), (u16, u16)) {
|
||||||
|
if let Event::Resize(x, y) = event {
|
||||||
|
let mut last_resize = (x, y);
|
||||||
|
loop {
|
||||||
|
if let Ok(true) = poll(Duration::from_millis(50)) {
|
||||||
|
if let Ok(Event::Resize(x, y)) = read() {
|
||||||
|
last_resize = (x, y);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ((x, y), last_resize);
|
||||||
|
}
|
||||||
|
((0, 0), (0, 0))
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
println!("{}", HELP);
|
println!("{}", HELP);
|
||||||
|
|
||||||
|
@ -15,6 +15,8 @@
|
|||||||
//! * use the [`read`](fn.read.html) & [`poll`](fn.poll.html) functions on any, but same, thread
|
//! * use the [`read`](fn.read.html) & [`poll`](fn.poll.html) functions on any, but same, thread
|
||||||
//! * or the [`EventStream`](struct.EventStream.html).
|
//! * or the [`EventStream`](struct.EventStream.html).
|
||||||
//!
|
//!
|
||||||
|
//! **Make sure to enable raw mode in order for keyboard events to work properly**
|
||||||
|
//!
|
||||||
//! ## Mouse Events
|
//! ## Mouse Events
|
||||||
//!
|
//!
|
||||||
//! Mouse events are not enabled by default. You have to enable them with the
|
//! Mouse events are not enabled by default. You have to enable them with the
|
||||||
@ -273,6 +275,7 @@ pub enum Event {
|
|||||||
/// A single mouse event with additional pressed modifiers.
|
/// A single mouse event with additional pressed modifiers.
|
||||||
Mouse(MouseEvent),
|
Mouse(MouseEvent),
|
||||||
/// An resize event with new dimensions after resize (columns, rows).
|
/// An resize event with new dimensions after resize (columns, rows).
|
||||||
|
/// **Note** that resize events can be occur in batches.
|
||||||
Resize(u16, u16),
|
Resize(u16, u16),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +34,8 @@ impl EventSource for WindowsEventSource {
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
if let Some(event_ready) = self.poll.poll(poll_timeout.leftover())? {
|
if let Some(event_ready) = self.poll.poll(poll_timeout.leftover())? {
|
||||||
if event_ready && self.console.number_of_console_input_events()? != 0 {
|
let number = self.console.number_of_console_input_events()?;
|
||||||
|
if event_ready && number != 0 {
|
||||||
let event = match self.console.read_single_input_event()? {
|
let event = match self.console.read_single_input_event()? {
|
||||||
InputRecord::KeyEvent(record) => handle_key_event(record)?,
|
InputRecord::KeyEvent(record) => handle_key_event(record)?,
|
||||||
InputRecord::MouseEvent(record) => handle_mouse_event(record)?,
|
InputRecord::MouseEvent(record) => handle_mouse_event(record)?,
|
||||||
|
Loading…
Reference in New Issue
Block a user