<http://webkit.org/b/91015> Remove BUILDING_ON / TARGETING macros in favor of system...
[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     , m_sendContentAreaScrolledTimer(this, &ScrollAnimatorMac::sendContentAreaScrolledTimerFired)
605 #if ENABLE(RUBBER_BANDING)
606     , m_scrollElasticityController(this)
607     , m_snapRubberBandTimer(this, &ScrollAnimatorMac::snapRubberBandTimerFired)
608 #endif
609     , m_haveScrolledSincePageLoad(false)
610     , m_needsScrollerStyleUpdate(false)
611 {
612     m_scrollAnimationHelperDelegate.adoptNS([[WebScrollAnimationHelperDelegate alloc] initWithScrollAnimator:this]);
613     m_scrollAnimationHelper.adoptNS([[NSClassFromString(@"NSScrollAnimationHelper") alloc] initWithDelegate:m_scrollAnimationHelperDelegate.get()]);
614
615     if (isScrollbarOverlayAPIAvailable()) {
616         m_scrollbarPainterControllerDelegate.adoptNS([[WebScrollbarPainterControllerDelegate alloc] initWithScrollableArea:scrollableArea]);
617         m_scrollbarPainterController = [[[NSClassFromString(@"NSScrollerImpPair") alloc] init] autorelease];
618         [m_scrollbarPainterController.get() setDelegate:m_scrollbarPainterControllerDelegate.get()];
619         [m_scrollbarPainterController.get() setScrollerStyle:recommendedScrollerStyle()];
620     }
621 }
622
623 ScrollAnimatorMac::~ScrollAnimatorMac()
624 {
625     if (isScrollbarOverlayAPIAvailable()) {
626         BEGIN_BLOCK_OBJC_EXCEPTIONS;
627         [m_scrollbarPainterControllerDelegate.get() invalidate];
628         [m_scrollbarPainterController.get() setDelegate:nil];
629         [m_horizontalScrollbarPainterDelegate.get() invalidate];
630         [m_verticalScrollbarPainterDelegate.get() invalidate];
631         [m_scrollAnimationHelperDelegate.get() invalidate];
632         END_BLOCK_OBJC_EXCEPTIONS;
633     }
634 }
635
636 static bool scrollAnimationEnabledForSystem()
637 {
638 #if __MAC_OS_X_VERSION_MIN_REQUIRED <= 1070 || PLATFORM(CHROMIUM)
639     return [[NSUserDefaults standardUserDefaults] boolForKey:@"AppleScrollAnimationEnabled"];
640 #else
641     return [[NSUserDefaults standardUserDefaults] boolForKey:@"NSScrollAnimationEnabled"];
642 #endif
643 }
644
645 bool ScrollAnimatorMac::scroll(ScrollbarOrientation orientation, ScrollGranularity granularity, float step, float multiplier)
646 {
647     m_haveScrolledSincePageLoad = true;
648
649     if (!scrollAnimationEnabledForSystem() || !m_scrollableArea->scrollAnimatorEnabled())
650         return ScrollAnimator::scroll(orientation, granularity, step, multiplier);
651
652     if (granularity == ScrollByPixel)
653         return ScrollAnimator::scroll(orientation, granularity, step, multiplier);
654
655     float currentPos = orientation == HorizontalScrollbar ? m_currentPosX : m_currentPosY;
656     float newPos = std::max<float>(std::min<float>(currentPos + (step * multiplier), static_cast<float>(m_scrollableArea->scrollSize(orientation))), 0);
657     if (currentPos == newPos)
658         return false;
659
660     NSPoint newPoint;
661     if ([m_scrollAnimationHelper.get() _isAnimating]) {
662         NSPoint targetOrigin = [m_scrollAnimationHelper.get() targetOrigin];
663         newPoint = orientation == HorizontalScrollbar ? NSMakePoint(newPos, targetOrigin.y) : NSMakePoint(targetOrigin.x, newPos);
664     } else
665         newPoint = orientation == HorizontalScrollbar ? NSMakePoint(newPos, m_currentPosY) : NSMakePoint(m_currentPosX, newPos);
666
667     [m_scrollAnimationHelper.get() scrollToPoint:newPoint];
668     return true;
669 }
670
671 void ScrollAnimatorMac::scrollToOffsetWithoutAnimation(const FloatPoint& offset)
672 {
673     [m_scrollAnimationHelper.get() _stopRun];
674     immediateScrollTo(offset);
675 }
676
677 FloatPoint ScrollAnimatorMac::adjustScrollPositionIfNecessary(const FloatPoint& position) const
678 {
679     if (!m_scrollableArea->constrainsScrollingToContentEdge())
680         return position;
681
682     float newX = max<float>(min<float>(position.x(), m_scrollableArea->contentsSize().width() - m_scrollableArea->visibleWidth()), 0);
683     float newY = max<float>(min<float>(position.y(), m_scrollableArea->contentsSize().height() - m_scrollableArea->visibleHeight()), 0);
684
685     return FloatPoint(newX, newY);
686 }
687
688 void ScrollAnimatorMac::immediateScrollTo(const FloatPoint& newPosition)
689 {
690     FloatPoint adjustedPosition = adjustScrollPositionIfNecessary(newPosition);
691  
692     bool positionChanged = adjustedPosition.x() != m_currentPosX || adjustedPosition.y() != m_currentPosY;
693     if (!positionChanged && !scrollableArea()->scrollOriginChanged())
694         return;
695
696     m_currentPosX = adjustedPosition.x();
697     m_currentPosY = adjustedPosition.y();
698     notifyPositionChanged();
699 }
700
701 bool ScrollAnimatorMac::isRubberBandInProgress() const
702 {
703 #if !ENABLE(RUBBER_BANDING)
704     return false;
705 #else
706     return m_scrollElasticityController.isRubberBandInProgress();
707 #endif
708 }
709
710 void ScrollAnimatorMac::immediateScrollToPointForScrollAnimation(const FloatPoint& newPosition)
711 {
712     ASSERT(m_scrollAnimationHelper);
713     immediateScrollTo(newPosition);
714 }
715
716 void ScrollAnimatorMac::notifyPositionChanged()
717 {
718     notifyContentAreaScrolled();
719     ScrollAnimator::notifyPositionChanged();
720 }
721
722 void ScrollAnimatorMac::contentAreaWillPaint() const
723 {
724     if (!scrollableArea()->isOnActivePage())
725         return;
726     if (isScrollbarOverlayAPIAvailable())
727         [m_scrollbarPainterController.get() contentAreaWillDraw];
728 }
729
730 void ScrollAnimatorMac::mouseEnteredContentArea() const
731 {
732     if (!scrollableArea()->isOnActivePage())
733         return;
734     if (isScrollbarOverlayAPIAvailable())
735         [m_scrollbarPainterController.get() mouseEnteredContentArea];
736 }
737
738 void ScrollAnimatorMac::mouseExitedContentArea() const
739 {
740     if (!scrollableArea()->isOnActivePage())
741         return;
742     if (isScrollbarOverlayAPIAvailable())
743         [m_scrollbarPainterController.get() mouseExitedContentArea];
744 }
745
746 void ScrollAnimatorMac::mouseMovedInContentArea() const
747 {
748     if (!scrollableArea()->isOnActivePage())
749         return;
750     if (isScrollbarOverlayAPIAvailable())
751         [m_scrollbarPainterController.get() mouseMovedInContentArea];
752 }
753
754 void ScrollAnimatorMac::mouseEnteredScrollbar(Scrollbar* scrollbar) const
755 {
756     // At this time, only legacy scrollbars needs to send notifications here.
757     if (recommendedScrollerStyle() != NSScrollerStyleLegacy)
758         return;
759
760     if (!scrollableArea()->isOnActivePage())
761         return;
762
763     if (isScrollbarOverlayAPIAvailable()) {
764         if (!supportsUIStateTransitionProgress())
765             return;
766         if (ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar))
767             [painter mouseEnteredScroller];
768     }
769 }
770
771 void ScrollAnimatorMac::mouseExitedScrollbar(Scrollbar* scrollbar) const
772 {
773     // At this time, only legacy scrollbars needs to send notifications here.
774     if (recommendedScrollerStyle() != NSScrollerStyleLegacy)
775         return;
776
777     if (!scrollableArea()->isOnActivePage())
778         return;
779
780     if (isScrollbarOverlayAPIAvailable()) {
781         if (!supportsUIStateTransitionProgress())
782             return;
783         if (ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar))
784             [painter mouseExitedScroller];
785     }
786 }
787
788 void ScrollAnimatorMac::willStartLiveResize()
789 {
790     if (!scrollableArea()->isOnActivePage())
791         return;
792     if (isScrollbarOverlayAPIAvailable())
793         [m_scrollbarPainterController.get() startLiveResize];
794 }
795
796 void ScrollAnimatorMac::contentsResized() const
797 {
798     if (!scrollableArea()->isOnActivePage())
799         return;
800     if (isScrollbarOverlayAPIAvailable())
801         [m_scrollbarPainterController.get() contentAreaDidResize];
802 }
803
804 void ScrollAnimatorMac::willEndLiveResize()
805 {
806     if (!scrollableArea()->isOnActivePage())
807         return;
808     if (isScrollbarOverlayAPIAvailable())
809         [m_scrollbarPainterController.get() endLiveResize];
810 }
811
812 void ScrollAnimatorMac::contentAreaDidShow() const
813 {
814     if (!scrollableArea()->isOnActivePage())
815         return;
816     if (isScrollbarOverlayAPIAvailable())
817         [m_scrollbarPainterController.get() windowOrderedIn];
818 }
819
820 void ScrollAnimatorMac::contentAreaDidHide() const
821 {
822     if (!scrollableArea()->isOnActivePage())
823         return;
824     if (isScrollbarOverlayAPIAvailable())
825         [m_scrollbarPainterController.get() windowOrderedOut];
826 }
827
828 void ScrollAnimatorMac::didBeginScrollGesture() const
829 {
830     if (!scrollableArea()->isOnActivePage())
831         return;
832     if (isScrollbarOverlayAPIAvailable())
833         [m_scrollbarPainterController.get() beginScrollGesture];
834 }
835
836 void ScrollAnimatorMac::didEndScrollGesture() const
837 {
838     if (!scrollableArea()->isOnActivePage())
839         return;
840     if (isScrollbarOverlayAPIAvailable())
841         [m_scrollbarPainterController.get() endScrollGesture];
842 }
843
844 void ScrollAnimatorMac::mayBeginScrollGesture() const
845 {
846     if (!scrollableArea()->isOnActivePage())
847         return;
848     if (!isScrollbarOverlayAPIAvailable())
849         return;
850
851     [m_scrollbarPainterController.get() beginScrollGesture];
852     [m_scrollbarPainterController.get() contentAreaScrolled];
853 }
854
855 void ScrollAnimatorMac::didAddVerticalScrollbar(Scrollbar* scrollbar)
856 {
857     if (!isScrollbarOverlayAPIAvailable())
858         return;
859
860     ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar);
861     if (!painter)
862         return;
863
864     ASSERT(!m_verticalScrollbarPainterDelegate);
865     m_verticalScrollbarPainterDelegate.adoptNS([[WebScrollbarPainterDelegate alloc] initWithScrollbar:scrollbar]);
866
867     [painter setDelegate:m_verticalScrollbarPainterDelegate.get()];
868     [m_scrollbarPainterController.get() setVerticalScrollerImp:painter];
869     if (scrollableArea()->inLiveResize())
870         [painter setKnobAlpha:1];
871 }
872
873 void ScrollAnimatorMac::willRemoveVerticalScrollbar(Scrollbar* scrollbar)
874 {
875     if (!isScrollbarOverlayAPIAvailable())
876         return;
877
878     ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar);
879     if (!painter)
880         return;
881
882     ASSERT(m_verticalScrollbarPainterDelegate);
883     [m_verticalScrollbarPainterDelegate.get() invalidate];
884     m_verticalScrollbarPainterDelegate = nullptr;
885
886     [painter setDelegate:nil];
887     [m_scrollbarPainterController.get() setVerticalScrollerImp:nil];
888 }
889
890 void ScrollAnimatorMac::didAddHorizontalScrollbar(Scrollbar* scrollbar)
891 {
892     if (!isScrollbarOverlayAPIAvailable())
893         return;
894
895     ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar);
896     if (!painter)
897         return;
898
899     ASSERT(!m_horizontalScrollbarPainterDelegate);
900     m_horizontalScrollbarPainterDelegate.adoptNS([[WebScrollbarPainterDelegate alloc] initWithScrollbar:scrollbar]);
901
902     [painter setDelegate:m_horizontalScrollbarPainterDelegate.get()];
903     [m_scrollbarPainterController.get() setHorizontalScrollerImp:painter];
904     if (scrollableArea()->inLiveResize())
905         [painter setKnobAlpha:1];
906 }
907
908 void ScrollAnimatorMac::willRemoveHorizontalScrollbar(Scrollbar* scrollbar)
909 {
910     if (!isScrollbarOverlayAPIAvailable())
911         return;
912
913     ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar);
914     if (!painter)
915         return;
916
917     ASSERT(m_horizontalScrollbarPainterDelegate);
918     [m_horizontalScrollbarPainterDelegate.get() invalidate];
919     m_horizontalScrollbarPainterDelegate = nullptr;
920
921     [painter setDelegate:nil];
922     [m_scrollbarPainterController.get() setHorizontalScrollerImp:nil];
923 }
924
925 bool ScrollAnimatorMac::shouldScrollbarParticipateInHitTesting(Scrollbar* scrollbar)
926 {
927     // Non-overlay scrollbars should always participate in hit testing.
928     if (recommendedScrollerStyle() != NSScrollerStyleOverlay)
929         return true;
930
931     if (!isScrollbarOverlayAPIAvailable())
932         return true;
933
934     // Overlay scrollbars should participate in hit testing whenever they are at all visible.
935     ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar);
936     if (!painter)
937         return false;
938     return [painter knobAlpha] > 0;
939 }
940
941 void ScrollAnimatorMac::notifyContentAreaScrolled()
942 {
943     if (!isScrollbarOverlayAPIAvailable())
944         return;
945
946     // This function is called when a page is going into the page cache, but the page 
947     // isn't really scrolling in that case. We should only pass the message on to the
948     // ScrollbarPainterController when we're really scrolling on an active page.
949     if (scrollableArea()->isOnActivePage())
950         sendContentAreaScrolledSoon();
951 }
952
953 void ScrollAnimatorMac::cancelAnimations()
954 {
955     m_haveScrolledSincePageLoad = false;
956
957     if (isScrollbarOverlayAPIAvailable()) {
958         if (scrollbarPaintTimerIsActive())
959             stopScrollbarPaintTimer();
960         [m_horizontalScrollbarPainterDelegate.get() cancelAnimations];
961         [m_verticalScrollbarPainterDelegate.get() cancelAnimations];
962     }
963 }
964
965 void ScrollAnimatorMac::handleWheelEventPhase(PlatformWheelEventPhase phase)
966 {
967     // This may not have been set to true yet if the wheel event was handled by the ScrollingTree,
968     // So set it to true here.
969     m_haveScrolledSincePageLoad = true;
970
971     if (phase == PlatformWheelEventPhaseBegan)
972         didBeginScrollGesture();
973     else if (phase == PlatformWheelEventPhaseEnded || phase == PlatformWheelEventPhaseCancelled)
974         didEndScrollGesture();
975     else if (phase == PlatformWheelEventPhaseMayBegin)
976         mayBeginScrollGesture();
977 }
978
979 #if ENABLE(RUBBER_BANDING)
980 bool ScrollAnimatorMac::handleWheelEvent(const PlatformWheelEvent& wheelEvent)
981 {
982     m_haveScrolledSincePageLoad = true;
983
984     if (!wheelEvent.hasPreciseScrollingDeltas())
985         return ScrollAnimator::handleWheelEvent(wheelEvent);
986
987     // FIXME: This is somewhat roundabout hack to allow forwarding wheel events
988     // up to the parent scrollable area. It takes advantage of the fact that
989     // the base class implementation of handleWheelEvent will not accept the
990     // wheel event if there is nowhere to scroll.
991     if (fabsf(wheelEvent.deltaY()) >= fabsf(wheelEvent.deltaX())) {
992         if (!allowsVerticalStretching())
993             return ScrollAnimator::handleWheelEvent(wheelEvent);
994     } else {
995         if (!allowsHorizontalStretching())
996             return ScrollAnimator::handleWheelEvent(wheelEvent);
997     }
998
999     bool didHandleEvent = m_scrollElasticityController.handleWheelEvent(wheelEvent);
1000
1001     if (didHandleEvent)
1002         handleWheelEventPhase(wheelEvent.phase());
1003
1004     return didHandleEvent;
1005 }
1006
1007 bool ScrollAnimatorMac::pinnedInDirection(float deltaX, float deltaY)
1008 {
1009     FloatSize limitDelta;
1010     if (fabsf(deltaY) >= fabsf(deltaX)) {
1011         if (deltaY < 0) {
1012             // We are trying to scroll up.  Make sure we are not pinned to the top
1013             limitDelta.setHeight(m_scrollableArea->visibleContentRect().y() + + m_scrollableArea->scrollOrigin().y());
1014         } else {
1015             // We are trying to scroll down.  Make sure we are not pinned to the bottom
1016             limitDelta.setHeight(m_scrollableArea->contentsSize().height() - (m_scrollableArea->visibleContentRect().maxY() + m_scrollableArea->scrollOrigin().y()));
1017         }
1018     } else if (deltaX != 0) {
1019         if (deltaX < 0) {
1020             // We are trying to scroll left.  Make sure we are not pinned to the left
1021             limitDelta.setWidth(m_scrollableArea->visibleContentRect().x() + m_scrollableArea->scrollOrigin().x());
1022         } else {
1023             // We are trying to scroll right.  Make sure we are not pinned to the right
1024             limitDelta.setWidth(m_scrollableArea->contentsSize().width() - (m_scrollableArea->visibleContentRect().maxX() + m_scrollableArea->scrollOrigin().x()));
1025         }
1026     }
1027     
1028     if ((deltaX != 0 || deltaY != 0) && (limitDelta.width() < 1 && limitDelta.height() < 1))
1029         return true;
1030     return false;
1031 }
1032
1033 bool ScrollAnimatorMac::allowsVerticalStretching()
1034 {
1035     switch (m_scrollableArea->verticalScrollElasticity()) {
1036     case ScrollElasticityAutomatic: {
1037         Scrollbar* hScroller = m_scrollableArea->horizontalScrollbar();
1038         Scrollbar* vScroller = m_scrollableArea->verticalScrollbar();
1039         return (((vScroller && vScroller->enabled()) || (!hScroller || !hScroller->enabled())));
1040     }
1041     case ScrollElasticityNone:
1042         return false;
1043     case ScrollElasticityAllowed:
1044         return true;
1045     }
1046
1047     ASSERT_NOT_REACHED();
1048     return false;
1049 }
1050
1051 bool ScrollAnimatorMac::allowsHorizontalStretching()
1052 {
1053     switch (m_scrollableArea->horizontalScrollElasticity()) {
1054     case ScrollElasticityAutomatic: {
1055         Scrollbar* hScroller = m_scrollableArea->horizontalScrollbar();
1056         Scrollbar* vScroller = m_scrollableArea->verticalScrollbar();
1057         return (((hScroller && hScroller->enabled()) || (!vScroller || !vScroller->enabled())));
1058     }
1059     case ScrollElasticityNone:
1060         return false;
1061     case ScrollElasticityAllowed:
1062         return true;
1063     }
1064
1065     ASSERT_NOT_REACHED();
1066     return false;
1067 }
1068
1069 IntSize ScrollAnimatorMac::stretchAmount()
1070 {
1071     return m_scrollableArea->overhangAmount();
1072 }
1073
1074 bool ScrollAnimatorMac::pinnedInDirection(const FloatSize& direction)
1075 {
1076     return pinnedInDirection(direction.width(), direction.height());
1077 }
1078
1079 bool ScrollAnimatorMac::canScrollHorizontally()
1080 {
1081     Scrollbar* scrollbar = m_scrollableArea->horizontalScrollbar();
1082     if (!scrollbar)
1083         return false;
1084     return scrollbar->enabled();
1085 }
1086
1087 bool ScrollAnimatorMac::canScrollVertically()
1088 {
1089     Scrollbar* scrollbar = m_scrollableArea->verticalScrollbar();
1090     if (!scrollbar)
1091         return false;
1092     return scrollbar->enabled();
1093 }
1094
1095 bool ScrollAnimatorMac::shouldRubberBandInDirection(ScrollDirection direction)
1096 {
1097     return m_scrollableArea->shouldRubberBandInDirection(direction);
1098 }
1099
1100 IntPoint ScrollAnimatorMac::absoluteScrollPosition()
1101 {
1102     return m_scrollableArea->visibleContentRect().location() + m_scrollableArea->scrollOrigin();
1103 }
1104
1105 void ScrollAnimatorMac::immediateScrollByWithoutContentEdgeConstraints(const FloatSize& delta)
1106 {
1107     m_scrollableArea->setConstrainsScrollingToContentEdge(false);
1108     immediateScrollBy(delta);
1109     m_scrollableArea->setConstrainsScrollingToContentEdge(true);
1110 }
1111
1112 void ScrollAnimatorMac::immediateScrollBy(const FloatSize& delta)
1113 {
1114     FloatPoint newPos = adjustScrollPositionIfNecessary(FloatPoint(m_currentPosX, m_currentPosY) + delta);
1115     if (newPos.x() == m_currentPosX && newPos.y() == m_currentPosY)
1116         return;
1117
1118     m_currentPosX = newPos.x();
1119     m_currentPosY = newPos.y();
1120     notifyPositionChanged();
1121 }
1122
1123 void ScrollAnimatorMac::startSnapRubberbandTimer()
1124 {
1125     m_snapRubberBandTimer.startRepeating(1.0 / 60.0);
1126 }
1127
1128 void ScrollAnimatorMac::stopSnapRubberbandTimer()
1129 {
1130     m_snapRubberBandTimer.stop();
1131 }
1132
1133 void ScrollAnimatorMac::snapRubberBandTimerFired(Timer<ScrollAnimatorMac>*)
1134 {
1135     m_scrollElasticityController.snapRubberBandTimerFired();
1136 }
1137 #endif
1138
1139 void ScrollAnimatorMac::setIsActive()
1140 {
1141     if (!isScrollbarOverlayAPIAvailable())
1142         return;
1143
1144     if (!m_needsScrollerStyleUpdate)
1145         return;
1146
1147     updateScrollerStyle();
1148 }
1149
1150 void ScrollAnimatorMac::updateScrollerStyle()
1151 {
1152     if (!isScrollbarOverlayAPIAvailable())
1153         return;
1154
1155     if (!scrollableArea()->isOnActivePage()) {
1156         m_needsScrollerStyleUpdate = true;
1157         return;
1158     }
1159
1160     ScrollbarThemeMac* macTheme = macScrollbarTheme();
1161     if (!macTheme) {
1162         m_needsScrollerStyleUpdate = false;
1163         return;
1164     }
1165
1166     NSScrollerStyle newStyle = [m_scrollbarPainterController.get() scrollerStyle];
1167
1168     if (Scrollbar* verticalScrollbar = scrollableArea()->verticalScrollbar()) {
1169         verticalScrollbar->invalidate();
1170
1171         ScrollbarPainter oldVerticalPainter = [m_scrollbarPainterController.get() verticalScrollerImp];
1172         ScrollbarPainter newVerticalPainter = [NSClassFromString(@"NSScrollerImp") scrollerImpWithStyle:newStyle 
1173                                                                                     controlSize:(NSControlSize)verticalScrollbar->controlSize() 
1174                                                                                     horizontal:NO 
1175                                                                                     replacingScrollerImp:oldVerticalPainter];
1176         [m_scrollbarPainterController.get() setVerticalScrollerImp:newVerticalPainter];
1177         macTheme->setNewPainterForScrollbar(verticalScrollbar, newVerticalPainter);
1178
1179         // The different scrollbar styles have different thicknesses, so we must re-set the 
1180         // frameRect to the new thickness, and the re-layout below will ensure the position
1181         // and length are properly updated.
1182         int thickness = macTheme->scrollbarThickness(verticalScrollbar->controlSize());
1183         verticalScrollbar->setFrameRect(IntRect(0, 0, thickness, thickness));
1184     }
1185
1186     if (Scrollbar* horizontalScrollbar = scrollableArea()->horizontalScrollbar()) {
1187         horizontalScrollbar->invalidate();
1188
1189         ScrollbarPainter oldHorizontalPainter = [m_scrollbarPainterController.get() horizontalScrollerImp];
1190         ScrollbarPainter newHorizontalPainter = [NSClassFromString(@"NSScrollerImp") scrollerImpWithStyle:newStyle 
1191                                                                                     controlSize:(NSControlSize)horizontalScrollbar->controlSize() 
1192                                                                                     horizontal:YES 
1193                                                                                     replacingScrollerImp:oldHorizontalPainter];
1194         [m_scrollbarPainterController.get() setHorizontalScrollerImp:newHorizontalPainter];
1195         macTheme->setNewPainterForScrollbar(horizontalScrollbar, newHorizontalPainter);
1196
1197         // The different scrollbar styles have different thicknesses, so we must re-set the 
1198         // frameRect to the new thickness, and the re-layout below will ensure the position
1199         // and length are properly updated.
1200         int thickness = macTheme->scrollbarThickness(horizontalScrollbar->controlSize());
1201         horizontalScrollbar->setFrameRect(IntRect(0, 0, thickness, thickness));
1202     }
1203
1204     // If m_needsScrollerStyleUpdate is true, then the page is restoring from the page cache, and 
1205     // a relayout will happen on its own. Otherwise, we must initiate a re-layout ourselves.
1206     scrollableArea()->scrollbarStyleChanged(newStyle, !m_needsScrollerStyleUpdate);
1207
1208     m_needsScrollerStyleUpdate = false;
1209 }
1210
1211 void ScrollAnimatorMac::startScrollbarPaintTimer()
1212 {
1213     m_initialScrollbarPaintTimer.startOneShot(0.1);
1214 }
1215
1216 bool ScrollAnimatorMac::scrollbarPaintTimerIsActive() const
1217 {
1218     return m_initialScrollbarPaintTimer.isActive();
1219 }
1220
1221 void ScrollAnimatorMac::stopScrollbarPaintTimer()
1222 {
1223     m_initialScrollbarPaintTimer.stop();
1224 }
1225
1226 void ScrollAnimatorMac::initialScrollbarPaintTimerFired(Timer<ScrollAnimatorMac>*)
1227 {
1228     if (isScrollbarOverlayAPIAvailable()) {
1229         // To force the scrollbars to flash, we have to call hide first. Otherwise, the ScrollbarPainterController
1230         // might think that the scrollbars are already showing and bail early.
1231         [m_scrollbarPainterController.get() hideOverlayScrollers];
1232         [m_scrollbarPainterController.get() flashScrollers];
1233     }
1234 }
1235
1236 void ScrollAnimatorMac::sendContentAreaScrolledSoon()
1237 {
1238     if (!m_sendContentAreaScrolledTimer.isActive())
1239         m_sendContentAreaScrolledTimer.startOneShot(0);
1240 }
1241
1242 void ScrollAnimatorMac::sendContentAreaScrolledTimerFired(Timer<ScrollAnimatorMac>*)
1243 {
1244     [m_scrollbarPainterController.get() contentAreaScrolled];
1245 }
1246
1247 void ScrollAnimatorMac::setVisibleScrollerThumbRect(const IntRect& scrollerThumb)
1248 {
1249     IntRect rectInViewCoordinates = scrollerThumb;
1250     if (Scrollbar* verticalScrollbar = m_scrollableArea->verticalScrollbar())
1251         rectInViewCoordinates = verticalScrollbar->convertToContainingView(scrollerThumb);
1252
1253     if (rectInViewCoordinates == m_visibleScrollerThumbRect)
1254         return;
1255
1256     m_scrollableArea->setVisibleScrollerThumbRect(rectInViewCoordinates);
1257     m_visibleScrollerThumbRect = rectInViewCoordinates;
1258 }
1259
1260 } // namespace WebCore
1261
1262 #endif // ENABLE(SMOOTH_SCROLLING)