@@ -528,36 +528,60 @@ impl<'a> UI<'a> {
528528 Ok ( ( ) )
529529 }
530530
531- fn run_loop ( & mut self , terminal : & mut Terminal < CrosstermBackend < io:: Stdout > > ) -> Result < ( ) > {
531+ fn update_viewport ( & mut self , size : Rect ) {
532+ // Editor area: 70% (right column) × 80% (editor pane) = 56% of total height
533+ let viewport_height = ( size. height as f32 * 0.70 * 0.80 ) as usize ;
534+ // Editor width: 70% (right column)
535+ let content_width = ( size. width as f32 * 0.70 ) as usize ;
536+ self . engine . set_viewport_height ( viewport_height) ;
537+ self . engine . set_content_width ( content_width) ;
538+ }
539+
540+ fn handle_pending_event < Poll , Read > ( & mut self , poll : & mut Poll , read : & mut Read ) -> Result < ( ) >
541+ where
542+ Poll : FnMut ( Duration ) -> io:: Result < bool > ,
543+ Read : FnMut ( ) -> io:: Result < Event > ,
544+ {
545+ if poll ( Duration :: from_millis ( 8 ) ) ? {
546+ self . handle_event ( read ( ) ?) ;
547+ }
548+ Ok ( ( ) )
549+ }
550+
551+ fn run_loop_with < B , Poll , Read , Now > (
552+ & mut self ,
553+ terminal : & mut Terminal < B > ,
554+ mut poll : Poll ,
555+ mut read : Read ,
556+ mut now : Now ,
557+ ) -> Result < ( ) >
558+ where
559+ B : Backend ,
560+ B :: Error : std:: error:: Error + Send + Sync + ' static ,
561+ Poll : FnMut ( Duration ) -> io:: Result < bool > ,
562+ Read : FnMut ( ) -> io:: Result < Event > ,
563+ Now : FnMut ( ) -> Instant ,
564+ {
532565 loop {
533566 self . sync_exit_state ( ) ;
534567
535- // Update viewport dimensions for scroll calculation
536- let size = terminal. size ( ) ?;
537- // Editor area: 70% (right column) × 80% (editor pane) = 56% of total height
538- let viewport_height = ( size. height as f32 * 0.70 * 0.80 ) as usize ;
539- // Editor width: 70% (right column)
540- let content_width = ( size. width as f32 * 0.70 ) as usize ;
541- self . engine . set_viewport_height ( viewport_height) ;
542- self . engine . set_content_width ( content_width) ;
543-
544- // Tick the animation engine
568+ self . update_viewport ( terminal. size ( ) ?. into ( ) ) ;
545569 let needs_redraw = self . engine . tick ( ) ;
546570 self . draw_if_needed ( terminal, needs_redraw) ?;
571+ self . handle_pending_event ( & mut poll, & mut read) ?;
547572
548- // Poll for keyboard events at frame rate
549- if event:: poll ( std:: time:: Duration :: from_millis ( 8 ) ) ? {
550- self . handle_event ( event:: read ( ) ?) ;
551- }
552-
553- if !self . advance_state_after_tick ( Instant :: now ( ) ) {
573+ if !self . advance_state_after_tick ( now ( ) ) {
554574 break ;
555575 }
556576 }
557577
558578 Ok ( ( ) )
559579 }
560580
581+ fn run_loop ( & mut self , terminal : & mut Terminal < CrosstermBackend < io:: Stdout > > ) -> Result < ( ) > {
582+ self . run_loop_with ( terminal, event:: poll, event:: read, Instant :: now)
583+ }
584+
561585 fn render ( & mut self , f : & mut Frame ) {
562586 let size = f. area ( ) ;
563587
@@ -968,6 +992,10 @@ mod tests {
968992 event:: KeyEvent :: new ( code, KeyModifiers :: NONE )
969993 }
970994
995+ fn quit_event ( ) -> io:: Result < Event > {
996+ Ok ( Event :: Key ( key_event ( KeyCode :: Char ( 'q' ) ) ) )
997+ }
998+
971999 #[ derive( Clone ) ]
9721000 struct SharedWriter ( Arc < Mutex < Vec < u8 > > > ) ;
9731001
@@ -1172,6 +1200,112 @@ mod tests {
11721200 assert_eq ! ( quit_ui. state, UIState :: Finished ) ;
11731201 }
11741202
1203+ #[ test]
1204+ fn handle_pending_event_reads_input_only_when_poll_reports_ready ( ) {
1205+ let mut idle_ui = test_ui ( ) ;
1206+ let idle_polls = Cell :: new ( 0 ) ;
1207+ idle_ui
1208+ . handle_pending_event (
1209+ & mut |duration| {
1210+ idle_polls. set ( idle_polls. get ( ) + 1 ) ;
1211+ assert_eq ! ( duration, Duration :: from_millis( 8 ) ) ;
1212+ Ok ( false )
1213+ } ,
1214+ & mut quit_event,
1215+ )
1216+ . unwrap ( ) ;
1217+ assert_eq ! ( idle_polls. get( ) , 1 ) ;
1218+ assert_eq ! ( idle_ui. state, UIState :: Playing ) ;
1219+
1220+ let mut ready_ui = test_ui ( ) ;
1221+ let ready_polls = Cell :: new ( 0 ) ;
1222+ let reads = Cell :: new ( 0 ) ;
1223+ ready_ui
1224+ . handle_pending_event (
1225+ & mut |duration| {
1226+ ready_polls. set ( ready_polls. get ( ) + 1 ) ;
1227+ assert_eq ! ( duration, Duration :: from_millis( 8 ) ) ;
1228+ Ok ( true )
1229+ } ,
1230+ & mut || {
1231+ reads. set ( reads. get ( ) + 1 ) ;
1232+ quit_event ( )
1233+ } ,
1234+ )
1235+ . unwrap ( ) ;
1236+ assert_eq ! ( ready_polls. get( ) , 1 ) ;
1237+ assert_eq ! ( reads. get( ) , 1 ) ;
1238+ assert_eq ! ( ready_ui. state, UIState :: Finished ) ;
1239+ }
1240+
1241+ #[ test]
1242+ fn run_loop_with_breaks_when_state_advance_requests_exit ( ) {
1243+ let backend = TestBackend :: new ( 80 , 24 ) ;
1244+ let mut terminal = Terminal :: new ( backend) . unwrap ( ) ;
1245+
1246+ let mut finished_ui = test_ui ( ) ;
1247+ finished_ui. state = UIState :: Finished ;
1248+ let idle_polls = Cell :: new ( 0 ) ;
1249+ finished_ui
1250+ . run_loop_with (
1251+ & mut terminal,
1252+ |duration| {
1253+ idle_polls. set ( idle_polls. get ( ) + 1 ) ;
1254+ assert_eq ! ( duration, Duration :: from_millis( 8 ) ) ;
1255+ Ok ( false )
1256+ } ,
1257+ quit_event,
1258+ Instant :: now,
1259+ )
1260+ . unwrap ( ) ;
1261+ assert_eq ! ( idle_polls. get( ) , 1 ) ;
1262+
1263+ let mut quitting_ui = test_ui ( ) ;
1264+ let ready_polls = Cell :: new ( 0 ) ;
1265+ let reads = Cell :: new ( 0 ) ;
1266+ quitting_ui
1267+ . run_loop_with (
1268+ & mut terminal,
1269+ |duration| {
1270+ ready_polls. set ( ready_polls. get ( ) + 1 ) ;
1271+ assert_eq ! ( duration, Duration :: from_millis( 8 ) ) ;
1272+ Ok ( true )
1273+ } ,
1274+ || {
1275+ reads. set ( reads. get ( ) + 1 ) ;
1276+ quit_event ( )
1277+ } ,
1278+ Instant :: now,
1279+ )
1280+ . unwrap ( ) ;
1281+ assert_eq ! ( ready_polls. get( ) , 1 ) ;
1282+ assert_eq ! ( reads. get( ) , 1 ) ;
1283+ assert_eq ! ( quitting_ui. state, UIState :: Finished ) ;
1284+
1285+ let mut looping_ui = test_ui ( ) ;
1286+ let loop_polls = Cell :: new ( 0 ) ;
1287+ let loop_reads = Cell :: new ( 0 ) ;
1288+ looping_ui
1289+ . run_loop_with (
1290+ & mut terminal,
1291+ |duration| {
1292+ let next = loop_polls. get ( ) + 1 ;
1293+ loop_polls. set ( next) ;
1294+ assert_eq ! ( duration, Duration :: from_millis( 8 ) ) ;
1295+ Ok ( next > 1 )
1296+ } ,
1297+ || {
1298+ loop_reads. set ( loop_reads. get ( ) + 1 ) ;
1299+ quit_event ( )
1300+ } ,
1301+ Instant :: now,
1302+ )
1303+ . unwrap ( ) ;
1304+ assert_eq ! ( loop_polls. get( ) , 2 ) ;
1305+ assert_eq ! ( loop_reads. get( ) , 1 ) ;
1306+ assert_eq ! ( looping_ui. state, UIState :: Finished ) ;
1307+ }
1308+
11751309 #[ test]
11761310 fn handle_key_event_navigates_menu_and_dialogs ( ) {
11771311 let mut ui = test_ui ( ) ;
0 commit comments