| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "SnapCoordinator.h" | 5 #include "SnapCoordinator.h" |
| 6 | 6 |
| 7 #include "core/dom/Element.h" | 7 #include "core/dom/Element.h" |
| 8 #include "core/dom/Node.h" | 8 #include "core/dom/Node.h" |
| 9 #include "core/dom/NodeComputedStyle.h" | 9 #include "core/dom/NodeComputedStyle.h" |
| 10 #include "core/layout/LayoutBlock.h" | 10 #include "core/layout/LayoutBlock.h" |
| 11 #include "core/layout/LayoutBox.h" | 11 #include "core/layout/LayoutBox.h" |
| 12 #include "core/layout/LayoutView.h" | 12 #include "core/layout/LayoutView.h" |
| 13 #include "core/style/ComputedStyle.h" | 13 #include "core/style/ComputedStyle.h" |
| 14 #include "platform/LengthFunctions.h" | 14 #include "platform/LengthFunctions.h" |
| 15 | 15 |
| 16 namespace blink { | 16 namespace blink { |
| 17 | 17 |
| 18 SnapCoordinator::SnapCoordinator() | 18 SnapCoordinator* SnapCoordinator::create(Client* client) |
| 19 : m_snapContainers() | 19 { |
| 20 return new SnapCoordinator(client); |
| 21 } |
| 22 |
| 23 SnapCoordinator::SnapCoordinator(Client* client) |
| 24 : m_client(client) |
| 20 { | 25 { |
| 21 } | 26 } |
| 22 | 27 |
| 23 SnapCoordinator::~SnapCoordinator() { } | 28 SnapCoordinator::~SnapCoordinator() { } |
| 24 | 29 |
| 25 SnapCoordinator* SnapCoordinator::create() | |
| 26 { | |
| 27 return new SnapCoordinator(); | |
| 28 } | |
| 29 | |
| 30 // Returns the scroll container that can be affected by this snap area. | 30 // Returns the scroll container that can be affected by this snap area. |
| 31 static LayoutBox* findSnapContainer(const LayoutBox& snapArea) | 31 static LayoutBox* findSnapContainer(const LayoutBox& snapArea) |
| 32 { | 32 { |
| 33 // According to the new spec https://drafts.csswg.org/css-scroll-snap/#snap-
model | 33 // According to the new spec https://drafts.csswg.org/css-scroll-snap/#snap-
model |
| 34 // "Snap positions must only affect the nearest ancestor (on the element’s | 34 // "Snap positions must only affect the nearest ancestor (on the element’s |
| 35 // containing block chain) scroll container". | 35 // containing block chain) scroll container". |
| 36 Element* viewportDefiningElement = snapArea.node()->document().viewportDefin
ingElement(); | 36 Element* viewportDefiningElement = snapArea.node()->document().viewportDefin
ingElement(); |
| 37 LayoutBox* box = snapArea.containingBlock(); | 37 LayoutBox* box = snapArea.containingBlock(); |
| 38 while (box && !box->hasOverflowClip() && !box->isLayoutView() && box->node()
!= viewportDefiningElement) | 38 while (box && !box->hasOverflowClip() && !box->isLayoutView() && box->node()
!= viewportDefiningElement) |
| 39 box = box->containingBlock(); | 39 box = box->containingBlock(); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 } | 140 } |
| 141 } | 141 } |
| 142 } | 142 } |
| 143 | 143 |
| 144 if (didAddSnapAreaOffset) | 144 if (didAddSnapAreaOffset) |
| 145 std::sort(result.begin(), result.end()); | 145 std::sort(result.begin(), result.end()); |
| 146 | 146 |
| 147 return result; | 147 return result; |
| 148 } | 148 } |
| 149 | 149 |
| 150 |
| 151 |
| 152 void SnapCoordinator::notifyLayoutUpdated() |
| 153 { |
| 154 // TODO(majidvp): This should be replaced with compositor worker precommit |
| 155 for (const LayoutBox* container : m_snapContainers) { |
| 156 ContainerNode& containerNode = toContainerNode(*container->node()); |
| 157 SnapOffsets newOffsets; |
| 158 newOffsets.horizontal = snapOffsets(containerNode, HorizontalScrollbar); |
| 159 newOffsets.vertical = snapOffsets(containerNode, VerticalScrollbar); |
| 160 |
| 161 // report only if it is different from the cached value |
| 162 if (m_offsetMap.contains(container) && m_offsetMap.get(container) == new
Offsets) |
| 163 continue; |
| 164 m_offsetMap.set(container, newOffsets); |
| 165 if (m_client) |
| 166 m_client->didUpdateSnapOffsets(*container, newOffsets); |
| 167 } |
| 168 } |
| 169 |
| 170 |
| 150 #ifndef NDEBUG | 171 #ifndef NDEBUG |
| 151 | 172 |
| 152 void SnapCoordinator::showSnapAreaMap() | 173 void SnapCoordinator::showSnapAreaMap() |
| 153 { | 174 { |
| 154 for (auto& container : m_snapContainers) | 175 for (auto& container : m_snapContainers) |
| 155 showSnapAreasFor(container); | 176 showSnapAreasFor(container); |
| 156 } | 177 } |
| 157 | 178 |
| 158 void SnapCoordinator::showSnapAreasFor(const LayoutBox* container) | 179 void SnapCoordinator::showSnapAreasFor(const LayoutBox* container) |
| 159 { | 180 { |
| 160 const char* prefix = " "; | 181 const char* prefix = " "; |
| 161 container->node()->showNode(); | 182 container->node()->showNode(); |
| 162 if (SnapAreaSet* snapAreas = container->snapAreas()) { | 183 if (SnapAreaSet* snapAreas = container->snapAreas()) { |
| 163 for (auto& snapArea : *snapAreas) { | 184 for (auto& snapArea : *snapAreas) { |
| 164 snapArea->node()->showNode(prefix); | 185 snapArea->node()->showNode(prefix); |
| 165 } | 186 } |
| 166 } | 187 } |
| 167 } | 188 } |
| 168 | 189 |
| 169 #endif | 190 #endif |
| 170 | 191 |
| 192 bool operator==(const SnapOffsets& a, const SnapOffsets& b) |
| 193 { |
| 194 return a.horizontal == b.horizontal && a.vertical == b.vertical; |
| 195 } |
| 196 |
| 171 } // namespace blink | 197 } // namespace blink |
| OLD | NEW |