2 * Copyright (C) 2010, 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #include "platform/mac/ScrollAnimatorMac.h"
30 #include "platform/PlatformGestureEvent.h"
31 #include "platform/PlatformWheelEvent.h"
32 #include "platform/geometry/FloatRect.h"
33 #include "platform/geometry/IntRect.h"
34 #include "platform/mac/BlockExceptions.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"
44 using namespace blink;
47 static bool supportsUIStateTransitionProgress()
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;
54 static bool supportsExpansionTransitionProgress()
56 static bool globalSupportsExpansionTransitionProgress = [NSClassFromString(@"NSScrollerImp") instancesRespondToSelector:@selector(expansionTransitionProgress)];
57 return globalSupportsExpansionTransitionProgress;
60 static bool supportsContentAreaScrolledInDirection()
62 static bool globalSupportsContentAreaScrolledInDirection = [NSClassFromString(@"NSScrollerImpPair") instancesRespondToSelector:@selector(contentAreaScrolledInDirection:)];
63 return globalSupportsContentAreaScrolledInDirection;
66 static ScrollbarThemeMacOverlayAPI* macOverlayScrollbarTheme()
68 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(ScrollbarThemeMacCommon::isOverlayAPIAvailable());
69 ScrollbarTheme* scrollbarTheme = ScrollbarTheme::theme();
70 return !scrollbarTheme->isMockTheme() ? static_cast<ScrollbarThemeMacOverlayAPI*>(scrollbarTheme) : 0;
73 static ScrollbarPainter scrollbarPainterForScrollbar(Scrollbar* scrollbar)
75 if (ScrollbarThemeMacOverlayAPI* scrollbarTheme = macOverlayScrollbarTheme())
76 return scrollbarTheme->painterForScrollbar(scrollbar);
81 @interface NSObject (ScrollAnimationHelperDetails)
82 - (id)initWithDelegate:(id)delegate;
85 - (NSPoint)targetOrigin;
89 @interface WebScrollAnimationHelperDelegate : NSObject
91 blink::ScrollAnimatorMac* _animator;
93 - (id)initWithScrollAnimator:(blink::ScrollAnimatorMac*)scrollAnimator;
96 static NSSize abs(NSSize size)
98 NSSize finalSize = size;
99 if (finalSize.width < 0)
100 finalSize.width = -finalSize.width;
101 if (finalSize.height < 0)
102 finalSize.height = -finalSize.height;
106 @implementation WebScrollAnimationHelperDelegate
108 - (id)initWithScrollAnimator:(blink::ScrollAnimatorMac*)scrollAnimator
114 _animator = scrollAnimator;
128 blink::FloatPoint currentPosition = _animator->currentPosition();
129 return NSMakeRect(currentPosition.x(), currentPosition.y(), 0, 0);
132 - (void)_immediateScrollToPoint:(NSPoint)newPosition
136 _animator->immediateScrollToPointForScrollAnimation(newPosition);
139 - (NSPoint)_pixelAlignProposedScrollPosition:(NSPoint)newOrigin
144 - (NSSize)convertSizeToBase:(NSSize)size
149 - (NSSize)convertSizeFromBase:(NSSize)size
154 - (NSSize)convertSizeToBacking:(NSSize)size
159 - (NSSize)convertSizeFromBacking:(NSSize)size
179 - (void)_recursiveRecomputeToolTips
185 @interface WebScrollbarPainterControllerDelegate : NSObject
187 ScrollableArea* _scrollableArea;
189 - (id)initWithScrollableArea:(ScrollableArea*)scrollableArea;
192 @implementation WebScrollbarPainterControllerDelegate
194 - (id)initWithScrollableArea:(ScrollableArea*)scrollableArea
200 _scrollableArea = scrollableArea;
209 - (NSRect)contentAreaRectForScrollerImpPair:(id)scrollerImpPair
211 if (!_scrollableArea)
214 blink::IntSize contentsSize = _scrollableArea->contentsSize();
215 return NSMakeRect(0, 0, contentsSize.width(), contentsSize.height());
218 - (BOOL)inLiveResizeForScrollerImpPair:(id)scrollerImpPair
220 if (!_scrollableArea)
223 return _scrollableArea->inLiveResize();
226 - (NSPoint)mouseLocationInContentAreaForScrollerImpPair:(id)scrollerImpPair
228 if (!_scrollableArea)
231 return _scrollableArea->lastKnownMousePosition();
234 - (NSPoint)scrollerImpPair:(id)scrollerImpPair convertContentPoint:(NSPoint)pointInContentArea toScrollerImp:(id)scrollerImp
237 if (!_scrollableArea || !scrollerImp)
240 blink::Scrollbar* scrollbar = 0;
241 if ([scrollerImp isHorizontal])
242 scrollbar = _scrollableArea->horizontalScrollbar();
244 scrollbar = _scrollableArea->verticalScrollbar();
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
254 ASSERT(scrollerImp == scrollbarPainterForScrollbar(scrollbar));
256 return scrollbar->convertFromContainingView(blink::IntPoint(pointInContentArea));
259 - (void)scrollerImpPair:(id)scrollerImpPair setContentAreaNeedsDisplayInRect:(NSRect)rect
261 if (!_scrollableArea)
264 if (!_scrollableArea->scrollbarsCanBeActive())
267 _scrollableArea->scrollAnimator()->contentAreaWillPaint();
270 - (void)scrollerImpPair:(id)scrollerImpPair updateScrollerStyleForNewRecommendedScrollerStyle:(NSScrollerStyle)newRecommendedScrollerStyle
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
279 if (!_scrollableArea)
282 [scrollerImpPair setScrollerStyle:newRecommendedScrollerStyle];
284 static_cast<ScrollAnimatorMac*>(_scrollableArea->scrollAnimator())->updateScrollerStyle();
289 enum FeatureToAnimate {
296 @interface WebScrollbarPartAnimation : NSAnimation
298 Scrollbar* _scrollbar;
299 RetainPtr<ScrollbarPainter> _scrollbarPainter;
300 FeatureToAnimate _featureToAnimate;
304 - (id)initWithScrollbar:(Scrollbar*)scrollbar featureToAnimate:(FeatureToAnimate)featureToAnimate animateFrom:(CGFloat)startValue animateTo:(CGFloat)endValue duration:(NSTimeInterval)duration;
307 @implementation WebScrollbarPartAnimation
309 - (id)initWithScrollbar:(Scrollbar*)scrollbar featureToAnimate:(FeatureToAnimate)featureToAnimate animateFrom:(CGFloat)startValue animateTo:(CGFloat)endValue duration:(NSTimeInterval)duration
311 self = [super initWithDuration:duration animationCurve:NSAnimationEaseInOut];
315 _scrollbar = scrollbar;
316 _featureToAnimate = featureToAnimate;
317 _startValue = startValue;
318 _endValue = endValue;
320 [self setAnimationBlockingMode:NSAnimationNonblocking];
325 - (void)startAnimation
329 _scrollbarPainter = scrollbarPainterForScrollbar(_scrollbar);
331 [super startAnimation];
334 - (void)setStartValue:(CGFloat)startValue
336 _startValue = startValue;
339 - (void)setEndValue:(CGFloat)endValue
341 _endValue = endValue;
344 - (void)setCurrentProgress:(NSAnimationProgress)progress
346 [super setCurrentProgress:progress];
350 CGFloat currentValue;
351 if (_startValue > _endValue)
352 currentValue = 1 - progress;
354 currentValue = progress;
356 switch (_featureToAnimate) {
358 [_scrollbarPainter.get() setKnobAlpha:currentValue];
361 [_scrollbarPainter.get() setTrackAlpha:currentValue];
363 case UIStateTransition:
364 [_scrollbarPainter.get() setUiStateTransitionProgress:currentValue];
366 case ExpansionTransition:
367 [_scrollbarPainter.get() setExpansionTransitionProgress:currentValue];
371 _scrollbar->invalidate();
376 BEGIN_BLOCK_OBJC_EXCEPTIONS;
377 [self stopAnimation];
378 END_BLOCK_OBJC_EXCEPTIONS;
384 @interface WebScrollbarPainterDelegate : NSObject<NSAnimationDelegate>
386 blink::Scrollbar* _scrollbar;
388 RetainPtr<WebScrollbarPartAnimation> _knobAlphaAnimation;
389 RetainPtr<WebScrollbarPartAnimation> _trackAlphaAnimation;
390 RetainPtr<WebScrollbarPartAnimation> _uiStateTransitionAnimation;
391 RetainPtr<WebScrollbarPartAnimation> _expansionTransitionAnimation;
393 - (id)initWithScrollbar:(blink::Scrollbar*)scrollbar;
394 - (void)cancelAnimations;
397 @implementation WebScrollbarPainterDelegate
399 - (id)initWithScrollbar:(blink::Scrollbar*)scrollbar
405 _scrollbar = scrollbar;
409 - (void)cancelAnimations
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;
419 - (ScrollAnimatorMac*)scrollAnimator
421 return static_cast<ScrollAnimatorMac*>(_scrollbar->scrollableArea()->scrollAnimator());
424 - (NSRect)convertRectToBacking:(NSRect)aRect
429 - (NSRect)convertRectFromBacking:(NSRect)aRect
434 - (NSPoint)mouseLocationInScrollerForScrollerImp:(id)scrollerImp
439 ASSERT_UNUSED(scrollerImp, scrollerImp == scrollbarPainterForScrollbar(_scrollbar));
441 return _scrollbar->convertFromContainingView(_scrollbar->scrollableArea()->lastKnownMousePosition());
444 - (void)setUpAlphaAnimation:(RetainPtr<WebScrollbarPartAnimation>&)scrollbarPartAnimation scrollerPainter:(ScrollbarPainter)scrollerPainter part:(blink::ScrollbarPart)part animateAlphaTo:(CGFloat)newAlpha duration:(NSTimeInterval)duration
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();
450 if ([self scrollAnimator]->scrollbarPaintTimerIsActive() && !mustAnimate)
453 if (_scrollbar->scrollableArea()->shouldSuspendScrollAnimations() && !mustAnimate) {
454 [self scrollAnimator]->startScrollbarPaintTimer();
458 // At this point, we are definitely going to animate now, so stop the timer.
459 [self scrollAnimator]->stopScrollbarPaintTimer();
461 // If we are currently animating, stop
462 if (scrollbarPartAnimation) {
463 [scrollbarPartAnimation.get() stopAnimation];
464 scrollbarPartAnimation = nil;
467 if (part == blink::ThumbPart && _scrollbar->orientation() == VerticalScrollbar) {
469 IntRect thumbRect = IntRect([scrollerPainter rectForPart:NSScrollerKnob]);
470 [self scrollAnimator]->setVisibleScrollerThumbRect(thumbRect);
472 [self scrollAnimator]->setVisibleScrollerThumbRect(IntRect());
475 scrollbarPartAnimation.adoptNS([[WebScrollbarPartAnimation alloc] initWithScrollbar:_scrollbar
476 featureToAnimate:part == ThumbPart ? ThumbAlpha : TrackAlpha
477 animateFrom:part == ThumbPart ? [scrollerPainter knobAlpha] : [scrollerPainter trackAlpha]
480 [scrollbarPartAnimation.get() startAnimation];
483 - (void)scrollerImp:(id)scrollerImp animateKnobAlphaTo:(CGFloat)newKnobAlpha duration:(NSTimeInterval)duration
488 ASSERT(scrollerImp == scrollbarPainterForScrollbar(_scrollbar));
490 ScrollbarPainter scrollerPainter = (ScrollbarPainter)scrollerImp;
491 [self setUpAlphaAnimation:_knobAlphaAnimation scrollerPainter:scrollerPainter part:blink::ThumbPart animateAlphaTo:newKnobAlpha duration:duration];
494 - (void)scrollerImp:(id)scrollerImp animateTrackAlphaTo:(CGFloat)newTrackAlpha duration:(NSTimeInterval)duration
499 ASSERT(scrollerImp == scrollbarPainterForScrollbar(_scrollbar));
501 ScrollbarPainter scrollerPainter = (ScrollbarPainter)scrollerImp;
502 [self setUpAlphaAnimation:_trackAlphaAnimation scrollerPainter:scrollerPainter part:blink::BackTrackPart animateAlphaTo:newTrackAlpha duration:duration];
505 - (void)scrollerImp:(id)scrollerImp animateUIStateTransitionWithDuration:(NSTimeInterval)duration
510 if (!supportsUIStateTransitionProgress())
513 ASSERT(scrollerImp == scrollbarPainterForScrollbar(_scrollbar));
515 ScrollbarPainter scrollbarPainter = (ScrollbarPainter)scrollerImp;
517 // UIStateTransition always animates to 1. In case an animation is in progress this avoids a hard transition.
518 [scrollbarPainter setUiStateTransitionProgress:1 - [scrollerImp uiStateTransitionProgress]];
520 if (!_uiStateTransitionAnimation)
521 _uiStateTransitionAnimation.adoptNS([[WebScrollbarPartAnimation alloc] initWithScrollbar:_scrollbar
522 featureToAnimate:UIStateTransition
523 animateFrom:[scrollbarPainter uiStateTransitionProgress]
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];
532 [_uiStateTransitionAnimation.get() startAnimation];
535 - (void)scrollerImp:(id)scrollerImp animateExpansionTransitionWithDuration:(NSTimeInterval)duration
540 if (!supportsExpansionTransitionProgress())
543 ASSERT(scrollerImp == scrollbarPainterForScrollbar(_scrollbar));
545 ScrollbarPainter scrollbarPainter = (ScrollbarPainter)scrollerImp;
547 // ExpansionTransition always animates to 1. In case an animation is in progress this avoids a hard transition.
548 [scrollbarPainter setExpansionTransitionProgress:1 - [scrollerImp expansionTransitionProgress]];
550 if (!_expansionTransitionAnimation) {
551 _expansionTransitionAnimation.adoptNS([[WebScrollbarPartAnimation alloc] initWithScrollbar:_scrollbar
552 featureToAnimate:ExpansionTransition
553 animateFrom:[scrollbarPainter expansionTransitionProgress]
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];
562 [_expansionTransitionAnimation.get() startAnimation];
565 - (void)scrollerImp:(id)scrollerImp overlayScrollerStateChangedTo:(NSUInteger)newOverlayScrollerState
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;
584 PassOwnPtr<ScrollAnimator> ScrollAnimator::create(ScrollableArea* scrollableArea)
586 return adoptPtr(new ScrollAnimatorMac(scrollableArea));
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)
597 , m_haveScrolledSincePageLoad(false)
598 , m_needsScrollerStyleUpdate(false)
600 m_scrollAnimationHelperDelegate.adoptNS([[WebScrollAnimationHelperDelegate alloc] initWithScrollAnimator:this]);
601 m_scrollAnimationHelper.adoptNS([[NSClassFromString(@"NSScrollAnimationHelper") alloc] initWithDelegate:m_scrollAnimationHelperDelegate.get()]);
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()];
611 ScrollAnimatorMac::~ScrollAnimatorMac()
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;
624 static bool scrollAnimationEnabledForSystem()
626 NSString* scrollAnimationDefaultsKey =
627 @"AppleScrollAnimationEnabled";
628 static bool enabled = [[NSUserDefaults standardUserDefaults] boolForKey:scrollAnimationDefaultsKey];
632 #if USE(RUBBER_BANDING)
633 static bool rubberBandingEnabledForSystem()
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.
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];
650 bool ScrollAnimatorMac::scroll(ScrollbarOrientation orientation, ScrollGranularity granularity, float step, float delta)
652 m_haveScrolledSincePageLoad = true;
654 if (!scrollAnimationEnabledForSystem() || !m_scrollableArea->scrollAnimatorEnabled())
655 return ScrollAnimator::scroll(orientation, granularity, step, delta);
657 if (granularity == ScrollByPixel)
658 return ScrollAnimator::scroll(orientation, granularity, step, delta);
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)
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);
670 newPoint = orientation == HorizontalScrollbar ? NSMakePoint(newPos, m_currentPosY) : NSMakePoint(m_currentPosX, newPos);
672 [m_scrollAnimationHelper.get() scrollToPoint:newPoint];
676 void ScrollAnimatorMac::scrollToOffsetWithoutAnimation(const FloatPoint& offset)
678 [m_scrollAnimationHelper.get() _stopRun];
679 immediateScrollTo(offset);
682 FloatPoint ScrollAnimatorMac::adjustScrollPositionIfNecessary(const FloatPoint& position) const
684 if (!m_scrollableArea->constrainsScrollingToContentEdge())
687 IntPoint minPos = m_scrollableArea->minimumScrollPosition();
688 IntPoint maxPos = m_scrollableArea->maximumScrollPosition();
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());
693 return FloatPoint(newX, newY);
696 void ScrollAnimatorMac::adjustScrollPositionToBoundsIfNecessary()
698 bool currentlyConstrainsToContentEdge = m_scrollableArea->constrainsScrollingToContentEdge();
699 m_scrollableArea->setConstrainsScrollingToContentEdge(true);
701 IntPoint currentScrollPosition = absoluteScrollPosition();
702 FloatPoint nearestPointWithinBounds = adjustScrollPositionIfNecessary(absoluteScrollPosition());
703 immediateScrollBy(nearestPointWithinBounds - currentScrollPosition);
705 m_scrollableArea->setConstrainsScrollingToContentEdge(currentlyConstrainsToContentEdge);
708 void ScrollAnimatorMac::immediateScrollTo(const FloatPoint& newPosition)
710 FloatPoint adjustedPosition = adjustScrollPositionIfNecessary(newPosition);
712 bool positionChanged = adjustedPosition.x() != m_currentPosX || adjustedPosition.y() != m_currentPosY;
713 if (!positionChanged && !scrollableArea()->scrollOriginChanged())
716 FloatSize delta = FloatSize(adjustedPosition.x() - m_currentPosX, adjustedPosition.y() - m_currentPosY);
718 m_currentPosX = adjustedPosition.x();
719 m_currentPosY = adjustedPosition.y();
720 notifyContentAreaScrolled(delta);
721 notifyPositionChanged();
724 bool ScrollAnimatorMac::isRubberBandInProgress() const
726 #if !USE(RUBBER_BANDING)
729 return m_scrollElasticityController.isRubberBandInProgress();
733 void ScrollAnimatorMac::immediateScrollToPointForScrollAnimation(const FloatPoint& newPosition)
735 ASSERT(m_scrollAnimationHelper);
736 immediateScrollTo(newPosition);
739 void ScrollAnimatorMac::contentAreaWillPaint() const
741 if (!scrollableArea()->scrollbarsCanBeActive())
743 if (ScrollbarThemeMacCommon::isOverlayAPIAvailable())
744 [m_scrollbarPainterController.get() contentAreaWillDraw];
747 void ScrollAnimatorMac::mouseEnteredContentArea() const
749 if (!scrollableArea()->scrollbarsCanBeActive())
751 if (ScrollbarThemeMacCommon::isOverlayAPIAvailable())
752 [m_scrollbarPainterController.get() mouseEnteredContentArea];
755 void ScrollAnimatorMac::mouseExitedContentArea() const
757 if (!scrollableArea()->scrollbarsCanBeActive())
759 if (ScrollbarThemeMacCommon::isOverlayAPIAvailable())
760 [m_scrollbarPainterController.get() mouseExitedContentArea];
763 void ScrollAnimatorMac::mouseMovedInContentArea() const
765 if (!scrollableArea()->scrollbarsCanBeActive())
767 if (ScrollbarThemeMacCommon::isOverlayAPIAvailable())
768 [m_scrollbarPainterController.get() mouseMovedInContentArea];
771 void ScrollAnimatorMac::mouseEnteredScrollbar(Scrollbar* scrollbar) const
773 // At this time, only legacy scrollbars needs to send notifications here.
774 if (ScrollbarThemeMacCommon::recommendedScrollerStyle() != NSScrollerStyleLegacy)
777 if (!scrollableArea()->scrollbarsCanBeActive())
780 if (ScrollbarThemeMacCommon::isOverlayAPIAvailable()) {
781 if (!supportsUIStateTransitionProgress())
783 if (ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar))
784 [painter mouseEnteredScroller];
788 void ScrollAnimatorMac::mouseExitedScrollbar(Scrollbar* scrollbar) const
790 // At this time, only legacy scrollbars needs to send notifications here.
791 if (ScrollbarThemeMacCommon::recommendedScrollerStyle() != NSScrollerStyleLegacy)
794 if (!scrollableArea()->scrollbarsCanBeActive())
797 if (ScrollbarThemeMacCommon::isOverlayAPIAvailable()) {
798 if (!supportsUIStateTransitionProgress())
800 if (ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar))
801 [painter mouseExitedScroller];
805 void ScrollAnimatorMac::willStartLiveResize()
807 if (!scrollableArea()->scrollbarsCanBeActive())
809 if (ScrollbarThemeMacCommon::isOverlayAPIAvailable())
810 [m_scrollbarPainterController.get() startLiveResize];
813 void ScrollAnimatorMac::contentsResized() const
815 if (!scrollableArea()->scrollbarsCanBeActive())
817 if (ScrollbarThemeMacCommon::isOverlayAPIAvailable())
818 [m_scrollbarPainterController.get() contentAreaDidResize];
821 void ScrollAnimatorMac::willEndLiveResize()
823 if (!scrollableArea()->scrollbarsCanBeActive())
825 if (ScrollbarThemeMacCommon::isOverlayAPIAvailable())
826 [m_scrollbarPainterController.get() endLiveResize];
829 void ScrollAnimatorMac::contentAreaDidShow() const
831 if (!scrollableArea()->scrollbarsCanBeActive())
833 if (ScrollbarThemeMacCommon::isOverlayAPIAvailable())
834 [m_scrollbarPainterController.get() windowOrderedIn];
837 void ScrollAnimatorMac::contentAreaDidHide() const
839 if (!scrollableArea()->scrollbarsCanBeActive())
841 if (ScrollbarThemeMacCommon::isOverlayAPIAvailable())
842 [m_scrollbarPainterController.get() windowOrderedOut];
845 void ScrollAnimatorMac::didBeginScrollGesture() const
847 if (!scrollableArea()->scrollbarsCanBeActive())
849 if (ScrollbarThemeMacCommon::isOverlayAPIAvailable())
850 [m_scrollbarPainterController.get() beginScrollGesture];
853 void ScrollAnimatorMac::didEndScrollGesture() const
855 if (!scrollableArea()->scrollbarsCanBeActive())
857 if (ScrollbarThemeMacCommon::isOverlayAPIAvailable())
858 [m_scrollbarPainterController.get() endScrollGesture];
861 void ScrollAnimatorMac::mayBeginScrollGesture() const
863 if (!scrollableArea()->scrollbarsCanBeActive())
865 if (!ScrollbarThemeMacCommon::isOverlayAPIAvailable())
868 [m_scrollbarPainterController.get() beginScrollGesture];
869 [m_scrollbarPainterController.get() contentAreaScrolled];
872 void ScrollAnimatorMac::finishCurrentScrollAnimations()
874 if (ScrollbarThemeMacCommon::isOverlayAPIAvailable()) {
875 [m_scrollbarPainterController.get() hideOverlayScrollers];
879 void ScrollAnimatorMac::didAddVerticalScrollbar(Scrollbar* scrollbar)
881 if (!ScrollbarThemeMacCommon::isOverlayAPIAvailable())
884 ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar);
888 ASSERT(!m_verticalScrollbarPainterDelegate);
889 m_verticalScrollbarPainterDelegate.adoptNS([[WebScrollbarPainterDelegate alloc] initWithScrollbar:scrollbar]);
891 [painter setDelegate:m_verticalScrollbarPainterDelegate.get()];
892 [m_scrollbarPainterController.get() setVerticalScrollerImp:painter];
893 if (scrollableArea()->inLiveResize())
894 [painter setKnobAlpha:1];
897 void ScrollAnimatorMac::willRemoveVerticalScrollbar(Scrollbar* scrollbar)
899 if (!ScrollbarThemeMacCommon::isOverlayAPIAvailable())
902 ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar);
906 ASSERT(m_verticalScrollbarPainterDelegate);
907 [m_verticalScrollbarPainterDelegate.get() invalidate];
908 m_verticalScrollbarPainterDelegate = nullptr;
910 [painter setDelegate:nil];
911 [m_scrollbarPainterController.get() setVerticalScrollerImp:nil];
914 void ScrollAnimatorMac::didAddHorizontalScrollbar(Scrollbar* scrollbar)
916 if (!ScrollbarThemeMacCommon::isOverlayAPIAvailable())
919 ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar);
923 ASSERT(!m_horizontalScrollbarPainterDelegate);
924 m_horizontalScrollbarPainterDelegate.adoptNS([[WebScrollbarPainterDelegate alloc] initWithScrollbar:scrollbar]);
926 [painter setDelegate:m_horizontalScrollbarPainterDelegate.get()];
927 [m_scrollbarPainterController.get() setHorizontalScrollerImp:painter];
928 if (scrollableArea()->inLiveResize())
929 [painter setKnobAlpha:1];
932 void ScrollAnimatorMac::willRemoveHorizontalScrollbar(Scrollbar* scrollbar)
934 if (!ScrollbarThemeMacCommon::isOverlayAPIAvailable())
937 ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar);
941 ASSERT(m_horizontalScrollbarPainterDelegate);
942 [m_horizontalScrollbarPainterDelegate.get() invalidate];
943 m_horizontalScrollbarPainterDelegate = nullptr;
945 [painter setDelegate:nil];
946 [m_scrollbarPainterController.get() setHorizontalScrollerImp:nil];
949 bool ScrollAnimatorMac::shouldScrollbarParticipateInHitTesting(Scrollbar* scrollbar)
951 // Non-overlay scrollbars should always participate in hit testing.
952 if (ScrollbarThemeMacCommon::recommendedScrollerStyle() != NSScrollerStyleOverlay)
955 if (!ScrollbarThemeMacCommon::isOverlayAPIAvailable())
958 if (scrollbar->isAlphaLocked())
961 // Overlay scrollbars should participate in hit testing whenever they are at all visible.
962 ScrollbarPainter painter = scrollbarPainterForScrollbar(scrollbar);
965 return [painter knobAlpha] > 0;
968 void ScrollAnimatorMac::notifyContentAreaScrolled(const FloatSize& delta)
970 if (!ScrollbarThemeMacCommon::isOverlayAPIAvailable())
973 // This function is called when a page is going into the page cache, but the page
974 // isn't really scrolling in that case. We should only pass the message on to the
975 // ScrollbarPainterController when we're really scrolling on an active page.
976 if (scrollableArea()->scrollbarsCanBeActive())
977 sendContentAreaScrolledSoon(delta);
980 void ScrollAnimatorMac::cancelAnimations()
982 m_haveScrolledSincePageLoad = false;
984 if (ScrollbarThemeMacCommon::isOverlayAPIAvailable()) {
985 if (scrollbarPaintTimerIsActive())
986 stopScrollbarPaintTimer();
987 [m_horizontalScrollbarPainterDelegate.get() cancelAnimations];
988 [m_verticalScrollbarPainterDelegate.get() cancelAnimations];
992 void ScrollAnimatorMac::handleWheelEventPhase(PlatformWheelEventPhase phase)
994 // This may not have been set to true yet if the wheel event was handled by the ScrollingTree,
995 // So set it to true here.
996 m_haveScrolledSincePageLoad = true;
998 if (phase == PlatformWheelEventPhaseBegan)
999 didBeginScrollGesture();
1000 else if (phase == PlatformWheelEventPhaseEnded || phase == PlatformWheelEventPhaseCancelled)
1001 didEndScrollGesture();
1002 else if (phase == PlatformWheelEventPhaseMayBegin)
1003 mayBeginScrollGesture();
1006 #if USE(RUBBER_BANDING)
1007 bool ScrollAnimatorMac::handleWheelEvent(const PlatformWheelEvent& wheelEvent)
1009 m_haveScrolledSincePageLoad = true;
1011 if (!wheelEvent.hasPreciseScrollingDeltas() || !rubberBandingEnabledForSystem())
1012 return ScrollAnimator::handleWheelEvent(wheelEvent);
1014 // FIXME: This is somewhat roundabout hack to allow forwarding wheel events
1015 // up to the parent scrollable area. It takes advantage of the fact that
1016 // the base class implementation of handleWheelEvent will not accept the
1017 // wheel event if there is nowhere to scroll.
1018 if (fabsf(wheelEvent.deltaY()) >= fabsf(wheelEvent.deltaX())) {
1019 if (!allowsVerticalStretching())
1020 return ScrollAnimator::handleWheelEvent(wheelEvent);
1022 if (!allowsHorizontalStretching())
1023 return ScrollAnimator::handleWheelEvent(wheelEvent);
1026 bool didHandleEvent = m_scrollElasticityController.handleWheelEvent(wheelEvent);
1028 // The elasticity controller can return false on a phase end event if rubber banding wasn't in progress.
1029 // In this case, the wheel phase must still be handled so that that overlay scroll bars get hidden.
1030 if (didHandleEvent || wheelEvent.phase() == PlatformWheelEventPhaseEnded || wheelEvent.phase() == PlatformWheelEventPhaseCancelled)
1031 handleWheelEventPhase(wheelEvent.phase());
1033 return didHandleEvent;
1036 bool ScrollAnimatorMac::pinnedInDirection(float deltaX, float deltaY)
1038 FloatSize limitDelta;
1039 if (fabsf(deltaY) >= fabsf(deltaX)) {
1041 // We are trying to scroll up. Make sure we are not pinned to the top
1042 limitDelta.setHeight(m_scrollableArea->visibleContentRect().y() + + m_scrollableArea->scrollOrigin().y());
1044 // We are trying to scroll down. Make sure we are not pinned to the bottom
1045 limitDelta.setHeight(m_scrollableArea->contentsSize().height() - (m_scrollableArea->visibleContentRect().maxY() + m_scrollableArea->scrollOrigin().y()));
1047 } else if (deltaX != 0) {
1049 // We are trying to scroll left. Make sure we are not pinned to the left
1050 limitDelta.setWidth(m_scrollableArea->visibleContentRect().x() + m_scrollableArea->scrollOrigin().x());
1052 // We are trying to scroll right. Make sure we are not pinned to the right
1053 limitDelta.setWidth(m_scrollableArea->contentsSize().width() - (m_scrollableArea->visibleContentRect().maxX() + m_scrollableArea->scrollOrigin().x()));
1057 if ((deltaX != 0 || deltaY != 0) && (limitDelta.width() < 1 && limitDelta.height() < 1))
1062 bool ScrollAnimatorMac::allowsVerticalStretching()
1064 switch (m_scrollableArea->verticalScrollElasticity()) {
1065 case ScrollElasticityAutomatic: {
1066 Scrollbar* hScroller = m_scrollableArea->horizontalScrollbar();
1067 Scrollbar* vScroller = m_scrollableArea->verticalScrollbar();
1068 return (((vScroller && vScroller->enabled()) || (!hScroller || !hScroller->enabled())));
1070 case ScrollElasticityNone:
1072 case ScrollElasticityAllowed:
1076 ASSERT_NOT_REACHED();
1080 bool ScrollAnimatorMac::allowsHorizontalStretching()
1082 switch (m_scrollableArea->horizontalScrollElasticity()) {
1083 case ScrollElasticityAutomatic: {
1084 Scrollbar* hScroller = m_scrollableArea->horizontalScrollbar();
1085 Scrollbar* vScroller = m_scrollableArea->verticalScrollbar();
1086 return (((hScroller && hScroller->enabled()) || (!vScroller || !vScroller->enabled())));
1088 case ScrollElasticityNone:
1090 case ScrollElasticityAllowed:
1094 ASSERT_NOT_REACHED();
1098 IntSize ScrollAnimatorMac::stretchAmount()
1100 return m_scrollableArea->overhangAmount();
1103 bool ScrollAnimatorMac::pinnedInDirection(const FloatSize& direction)
1105 return pinnedInDirection(direction.width(), direction.height());
1108 bool ScrollAnimatorMac::canScrollHorizontally()
1110 Scrollbar* scrollbar = m_scrollableArea->horizontalScrollbar();
1113 return scrollbar->enabled();
1116 bool ScrollAnimatorMac::canScrollVertically()
1118 Scrollbar* scrollbar = m_scrollableArea->verticalScrollbar();
1121 return scrollbar->enabled();
1124 IntPoint ScrollAnimatorMac::absoluteScrollPosition()
1126 return m_scrollableArea->visibleContentRect().location() + m_scrollableArea->scrollOrigin();
1129 void ScrollAnimatorMac::immediateScrollByWithoutContentEdgeConstraints(const FloatSize& delta)
1131 m_scrollableArea->setConstrainsScrollingToContentEdge(false);
1132 immediateScrollBy(delta);
1133 m_scrollableArea->setConstrainsScrollingToContentEdge(true);
1136 void ScrollAnimatorMac::immediateScrollBy(const FloatSize& delta)
1138 FloatPoint newPos = adjustScrollPositionIfNecessary(FloatPoint(m_currentPosX, m_currentPosY) + delta);
1139 if (newPos.x() == m_currentPosX && newPos.y() == m_currentPosY)
1142 FloatSize adjustedDelta = FloatSize(newPos.x() - m_currentPosX, newPos.y() - m_currentPosY);
1144 m_currentPosX = newPos.x();
1145 m_currentPosY = newPos.y();
1146 notifyContentAreaScrolled(adjustedDelta);
1147 notifyPositionChanged();
1150 void ScrollAnimatorMac::startSnapRubberbandTimer()
1152 m_snapRubberBandTimer.startRepeating(1.0 / 60.0, FROM_HERE);
1155 void ScrollAnimatorMac::stopSnapRubberbandTimer()
1157 m_snapRubberBandTimer.stop();
1160 void ScrollAnimatorMac::snapRubberBandTimerFired(Timer<ScrollAnimatorMac>*)
1162 m_scrollElasticityController.snapRubberBandTimerFired();
1166 void ScrollAnimatorMac::setIsActive()
1168 if (!ScrollbarThemeMacCommon::isOverlayAPIAvailable())
1171 if (!m_needsScrollerStyleUpdate)
1174 updateScrollerStyle();
1177 void ScrollAnimatorMac::updateScrollerStyle()
1179 if (!ScrollbarThemeMacCommon::isOverlayAPIAvailable())
1182 if (!scrollableArea()->scrollbarsCanBeActive()) {
1183 m_needsScrollerStyleUpdate = true;
1187 ScrollbarThemeMacOverlayAPI* macTheme = macOverlayScrollbarTheme();
1189 m_needsScrollerStyleUpdate = false;
1193 NSScrollerStyle newStyle = [m_scrollbarPainterController.get() scrollerStyle];
1195 if (Scrollbar* verticalScrollbar = scrollableArea()->verticalScrollbar()) {
1196 verticalScrollbar->invalidate();
1198 ScrollbarPainter oldVerticalPainter = [m_scrollbarPainterController.get() verticalScrollerImp];
1199 ScrollbarPainter newVerticalPainter = [NSClassFromString(@"NSScrollerImp") scrollerImpWithStyle:newStyle
1200 controlSize:(NSControlSize)verticalScrollbar->controlSize()
1202 replacingScrollerImp:oldVerticalPainter];
1203 [m_scrollbarPainterController.get() setVerticalScrollerImp:newVerticalPainter];
1204 macTheme->setNewPainterForScrollbar(verticalScrollbar, newVerticalPainter);
1206 // The different scrollbar styles have different thicknesses, so we must re-set the
1207 // frameRect to the new thickness, and the re-layout below will ensure the position
1208 // and length are properly updated.
1209 int thickness = macTheme->scrollbarThickness(verticalScrollbar->controlSize());
1210 verticalScrollbar->setFrameRect(IntRect(0, 0, thickness, thickness));
1213 if (Scrollbar* horizontalScrollbar = scrollableArea()->horizontalScrollbar()) {
1214 horizontalScrollbar->invalidate();
1216 ScrollbarPainter oldHorizontalPainter = [m_scrollbarPainterController.get() horizontalScrollerImp];
1217 ScrollbarPainter newHorizontalPainter = [NSClassFromString(@"NSScrollerImp") scrollerImpWithStyle:newStyle
1218 controlSize:(NSControlSize)horizontalScrollbar->controlSize()
1220 replacingScrollerImp:oldHorizontalPainter];
1221 [m_scrollbarPainterController.get() setHorizontalScrollerImp:newHorizontalPainter];
1222 macTheme->setNewPainterForScrollbar(horizontalScrollbar, newHorizontalPainter);
1224 // The different scrollbar styles have different thicknesses, so we must re-set the
1225 // frameRect to the new thickness, and the re-layout below will ensure the position
1226 // and length are properly updated.
1227 int thickness = macTheme->scrollbarThickness(horizontalScrollbar->controlSize());
1228 horizontalScrollbar->setFrameRect(IntRect(0, 0, thickness, thickness));
1231 // If m_needsScrollerStyleUpdate is true, then the page is restoring from the page cache, and
1232 // a relayout will happen on its own. Otherwise, we must initiate a re-layout ourselves.
1233 if (!m_needsScrollerStyleUpdate)
1234 scrollableArea()->scrollbarStyleChanged();
1236 m_needsScrollerStyleUpdate = false;
1239 void ScrollAnimatorMac::startScrollbarPaintTimer()
1241 m_initialScrollbarPaintTimer.startOneShot(0.1, FROM_HERE);
1244 bool ScrollAnimatorMac::scrollbarPaintTimerIsActive() const
1246 return m_initialScrollbarPaintTimer.isActive();
1249 void ScrollAnimatorMac::stopScrollbarPaintTimer()
1251 m_initialScrollbarPaintTimer.stop();
1254 void ScrollAnimatorMac::initialScrollbarPaintTimerFired(Timer<ScrollAnimatorMac>*)
1256 if (ScrollbarThemeMacCommon::isOverlayAPIAvailable()) {
1257 // To force the scrollbars to flash, we have to call hide first. Otherwise, the ScrollbarPainterController
1258 // might think that the scrollbars are already showing and bail early.
1259 [m_scrollbarPainterController.get() hideOverlayScrollers];
1260 [m_scrollbarPainterController.get() flashScrollers];
1264 void ScrollAnimatorMac::sendContentAreaScrolledSoon(const FloatSize& delta)
1266 m_contentAreaScrolledTimerScrollDelta = delta;
1268 if (!m_sendContentAreaScrolledTimer.isActive())
1269 m_sendContentAreaScrolledTimer.startOneShot(0, FROM_HERE);
1272 void ScrollAnimatorMac::sendContentAreaScrolledTimerFired(Timer<ScrollAnimatorMac>*)
1274 if (supportsContentAreaScrolledInDirection()) {
1275 [m_scrollbarPainterController.get() contentAreaScrolledInDirection:NSMakePoint(m_contentAreaScrolledTimerScrollDelta.width(), m_contentAreaScrolledTimerScrollDelta.height())];
1276 m_contentAreaScrolledTimerScrollDelta = FloatSize();
1278 [m_scrollbarPainterController.get() contentAreaScrolled];
1281 void ScrollAnimatorMac::setVisibleScrollerThumbRect(const IntRect& scrollerThumb)
1283 IntRect rectInViewCoordinates = scrollerThumb;
1284 if (Scrollbar* verticalScrollbar = m_scrollableArea->verticalScrollbar())
1285 rectInViewCoordinates = verticalScrollbar->convertToContainingView(scrollerThumb);
1287 if (rectInViewCoordinates == m_visibleScrollerThumbRect)
1290 m_visibleScrollerThumbRect = rectInViewCoordinates;
1293 bool ScrollAnimatorMac::canUseCoordinatedScrollbar() {
1294 return ScrollbarThemeMacCommon::isOverlayAPIAvailable();
1297 } // namespace blink