2021-04-02 12:33:56 +00:00
|
|
|
use crate::app::Task;
|
|
|
|
use crate::app::{ExternalMsg, InternalMsg, MsgIn};
|
|
|
|
use crate::input::Key;
|
2021-10-30 04:23:39 +00:00
|
|
|
use anyhow::Error;
|
2021-06-04 16:09:14 +00:00
|
|
|
use crossterm::event::{self, Event, MouseEventKind};
|
2021-10-05 17:44:46 +00:00
|
|
|
use std::sync::mpsc;
|
2021-04-02 12:33:56 +00:00
|
|
|
use std::sync::mpsc::{Receiver, Sender};
|
|
|
|
use std::thread;
|
2021-10-30 04:23:39 +00:00
|
|
|
use std::time::Duration;
|
2021-04-02 12:33:56 +00:00
|
|
|
|
2021-10-05 17:44:46 +00:00
|
|
|
pub(crate) struct EventReader {
|
|
|
|
task_sender: Sender<Task>,
|
|
|
|
stopper: Option<(Sender<bool>, Receiver<()>)>,
|
2021-10-04 11:43:54 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 17:44:46 +00:00
|
|
|
impl EventReader {
|
|
|
|
pub(crate) fn new(task_sender: Sender<Task>) -> Self {
|
|
|
|
Self {
|
|
|
|
task_sender,
|
|
|
|
stopper: None,
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 11:43:54 +00:00
|
|
|
|
2021-10-05 17:44:46 +00:00
|
|
|
pub(crate) fn start(&mut self) {
|
|
|
|
let sender = self.task_sender.clone();
|
|
|
|
let (tx_stopper, rx_stopper) = mpsc::channel();
|
|
|
|
let (tx_ack, rx_ack) = mpsc::channel();
|
|
|
|
self.stopper = Some((tx_stopper, rx_ack));
|
2021-04-02 12:33:56 +00:00
|
|
|
|
2021-10-05 17:44:46 +00:00
|
|
|
thread::spawn(move || {
|
|
|
|
keep_reading(sender, rx_stopper, tx_ack);
|
|
|
|
});
|
|
|
|
}
|
2021-04-02 12:33:56 +00:00
|
|
|
|
2021-10-05 17:44:46 +00:00
|
|
|
pub(crate) fn stop(&self) {
|
|
|
|
if let Some((stopper, ack)) = &self.stopper {
|
2021-10-30 04:23:39 +00:00
|
|
|
stopper.send(true).unwrap_or_default(); // Let's not panic when xplr stops.
|
|
|
|
ack.recv().unwrap_or_default();
|
2021-10-05 17:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-04 16:09:14 +00:00
|
|
|
|
2021-10-30 06:29:17 +00:00
|
|
|
fn keep_reading(
|
|
|
|
tx_msg_in: Sender<Task>,
|
|
|
|
rx_stopper: Receiver<bool>,
|
|
|
|
tx_ack: Sender<()>,
|
|
|
|
) {
|
2021-10-05 17:44:46 +00:00
|
|
|
loop {
|
|
|
|
if rx_stopper.try_recv().unwrap_or(false) {
|
|
|
|
tx_ack.send(()).unwrap();
|
|
|
|
break;
|
2022-06-02 16:40:58 +00:00
|
|
|
} else if event::poll(std::time::Duration::from_millis(150)).unwrap_or_default()
|
2021-10-30 06:29:17 +00:00
|
|
|
{
|
2021-10-05 17:44:46 +00:00
|
|
|
// NOTE: The poll timeout need to stay low, else spawning sub subshell
|
|
|
|
// and start typing immediately will cause panic.
|
|
|
|
// To reproduce, press `:`, then press and hold `!`.
|
2021-10-30 04:23:39 +00:00
|
|
|
let res = match event::read() {
|
2021-10-05 17:44:46 +00:00
|
|
|
Ok(Event::Key(key)) => {
|
|
|
|
let key = Key::from_event(key);
|
|
|
|
let msg = MsgIn::Internal(InternalMsg::HandleKey(key));
|
2021-10-30 04:23:39 +00:00
|
|
|
tx_msg_in
|
|
|
|
.send(Task::new(msg, Some(key)))
|
|
|
|
.map_err(Error::new)
|
2021-10-05 17:44:46 +00:00
|
|
|
}
|
2021-06-04 16:09:14 +00:00
|
|
|
|
2021-10-05 17:44:46 +00:00
|
|
|
Ok(Event::Mouse(evt)) => match evt.kind {
|
|
|
|
MouseEventKind::ScrollUp => {
|
|
|
|
let msg = MsgIn::External(ExternalMsg::FocusPrevious);
|
2021-10-30 04:23:39 +00:00
|
|
|
tx_msg_in.send(Task::new(msg, None)).map_err(Error::new)
|
2021-04-05 05:36:53 +00:00
|
|
|
}
|
2021-05-21 12:56:21 +00:00
|
|
|
|
2021-10-05 17:44:46 +00:00
|
|
|
MouseEventKind::ScrollDown => {
|
|
|
|
let msg = MsgIn::External(ExternalMsg::FocusNext);
|
2021-10-30 04:23:39 +00:00
|
|
|
tx_msg_in.send(Task::new(msg, None)).map_err(Error::new)
|
2021-04-02 12:33:56 +00:00
|
|
|
}
|
2021-10-30 04:23:39 +00:00
|
|
|
_ => Ok(()),
|
2021-10-05 17:44:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Ok(Event::Resize(_, _)) => {
|
|
|
|
let msg = MsgIn::External(ExternalMsg::Refresh);
|
2021-10-30 04:23:39 +00:00
|
|
|
tx_msg_in.send(Task::new(msg, None)).map_err(Error::new)
|
2021-10-05 17:44:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 17:06:15 +00:00
|
|
|
Ok(Event::FocusGained) => Ok(()),
|
|
|
|
Ok(Event::FocusLost) => Ok(()),
|
|
|
|
|
|
|
|
Ok(Event::Paste(text)) => {
|
|
|
|
let msg = MsgIn::External(ExternalMsg::BufferInput(text));
|
|
|
|
tx_msg_in.send(Task::new(msg, None)).map_err(Error::new)
|
|
|
|
}
|
|
|
|
|
2021-10-30 04:23:39 +00:00
|
|
|
Err(e) => Err(Error::new(e)),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
tx_msg_in
|
|
|
|
.send(Task::new(
|
|
|
|
MsgIn::External(ExternalMsg::LogError(e.to_string())),
|
|
|
|
None,
|
|
|
|
))
|
|
|
|
.unwrap_or_default(); // Let's not panic when xplr stops
|
|
|
|
thread::sleep(Duration::from_secs(1));
|
2021-04-02 12:33:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-05 17:44:46 +00:00
|
|
|
}
|
2021-04-02 12:33:56 +00:00
|
|
|
}
|