OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "web/ResizeViewportAnchor.h" | 5 #include "web/ResizeViewportAnchor.h" |
6 | 6 |
7 #include "core/frame/FrameHost.h" | 7 #include "core/frame/FrameHost.h" |
8 #include "core/frame/FrameView.h" | 8 #include "core/frame/FrameView.h" |
| 9 #include "core/frame/RootFrameViewport.h" |
9 #include "core/frame/VisualViewport.h" | 10 #include "core/frame/VisualViewport.h" |
10 #include "core/page/Page.h" | 11 #include "core/page/Page.h" |
11 #include "platform/geometry/DoubleRect.h" | 12 #include "platform/geometry/DoubleRect.h" |
12 #include "platform/geometry/FloatSize.h" | 13 #include "platform/geometry/FloatSize.h" |
13 | 14 |
14 namespace blink { | 15 namespace blink { |
15 | 16 |
16 void ResizeViewportAnchor::resizeFrameView(IntSize size) | 17 void ResizeViewportAnchor::resizeFrameView(IntSize size) |
17 { | 18 { |
18 FrameView* frameView = rootFrameView(); | 19 FrameView* frameView = rootFrameView(); |
19 DCHECK(frameView); | 20 DCHECK(frameView); |
20 | 21 |
21 ScrollableArea* rootViewport = frameView->getScrollableArea(); | 22 ScrollableArea* rootViewport = frameView->getScrollableArea(); |
22 DoublePoint position = rootViewport->scrollPositionDouble(); | 23 DoublePoint position = rootViewport->scrollPositionDouble(); |
23 | 24 |
24 frameView->resize(size); | 25 frameView->resize(size); |
25 m_drift += rootViewport->scrollPositionDouble() - position; | 26 m_drift += rootViewport->scrollPositionDouble() - position; |
26 } | 27 } |
27 | 28 |
28 void ResizeViewportAnchor::endScope() | 29 void ResizeViewportAnchor::endScope() |
29 { | 30 { |
30 if (--m_scopeCount > 0) | 31 if (--m_scopeCount > 0) |
31 return; | 32 return; |
32 | 33 |
33 FrameView* frameView = rootFrameView(); | 34 FrameView* frameView = rootFrameView(); |
34 if (!frameView) | 35 if (!frameView) |
35 return; | 36 return; |
36 | 37 |
37 VisualViewport& visualViewport = m_page->frameHost().visualViewport(); | |
38 DoublePoint visualViewportInDocument = | 38 DoublePoint visualViewportInDocument = |
39 frameView->getScrollableArea()->scrollPositionDouble() - m_drift; | 39 frameView->getScrollableArea()->scrollPositionDouble() - m_drift; |
40 | 40 |
41 // TODO(bokan): Don't use RootFrameViewport::setScrollPosition since it | 41 // TODO(bokan): Don't use RootFrameViewport::setScrollPosition since it |
42 // assumes we can just set a sub-pixel precision offset on the FrameView. | 42 // assumes we can just set a sub-pixel precision offset on the FrameView. |
43 // While we "can" do this, the offset that will be shipped to CC will be the | 43 // While we "can" do this, the offset that will be shipped to CC will be the |
44 // truncated number and this class is used to handle TopControl movement | 44 // truncated number and this class is used to handle TopControl movement |
45 // which needs the two threads to match exactly pixel-for-pixel. We can | 45 // which needs the two threads to match exactly pixel-for-pixel. We can |
46 // replace this with RFV::setScrollPosition once Blink is sub-pixel scroll | 46 // replace this with RFV::setScrollPosition once Blink is sub-pixel scroll |
47 // offset aware. crbug.com/414283. | 47 // offset aware. crbug.com/414283. |
| 48 DCHECK(frameView->getRootFrameViewport()); |
| 49 frameView->getRootFrameViewport()->restoreToAnchor(visualViewportInDocument)
; |
48 | 50 |
49 ScrollableArea* rootViewport = frameView->getScrollableArea(); | |
50 ScrollableArea* layoutViewport = | |
51 frameView->layoutViewportScrollableArea(); | |
52 | |
53 // Clamp the scroll offset of each viewport now so that we force any invalid | |
54 // offsets to become valid so we can compute the correct deltas. | |
55 visualViewport.clampToBoundaries(); | |
56 layoutViewport->setScrollPosition( | |
57 layoutViewport->scrollPositionDouble(), ProgrammaticScroll); | |
58 | |
59 DoubleSize delta = visualViewportInDocument | |
60 - rootViewport->scrollPositionDouble(); | |
61 | |
62 visualViewport.move(toFloatSize(delta)); | |
63 | |
64 delta = visualViewportInDocument | |
65 - rootViewport->scrollPositionDouble(); | |
66 | |
67 // Since the main thread FrameView has integer scroll offsets, scroll it to | |
68 // the next pixel and then we'll scroll the visual viewport again to | |
69 // compensate for the sub-pixel offset. We need this "overscroll" to ensure | |
70 // the pixel of which we want to be partially in appears fully inside the | |
71 // FrameView since the VisualViewport is bounded by the FrameView. | |
72 IntSize layoutDelta = IntSize( | |
73 delta.width() < 0 ? floor(delta.width()) : ceil(delta.width()), | |
74 delta.height() < 0 ? floor(delta.height()) : ceil(delta.height())); | |
75 | |
76 layoutViewport->setScrollPosition( | |
77 layoutViewport->scrollPosition() + layoutDelta, | |
78 ProgrammaticScroll); | |
79 | |
80 delta = visualViewportInDocument | |
81 - rootViewport->scrollPositionDouble(); | |
82 visualViewport.move(toFloatSize(delta)); | |
83 m_drift = DoubleSize(); | 51 m_drift = DoubleSize(); |
84 } | 52 } |
85 | 53 |
86 FrameView* ResizeViewportAnchor::rootFrameView() | 54 FrameView* ResizeViewportAnchor::rootFrameView() |
87 { | 55 { |
88 if (Frame* frame = m_page->mainFrame()) { | 56 if (Frame* frame = m_page->mainFrame()) { |
89 if (frame->isLocalFrame()) | 57 if (frame->isLocalFrame()) |
90 return toLocalFrame(frame)->view(); | 58 return toLocalFrame(frame)->view(); |
91 } | 59 } |
92 return nullptr; | 60 return nullptr; |
93 } | 61 } |
94 | 62 |
95 } // namespace blink | 63 } // namespace blink |
OLD | NEW |