2 * Copyright (C) 2010 Google 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 are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "DragScrollTimer.h"
34 #include "FrameView.h"
36 using namespace WebCore;
40 // Computes the distance from a point outside a rect to the nearest edge of the rect.
41 static IntSize distanceToRect(const IntPoint& point, const IntRect& rect)
44 if (point.x() < rect.x())
45 dx = point.x() - rect.x();
46 else if (rect.maxX() < point.x())
47 dx = point.x() - rect.maxX();
48 if (point.y() < rect.y())
49 dy = point.y() - rect.y();
50 else if (rect.maxY() < point.y())
51 dy = point.y() - rect.maxY();
52 return IntSize(dx, dy);
55 DragScrollTimer::DragScrollTimer()
56 : m_timer(this, &DragScrollTimer::fired)
62 DragScrollTimer::~DragScrollTimer()
64 // We do this for detecting dead object earlier
68 void DragScrollTimer::stop()
75 void DragScrollTimer::scroll()
77 m_view->scrollBy(m_lastDistance);
81 void DragScrollTimer::update()
89 void DragScrollTimer::triggerScroll(FrameView* view, const WebPoint& location)
94 // Approximates Safari
95 static const double scrollStartDelay = 0.2;
98 m_lastDistance = scrollDistanceFor(view, location);
102 else if (shouldScroll() && !m_timer.isActive())
103 m_timer.startOneShot(scrollStartDelay);
106 IntSize DragScrollTimer::scrollDistanceFor(FrameView* view, const WebPoint& location) const
108 static const int scrollMargin = 30;
110 IntRect bounds(0, 0, view->visibleWidth(), view->visibleHeight());
111 if (!bounds.contains(location))
112 return IntSize(0, 0); // The location is outside the border belt.
114 bounds.setY(bounds.y() + scrollMargin);
115 bounds.setHeight(bounds.height() - scrollMargin * 2);
116 bounds.setX(bounds.x() + scrollMargin);
117 bounds.setWidth(bounds.width() - scrollMargin * 2);
119 if (bounds.contains(location))
120 return IntSize(0, 0); // The location is inside the border belt.
122 // The location is over the border belt.
123 return distanceToRect(location, bounds);
126 } // namespace WebKit