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