@@ -6,6 +6,7 @@ use ratatui::{
66 widgets:: { Block , Borders , Clear , Paragraph } ,
77 Frame ,
88} ;
9+ use strum:: { EnumCount , IntoEnumIterator } ;
910
1011use crate :: {
1112 app:: Environment ,
@@ -22,6 +23,7 @@ use crate::{
2223pub struct BranchSortPopup {
2324 queue : Queue ,
2425 visible : bool ,
26+ selection : BranchListSortBy ,
2527 key_config : SharedKeyConfig ,
2628 theme : SharedTheme ,
2729}
@@ -32,6 +34,7 @@ impl BranchSortPopup {
3234 Self {
3335 queue : env. queue . clone ( ) ,
3436 visible : false ,
37+ selection : BranchListSortBy :: BranchNameAsc ,
3538 key_config : env. key_config . clone ( ) ,
3639 theme : env. theme . clone ( ) ,
3740 }
@@ -47,21 +50,46 @@ impl BranchSortPopup {
4750 self . queue . push ( InternalEvent :: BranchListSort ( sort_by) ) ;
4851 }
4952
53+ fn move_selection ( & mut self , up : bool ) {
54+ let diff = if up {
55+ BranchListSortBy :: COUNT . saturating_sub ( 1 )
56+ } else {
57+ 1
58+ } ;
59+ let new_selection = ( self . selection as usize )
60+ . saturating_add ( diff)
61+ . rem_euclid ( BranchListSortBy :: COUNT ) ;
62+ self . selection = BranchListSortBy :: iter ( )
63+ . collect :: < Vec < BranchListSortBy > > ( ) [ new_selection] ;
64+ }
65+
5066 fn get_sort_key_lines ( & self ) -> Vec < Line > {
5167 let texts = [
52- strings:: sort_branch_by_name_msg ( & self . key_config ) ,
53- strings:: sort_branch_by_name_rev_msg ( & self . key_config ) ,
54- strings:: sort_branch_by_time_msg ( & self . key_config ) ,
55- strings:: sort_branch_by_time_rev_msg ( & self . key_config ) ,
56- strings:: sort_branch_by_author_msg ( & self . key_config ) ,
57- strings:: sort_branch_by_author_rev_msg ( & self . key_config ) ,
68+ strings:: sort_branch_by_name_msg (
69+ self . selection . is_branch_name_asc ( ) ,
70+ ) ,
71+ strings:: sort_branch_by_name_rev_msg (
72+ self . selection . is_branch_name_desc ( ) ,
73+ ) ,
74+ strings:: sort_branch_by_time_msg (
75+ self . selection . is_last_commit_time_desc ( ) ,
76+ ) ,
77+ strings:: sort_branch_by_time_rev_msg (
78+ self . selection . is_last_commit_time_asc ( ) ,
79+ ) ,
80+ strings:: sort_branch_by_author_msg (
81+ self . selection . is_last_commit_author_asc ( ) ,
82+ ) ,
83+ strings:: sort_branch_by_author_rev_msg (
84+ self . selection . is_last_commit_author_desc ( ) ,
85+ ) ,
5886 ] ;
5987 texts
6088 . iter ( )
6189 . map ( |t| {
6290 Line :: from ( vec ! [ Span :: styled(
6391 t. clone( ) ,
64- self . theme. text( true , false ) ,
92+ self . theme. text( true , t . starts_with ( "[x]" ) ) ,
6593 ) ] )
6694 } )
6795 . collect ( )
@@ -71,10 +99,12 @@ impl BranchSortPopup {
7199impl DrawableComponent for BranchSortPopup {
72100 fn draw ( & self , f : & mut Frame , area : Rect ) -> Result < ( ) > {
73101 if self . is_visible ( ) {
74- const MAX_SIZE : ( u16 , u16 ) = ( 50 , 8 ) ;
102+ let height = u16:: try_from ( BranchListSortBy :: COUNT ) ?
103+ . saturating_add ( 2 ) ;
104+ let max_size: ( u16 , u16 ) = ( 50 , height) ;
75105
76106 let mut area = ui:: centered_rect_absolute (
77- MAX_SIZE . 0 , MAX_SIZE . 1 , area,
107+ max_size . 0 , max_size . 1 , area,
78108 ) ;
79109
80110 f. render_widget ( Clear , area) ;
@@ -116,7 +146,14 @@ impl Component for BranchSortPopup {
116146 ) -> CommandBlocking {
117147 if self . is_visible ( ) || force_all {
118148 out. push ( CommandInfo :: new (
119- strings:: commands:: close_popup ( & self . key_config ) ,
149+ strings:: commands:: close_branch_sort_popup (
150+ & self . key_config ,
151+ ) ,
152+ true ,
153+ true ,
154+ ) ) ;
155+ out. push ( CommandInfo :: new (
156+ strings:: commands:: scroll ( & self . key_config ) ,
120157 true ,
121158 true ,
122159 ) ) ;
@@ -131,56 +168,20 @@ impl Component for BranchSortPopup {
131168 ) -> Result < EventState > {
132169 if self . is_visible ( ) {
133170 if let Event :: Key ( key) = event {
134- if key_match ( key, self . key_config . keys . exit_popup ) {
135- self . hide ( ) ;
136- } else if key_match (
137- key,
138- self . key_config . keys . branch_sort_by_name ,
139- ) {
140- self . update_sort_key (
141- BranchListSortBy :: BranchNameAsc ,
142- ) ;
143- self . hide ( ) ;
144- } else if key_match (
145- key,
146- self . key_config . keys . branch_sort_by_name_rev ,
147- ) {
148- self . update_sort_key (
149- BranchListSortBy :: BranchNameDesc ,
150- ) ;
171+ if key_match ( key, self . key_config . keys . exit_popup )
172+ || key_match ( key, self . key_config . keys . enter )
173+ {
151174 self . hide ( ) ;
175+ } else if key_match ( key, self . key_config . keys . move_up )
176+ {
177+ self . move_selection ( true ) ;
178+ self . update_sort_key ( self . selection ) ;
152179 } else if key_match (
153180 key,
154- self . key_config . keys . branch_sort_by_time ,
181+ self . key_config . keys . move_down ,
155182 ) {
156- self . update_sort_key (
157- BranchListSortBy :: LastCommitTimeDesc ,
158- ) ;
159- self . hide ( ) ;
160- } else if key_match (
161- key,
162- self . key_config . keys . branch_sort_by_time_rev ,
163- ) {
164- self . update_sort_key (
165- BranchListSortBy :: LastCommitTimeAsc ,
166- ) ;
167- self . hide ( ) ;
168- } else if key_match (
169- key,
170- self . key_config . keys . branch_sort_by_author ,
171- ) {
172- self . update_sort_key (
173- BranchListSortBy :: LastCommitAuthorAsc ,
174- ) ;
175- self . hide ( ) ;
176- } else if key_match (
177- key,
178- self . key_config . keys . branch_sort_by_author_rev ,
179- ) {
180- self . update_sort_key (
181- BranchListSortBy :: LastCommitAuthorDesc ,
182- ) ;
183- self . hide ( ) ;
183+ self . move_selection ( false ) ;
184+ self . update_sort_key ( self . selection ) ;
184185 }
185186 }
186187 return Ok ( EventState :: Consumed ) ;
0 commit comments