Update pancurses backend handling of multiple events

This commit is contained in:
Alexandre Bury 2018-07-22 20:13:49 -07:00
parent 6a728bf234
commit 4ccc07c6d3

View File

@ -42,7 +42,7 @@ pub struct Backend {
struct InputParser { struct InputParser {
key_codes: HashMap<i32, Event>, key_codes: HashMap<i32, Event>,
last_mouse_button: Option<MouseButton>, last_mouse_button: Option<MouseButton>,
event_sink: Sender<Option<Event>>, input_buffer: Option<Event>,
window: Arc<pancurses::Window>, window: Arc<pancurses::Window>,
} }
@ -53,18 +53,20 @@ struct InputParser {
unsafe impl Send for InputParser {} unsafe impl Send for InputParser {}
impl InputParser { impl InputParser {
fn new( fn new(window: Arc<pancurses::Window>) -> Self {
event_sink: Sender<Option<Event>>, window: Arc<pancurses::Window>,
) -> Self {
InputParser { InputParser {
key_codes: initialize_keymap(), key_codes: initialize_keymap(),
last_mouse_button: None, last_mouse_button: None,
event_sink, input_buffer: None,
window, window,
} }
} }
fn parse_next(&mut self) { fn parse_next(&mut self) -> Option<Event> {
if let Some(event) = self.input_buffer.take() {
return Some(event);
}
let event = if let Some(ev) = self.window.getch() { let event = if let Some(ev) = self.window.getch() {
Some(match ev { Some(match ev {
pancurses::Input::Character('\n') => Event::Key(Key::Enter), pancurses::Input::Character('\n') => Event::Key(Key::Enter),
@ -215,7 +217,7 @@ impl InputParser {
} else { } else {
None None
}; };
self.event_sink.send(event); event
} }
fn parse_mouse_event(&mut self) -> Event { fn parse_mouse_event(&mut self) -> Event {
@ -262,7 +264,7 @@ impl InputParser {
if event.is_none() { if event.is_none() {
event = Some(e); event = Some(e);
} else { } else {
self.event_sink.send(Some(make_event(e))); self.input_buffer = Some(make_event(e));
} }
}); });
} }
@ -466,8 +468,7 @@ impl backend::Backend for Backend {
); );
} }
let mut input_parser = let mut input_parser = InputParser::new(Arc::clone(&self.window));
InputParser::new(event_sink, Arc::clone(&self.window));
thread::spawn(move || { thread::spawn(move || {
for req in input_request { for req in input_request {
@ -479,7 +480,7 @@ impl backend::Backend for Backend {
input_parser.window.timeout(-1); input_parser.window.timeout(-1);
} }
} }
input_parser.parse_next(); event_sink.send(input_parser.parse_next());
} }
running.store(false, Ordering::Relaxed); running.store(false, Ordering::Relaxed);
}); });