@@ -14,7 +14,7 @@ use std::{fs, io, panic};
14
14
15
15
use crossterm:: event:: {
16
16
DisableMouseCapture , EnableMouseCapture , KeyCode , KeyEvent , KeyEventKind , KeyModifiers ,
17
- MouseEvent , MouseEventKind ,
17
+ MouseButton , MouseEvent , MouseEventKind ,
18
18
} ;
19
19
use crossterm:: terminal:: {
20
20
disable_raw_mode, enable_raw_mode, is_raw_mode_enabled, EnterAlternateScreen ,
@@ -68,6 +68,12 @@ enum SelectionKey {
68
68
Line ( LineKey ) ,
69
69
}
70
70
71
+ impl Default for SelectionKey {
72
+ fn default ( ) -> Self {
73
+ Self :: None
74
+ }
75
+ }
76
+
71
77
/// A copy of the contents of the screen at a certain point in time.
72
78
#[ derive( Clone , Debug , Default , Eq , PartialEq ) ]
73
79
pub struct TestingScreenshot {
@@ -116,6 +122,7 @@ pub enum Event {
116
122
FocusOuter ,
117
123
ToggleItem ,
118
124
ToggleItemAndAdvance ,
125
+ Click { row : usize , column : usize } ,
119
126
}
120
127
121
128
impl From < crossterm:: event:: Event > for Event {
@@ -252,6 +259,16 @@ impl From<crossterm::event::Event> for Event {
252
259
state : _,
253
260
} ) => Self :: ToggleItemAndAdvance ,
254
261
262
+ Event :: Mouse ( MouseEvent {
263
+ kind : MouseEventKind :: Down ( MouseButton :: Left ) ,
264
+ column,
265
+ row,
266
+ modifiers : _,
267
+ } ) => Self :: Click {
268
+ row : row. into ( ) ,
269
+ column : column. into ( ) ,
270
+ } ,
271
+
255
272
_event => Self :: None ,
256
273
}
257
274
}
@@ -763,6 +780,11 @@ impl<'a> Recorder<'a> {
763
780
let advanced_key = self . advance_to_next_of_kind ( ) ;
764
781
StateUpdate :: ToggleItemAndAdvance ( self . selection_key , advanced_key)
765
782
}
783
+
784
+ ( _, Event :: Click { row, column } ) => {
785
+ let component_id = self . find_component_at ( drawn_rects, row, column) ;
786
+ self . click_component ( component_id)
787
+ }
766
788
} ;
767
789
Ok ( state_update)
768
790
}
@@ -994,6 +1016,37 @@ impl<'a> Recorder<'a> {
994
1016
}
995
1017
}
996
1018
1019
+ fn find_component_at (
1020
+ & self ,
1021
+ drawn_rects : & HashMap < ComponentId , Rect > ,
1022
+ row : usize ,
1023
+ column : usize ,
1024
+ ) -> ComponentId {
1025
+ let x = column. unwrap_isize ( ) ;
1026
+ let y = row. unwrap_isize ( ) + self . scroll_offset_y ;
1027
+ drawn_rects
1028
+ . iter ( )
1029
+ . filter ( |( _id, rect) | rect. contains_point ( x, y) )
1030
+ . min_by ( |( _, lhs) , ( _, rhs) | lhs. size ( ) . area ( ) . cmp ( & rhs. size ( ) . area ( ) ) )
1031
+ . map ( |( id, _rect) | id. clone ( ) )
1032
+ . unwrap_or ( ComponentId :: App )
1033
+ }
1034
+
1035
+ fn click_component ( & self , component_id : ComponentId ) -> StateUpdate {
1036
+ match component_id {
1037
+ ComponentId :: App | ComponentId :: QuitDialog => StateUpdate :: None ,
1038
+ ComponentId :: SelectableItem ( selection_key) => StateUpdate :: SelectItem ( selection_key) ,
1039
+ ComponentId :: TristateBox => {
1040
+ // TODO: implement toggling the checkbox
1041
+ StateUpdate :: None
1042
+ }
1043
+ ComponentId :: QuitDialogButton ( QuitDialogButtonId :: GoBack ) => {
1044
+ StateUpdate :: SetQuitDialog ( None )
1045
+ }
1046
+ ComponentId :: QuitDialogButton ( QuitDialogButtonId :: Quit ) => StateUpdate :: QuitCancel ,
1047
+ }
1048
+ }
1049
+
997
1050
fn toggle_item ( & mut self , selection : SelectionKey ) -> Result < ( ) , RecordError > {
998
1051
match selection {
999
1052
SelectionKey :: None => { }
0 commit comments