@@ -22,7 +22,7 @@ use std::{
22
22
rc:: { Rc , Weak } ,
23
23
} ;
24
24
25
- use cursive_core:: { event:: EventResult , view:: IntoBoxedView , Rect , Vec2 , View , XY } ;
25
+ use cursive_core:: { event:: { EventResult , Event , Key } , view:: IntoBoxedView , Rect , Vec2 , View , XY , direction :: { Relative , Absolute , Direction } } ;
26
26
use layout:: { Layout , PlacedElement } ;
27
27
28
28
/// A container that can be used to display a list of items in a flexible way.
@@ -940,16 +940,43 @@ impl Flexbox {
940
940
} ) ;
941
941
result
942
942
}
943
+
944
+ fn try_focus ( & mut self , child_index : usize , source : Direction ) -> Option < EventResult > {
945
+ if let Some ( view) = self . content . get ( child_index) {
946
+ if let Ok ( result) = view. borrow_mut ( ) . view . take_focus ( source) {
947
+ self . focused = Some ( child_index) ;
948
+ return Some ( result) ;
949
+ }
950
+ }
951
+ None
952
+ }
953
+
954
+ fn move_focus_rel ( & mut self , target : Relative ) -> EventResult {
955
+ let adv: isize = if target == Relative :: Front { -1 } else { 1 } ;
956
+ let child_count = self . content . len ( ) ;
957
+ let get_next = |n| ( n as isize + adv) . rem_euclid ( child_count as isize ) as usize ;
958
+ let mut attempts = child_count - 1 ;
959
+ let mut next = if let Some ( n) = self . focused { get_next ( n) } else { 0 } ;
960
+ while attempts > 0 {
961
+ if let Some ( result) = self . try_focus ( next, Direction :: Rel ( target) ) {
962
+ return result;
963
+ }
964
+ next = get_next ( next) ;
965
+ attempts -= 1 ;
966
+ }
967
+ EventResult :: Ignored
968
+ }
969
+
943
970
}
944
971
945
972
impl View for Flexbox {
946
973
/// Draw this view using the printer.
947
974
fn draw ( & self , printer : & cursive_core:: Printer < ' _ , ' _ > ) {
948
975
if let Some ( ref layout) = self . layout {
949
- for placed_element in layout {
976
+ for ( i , placed_element) in layout. iter ( ) . enumerate ( ) {
950
977
RefCell :: borrow ( & placed_element. element )
951
978
. view
952
- . draw ( & printer. windowed ( placed_element. position ) ) ;
979
+ . draw ( & printer. windowed ( placed_element. position ) . focused ( Some ( i ) == self . focused ) ) ;
953
980
}
954
981
}
955
982
}
@@ -992,7 +1019,13 @@ impl View for Flexbox {
992
1019
& mut self ,
993
1020
mut event : cursive_core:: event:: Event ,
994
1021
) -> cursive_core:: event:: EventResult {
995
- if let cursive_core:: event:: Event :: Mouse {
1022
+ if let Some ( result) = match event {
1023
+ Event :: Shift ( Key :: Tab ) => Some ( self . move_focus_rel ( Relative :: Front ) ) ,
1024
+ Event :: Key ( Key :: Tab ) => Some ( self . move_focus_rel ( Relative :: Back ) ) ,
1025
+ _ => None ,
1026
+ } {
1027
+ result
1028
+ } else if let cursive_core:: event:: Event :: Mouse {
996
1029
ref mut offset,
997
1030
ref mut position,
998
1031
..
@@ -1003,9 +1036,13 @@ impl View for Flexbox {
1003
1036
layout. element_at ( global_to_view_coordinates ( * position, * offset) )
1004
1037
{
1005
1038
* offset = * offset + placed_element. position . top_left ( ) ;
1006
- RefCell :: borrow_mut ( & placed_element. element )
1007
- . view
1008
- . on_event ( event)
1039
+ if let Some ( index) = self . content . iter ( ) . position ( |v| v. as_ptr ( ) == placed_element. element . as_ptr ( ) ) {
1040
+ self . try_focus ( index, Direction :: Abs ( Absolute :: None ) ) ;
1041
+ self . content [ index] . borrow_mut ( ) . view . on_event ( event)
1042
+ }
1043
+ else {
1044
+ RefCell :: borrow_mut ( & placed_element. element ) . view . on_event ( event)
1045
+ }
1009
1046
} else {
1010
1047
EventResult :: Ignored
1011
1048
}
0 commit comments