cd8fb90562f6338c335878117b68d125cffd8e1f
[WebKit-https.git] / Source / WebCore / platform / mac / ScrollAnimatorMac.mm
1 /*
2  * Copyright (C) 2010, 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #if ENABLE(SMOOTH_SCROLLING)
29
30 #include "ScrollAnimatorMac.h"
31
32 #include "BlockExceptions.h"
33 #include "EmptyProtocolDefinitions.h"
34 #include "FloatPoint.h"
35 #include "NSScrollerImpDetails.h"
36 #include "PlatformGestureEvent.h"
37 #include "PlatformWheelEvent.h"
38 #include "ScrollView.h"
39 #include "ScrollableArea.h"
40 #include "ScrollbarTheme.h"
41 #include "ScrollbarThemeMac.h"
42 #include "WebCoreSystemInterface.h"
43 #include <wtf/PassOwnPtr.h>
44 #include <wtf/UnusedParam.h>
45
46 using namespace WebCore;
47 using namespace std;
48
49 static bool supportsUIStateTransitionProgress()
50 {
51     // FIXME: This is temporary until all platforms that support ScrollbarPainter support this part of the API.
52     static bool globalSupportsUIStateTransitionProgress = [NSClassFromString(@"NSScrollerImp") instancesRespondToSelector:@selector(mouseEnteredScroller)];
53     return globalSupportsUIStateTransitionProgress;
54 }
55
56 static bool supportsExpansionTransitionProgress()
57 {
58     static bool globalSupportsExpansionTransitionProgress = [NSClassFromString(@"NSScrollerImp") instancesRespondToSelector:@selector(expansionTransitionProgress)];
59     return globalSupportsExpansionTransitionProgress;
60 }
61
62 static ScrollbarThemeMac* macScrollbarTheme()
63 {
64     ScrollbarTheme* scrollbarTheme = ScrollbarTheme::theme();
65     return !scrollbarTheme->isMockTheme() ? static_cast<ScrollbarThemeMac*>(scrollbarTheme) : 0;
66 }
67
68 static ScrollbarPainter scrollbarPainterForScrollbar(Scrollbar* scrollbar)
69 {
70     if (ScrollbarThemeMac* scrollbarTheme = macScrollbarTheme())
71         return scrollbarTheme->painterForScrollbar(scrollbar);
72
73     return nil;
74 }
75
76 @interface NSObject (ScrollAnimationHelperDetails)
77 - (id)initWithDelegate:(id)delegate;
78 - (void)_stopRun;
79 - (BOOL)_isAnimating;
80 - (NSPoint)targetOrigin;
81 - (CGFloat)_progress;
82 @end
83
84 @interface WebScrollAnimationHelperDelegate : NSObject
85 {
86     WebCore::ScrollAnimatorMac* _animator;
87 }
88 - (id)initWithScrollAnimator:(WebCore::ScrollAnimatorMac*)scrollAnimator;
89 @end
90
91 static NSSize abs(NSSize size)
92 {
93     NSSize finalSize = size;
94     if (finalSize.width < 0)
95         finalSize.width = -finalSize.width;
96     if (finalSize.height < 0)
97         finalSize.height = -finalSize.height;
98     return finalSize;    
99 }
100
101 @implementation WebScrollAnimationHelperDelegate
102
103 - (id)initWithScrollAnimator:(WebCore::ScrollAnimatorMac*)scrollAnimator
104 {
105     self = [super init];
106     if (!self)
107         return nil;
108
109     _animator = scrollAnimator;
110     return self;
111 }
112
113 - (void)invalidate
114 {
115     _animator = 0;
116 }
117
118 - (NSRect)bounds
119 {
120     if (!_animator)
121         return NSZeroRect;
122
123     WebCore::FloatPoint currentPosition = _animator->currentPosition();
124     return NSMakeRect(currentPosition.x(), currentPosition.y(), 0, 0);
125 }
126
127 - (void)_immediateScrollToPoint:(NSPoint)newPosition
128 {
129     if (!_animator)
130         return;
131     _animator->immediateScrollToPointForScrollAnimation(newPosition);
132 }
133
134 - (NSPoint)_pixelAlignProposedScrollPosition:(NSPoint)newOrigin
135 {
136     return newOrigin;
137 }
138
139 - (NSSize)convertSizeToBase:(NSSize)size
140 {
141     return abs(size);
142 }
143
144 - (NSSize)convertSizeFromBase:(NSSize)size
145 {
146     return abs(size);
147 }
148
149 - (NSSize)convertSizeToBacking:(NSSize)size
150 {
151     return abs(size);
152 }
153
154 - (NSSize)convertSizeFromBacking:(NSSize)size
155 {
156     return abs(size);
157 }
158
159 - (id)superview
160 {
161     return nil;
162 }
163
164 - (id)documentView
165 {
166     return nil;
167 }
168
169 - (id)window
170 {
171     return nil;
172 }
173
174 - (void)_recursiveRecomputeToolTips
175 {
176 }
177
178 @end
179
180 @interface WebScrollbarPainterControllerDelegate : NSObject
181 {
182     ScrollableArea* _scrollableArea;
183 }
184 - (id)initWithScrollableArea:(ScrollableArea*)scrollableArea;
185 @end
186
187 @implementation WebScrollbarPainterControllerDelegate
188
189 - (id)initWithScrollableArea:(ScrollableArea*)scrollableArea
190 {
191     self = [super init];
192     if (!self)
193         return nil;
194     
195     _scrollableArea = scrollableArea;
196     return self;
197 }
198
199 - (void)invalidate
200 {
201     _scrollableArea = 0;
202 }
203
204 - (NSRect)contentAreaRectForScrollerImpPair:(id)scrollerImpPair
205 {
206     UNUSED_PARAM(scrollerImpPair);
207     if (!_scrollableArea)
208         return NSZeroRect;
209
210     WebCore::IntSize contentsSize = _scrollableArea->contentsSize();
211     return NSMakeRect(0, 0, contentsSize.width(), contentsSize.height());
212 }
213
214 - (BOOL)inLiveResizeForScrollerImpPair:(id)scrollerImpPair
215 {
216     UNUSED_PARAM(scrollerImpPair);
217     if (!_scrollableArea)
218         return NO;
219
220     return _scrollableArea->inLiveResize();
221 }
222
223 - (NSPoint)mouseLocationInContentAreaForScrollerImpPair:(id)scrollerImpPair
224 {
225     UNUSED_PARAM(scrollerImpPair);
226     if (!_scrollableArea)
227         return NSZeroPoint;
228
229     return _scrollableArea->currentMousePosition();
230 }
231
232 - (NSPoint)scrollerImpPair:(id)scrollerImpPair convertContentPoint:(NSPoint)pointInContentArea toScrollerImp:(id)scrollerImp
233 {
234     UNUSED_PARAM(scrollerImpPair);
235
236     if (!_scrollableArea || !scrollerImp)
237         return NSZeroPoint;
238
239     WebCore::Scrollbar* scrollbar = 0;
240     if ([scrollerImp isHorizontal])
241         scrollbar = _scrollableArea->horizontalScrollbar();
242     else 
243         scrollbar = _scrollableArea->verticalScrollbar();
244
245     // It is possible to have a null scrollbar here since it is possible for this delegate
246     // method to be called between the moment when a scrollbar has been set to 0 and the
247     // moment when its destructor has been called. We should probably de-couple some
248     // of the clean-up work in ScrollbarThemeMac::unregisterScrollbar() to avoid this
249     // issue.
250     if (!scrollbar)
251         return NSZeroPoint;
252
253     ASSERT(scrollerImp == scrollbarPainterForScrollbar(scrollbar));
254
255     return scrollbar->convertFromContainingView(WebCore::IntPoint(pointInContentArea));
256 }
257
258 - (void)scrollerImpPair:(id)scrollerImpPair setContentAreaNeedsDisplayInRect:(NSRect)rect
259 {
260     UNUSED_PARAM(scrollerImpPair);
261     UNUSED_PARAM(rect);
262
263     if (!_scrollableArea)
264         return;
265
266     if (!_scrollableArea->isOnActivePage())
267         return;
268
269     _scrollableArea->scrollAnimator()->contentAreaWillPaint();
270 }
271
272 - (void)scrollerImpPair:(id)scrollerImpPair updateScrollerStyleForNewRecommendedScrollerStyle:(NSScrollerStyle)newRecommendedScrollerStyle
273 {
274     if (!_scrollableArea)
275         return;
276
277     [scrollerImpPair setScrollerStyle:newRecommendedScrollerStyle];
278
279     static_cast<ScrollAnimatorMac*>(_scrollableArea->scrollAnimator())->updateScrollerStyle();
280 }
281
282 @end
283
284 enum FeatureToAnimate {
285     ThumbAlpha,
286     TrackAlpha,
287     UIStateTransition,
288     ExpansionTransition
289 };
290
291 @interface WebScrollbarPartAnimation : NSAnimation
292 {
293     Scrollbar* _scrollbar;
294     RetainPtr<ScrollbarPainter> _scrollbarPainter;
295     FeatureToAnimate _featureToAnimate;
296     CGFloat _startValue;
297     CGFloat _endValue;
298 }
299 - (id)initWithScrollbar:(Scrollbar*)scrollbar featureToAnimate:(FeatureToAnimate)featureToAnimate animateFrom:(CGFloat)startValue animateTo:(CGFloat)endValue duration:(NSTimeInterval)duration;
300 @end
301
302 @implementation WebScrollbarPartAnimation
303
304 - (id)initWithScrollbar:(Scrollbar*)scrollbar featureToAnimate:(FeatureToAnimate)featureToAnimate animateFrom:(CGFloat)startValue animateTo:(CGFloat)endValue duration:(NSTimeInterval)duration
305 {
306     self = [super initWithDuration:duration animationCurve:NSAnimationEaseInOut];
307     if (!self)
308         return nil;
309
310     _scrollbar = scrollbar;
311     _featureToAnimate = featureToAnimate;
312     _startValue = startValue;
313     _endValue = endValue;
314
315     [self setAnimationBlockingMode:NSAnimationNonblocking];
316
317     return self;
318 }
319
320 - (void)startAnimation
321 {
322     ASSERT(_scrollbar);
323
324     _scrollbarPainter = scrollbarPainterForScrollbar(_scrollbar);
325
326     [super startAnimation];
327 }
328
329 - (void)setStartValue:(CGFloat)startValue
330 {
331     _startValue = startValue;
332 }
333
334 - (void)setEndValue:(CGFloat)endValue
335 {
336     _endValue = endValue;
337 }
338
339 - (void)setCurrentProgress:(NSAnimationProgress)progress
340 {
341     [super setCurrentProgress:progress];
342
343     ASSERT(_scrollbar);
344
345     CGFloat currentValue;
346     if (_startValue > _endValue)
347         currentValue = 1 - progress;
348     else
349         currentValue = progress;
350
351     switch (_featureToAnimate) {
352     case ThumbAlpha:
353         [_scrollbarPainter.get() setKnobAlpha:currentValue];
354         break;
355     case TrackAlpha:
356         [_scrollbarPainter.get() setTrackAlpha:currentValue];
357         break;
358     case UIStateTransition:
359         [_scrollbarPainter.get() setUiStateTransitionProgress:currentValue];
360         break;
361     case ExpansionTransition:
362         [_scrollbarPainter.get() setExpansionTransitionProgress:currentValue];
363         break;
364     }
365
366     _scrollbar->invalidate();
367 }
368
369 - (void)invalidate
370 {
371     BEGIN_BLOCK_OBJC_EXCEPTIONS;
372     [self stopAnimation];
373     END_BLOCK_OBJC_EXCEPTIONS;
374     _scrollbar = 0;
375 }
376
377 @end
378
379 @interface WebScrollbarPainterDelegate : NSObject<NSAnimationDelegate>
380 {
381     WebCore::Scrollbar* _scrollbar;
382
383     RetainPtr<WebScrollbarPartAnimation> _knobAlphaAnimation;
384     RetainPtr<WebScrollbarPartAnimation> _trackAlphaAnimation;
385     RetainPtr<WebScrollbarPartAnimation> _uiStateTransitionAnimation;
386     RetainPtr<WebScrollbarPartAnimation> _expansionTransitionAnimation;
387 }
388 - (id)initWithScrollbar:(WebCore::Scrollbar*)scrollbar;
389 - (void)cancelAnimations;
390 @end
391
392 @implementation WebScrollbarPainterDelegate
393
394 - (id)initWithScrollbar:(WebCore::Scrollbar*)scrollbar
395 {
396     self = [super init];
397     if (!self)
398         return nil;
399     
400     _scrollbar = scrollbar;
401     return self;
402 }
403
404 - (void)cancelAnimations
405 {
406     BEGIN_BLOCK_OBJC_EXCEPTIONS;
407     [_knobAlphaAnimation.get() stopAnimation];
408     [_trackAlphaAnimation.get() stopAnimation];
409     [_uiStateTransitionAnimation.get() stopAnimation];
410     [_expansionTransitionAnimation.get() stopAnimation];
411     END_BLOCK_OBJC_EXCEPTIONS;
412 }
413
414 - (ScrollAnimatorMac*)scrollAnimator
415 {
416     return static_cast<ScrollAnimatorMac*>(_scrollbar->scrollableArea()->scrollAnimator());
417 }
418
419 - (NSRect)convertRectToBacking:(NSRect)aRect
420 {
421     return aRect;
422 }
423
424 - (NSRect)convertRectFromBacking:(NSRect)aRect
425 {
426     return aRect;
427 }
428
429 #if !PLATFORM(CHROMIUM)
430 - (CALayer *)layer
431 {
432     if (!_scrollbar)
433         return nil;
434
435     if (!ScrollbarThemeMac::isCurrentlyDrawingIntoLayer())
436         return nil;
437
438     // FIXME: This should attempt to return an actual layer.
439     static CALayer *dummyLayer = [[CALayer alloc] init];
440     return dummyLayer;
441 }
442 #endif
443
444 - (NSPoint)mouseLocationInScrollerForScrollerImp:(id)scrollerImp
445 {
446     if (!_scrollbar)
447         return NSZeroPoint;
448
449     ASSERT_UNUSED(scrollerImp, scrollerImp == scrollbarPainterForScrollbar(_scrollbar));
450
451     return _scrollbar->convertFromContainingView(_scrollbar->scrollableArea()->currentMousePosition());
452 }
453
454 - (void)setUpAlphaAnimation:(RetainPtr<WebScrollbarPartAnimation>&)scrollbarPartAnimation scrollerPainter:(ScrollbarPainter)scrollerPainter part:(WebCore::ScrollbarPart)part animateAlphaTo:(CGFloat)newAlpha duration:(NSTimeInterval)duration
455 {
456     // If the user has scrolled the page, then the scrollbars must be animated here. 
457     // This overrides the early returns.
458     bool mustAnimate = [self scrollAnimator]->haveScrolledSincePageLoad();
459
460     if ([self scrollAnimator]->scrollbarPaintTimerIsActive() && !mustAnimate)
461         return;
462
463     if (_scrollbar->scrollableArea()->shouldSuspendScrollAnimations() && !mustAnimate) {
464         [self scrollAnimator]->startScrollbarPaintTimer();
465         return;
466     }
467
468     // At this point, we are definitely going to animate now, so stop the timer.
469     [self scrollAnimator]->stopScrollbarPaintTimer();
470
471     // If we are currently animating, stop
472     if (scrollbarPartAnimation) {
473         [scrollbarPartAnimation.get() stopAnimation];
474         scrollbarPartAnimation = nil;
475     }
476
477     if (part == WebCore::ThumbPart && _scrollbar->orientation() == VerticalScrollbar) {
478         if (newAlpha == 1) {
479             IntRect thumbRect = IntRect([scrollerPainter rectForPart:NSScrollerKnob]);
480             [self scrollAnimator]->setVisibleScrollerThumbRect(thumbRect);
481         } else
482             [self scrollAnimator]->setVisibleScrollerThumbRect(IntRect());
483     }
484
485     scrollbarPartAnimation.adoptNS([[WebScrollbarPartAnimation alloc] initWithScrollbar:_scrollbar 
486                                                                        featureToAnimate:part == ThumbPart ? ThumbAlpha : TrackAlpha
487                                                                             animateFrom:part == ThumbPart ? [scrollerPainter knobAlpha] : [scrollerPainter trackAlpha]
488                                                                               animateTo:newAlpha 
489                                                                                duration:duration]);
490     [scrollbarPartAnimation.get() startAnimation];
491 }
492
493 - (void)scrollerImp:(id)scrollerImp animateKnobAlphaTo:(CGFloat)newKnobAlpha duration:(NSTimeInterval)duration
494 {
495     if (!_scrollbar)
496         return;
497
498     ASSERT(scrollerImp == scrollbarPainterForScrollbar(_scrollbar));
499
500     ScrollbarPainter scrollerPainter = (ScrollbarPainter)scrollerImp;
501     [self setUpAlphaAnimation:_knobAlphaAnimation scrollerPainter:scrollerPainter part:WebCore::ThumbPart animateAlphaTo:newKnobAlpha duration:duration];
502 }
503
504 - (void)scrollerImp:(id)scrollerImp animateTrackAlphaTo:(CGFloat)newTrackAlpha duration:(NSTimeInterval)duration
505 {
506     if (!_scrollbar)
507         return;
508
509     ASSERT(scrollerImp == scrollbarPainterForScrollbar(_scrollbar));
510     
511     ScrollbarPainter scrollerPainter = (ScrollbarPainter)scrollerImp;
512     [self setUpAlphaAnimation:_trackAlphaAnimation scrollerPainter:scrollerPainter part:WebCore::BackTrackPart animateAlphaTo:newTrackAlpha duration:duration];
513 }
514
515 - (void)scrollerImp:(id)scrollerImp animateUIStateTransitionWithDuration:(NSTimeInterval)duration
516 {
517     if (!_scrollbar)
518         return;
519
520     if (!supportsUIStateTransitionProgress())
521         return;
522
523     ASSERT(scrollerImp == scrollbarPainterForScrollbar(_scrollbar));
524
525     ScrollbarPainter scrollbarPainter = (ScrollbarPainter)scrollerImp;
526
527     // UIStateTransition always animates to 1. In case an animation is in progress this avoids a hard transition.
528     [scrollbarPainter setUiStateTransitionProgress:1 - [scrollerImp uiStateTransitionProgress]];
529
530     if (!_uiStateTransitionAnimation)
531         _uiStateTransitionAnimation.adoptNS([[WebScrollbarPartAnimation alloc] initWithScrollbar:_scrollbar 
532                                                                                 featureToAnimate:UIStateTransition
533                                                                                      animateFrom:[scrollbarPainter uiStateTransitionProgress]
534                                                                                        animateTo:1.0
535                                                                                         duration:duration]);
536     else {
537         // If we don't need to initialize the animation, just reset the values in case they have changed.
538         [_uiStateTransitionAnimation.get() setStartValue:[scrollbarPainter uiStateTransitionProgress]];
539         [_uiStateTransitionAnimation.get() setEndValue:1.0];
540         [_uiStateTransitionAnimation.get() setDuration:duration];
541     }
542     [_uiStateTransitionAnimation.get() startAnimation];
543 }
544
545 - (void)scrollerImp:(id)scrollerImp animateExpansionTransitionWithDuration:(NSTimeInterval)duration
546 {
547     if (!_scrollbar)
548         return;
549
550     if (!supportsExpansionTransitionProgress())
551         return;
552
553     ASSERT(scrollerImp == scrollbarPainterForScrollbar(_scrollbar));
554
555     ScrollbarPainter scrollbarPainter = (ScrollbarPainter)scrollerImp;
556
557     // ExpansionTransition always animates to 1. In case an animation is in progress this avoids a hard transition.
558     [scrollbarPainter setExpansionTransitionProgress:1 - [scrollerImp expansionTransitionProgress]];
559
560     if (!_expansionTransitionAnimation) {
561         _expansionTransitionAnimation.adoptNS([[WebScrollbarPartAnimation alloc] initWithScrollbar:_scrollbar
562                                                                                   featureToAnimate:ExpansionTransition
563                                                                                        animateFrom:[scrollbarPainter expansionTransitionProgress]
564                                                                                          animateTo:1.0
565                                                                                           duration:duration]);
566     } else {
567         // If we don't need to initialize the animation, just reset the values in case they have changed.
568         [_expansionTransitionAnimation.get() setStartValue:[scrollbarPainter uiStateTransitionProgress]];
569         [_expansionTransitionAnimation.get() setEndValue:1.0];
570         [_expansionTransitionAnimation.get() setDuration:duration];
571     }
572     [_expansionTransitionAnimation.get() startAnimation];
573 }
574
575 - (void)scrollerImp:(id)scrollerImp overlayScrollerStateChangedTo:(NSUInteger)newOverlayScrollerState
576 {
577     UNUSED_PARAM(scrollerImp);
578     UNUSED_PARAM(newOverlayScrollerState);
579 }
580
581 - (void)invalidate
582 {
583     _scrollbar = 0;
584     BEGIN_BLOCK_OBJC_EXCEPTIONS;
585     [_knobAlphaAnimation.get() invalidate];
586     [_trackAlphaAnimation.get() invalidate];
587     [_uiStateTransitionAnimation.get() invalidate];
588     [_expansionTransitionAnimation.get() invalidate];
589     END_BLOCK_OBJC_EXCEPTIONS;
590 }
591
592 @end
593
594 namespace WebCore {
595
596 PassOwnPtr<ScrollAnimator> ScrollAnimator::create(ScrollableArea* scrollableArea)
597 {
598     return adoptPtr(new ScrollAnimatorMac(scrollableArea));
599 }
600
601 ScrollAnimatorMac::ScrollAnimatorMac(ScrollableArea* scrollableArea)
602     : ScrollAnimator(scrollableArea)
603     , m_initialScrollbarPaintTimer(this, &ScrollAnimatorMac::initialScrollbarPaintTimerFired)
604 #if ENABLE(RUBBER_BANDING)
605     , m_scrollElasticityController(this)
606     , m_snapRubberBandTimer(this, &ScrollAnimatorMac::snapRubberBandTimerFired)
607 #endif
608     , m_haveScrolledSincePageLoad(false)
609     , m_needsScrollerStyleUpdate(false)
610 {
611     m_scrollAnimationHelperDelegate.adoptNS([[WebScrollAnimationHelperDelegate alloc] initWithScrollAnimator:this]);
612     m_scrollAnimationHelper.adoptNS([[NSClassFromString(@"NSScrollAnimationHelper") alloc] initWithDelegate:m_scrollAnimationHelperDelegate.get()]);
613
614     if (isScrollbarOverlayAPIAvailable()) {
615         m_scrollbarPainterControllerDelegate.adoptNS([[WebScrollbarPainterControllerDelegate alloc] initWithScrollableArea:scrollableArea]);
616         m_scrollbarPainterController = [[[NSClassFromString(@"NSScrollerImpPair") alloc] init] autorelease];
617         [m_scrollbarPainterController.get() setDelegate:m_scrollbarPainterControllerDelegate.get()];
618         [m_scrollbarPainterController.get() setScrollerStyle:recommendedScrollerStyle()];
619     }
620 }
621
622 ScrollAnimatorMac::~ScrollAnimatorMac()
623 {
624     if (isScrollbarOverlayAPIAvailable()) {
625         BEGIN_BLOCK_OBJC_EXCEPTIONS;
626         [m_scrollbarPainterControllerDelegate.get() invalidate];
627         [m_scrollbarPainterController.get() setDelegate:nil];
628         [m_horizontalScrollbarPainterDelegate.get() invalidate];
629         [m_verticalScrollbarPainterDelegate.get() invalidate];
630         [m_scrollAnimationHelperDelegate.get() invalidate];
631         END_BLOCK_OBJC_EXCEPTIONS;
632     }
633 }
634
635 static bool scrollAnimationEnabledForSystem()
636 {
637 #if defined(BUILDING_ON_SNOW_LEOPARD) || defined(BUILDING_ON_LION)
638     return [[NSUserDefaults standardUserDefaults] boolForKey:@"AppleScrollAnimationEnabled"];
639 #else
640     return [[NSUserDefaults standardUserDefaults] boolForKey:@"NSScrollAnimationEnabled"];
641 #endif
642 }
643
644 bool ScrollAnimatorMac::scroll(ScrollbarOrientation orientation, ScrollGranularity granularity, float step, float multiplier)
645 {
646     m_haveScrolledSincePageLoad = true;
647
648     if (!scrollAnimationEnabledForSystem() || !m_scrollableArea->scrollAnimatorEnabled())
649         return ScrollAnimator::scroll(orientation, granularity, step, multiplier);
650
651     if (granularity == ScrollByPixel)
652         return ScrollAnimator::scroll(orientation, granularity, step, multiplier);
653
654     float currentPos = orientation == HorizontalScrollbar ? m_currentPosX : m_currentPosY;
655     float newPos = std::max<float>(std::min<float>(currentPos + (step * multiplier), static_cast<float>(m_scrollableArea->scrollSize(orientation))), 0);
656     if (currentPos == newPos)
657         return false;
658
659     NSPoint newPoint;
660     if ([m_scrollAnimationHelper.get() _isAnimating]) {
661         NSPoint targetOrigin = [m_scrollAnimationHelper.get() targetOrigin];
662         newPoint = orientation == HorizontalScrollbar ? NSMakePoint(newPos, targetOrigin.y) : NSMakePoint(targetOrigin.x, newPos);
663     } else
664         newPoint = orientation == HorizontalScrollbar ? NSMakePoint(newPos, m_currentPosY) : NSMakePoint(m_currentPosX, newPos);
665
666     [m_scrollAnimationHelper.get() scrollToPoint:newPoint];
667     return true;
668 }
669
670 void ScrollAnimatorMac::scrollToOffsetWithoutAnimation(const FloatPoint& offset)
671 {
672     [m_scrollAnimationHelper.get() _stopRun];
673     immediateScrollTo(offset);
674 }
675
676 FloatPoint ScrollAnimatorMac::adjustScrollPositionIfNecessary(const FloatPoint& position) const
677 {
678     if (!m_scrollableArea->constrainsScrollingToContentEdge())
679         return position;
680
681     float newX = max<float>(min<float>(position.x(), m_scrollableArea->contentsSize().width() - m_scrollableArea->visibleWidth()), 0);
682     float newY = max<float>(min<float>(position.y(), m_scrollableArea->contentsSize().height() - m_scrollableArea->visibleHeight()), 0);
683
684     return FloatPoint(newX, newY);
685 }
686
687 void ScrollAnimatorMac::immediateScrollTo(const FloatPoint& newPosition)
688 {
689     FloatPoint adjustedPosition = adjustScrollPositionIfNecessary(newPosition);
690  
691     bool positionChanged = adjustedPosition.x() != m_currentPosX || adjustedPosition.y() != m_currentPosY;
692     if (!positionChanged && !scrollableArea()->scrollOriginChanged())
693         return;
694
695     m_currentPosX = adjustedPosition.x();
696     m_currentPosY = adjustedPosition.y();
697     notifyPositionChanged();
698 }
699
700 bool ScrollAnimatorMac::isRubberBandInProgress() const
701 {
702 #if !ENABLE(RUBBER_BANDING)
703     return false;
704 #else
705     return m_scrollElasticityController.isRubberBandInProgress();
706 #endif
707 }
708
709 void ScrollAnimatorMac::immediateScrollToPointForScrollAnimation(const FloatPoint& newPosition)
710 {
711     ASSERT(m_scrollAnimationHelper);
712     immediateScrollTo(newPosition);
713 }
714
715 void ScrollAnimatorMac::notifyPositionChanged()
716 {
717     notifyContentAreaScrolled();
718     ScrollAnimator::notifyPositionChanged();
719 }
720
721 void ScrollAnimatorMac::contentAreaWillPaint() const
722 {
723     if (!scrollableArea()->isOnActivePage())
724         return;
725     if (isScrollbarOverlayAPIAvailable())
726         [m_scrollbarPainterController.get() contentAreaWillDraw];
727 }
728
729 void ScrollAnimatorMac::mouseEnteredContentArea() const
730 {
731     if (!scrollableArea()->isOnActivePage())
732         return;
733     if (isScrollbarOverlayAPIAvailable())
734         [m_scrollbarPainterController.get() mouseEnteredContentArea];
735 }
736
737 void ScrollAnimatorMac::mouseExitedContentArea() const
738 {
739     if (!scrollableArea()->isOnActivePage())
740         return;
741     if (isScrollbarOverlayAPIAvailable())
742         [m_scrollbarPainterController.get() mouseExitedContentArea];
743 }
744
745 void ScrollAnimatorMac::mouseMovedInContentArea() const
746 {
747     if (!scrollableArea()->isOnActivePage())
748         return;
749     if (isScrollbarOverlayAPIAvailable())
750         [m_scrollbarPainterController.get() mouseMovedInContentArea];
751 }
752
753 void ScrollAnimatorMac::mouseEnteredScrollbar(Scrollbar* scrollbar) const
754 {
755     // At this time, only legacy scrollbars needs to send notifications here.
756     if (recommendedScrollerStyle() != NSScrollerStyleLegacy)
757         return;
758
759     if (!scrollableArea()->isOnActivePage())
760         return;
761
762     if (isScrollbarOverlayAPIAvailable()) {
763         if (!supportsUIStateTransitionProgress())
764             return;
765         if (ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar))
766             [painter mouseEnteredScroller];
767     }
768 }
769
770 void ScrollAnimatorMac::mouseExitedScrollbar(Scrollbar* scrollbar) const
771 {
772     // At this time, only legacy scrollbars needs to send notifications here.
773     if (recommendedScrollerStyle() != NSScrollerStyleLegacy)
774         return;
775
776     if (!scrollableArea()->isOnActivePage())
777         return;
778
779     if (isScrollbarOverlayAPIAvailable()) {
780         if (!supportsUIStateTransitionProgress())
781             return;
782         if (ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar))
783             [painter mouseExitedScroller];
784     }
785 }
786
787 void ScrollAnimatorMac::willStartLiveResize()
788 {
789     if (!scrollableArea()->isOnActivePage())
790         return;
791     if (isScrollbarOverlayAPIAvailable())
792         [m_scrollbarPainterController.get() startLiveResize];
793 }
794
795 void ScrollAnimatorMac::contentsResized() const
796 {
797     if (!scrollableArea()->isOnActivePage())
798         return;
799     if (isScrollbarOverlayAPIAvailable())
800         [m_scrollbarPainterController.get() contentAreaDidResize];
801 }
802
803 void ScrollAnimatorMac::willEndLiveResize()
804 {
805     if (!scrollableArea()->isOnActivePage())
806         return;
807     if (isScrollbarOverlayAPIAvailable())
808         [m_scrollbarPainterController.get() endLiveResize];
809 }
810
811 void ScrollAnimatorMac::contentAreaDidShow() const
812 {
813     if (!scrollableArea()->isOnActivePage())
814         return;
815     if (isScrollbarOverlayAPIAvailable())
816         [m_scrollbarPainterController.get() windowOrderedIn];
817 }
818
819 void ScrollAnimatorMac::contentAreaDidHide() const
820 {
821     if (!scrollableArea()->isOnActivePage())
822         return;
823     if (isScrollbarOverlayAPIAvailable())
824         [m_scrollbarPainterController.get() windowOrderedOut];
825 }
826
827 void ScrollAnimatorMac::didBeginScrollGesture() const
828 {
829     if (!scrollableArea()->isOnActivePage())
830         return;
831     if (isScrollbarOverlayAPIAvailable())
832         [m_scrollbarPainterController.get() beginScrollGesture];
833 }
834
835 void ScrollAnimatorMac::didEndScrollGesture() const
836 {
837     if (!scrollableArea()->isOnActivePage())
838         return;
839     if (isScrollbarOverlayAPIAvailable())
840         [m_scrollbarPainterController.get() endScrollGesture];
841 }
842
843 void ScrollAnimatorMac::mayBeginScrollGesture() const
844 {
845     if (!scrollableArea()->isOnActivePage())
846         return;
847     if (!isScrollbarOverlayAPIAvailable())
848         return;
849
850     [m_scrollbarPainterController.get() beginScrollGesture];
851     [m_scrollbarPainterController.get() contentAreaScrolled];
852 }
853
854 void ScrollAnimatorMac::didAddVerticalScrollbar(Scrollbar* scrollbar)
855 {
856     if (!isScrollbarOverlayAPIAvailable())
857         return;
858
859     ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar);
860     if (!painter)
861         return;
862
863     ASSERT(!m_verticalScrollbarPainterDelegate);
864     m_verticalScrollbarPainterDelegate.adoptNS([[WebScrollbarPainterDelegate alloc] initWithScrollbar:scrollbar]);
865
866     [painter setDelegate:m_verticalScrollbarPainterDelegate.get()];
867     [m_scrollbarPainterController.get() setVerticalScrollerImp:painter];
868     if (scrollableArea()->inLiveResize())
869         [painter setKnobAlpha:1];
870 }
871
872 void ScrollAnimatorMac::willRemoveVerticalScrollbar(Scrollbar* scrollbar)
873 {
874     if (!isScrollbarOverlayAPIAvailable())
875         return;
876
877     ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar);
878     if (!painter)
879         return;
880
881     ASSERT(m_verticalScrollbarPainterDelegate);
882     [m_verticalScrollbarPainterDelegate.get() invalidate];
883     m_verticalScrollbarPainterDelegate = nullptr;
884
885     [painter setDelegate:nil];
886     [m_scrollbarPainterController.get() setVerticalScrollerImp:nil];
887 }
888
889 void ScrollAnimatorMac::didAddHorizontalScrollbar(Scrollbar* scrollbar)
890 {
891     if (!isScrollbarOverlayAPIAvailable())
892         return;
893
894     ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar);
895     if (!painter)
896         return;
897
898     ASSERT(!m_horizontalScrollbarPainterDelegate);
899     m_horizontalScrollbarPainterDelegate.adoptNS([[WebScrollbarPainterDelegate alloc] initWithScrollbar:scrollbar]);
900
901     [painter setDelegate:m_horizontalScrollbarPainterDelegate.get()];
902     [m_scrollbarPainterController.get() setHorizontalScrollerImp:painter];
903     if (scrollableArea()->inLiveResize())
904         [painter setKnobAlpha:1];
905 }
906
907 void ScrollAnimatorMac::willRemoveHorizontalScrollbar(Scrollbar* scrollbar)
908 {
909     if (!isScrollbarOverlayAPIAvailable())
910         return;
911
912     ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar);
913     if (!painter)
914         return;
915
916     ASSERT(m_horizontalScrollbarPainterDelegate);
917     [m_horizontalScrollbarPainterDelegate.get() invalidate];
918     m_horizontalScrollbarPainterDelegate = nullptr;
919
920     [painter setDelegate:nil];
921     [m_scrollbarPainterController.get() setHorizontalScrollerImp:nil];
922 }
923
924 bool ScrollAnimatorMac::shouldScrollbarParticipateInHitTesting(Scrollbar* scrollbar)
925 {
926     // Non-overlay scrollbars should always participate in hit testing.
927     if (recommendedScrollerStyle() != NSScrollerStyleOverlay)
928         return true;
929
930     if (!isScrollbarOverlayAPIAvailable())
931         return true;
932
933     // Overlay scrollbars should participate in hit testing whenever they are at all visible.
934     ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar);
935     if (!painter)
936         return false;
937     return [painter knobAlpha] > 0;
938 }
939
940 void ScrollAnimatorMac::notifyContentAreaScrolled()
941 {
942     if (!isScrollbarOverlayAPIAvailable())
943         return;
944
945     // This function is called when a page is going into the page cache, but the page 
946     // isn't really scrolling in that case. We should only pass the message on to the
947     // ScrollbarPainterController when we're really scrolling on an active page.
948     if (scrollableArea()->isOnActivePage())
949         [m_scrollbarPainterController.get() contentAreaScrolled];
950 }
951
952 void ScrollAnimatorMac::cancelAnimations()
953 {
954     m_haveScrolledSincePageLoad = false;
955
956     if (isScrollbarOverlayAPIAvailable()) {
957         if (scrollbarPaintTimerIsActive())
958             stopScrollbarPaintTimer();
959         [m_horizontalScrollbarPainterDelegate.get() cancelAnimations];
960         [m_verticalScrollbarPainterDelegate.get() cancelAnimations];
961     }
962 }
963
964 void ScrollAnimatorMac::handleWheelEventPhase(PlatformWheelEventPhase phase)
965 {
966     // This may not have been set to true yet if the wheel event was handled by the ScrollingTree,
967     // So set it to true here.
968     m_haveScrolledSincePageLoad = true;
969
970     if (phase == PlatformWheelEventPhaseBegan)
971         didBeginScrollGesture();
972     else if (phase == PlatformWheelEventPhaseEnded || phase == PlatformWheelEventPhaseCancelled)
973         didEndScrollGesture();
974     else if (phase == PlatformWheelEventPhaseMayBegin)
975         mayBeginScrollGesture();
976 }
977
978 #if ENABLE(RUBBER_BANDING)
979 bool ScrollAnimatorMac::handleWheelEvent(const PlatformWheelEvent& wheelEvent)
980 {
981     m_haveScrolledSincePageLoad = true;
982
983     if (!wheelEvent.hasPreciseScrollingDeltas())
984         return ScrollAnimator::handleWheelEvent(wheelEvent);
985
986     // FIXME: This is somewhat roundabout hack to allow forwarding wheel events
987     // up to the parent scrollable area. It takes advantage of the fact that
988     // the base class implementation of handleWheelEvent will not accept the
989     // wheel event if there is nowhere to scroll.
990     if (fabsf(wheelEvent.deltaY()) >= fabsf(wheelEvent.deltaX())) {
991         if (!allowsVerticalStretching())
992             return ScrollAnimator::handleWheelEvent(wheelEvent);
993     } else {
994         if (!allowsHorizontalStretching())
995             return ScrollAnimator::handleWheelEvent(wheelEvent);
996     }
997
998     bool didHandleEvent = m_scrollElasticityController.handleWheelEvent(wheelEvent);
999
1000     if (didHandleEvent)
1001         handleWheelEventPhase(wheelEvent.phase());
1002
1003     return didHandleEvent;
1004 }
1005
1006 bool ScrollAnimatorMac::pinnedInDirection(float deltaX, float deltaY)
1007 {
1008     FloatSize limitDelta;
1009     if (fabsf(deltaY) >= fabsf(deltaX)) {
1010         if (deltaY < 0) {
1011             // We are trying to scroll up.  Make sure we are not pinned to the top
1012             limitDelta.setHeight(m_scrollableArea->visibleContentRect().y() + + m_scrollableArea->scrollOrigin().y());
1013         } else {
1014             // We are trying to scroll down.  Make sure we are not pinned to the bottom
1015             limitDelta.setHeight(m_scrollableArea->contentsSize().height() - (m_scrollableArea->visibleContentRect().maxY() + m_scrollableArea->scrollOrigin().y()));
1016         }
1017     } else if (deltaX != 0) {
1018         if (deltaX < 0) {
1019             // We are trying to scroll left.  Make sure we are not pinned to the left
1020             limitDelta.setWidth(m_scrollableArea->visibleContentRect().x() + m_scrollableArea->scrollOrigin().x());
1021         } else {
1022             // We are trying to scroll right.  Make sure we are not pinned to the right
1023             limitDelta.setWidth(m_scrollableArea->contentsSize().width() - (m_scrollableArea->visibleContentRect().maxX() + m_scrollableArea->scrollOrigin().x()));
1024         }
1025     }
1026     
1027     if ((deltaX != 0 || deltaY != 0) && (limitDelta.width() < 1 && limitDelta.height() < 1))
1028         return true;
1029     return false;
1030 }
1031
1032 bool ScrollAnimatorMac::allowsVerticalStretching()
1033 {
1034     switch (m_scrollableArea->verticalScrollElasticity()) {
1035     case ScrollElasticityAutomatic: {
1036         Scrollbar* hScroller = m_scrollableArea->horizontalScrollbar();
1037         Scrollbar* vScroller = m_scrollableArea->verticalScrollbar();
1038         return (((vScroller && vScroller->enabled()) || (!hScroller || !hScroller->enabled())));
1039     }
1040     case ScrollElasticityNone:
1041         return false;
1042     case ScrollElasticityAllowed:
1043         return true;
1044     }
1045
1046     ASSERT_NOT_REACHED();
1047     return false;
1048 }
1049
1050 bool ScrollAnimatorMac::allowsHorizontalStretching()
1051 {
1052     switch (m_scrollableArea->horizontalScrollElasticity()) {
1053     case ScrollElasticityAutomatic: {
1054         Scrollbar* hScroller = m_scrollableArea->horizontalScrollbar();
1055         Scrollbar* vScroller = m_scrollableArea->verticalScrollbar();
1056         return (((hScroller && hScroller->enabled()) || (!vScroller || !vScroller->enabled())));
1057     }
1058     case ScrollElasticityNone:
1059         return false;
1060     case ScrollElasticityAllowed:
1061         return true;
1062     }
1063
1064     ASSERT_NOT_REACHED();
1065     return false;
1066 }
1067
1068 IntSize ScrollAnimatorMac::stretchAmount()
1069 {
1070     return m_scrollableArea->overhangAmount();
1071 }
1072
1073 bool ScrollAnimatorMac::pinnedInDirection(const FloatSize& direction)
1074 {
1075     return pinnedInDirection(direction.width(), direction.height());
1076 }
1077
1078 bool ScrollAnimatorMac::canScrollHorizontally()
1079 {
1080     Scrollbar* scrollbar = m_scrollableArea->horizontalScrollbar();
1081     if (!scrollbar)
1082         return false;
1083     return scrollbar->enabled();
1084 }
1085
1086 bool ScrollAnimatorMac::canScrollVertically()
1087 {
1088     Scrollbar* scrollbar = m_scrollableArea->verticalScrollbar();
1089     if (!scrollbar)
1090         return false;
1091     return scrollbar->enabled();
1092 }
1093
1094 bool ScrollAnimatorMac::shouldRubberBandInDirection(ScrollDirection direction)
1095 {
1096     return m_scrollableArea->shouldRubberBandInDirection(direction);
1097 }
1098
1099 IntPoint ScrollAnimatorMac::absoluteScrollPosition()
1100 {
1101     return m_scrollableArea->visibleContentRect().location() + m_scrollableArea->scrollOrigin();
1102 }
1103
1104 void ScrollAnimatorMac::immediateScrollByWithoutContentEdgeConstraints(const FloatSize& delta)
1105 {
1106     m_scrollableArea->setConstrainsScrollingToContentEdge(false);
1107     immediateScrollBy(delta);
1108     m_scrollableArea->setConstrainsScrollingToContentEdge(true);
1109 }
1110
1111 void ScrollAnimatorMac::immediateScrollBy(const FloatSize& delta)
1112 {
1113     FloatPoint newPos = adjustScrollPositionIfNecessary(FloatPoint(m_currentPosX, m_currentPosY) + delta);
1114     if (newPos.x() == m_currentPosX && newPos.y() == m_currentPosY)
1115         return;
1116
1117     m_currentPosX = newPos.x();
1118     m_currentPosY = newPos.y();
1119     notifyPositionChanged();
1120 }
1121
1122 void ScrollAnimatorMac::startSnapRubberbandTimer()
1123 {
1124     m_snapRubberBandTimer.startRepeating(1.0 / 60.0);
1125 }
1126
1127 void ScrollAnimatorMac::stopSnapRubberbandTimer()
1128 {
1129     m_snapRubberBandTimer.stop();
1130 }
1131
1132 void ScrollAnimatorMac::snapRubberBandTimerFired(Timer<ScrollAnimatorMac>*)
1133 {
1134     m_scrollElasticityController.snapRubberBandTimerFired();
1135 }
1136 #endif
1137
1138 void ScrollAnimatorMac::setIsActive()
1139 {
1140     if (!isScrollbarOverlayAPIAvailable())
1141         return;
1142
1143     if (!m_needsScrollerStyleUpdate)
1144         return;
1145
1146     updateScrollerStyle();
1147 }
1148
1149 void ScrollAnimatorMac::updateScrollerStyle()
1150 {
1151     if (!isScrollbarOverlayAPIAvailable())
1152         return;
1153
1154     if (!scrollableArea()->isOnActivePage()) {
1155         m_needsScrollerStyleUpdate = true;
1156         return;
1157     }
1158
1159     ScrollbarThemeMac* macTheme = macScrollbarTheme();
1160     if (!macTheme) {
1161         m_needsScrollerStyleUpdate = false;
1162         return;
1163     }
1164
1165     NSScrollerStyle newStyle = [m_scrollbarPainterController.get() scrollerStyle];
1166
1167     if (Scrollbar* verticalScrollbar = scrollableArea()->verticalScrollbar()) {
1168         verticalScrollbar->invalidate();
1169
1170         ScrollbarPainter oldVerticalPainter = [m_scrollbarPainterController.get() verticalScrollerImp];
1171         ScrollbarPainter newVerticalPainter = [NSClassFromString(@"NSScrollerImp") scrollerImpWithStyle:newStyle 
1172                                                                                     controlSize:(NSControlSize)verticalScrollbar->controlSize() 
1173                                                                                     horizontal:NO 
1174                                                                                     replacingScrollerImp:oldVerticalPainter];
1175         [m_scrollbarPainterController.get() setVerticalScrollerImp:newVerticalPainter];
1176         macTheme->setNewPainterForScrollbar(verticalScrollbar, newVerticalPainter);
1177
1178         // The different scrollbar styles have different thicknesses, so we must re-set the 
1179         // frameRect to the new thickness, and the re-layout below will ensure the position
1180         // and length are properly updated.
1181         int thickness = macTheme->scrollbarThickness(verticalScrollbar->controlSize());
1182         verticalScrollbar->setFrameRect(IntRect(0, 0, thickness, thickness));
1183     }
1184
1185     if (Scrollbar* horizontalScrollbar = scrollableArea()->horizontalScrollbar()) {
1186         horizontalScrollbar->invalidate();
1187
1188         ScrollbarPainter oldHorizontalPainter = [m_scrollbarPainterController.get() horizontalScrollerImp];
1189         ScrollbarPainter newHorizontalPainter = [NSClassFromString(@"NSScrollerImp") scrollerImpWithStyle:newStyle 
1190                                                                                     controlSize:(NSControlSize)horizontalScrollbar->controlSize() 
1191                                                                                     horizontal:YES 
1192                                                                                     replacingScrollerImp:oldHorizontalPainter];
1193         [m_scrollbarPainterController.get() setHorizontalScrollerImp:newHorizontalPainter];
1194         macTheme->setNewPainterForScrollbar(horizontalScrollbar, newHorizontalPainter);
1195
1196         // The different scrollbar styles have different thicknesses, so we must re-set the 
1197         // frameRect to the new thickness, and the re-layout below will ensure the position
1198         // and length are properly updated.
1199         int thickness = macTheme->scrollbarThickness(horizontalScrollbar->controlSize());
1200         horizontalScrollbar->setFrameRect(IntRect(0, 0, thickness, thickness));
1201     }
1202
1203     // If m_needsScrollerStyleUpdate is true, then the page is restoring from the page cache, and 
1204     // a relayout will happen on its own. Otherwise, we must initiate a re-layout ourselves.
1205     scrollableArea()->scrollbarStyleChanged(newStyle, !m_needsScrollerStyleUpdate);
1206
1207     m_needsScrollerStyleUpdate = false;
1208 }
1209
1210 void ScrollAnimatorMac::startScrollbarPaintTimer()
1211 {
1212     m_initialScrollbarPaintTimer.startOneShot(0.1);
1213 }
1214
1215 bool ScrollAnimatorMac::scrollbarPaintTimerIsActive() const
1216 {
1217     return m_initialScrollbarPaintTimer.isActive();
1218 }
1219
1220 void ScrollAnimatorMac::stopScrollbarPaintTimer()
1221 {
1222     m_initialScrollbarPaintTimer.stop();
1223 }
1224
1225 void ScrollAnimatorMac::initialScrollbarPaintTimerFired(Timer<ScrollAnimatorMac>*)
1226 {
1227     if (isScrollbarOverlayAPIAvailable()) {
1228         // To force the scrollbars to flash, we have to call hide first. Otherwise, the ScrollbarPainterController
1229         // might think that the scrollbars are already showing and bail early.
1230         [m_scrollbarPainterController.get() hideOverlayScrollers];
1231         [m_scrollbarPainterController.get() flashScrollers];
1232     }
1233 }
1234
1235 void ScrollAnimatorMac::setVisibleScrollerThumbRect(const IntRect& scrollerThumb)
1236 {
1237     IntRect rectInViewCoordinates = scrollerThumb;
1238     if (Scrollbar* verticalScrollbar = m_scrollableArea->verticalScrollbar())
1239         rectInViewCoordinates = verticalScrollbar->convertToContainingView(scrollerThumb);
1240
1241     if (rectInViewCoordinates == m_visibleScrollerThumbRect)
1242         return;
1243
1244     m_scrollableArea->setVisibleScrollerThumbRect(rectInViewCoordinates);
1245     m_visibleScrollerThumbRect = rectInViewCoordinates;
1246 }
1247
1248 } // namespace WebCore
1249
1250 #endif // ENABLE(SMOOTH_SCROLLING)