Fixed input handler

This commit is contained in:
ChristianVisintin
2020-11-21 12:12:10 +01:00
parent 325fd8eeed
commit 1a58e7bce4
2 changed files with 37 additions and 75 deletions

View File

@@ -25,60 +25,21 @@
extern crate crossterm; extern crate crossterm;
// Deps use crossterm::event::{poll, read, Event};
use crossterm::event::{read, Event};
use std::sync::{mpsc, Arc, Mutex};
use std::thread;
use std::time::Duration; use std::time::Duration;
/// ## InputHandler /// ## InputHandler
/// ///
/// InputHandler is the struct which runs a thread which waits for /// InputHandler is the struct which runs a thread which waits for
/// input events from the user and reports them through a receiver /// input events from the user and reports them through a receiver
pub(crate) struct InputHandler { pub(crate) struct InputHandler {}
running: Arc<Mutex<bool>>,
join_hnd: Option<thread::JoinHandle<()>>,
receiver: mpsc::Receiver<Event>,
}
impl InputHandler { impl InputHandler {
/// ### InputHandler /// ### InputHandler
/// ///
/// ///
pub(crate) fn new() -> InputHandler { pub(crate) fn new() -> InputHandler {
let (thread_sender, client_receiver) = mpsc::channel(); InputHandler {}
let client_running = Arc::new(Mutex::new(false));
let thread_running = Arc::clone(&client_running);
let join_hnd = thread::spawn(move || {
InputHandler::thread_run(thread_sender, thread_running);
});
InputHandler {
running: client_running,
join_hnd: Some(join_hnd),
receiver: client_receiver,
}
}
/// ### stop
///
/// Stop InputHandler
pub(crate) fn stop(&mut self) {
if self.join_hnd.is_some() {
//Set join to true
{
// Set running to false
{
let mut running = self.running.lock().unwrap();
*running = false;
}
// Join
self.join_hnd
.take()
.map(thread::JoinHandle::join)
.unwrap()
.unwrap();
}
}
} }
/// ### fetch_messages /// ### fetch_messages
@@ -87,43 +48,40 @@ impl InputHandler {
pub(crate) fn fetch_messages(&self) -> Result<Vec<Event>, ()> { pub(crate) fn fetch_messages(&self) -> Result<Vec<Event>, ()> {
let mut inbox: Vec<Event> = Vec::new(); let mut inbox: Vec<Event> = Vec::new();
loop { loop {
match self.receiver.try_recv() { if let Ok(available) = poll(Duration::from_millis(10)) {
Ok(message) => inbox.push(message), match available {
Err(err) => match err { true => {
mpsc::TryRecvError::Empty => break, // Read event
_ => return Err(()), if let Ok(ev) = read() {
}, inbox.push(ev);
} else {
return Err(());
}
}
false => break,
}
} else {
return Err(());
} }
} }
Ok(inbox) Ok(inbox)
} }
}
// ### run #[cfg(test)]
/// mod tests {
/// Run method for thread
fn thread_run(sender: mpsc::Sender<Event>, running: Arc<Mutex<bool>>) { use super::*;
{
let mut running = running.lock().unwrap(); #[test]
*running = true; fn test_ui_input_new() {
} let input_hnd: InputHandler = InputHandler::new();
loop {
// Check if running is false
{
let running = running.lock().unwrap();
if *running == false {
break;
}
}
// Fetch events
if let Ok(ev) = read() {
// Send event
if let Err(_) = sender.send(ev) {
// The counterpart has died
break;
}
}
// Sleep
thread::sleep(Duration::from_millis(50));
} }
#[test]
fn test_ui_input_fetch() {
let input_hnd: InputHandler = InputHandler::new();
// Try recv
assert_eq!(input_hnd.fetch_messages().ok().unwrap().len(), 0);
} }
} }

View File

@@ -23,4 +23,8 @@
* *
*/ */
// Modules
pub mod context;
pub(crate) mod input; pub(crate) mod input;
// Context