11use super :: helper:: * ;
22use crate :: configure:: ui:: ir_enabler:: { IrEnablerCtx , View , ui} ;
3- use crate :: configure:: ui:: keys :: * ;
3+ use crate :: configure:: ui:: { DeviceSettingsCtx , SearchSettingsCtx } ;
44use crate :: video:: ir:: analyzer:: {
55 self , IsIrWorking as AnalyzerResponse , Message as AnalyzerRequest , StreamAnalyzer ,
66} ;
@@ -24,6 +24,13 @@ use tokio::{
2424 task,
2525} ;
2626
27+ const KEY_YES : KeyCode = KeyCode :: Char ( 'y' ) ;
28+ const KEY_NO : KeyCode = KeyCode :: Char ( 'n' ) ;
29+ const KEY_EXIT : KeyCode = KeyCode :: Esc ;
30+ const KEY_NAVIGATE : KeyCode = KeyCode :: Tab ;
31+ const KEY_CONTINUE : KeyCode = KeyCode :: Enter ;
32+ const KEY_DELETE : KeyCode = KeyCode :: Backspace ;
33+
2734#[ derive( Debug ) ]
2835pub struct Config {
2936 /// Path to the video device.
@@ -422,13 +429,13 @@ impl App {
422429 }
423430
424431 /// Handles a key event based on the current application state.
425- async fn handle_key_press ( & mut self , key : Key ) -> Result < ( ) > {
432+ async fn handle_key_press ( & mut self , key : KeyCode ) -> Result < ( ) > {
426433 match self . state ( ) {
427434 State :: Menu => match key {
428435 KEY_EXIT => self . set_state ( State :: Failure ) ,
429436 KEY_NAVIGATE => self . next_setting ( ) ,
430437 KEY_DELETE => self . edit_setting ( None ) ,
431- Key ( KeyCode :: Char ( c) ) => self . edit_setting ( Some ( c) ) ,
438+ KeyCode :: Char ( c) => self . edit_setting ( Some ( c) ) ,
432439 KEY_CONTINUE => self . set_state ( State :: ConfirmStart ) ,
433440 _ => { }
434441 } ,
@@ -458,7 +465,7 @@ impl App {
458465 /// In both of the two case, also changes the state to [`State::Running`].
459466 ///
460467 /// Otherwise, does nothing.
461- async fn confirm_working ( & mut self , k : Key ) -> Result < ( ) > {
468+ async fn confirm_working ( & mut self , k : KeyCode ) -> Result < ( ) > {
462469 let mut response = IREnablerResponse :: No ;
463470 if k == KEY_YES {
464471 response = IREnablerResponse :: Yes ;
@@ -477,7 +484,7 @@ impl App {
477484 /// and sends [`IREnablerResponse::Abort`] to the configurator task.
478485 ///
479486 /// If the key is [`KEY_NO`], change the state back to [`State::Running`].
480- async fn abort_or_continue ( & mut self , k : Key ) -> Result < ( ) > {
487+ async fn abort_or_continue ( & mut self , k : KeyCode ) -> Result < ( ) > {
481488 match k {
482489 KEY_NO | KEY_EXIT => self . set_state ( self . prev_state ( ) ) ,
483490 KEY_YES => {
@@ -498,7 +505,7 @@ impl App {
498505 /// If the key is [`KEY_NO`], change the state back to the previous state.
499506 ///
500507 /// Returns directly an error if the video stream is already started.
501- fn start_or_back ( & mut self , k : Key ) -> Result < ( ) > {
508+ fn start_or_back ( & mut self , k : KeyCode ) -> Result < ( ) > {
502509 // check that the path exists
503510 if !self . is_device_valid ( ) {
504511 self . set_state ( State :: Menu ) ;
@@ -594,6 +601,18 @@ impl IrEnablerCtx for App {
594601 fn show_menu_start_prompt ( & self ) -> bool {
595602 self . state ( ) == State :: ConfirmStart
596603 }
604+ fn controls_list_state ( & mut self ) -> & mut ListState {
605+ & mut self . controls_list_state
606+ }
607+ fn controls ( & self ) -> & [ XuControl ] {
608+ & self . controls
609+ }
610+ fn image ( & self ) -> Option < & Image > {
611+ self . image . as_ref ( )
612+ }
613+ }
614+
615+ impl DeviceSettingsCtx for App {
597616 fn device_settings_list_state ( & mut self ) -> & mut ListState {
598617 & mut self . device_settings_list_state
599618 }
@@ -615,6 +634,9 @@ impl IrEnablerCtx for App {
615634 fn fps ( & self ) -> Option < u32 > {
616635 self . config . fps
617636 }
637+ }
638+
639+ impl SearchSettingsCtx for App {
618640 fn search_settings_list_state ( & mut self ) -> & mut ListState {
619641 & mut self . search_settings_list_state
620642 }
@@ -633,15 +655,6 @@ impl IrEnablerCtx for App {
633655 fn inc_step ( & self ) -> u8 {
634656 self . config . inc_step
635657 }
636- fn controls_list_state ( & mut self ) -> & mut ListState {
637- & mut self . controls_list_state
638- }
639- fn controls ( & self ) -> & [ XuControl ] {
640- & self . controls
641- }
642- fn image ( & self ) -> Option < & Image > {
643- self . image . as_ref ( )
644- }
645658}
646659
647660#[ cfg( test) ]
@@ -655,16 +668,16 @@ mod tests {
655668 App :: new ( )
656669 }
657670
658- fn make_key_event ( keycode : Key ) -> KeyEvent {
671+ fn make_key_event ( keycode : KeyCode ) -> KeyEvent {
659672 KeyEvent {
660- code : keycode. into ( ) ,
673+ code : keycode,
661674 modifiers : KeyModifiers :: NONE ,
662675 kind : KeyEventKind :: Press ,
663676 state : crossterm:: event:: KeyEventState :: NONE ,
664677 }
665678 }
666679
667- fn make_term_key_event ( keycode : Key ) -> Event {
680+ fn make_term_key_event ( keycode : KeyCode ) -> Event {
668681 Event :: Key ( make_key_event ( keycode) )
669682 }
670683
0 commit comments