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