1+ #import < time.h>
12#import < QuartzCore/QuartzCore.h>
23#import " MMTabline.h"
34
@@ -554,6 +555,19 @@ - (void)scrollTabToVisibleAtIndex:(NSUInteger)index
554555{
555556 if (_tabs.count == 0 ) return ;
556557
558+ // Get the amount of time elapsed between the previous invocation
559+ // of this method and now. Use this elapsed time to set the animation
560+ // duration such that rapid invocations of this method result in
561+ // faster animations. For example, the user might hold down the tab
562+ // scrolling buttons (causing them to repeatedly fire) or they might
563+ // rapidly click them.
564+ static NSTimeInterval lastTime = 0 ;
565+ struct timespec t;
566+ clock_gettime (CLOCK_MONOTONIC, &t);
567+ NSTimeInterval currentTime = t.tv_sec + t.tv_nsec * 1 .e -9 ;
568+ NSTimeInterval elapsedTime = currentTime - lastTime;
569+ lastTime = currentTime;
570+
557571 NSRect tabFrame = _tabs[index].frame ;
558572 NSRect clipBounds = _scrollView.contentView .bounds ;
559573 // One side or the other of the selected tab is clipped.
@@ -565,7 +579,10 @@ - (void)scrollTabToVisibleAtIndex:(NSUInteger)index
565579 // Left side of the selected tab is clipped.
566580 clipBounds.origin .x = tabFrame.origin .x ;
567581 }
582+ [NSAnimationContext beginGrouping ];
583+ [NSAnimationContext .currentContext setDuration: elapsedTime < 0.2 ? 0.05 : 0.2 ];
568584 _scrollView.contentView .animator .bounds = clipBounds;
585+ [NSAnimationContext endGrouping ];
569586 }
570587}
571588
0 commit comments