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)
57 , m_pageOverlayShouldApplyFadeWhenPainting(true)
61 PageOverlay::~PageOverlay()
65 IntRect PageOverlay::bounds() const
67 FrameView* frameView = m_webPage->corePage()->mainFrame()->view();
69 int width = frameView->width();
70 int height = frameView->height();
72 if (!ScrollbarTheme::theme()->usesOverlayScrollbars()) {
73 if (frameView->verticalScrollbar())
74 width -= frameView->verticalScrollbar()->width();
75 if (frameView->horizontalScrollbar())
76 height -= frameView->horizontalScrollbar()->height();
78 return IntRect(0, 0, width, height);
81 void PageOverlay::setPage(WebPage* webPage)
83 m_client->willMoveToWebPage(this, webPage);
85 m_client->didMoveToWebPage(this, webPage);
88 m_pageOverlayShouldApplyFadeWhenPainting = m_webPage->drawingArea()->pageOverlayShouldApplyFadeWhenPainting();
90 m_fadeAnimationTimer.stop();
93 void PageOverlay::setNeedsDisplay(const IntRect& dirtyRect)
96 if (!m_pageOverlayShouldApplyFadeWhenPainting)
97 m_webPage->drawingArea()->setPageOverlayOpacity(m_fractionFadedIn);
98 m_webPage->drawingArea()->setPageOverlayNeedsDisplay(dirtyRect);
102 void PageOverlay::setNeedsDisplay()
104 setNeedsDisplay(bounds());
107 void PageOverlay::drawRect(GraphicsContext& graphicsContext, const IntRect& dirtyRect)
109 // If the dirty rect is outside the bounds, ignore it.
110 IntRect paintRect = intersection(dirtyRect, bounds());
111 if (paintRect.isEmpty())
114 GraphicsContextStateSaver stateSaver(graphicsContext);
115 graphicsContext.beginTransparencyLayer(1);
116 graphicsContext.setCompositeOperation(CompositeCopy);
118 m_client->drawRect(this, graphicsContext, paintRect);
120 graphicsContext.endTransparencyLayer();
123 bool PageOverlay::mouseEvent(const WebMouseEvent& mouseEvent)
125 // Ignore events outside the bounds.
126 if (!bounds().contains(mouseEvent.position()))
129 return m_client->mouseEvent(this, mouseEvent);
132 void PageOverlay::startFadeInAnimation()
134 m_fractionFadedIn = 0.0;
135 m_fadeAnimationType = FadeInAnimation;
137 startFadeAnimation();
140 void PageOverlay::startFadeOutAnimation()
142 m_fractionFadedIn = 1.0;
143 m_fadeAnimationType = FadeOutAnimation;
145 startFadeAnimation();
148 void PageOverlay::startFadeAnimation()
150 m_fadeAnimationStartTime = currentTime();
153 m_fadeAnimationTimer.startRepeating(1 / fadeAnimationFrameRate);
156 void PageOverlay::fadeAnimationTimerFired()
158 float animationProgress = (currentTime() - m_fadeAnimationStartTime) / m_fadeAnimationDuration;
160 if (animationProgress >= 1.0)
161 animationProgress = 1.0;
163 double sine = sin(piOverTwoFloat * animationProgress);
164 float fadeAnimationValue = sine * sine;
166 m_fractionFadedIn = (m_fadeAnimationType == FadeInAnimation) ? fadeAnimationValue : 1 - fadeAnimationValue;
168 if (m_pageOverlayShouldApplyFadeWhenPainting)
171 m_webPage->drawingArea()->setPageOverlayOpacity(m_fractionFadedIn);
173 if (animationProgress == 1.0) {
174 m_fadeAnimationTimer.stop();
176 bool wasFadingOut = m_fadeAnimationType == FadeOutAnimation;
177 m_fadeAnimationType = NoAnimation;
180 // If this was a fade out, go ahead and uninstall the page overlay.
181 m_webPage->uninstallPageOverlay(this, false);
186 } // namespace WebKit