1+ use crate :: app_ui:: widgets:: CommonStateManager ;
12use std:: collections:: VecDeque ;
23use std:: rc:: Rc ;
34use std:: sync:: atomic:: AtomicBool ;
45use std:: sync:: Arc ;
56
6- use super :: async_task_channel:: { ChannelRequestSender , ChannelResponseReceiver } ;
7+ use super :: async_task_channel:: { ChannelRequestSender , TaskResponse } ;
78use super :: event:: VimPingSender ;
89use super :: widgets:: help_bar:: HelpBar ;
910use super :: widgets:: notification:: { Notification , WidgetName , WidgetVariant } ;
@@ -14,6 +15,7 @@ use super::widgets::topic_list::TopicTagListWidget;
1415use super :: widgets:: Widget ;
1516use crate :: config:: Config ;
1617use crate :: errors:: AppResult ;
18+ use crossterm:: event:: { KeyCode , KeyEvent , KeyModifiers } ;
1719use indexmap:: IndexMap ;
1820
1921/// Application.
@@ -28,8 +30,6 @@ pub struct App {
2830
2931 pub task_request_sender : ChannelRequestSender ,
3032
31- pub task_response_recv : ChannelResponseReceiver ,
32-
3333 pub pending_notifications : VecDeque < Option < Notification > > ,
3434
3535 pub ( crate ) popup_stack : Vec < Popup > ,
@@ -45,7 +45,6 @@ impl App {
4545 /// Constructs a new instance of [`App`].
4646 pub fn new (
4747 task_request_sender : ChannelRequestSender ,
48- task_response_recv : ChannelResponseReceiver ,
4948 vim_tx : VimPingSender ,
5049 vim_running : Arc < AtomicBool > ,
5150 config : Rc < Config > ,
@@ -82,7 +81,6 @@ impl App {
8281 widget_map : IndexMap :: from ( order) ,
8382 selected_wid_idx : 0 ,
8483 task_request_sender,
85- task_response_recv,
8684 pending_notifications : vec ! [ ] . into ( ) ,
8785 popup_stack : vec ! [ ] ,
8886 vim_running,
@@ -158,7 +156,11 @@ impl App {
158156 }
159157
160158 /// Handles the tick event of the terminal.
161- pub fn tick ( & mut self ) -> AppResult < ( ) > {
159+ pub fn tick ( & mut self , task : Option < TaskResponse > ) -> AppResult < ( ) > {
160+ if let Some ( task) = task {
161+ self . process_task ( task) ?;
162+ }
163+
162164 if let Some ( popup) = self . get_current_popup_mut ( ) {
163165 if !popup. is_active ( ) {
164166 self . popup_stack . pop ( ) ;
@@ -177,19 +179,15 @@ impl App {
177179 self . pending_notifications . push_back ( Some ( notif) ) ;
178180 }
179181 }
180-
181- self . check_for_task ( ) ?;
182182 self . process_pending_notification ( ) ?;
183183 Ok ( ( ) )
184184 }
185185
186- fn check_for_task ( & mut self ) -> AppResult < ( ) > {
187- if let Ok ( task_result) = self . task_response_recv . try_recv ( ) {
188- self . widget_map
189- . get_mut ( & task_result. get_widget_name ( ) )
190- . unwrap ( )
191- . process_task_response ( task_result) ?;
192- }
186+ pub fn process_task ( & mut self , task : TaskResponse ) -> AppResult < ( ) > {
187+ self . widget_map
188+ . get_mut ( & task. get_widget_name ( ) )
189+ . unwrap ( )
190+ . process_task_response ( task) ?;
193191 Ok ( ( ) )
194192 }
195193
@@ -218,4 +216,36 @@ impl App {
218216 pub fn quit ( & mut self ) {
219217 self . running = false ;
220218 }
219+
220+ pub fn handle_key_events ( & mut self , key_event : KeyEvent ) -> AppResult < ( ) > {
221+ let mut p_notif = None ;
222+
223+ // if ui has active popups then send only events registered with popup
224+ if let Some ( popup) = self . get_current_popup_mut ( ) {
225+ p_notif = popup. handler ( key_event) ?;
226+ } else if self . get_current_widget ( ) . parent_can_handle_events ( ) {
227+ match key_event. code {
228+ KeyCode :: Left => p_notif = self . next_widget ( ) ?,
229+ KeyCode :: Right => p_notif = self . prev_widget ( ) ?,
230+ KeyCode :: Char ( 'q' ) | KeyCode :: Char ( 'Q' ) => {
231+ self . running = false ;
232+ }
233+ KeyCode :: Char ( 'c' ) | KeyCode :: Char ( 'C' ) => {
234+ if key_event. modifiers == KeyModifiers :: CONTROL {
235+ self . running = false ;
236+ }
237+ }
238+ _ => {
239+ p_notif = self . get_current_widget_mut ( ) . handler ( key_event) ?;
240+ }
241+ }
242+ } else {
243+ p_notif = self . get_current_widget_mut ( ) . handler ( key_event) ?;
244+ }
245+
246+ self . pending_notifications . push_back ( p_notif) ;
247+ self . tick ( None ) ?;
248+
249+ Ok ( ( ) )
250+ }
221251}
0 commit comments