2 * Copyright (C) 2010 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.
27 #include "PageOverlay.h"
30 #include "WebProcess.h"
31 #include <WebCore/Frame.h>
32 #include <WebCore/FrameView.h>
33 #include <WebCore/GraphicsContext.h>
34 #include <WebCore/Page.h>
35 #include <WebCore/ScrollbarTheme.h>
37 using namespace WebCore;
41 static const double fadeAnimationDuration = 0.2;
42 static const double fadeAnimationFrameRate = 30;
44 PassRefPtr<PageOverlay> PageOverlay::create(Client* client)
46 return adoptRef(new PageOverlay(client));
49 PageOverlay::PageOverlay(Client* client)
52 , m_fadeAnimationTimer(WebProcess::shared().runLoop(), this, &PageOverlay::fadeAnimationTimerFired)
53 , m_fadeAnimationStartTime(0.0)
54 , m_fadeAnimationDuration(fadeAnimationDuration)
55 , m_fadeAnimationType(NoAnimation)
56 , m_fractionFadedIn(1.0)
60 PageOverlay::~PageOverlay()
64 IntRect PageOverlay::bounds() const
66 FrameView* frameView = m_webPage->corePage()->mainFrame()->view();
68 int width = frameView->width();
69 int height = frameView->height();
71 if (!ScrollbarTheme::nativeTheme()->usesOverlayScrollbars()) {
72 if (frameView->verticalScrollbar())
73 width -= frameView->verticalScrollbar()->width();
74 if (frameView->horizontalScrollbar())
75 height -= frameView->horizontalScrollbar()->height();
77 return IntRect(0, 0, width, height);
80 void PageOverlay::setPage(WebPage* webPage)
82 m_client->willMoveToWebPage(this, webPage);
84 m_client->didMoveToWebPage(this, webPage);
86 m_fadeAnimationTimer.stop();
89 void PageOverlay::setNeedsDisplay(const IntRect& dirtyRect)
92 m_webPage->drawingArea()->setPageOverlayNeedsDisplay(dirtyRect);
95 void PageOverlay::setNeedsDisplay()
97 setNeedsDisplay(bounds());
100 void PageOverlay::drawRect(GraphicsContext& graphicsContext, const IntRect& dirtyRect)
102 // If the dirty rect is outside the bounds, ignore it.
103 IntRect paintRect = intersection(dirtyRect, bounds());
104 if (paintRect.isEmpty())
107 GraphicsContextStateSaver stateSaver(graphicsContext);
108 graphicsContext.beginTransparencyLayer(1);
109 graphicsContext.setCompositeOperation(CompositeCopy);
111 m_client->drawRect(this, graphicsContext, paintRect);
113 graphicsContext.endTransparencyLayer();
116 bool PageOverlay::mouseEvent(const WebMouseEvent& mouseEvent)
118 // Ignore events outside the bounds.
119 if (!bounds().contains(mouseEvent.position()))
122 return m_client->mouseEvent(this, mouseEvent);
125 void PageOverlay::startFadeInAnimation()
127 m_fractionFadedIn = 0.0;
128 m_fadeAnimationType = FadeInAnimation;
130 startFadeAnimation();
133 void PageOverlay::startFadeOutAnimation()
135 m_fractionFadedIn = 1.0;
136 m_fadeAnimationType = FadeOutAnimation;
138 startFadeAnimation();
141 void PageOverlay::startFadeAnimation()
143 m_fadeAnimationStartTime = currentTime();
146 m_fadeAnimationTimer.startRepeating(1 / fadeAnimationFrameRate);
149 void PageOverlay::fadeAnimationTimerFired()
151 float animationProgress = (currentTime() - m_fadeAnimationStartTime) / m_fadeAnimationDuration;
153 if (animationProgress >= 1.0)
154 animationProgress = 1.0;
156 double sine = sin(piOverTwoFloat * animationProgress);
157 float fadeAnimationValue = sine * sine;
159 m_fractionFadedIn = (m_fadeAnimationType == FadeInAnimation) ? fadeAnimationValue : 1 - fadeAnimationValue;
162 if (animationProgress == 1.0) {
163 m_fadeAnimationTimer.stop();
165 bool wasFadingOut = m_fadeAnimationType == FadeOutAnimation;
166 m_fadeAnimationType = NoAnimation;
169 // If this was a fade out, go ahead and uninstall the page overlay.
170 m_webPage->uninstallPageOverlay(this, false);
175 } // namespace WebKit