@@ -9,7 +9,7 @@ namespace PostCodeSerialMonitor.Views;
99
1010public partial class MainWindow : Window
1111{
12- private bool _autoScroll = true ;
12+ private readonly AutoScrollTracker _autoScroll = new ( ) ;
1313 private ScrollViewer ? _scrollViewer ;
1414 private ItemsRepeater ? _itemsRepeater ;
1515
@@ -38,33 +38,28 @@ protected override void OnLoaded(RoutedEventArgs e)
3838 }
3939 }
4040
41- // Distance from the bottom (in pixels) still considered "at the bottom", to absorb layout jitter
42- // from item virtualization/resizing so autoscroll doesn't flicker on/off during normal updates.
43- private const double BottomThreshold = 4 ;
44-
4541 private void OnScrollChanged ( object ? sender , ScrollChangedEventArgs e )
4642 {
4743 if ( _scrollViewer == null ) return ;
4844
49- var atBottom = _scrollViewer . Offset . Y >= _scrollViewer . Extent . Height - _scrollViewer . Viewport . Height - BottomThreshold ;
50- _autoScroll = atBottom ;
45+ _autoScroll . OnScrollChanged ( _scrollViewer . Offset . Y , _scrollViewer . Extent . Height , _scrollViewer . Viewport . Height ) ;
5146 if ( AutoScrollButton != null )
5247 {
53- AutoScrollButton . IsVisible = ! atBottom ;
48+ AutoScrollButton . IsVisible = ! _autoScroll . AutoScroll ;
5449 }
5550 }
5651
5752 private void OnItemsRepeaterLayoutUpdated ( object ? sender , EventArgs e )
5853 {
59- if ( _autoScroll && _scrollViewer != null )
54+ if ( _autoScroll . ShouldScrollToEnd ( ) && _scrollViewer != null )
6055 {
6156 _scrollViewer . ScrollToEnd ( ) ;
6257 }
6358 }
6459
6560 private void OnAutoScrollButtonClick ( object ? sender , RoutedEventArgs e )
6661 {
67- _autoScroll = true ;
62+ _autoScroll . Reset ( ) ;
6863 if ( AutoScrollButton != null )
6964 {
7065 AutoScrollButton . IsVisible = false ;
0 commit comments