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 "core/layout/ScrollAnchor.h" | 5 #include "core/layout/ScrollAnchor.h" |
6 | 6 |
| 7 #include "core/frame/FrameView.h" |
| 8 #include "core/layout/LayoutBox.h" |
| 9 #include "core/layout/LayoutView.h" |
| 10 #include "core/paint/PaintLayerScrollableArea.h" |
7 #include "platform/scroll/ScrollableArea.h" | 11 #include "platform/scroll/ScrollableArea.h" |
8 #include "wtf/Assertions.h" | 12 #include "wtf/Assertions.h" |
9 | 13 |
10 namespace blink { | 14 namespace blink { |
11 | 15 |
12 ScrollAnchor::ScrollAnchor(ScrollableArea* scroller) | 16 ScrollAnchor::ScrollAnchor(ScrollableArea* scroller) |
13 : m_scroller(scroller) | 17 : m_scroller(scroller) |
14 , m_anchorObject(nullptr) | 18 , m_anchorObject(nullptr) |
15 { | 19 { |
16 ASSERT(m_scroller); | 20 ASSERT(m_scroller); |
17 ASSERT(m_scroller->isFrameView() || m_scroller->isPaintLayerScrollableArea()
); | 21 ASSERT(m_scroller->isFrameView() || m_scroller->isPaintLayerScrollableArea()
); |
18 } | 22 } |
19 | 23 |
20 ScrollAnchor::~ScrollAnchor() | 24 ScrollAnchor::~ScrollAnchor() |
21 { | 25 { |
22 } | 26 } |
23 | 27 |
| 28 static LayoutBox* scrollerLayoutBox(const ScrollableArea* scroller) |
| 29 { |
| 30 LayoutBox* box = scroller->isFrameView() |
| 31 ? toFrameView(scroller)->layoutView() |
| 32 : &toPaintLayerScrollableArea(scroller)->box(); |
| 33 ASSERT(box); |
| 34 return box; |
| 35 } |
| 36 |
24 static LayoutObject* findAnchor(const ScrollableArea* scroller) | 37 static LayoutObject* findAnchor(const ScrollableArea* scroller) |
25 { | 38 { |
26 // TODO(skobes): implement. | 39 LayoutBox* scrollerBox = scrollerLayoutBox(scroller); |
27 return nullptr; | 40 |
| 41 FloatRect absoluteVisibleRect = scroller->isFrameView() |
| 42 ? scroller->visibleContentRect() |
| 43 : scrollerBox->localToAbsoluteQuad( |
| 44 FloatQuad(FloatRect(scrollerBox->overflowClipRect(LayoutPoint())))).
boundingBox(); |
| 45 |
| 46 LayoutObject* child = scrollerBox->nextInPreOrder(scrollerBox); |
| 47 LayoutObject* candidate = nullptr; |
| 48 while (child) { |
| 49 // TODO(skobes): Compute scroller-relative bounds instead of absolute bo
unds. |
| 50 FloatRect childRect = child->absoluteBoundingBoxFloatRect(); |
| 51 if (absoluteVisibleRect.intersects(childRect)) |
| 52 candidate = child; |
| 53 if (absoluteVisibleRect.contains(childRect)) |
| 54 break; |
| 55 child = child->nextInPreOrder(scrollerBox); |
| 56 } |
| 57 return candidate; |
28 } | 58 } |
29 | 59 |
30 static DoublePoint computeRelativeOffset(const ScrollableArea* scroller, const L
ayoutObject* layoutObject) | 60 static DoublePoint computeRelativeOffset(const ScrollableArea* scroller, const L
ayoutObject* layoutObject) |
31 { | 61 { |
32 // TODO(skobes): implement. | 62 return DoublePoint(layoutObject->localToAbsolute() - scrollerLayoutBox(scrol
ler)->localToAbsolute()); |
33 return DoublePoint(); | |
34 } | 63 } |
35 | 64 |
36 void ScrollAnchor::save() | 65 void ScrollAnchor::save() |
37 { | 66 { |
38 if (!m_anchorObject) | 67 if (!m_anchorObject) |
39 m_anchorObject = findAnchor(m_scroller); | 68 m_anchorObject = findAnchor(m_scroller); |
40 | 69 |
41 if (m_anchorObject) | 70 if (m_anchorObject) |
42 m_savedRelativeOffset = computeRelativeOffset(m_scroller, m_anchorObject
); | 71 m_savedRelativeOffset = computeRelativeOffset(m_scroller, m_anchorObject
); |
43 } | 72 } |
44 | 73 |
45 void ScrollAnchor::restore() | 74 void ScrollAnchor::restore() |
46 { | 75 { |
47 if (!m_anchorObject) | 76 if (!m_anchorObject) |
48 return; | 77 return; |
49 | 78 |
50 DoubleSize adjustment = computeRelativeOffset(m_scroller, m_anchorObject) -
m_savedRelativeOffset; | 79 DoubleSize adjustment = computeRelativeOffset(m_scroller, m_anchorObject) -
m_savedRelativeOffset; |
51 if (!adjustment.isZero()) | 80 if (!adjustment.isZero()) |
52 m_scroller->setScrollPosition(m_scroller->scrollPositionDouble() + adjus
tment, AnchoringScroll); | 81 m_scroller->setScrollPosition(m_scroller->scrollPositionDouble() + adjus
tment, AnchoringScroll); |
53 } | 82 } |
54 | 83 |
55 } // namespace blink | 84 } // namespace blink |
OLD | NEW |