@@ -199,6 +199,25 @@ fn piano_step(snap: Snap, beat_px: f32, beats_per_bar: f64, beat_unit: f64) -> f
199199 }
200200}
201201
202+ /// Pixels a page-follow leaves between the view's left edge and the playhead.
203+ const FOLLOW_MARGIN : f32 = 8.0 ;
204+
205+ /// The horizontal scroll that keeps the playhead on screen while the transport plays or records.
206+ /// `playhead_px` is the playhead in content pixels (beats times the beat width). While it is in
207+ /// the view, the scroll is unchanged; once it leaves the view on either side, the view pages so
208+ /// the playhead sits near its left edge, as Logic's Page follow does. The caller clamps the result
209+ /// to the content.
210+ pub fn follow_scroll ( scroll_x : f32 , view_w : f32 , playhead_px : f32 ) -> f32 {
211+ if !( scroll_x. is_finite ( ) && view_w. is_finite ( ) && playhead_px. is_finite ( ) ) {
212+ return scroll_x;
213+ }
214+ if playhead_px >= scroll_x && playhead_px <= scroll_x + view_w {
215+ scroll_x
216+ } else {
217+ ( playhead_px - FOLLOW_MARGIN ) . max ( 0.0 )
218+ }
219+ }
220+
202221/// Shortest a scrollbar thumb is drawn, so it stays easy to grab.
203222const MIN_THUMB : f32 = 24.0 ;
204223
@@ -778,6 +797,10 @@ pub fn show(ui: &mut Ui, p: &mut Project, s: &mut UiState) {
778797 } ) ,
779798 ) ;
780799 }
800+ // While the transport plays or records, the view follows the playhead (see `follow_scroll`).
801+ if ( p. playing || p. recording ) && ( 0.0 ..=f64:: from ( len_beats) ) . contains ( & position) {
802+ v. scroll_x = follow_scroll ( v. scroll_x , grid. width ( ) , position as f32 * v. beat_px ) ;
803+ }
781804 v. scroll_y = v. scroll_y . clamp ( 0.0 , ( content_h - grid. height ( ) ) . max ( 0.0 ) ) ;
782805 v. scroll_x = v. scroll_x . clamp ( 0.0 , ( content_w - grid. width ( ) ) . max ( 0.0 ) ) ;
783806 let ( beat_px, key_h, scroll_x, scroll_y) = ( v. beat_px , v. key_h , v. scroll_x , v. scroll_y ) ;
@@ -1679,6 +1702,39 @@ mod tests {
16791702 assert_eq ! ( piano_step( Snap :: Beat , 100.0 , 3.0 , 0.5 ) , 0.5 ) ;
16801703 }
16811704
1705+ #[ test]
1706+ fn follow_keeps_the_view_while_the_playhead_is_visible ( ) {
1707+ // Visible range is [100, 500]; a playhead inside it changes nothing.
1708+ assert_eq ! ( follow_scroll( 100.0 , 400.0 , 300.0 ) , 100.0 ) ;
1709+ assert_eq ! ( follow_scroll( 100.0 , 400.0 , 100.0 ) , 100.0 ) ;
1710+ assert_eq ! ( follow_scroll( 100.0 , 400.0 , 500.0 ) , 100.0 ) ;
1711+ }
1712+
1713+ #[ test]
1714+ fn follow_pages_forward_when_the_playhead_leaves_the_right_edge ( ) {
1715+ // Issue 150: once the playhead passes the visible range the view must page to it.
1716+ let scroll = follow_scroll ( 100.0 , 400.0 , 900.0 ) ;
1717+ assert_eq ! ( scroll, 900.0 - FOLLOW_MARGIN ) ;
1718+ // And the playhead is then on screen.
1719+ assert ! ( ( scroll..=scroll + 400.0 ) . contains( & 900.0 ) ) ;
1720+ }
1721+
1722+ #[ test]
1723+ fn follow_pages_back_when_the_playhead_is_before_the_view ( ) {
1724+ // A loop back or a locate to the left.
1725+ assert_eq ! (
1726+ follow_scroll( 800.0 , 400.0 , 50.0 ) ,
1727+ ( 50.0 - FOLLOW_MARGIN ) . max( 0.0 )
1728+ ) ;
1729+ assert_eq ! ( follow_scroll( 800.0 , 400.0 , 2.0 ) , 0.0 ) ;
1730+ }
1731+
1732+ #[ test]
1733+ fn follow_ignores_a_non_finite_playhead ( ) {
1734+ assert_eq ! ( follow_scroll( 120.0 , 400.0 , f32 :: NAN ) , 120.0 ) ;
1735+ assert_eq ! ( follow_scroll( 120.0 , f32 :: INFINITY , 300.0 ) , 120.0 ) ;
1736+ }
1737+
16821738 #[ test]
16831739 fn a_scrollbar_shorter_than_its_thumb_does_not_crash ( ) {
16841740 // The crash report: a 23 px scrollbar, thumb at least 24 px.
0 commit comments