I run event reading using crossterm::event::poll
inside separate tokio-rs task, but seems like no events captured. This is part where I read for events:
async fn handle_ui_input(&mut self) -> JoinHandle<()> {
let key_event_income = self._message_duplex.lock().await.key_event_income.clone();
tokio::spawn(async move {
let mut ui_input = UIInput::new(key_event_income);
loop {
ui_input.handle();
}
})
}
pub struct UIInput {
_key_event_income: KeyEventIncome,
}
impl UIInput {
pub fn new(key_event_income: KeyEventIncome) -> Self {
Self {
_key_event_income: key_event_income,
}
}
pub fn handle(&mut self) {
if event::poll(Duration::from_millis(UI_INPUT_TICK_RATE)).unwrap() {
if let Event::Key(key) = event::read().unwrap() {
self._key_event_income.send_key_event(key);
}
}
}
}
if condition never true
here. So, this is my task where I render UI:
async fn handle_ui(&mut self) -> JoinHandle<()> {
let message_duplex = Arc::clone(&self._message_duplex);
tokio::spawn(async move {
let mut ui = UI::new(CrosstermBackend::new(std::io::stdout()));
loop {
ui.render(UIOptions {
message: message_duplex.lock().await.get_income(),
});
sleep(Duration::from_millis(WRITE_TIMEOUT)).await;
}
})
}
pub struct UI<'a, B: Backend> {
_terminal: Terminal<B>,
_state_flags: UIStateFlags,
_debug_panel: DebugPanel<'a>,
// ... rest components
}
impl<'a, B: Backend> UI<'a, B> {
pub fn new(backend: B) -> Self {
enable_raw_mode().unwrap();
execute!(std::io::stdout(), EnterAlternateScreen, EnableMouseCapture).unwrap();
let mut _terminal = Terminal::new(backend).unwrap();
_terminal.clear().unwrap();
_terminal.hide_cursor().unwrap();
Self {
_terminal,
_debug_panel: DebugPanel::new(),
// ... rest components
}
}
}
in case it will be useful, this is code of MessageDuplex
:
pub struct MessageDuplex {
pub dialog_income: DialogIncome,
pub dialog_outcome: DialogOutcome,
pub message_income: MessageIncome,
pub key_event_income: KeyEventIncome,
_income_receiver: Receiver<IncomeMessageType>,
_outcome_receiver: Receiver<OutcomeMessageType>,
}
impl MessageDuplex {
pub fn new() -> Self {
let (input_tx, input_rx) = mpsc::channel::<IncomeMessageType>();
let (output_tx, output_rx) = mpsc::channel::<OutcomeMessageType>();
Self {
// from client to UI
dialog_income: DialogIncome::new(input_tx.clone()),
// from UI (dialog/modal/popup) to client
dialog_outcome: DialogOutcome::new(output_tx.clone()),
// from client to UI
message_income: MessageIncome::new(input_tx.clone()),
// from client to UI
key_event_income: KeyEventIncome::new(input_tx.clone()),
_income_receiver: input_rx,
_outcome_receiver: output_rx,
}
}
// messages from client to UI
pub fn get_income(&mut self) -> IncomeMessageType {
self._income_receiver.recv().unwrap()
}
// messages from UI to client
pub fn get_outcome(&mut self) -> OutcomeMessageType {
self._outcome_receiver.recv().unwrap()
}
}
Could somebody help me to find the reason why crossterm::event::poll not work ? Or what possible reasons can be ? I can't capture key events for now.