@@ -9,14 +9,16 @@ use tuirealm::{
99use crate :: {
1010 app:: {
1111 ids:: Id ,
12- messages:: { CscsMsg , ErrorPopupMsg , InfoPopupMsg , LoginPopupMsg , MenuMsg , Msg } ,
12+ messages:: {
13+ CscsMsg , ErrorPopupMsg , InfoPopupMsg , LoginPopupMsg , MenuMsg , Msg , SystemSelectMsg ,
14+ } ,
1315 user_events:: UserEvent ,
1416 } ,
1517 components:: {
1618 error_popup:: ErrorPopup , info_popup:: InfoPopup , login_popup:: LoginPopup ,
17- workload_menu:: WorkloadMenu ,
19+ system_select_popup :: SystemSelectPopup , workload_menu:: WorkloadMenu ,
1820 } ,
19- cscs:: cli:: cscs_login,
21+ cscs:: { cli:: cscs_login, handlers :: cscs_system_set } ,
2022 trace_dbg,
2123 util:: ui:: draw_area_in_absolute,
2224} ;
3739
3840 ///Used to allow sending errors from tokio::spawn async jobs
3941 pub error_tx : mpsc:: Sender < String > ,
42+
43+ /// Triggers async request to select current system
44+ pub select_system_tx : mpsc:: Sender < ( ) > ,
4045}
4146
4247impl < T > Model < T >
@@ -47,13 +52,15 @@ where
4752 app : Application < Id , Msg , UserEvent > ,
4853 bridge : TerminalBridge < T > ,
4954 error_tx : mpsc:: Sender < String > ,
55+ select_system_tx : mpsc:: Sender < ( ) > ,
5056 ) -> Self {
5157 Self {
5258 app,
5359 quit : false ,
5460 redraw : true ,
5561 terminal : bridge,
5662 error_tx,
63+ select_system_tx,
5764 }
5865 }
5966
9198 let popup = draw_area_in_absolute( f. area( ) , 10 ) ;
9299 f. render_widget( Clear , popup) ;
93100 self . app. view( & Id :: LoginPopup , f, popup) ;
101+ } else if self . app. mounted( & Id :: SystemSelectPopup ) {
102+ let popup = draw_area_in_absolute( f. area( ) , 10 ) ;
103+ f. render_widget( Clear , popup) ;
104+ self . app. view( & Id :: SystemSelectPopup , f, popup) ;
94105 }
95106 } )
96107 . is_ok( )
@@ -117,6 +128,32 @@ where
117128 }
118129 }
119130 }
131+ fn handle_system_select_popup_msg ( & mut self , msg : SystemSelectMsg ) -> Option < Msg > {
132+ match msg {
133+ SystemSelectMsg :: Opened ( systems) => {
134+ assert ! (
135+ self . app
136+ . mount(
137+ Id :: SystemSelectPopup ,
138+ Box :: new( SystemSelectPopup :: new( systems) ) ,
139+ vec![ ]
140+ )
141+ . is_ok( )
142+ ) ;
143+ assert ! ( self . app. active( & Id :: SystemSelectPopup ) . is_ok( ) ) ;
144+ trace_dbg ! ( "mounted system select popup" ) ;
145+ None
146+ }
147+ SystemSelectMsg :: Closed => {
148+ assert ! ( self . app. umount( & Id :: SystemSelectPopup ) . is_ok( ) ) ;
149+ None
150+ }
151+ SystemSelectMsg :: SystemSelected ( system) => {
152+ assert ! ( self . app. umount( & Id :: SystemSelectPopup ) . is_ok( ) ) ;
153+ Some ( Msg :: Cscs ( CscsMsg :: SystemSelected ( system) ) )
154+ }
155+ }
156+ }
120157 fn handle_error_popup_msg ( & mut self , msg : ErrorPopupMsg ) -> Option < Msg > {
121158 match msg {
122159 ErrorPopupMsg :: Opened ( error_msg) => {
@@ -174,6 +211,10 @@ where
174211 assert ! ( self . app. umount( & Id :: Menu ) . is_ok( ) ) ;
175212 Some ( Msg :: LoginPopup ( LoginPopupMsg :: Opened ) )
176213 }
214+ MenuMsg :: CscsSwitchSystem => {
215+ assert ! ( self . app. umount( & Id :: Menu ) . is_ok( ) ) ;
216+ Some ( Msg :: Cscs ( CscsMsg :: SelectSystem ) )
217+ }
177218 }
178219 }
179220}
@@ -218,8 +259,32 @@ where
218259 } ) ;
219260 None
220261 }
221- Msg :: None => None ,
262+ Msg :: Cscs ( CscsMsg :: SelectSystem ) => {
263+ let system_select_tx = self . select_system_tx . clone ( ) ;
264+ tokio:: spawn ( async move {
265+ system_select_tx. send ( ( ) ) . await . unwrap ( ) ;
266+ } ) ;
267+ None
268+ }
269+ Msg :: Cscs ( CscsMsg :: SystemSelected ( system) ) => {
270+ let error_tx = self . error_tx . clone ( ) ;
271+ tokio:: spawn ( async move {
272+ match cscs_system_set ( system, true ) . await {
273+ Ok ( _) => { }
274+ Err ( e) => error_tx
275+ . send ( format ! (
276+ "{:?}" ,
277+ Err :: <( ) , Report >( e) . wrap_err( "failed to set current system" )
278+ ) )
279+ . await
280+ . unwrap ( ) ,
281+ } ;
282+ } ) ;
283+ None
284+ }
222285 Msg :: LoginPopup ( msg) => self . handle_login_popup_msg ( msg) ,
286+ Msg :: SystemSelectPopup ( msg) => self . handle_system_select_popup_msg ( msg) ,
287+ Msg :: None => None ,
223288 }
224289 } else {
225290 None
0 commit comments