Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Unified Diff: third_party/WebKit/Source/core/observer/ResizeObserver.cpp

Issue 2005593002: Initial ResizeObserver implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/observer/ResizeObserver.cpp
diff --git a/third_party/WebKit/Source/core/observer/ResizeObserver.cpp b/third_party/WebKit/Source/core/observer/ResizeObserver.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..e491469b4611a3a69d5d9aa78f33f991832f9414
--- /dev/null
+++ b/third_party/WebKit/Source/core/observer/ResizeObserver.cpp
@@ -0,0 +1,96 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "core/observer/ResizeObserver.h"
+
+#include "core/observer/ResizeObservation.h"
+#include "core/observer/ResizeObserverCallback.h"
+#include "core/observer/ResizeObserverController.h"
+
+namespace blink {
+
+ResizeObserver* ResizeObserver::create(Document& document, ResizeObserverCallback* callback)
+{
+ auto observer = new ResizeObserver(callback);
+ document.ensureResizeObserverController().addObserver(*observer);
+ return observer;
+}
+
+ResizeObserver::ResizeObserver(ResizeObserverCallback* callback)
+ : m_callback(callback)
+{
+}
+
+bool compareObservationToElement(Member<blink::ResizeObservation> observation, Element* el)
szager1 2016/05/23 19:49:11 This isn't used.
atotic1 2016/05/25 00:15:53 Done.
+{
+ return observation->target() == el;
+}
szager1 2016/05/23 19:49:11 Add blank line.
atotic1 2016/05/25 00:15:53 Done.
+bool operator==(Member<blink::ResizeObservation> observation, Element* el)
szager1 2016/05/23 19:49:11 meh, this is obfuscating. Just use (observation->
atotic1 2016/05/25 00:15:53 Leftover from when I was using the std::find algor
+{
+ return observation->target() == el;
+}
+
+void ResizeObserver::observe(Element* target)
+{
+ if (target && m_observations.find(target) == kNotFound)
szager1 2016/05/23 19:49:11 This is why, for IntersectionObserver, I did this:
atotic1 2016/05/25 00:15:53 Done. I was not optimizing this case because we t
+ m_observations.append(new ResizeObservation(target, this));
+}
+
+void ResizeObserver::unobserve(Element* target)
+{
+ auto pos = m_observations.find(target);
+ if (pos != kNotFound)
+ m_observations.remove(pos);
+}
+
+void ResizeObserver::disconnect()
+{
+ m_observations.clear();
+ m_activeObservations.clear();
szager1 2016/05/23 19:49:11 Also: m_callback.clear();
atotic1 2016/05/25 00:15:53 That'd violate the spec, because no elements could
+}
+
+void ResizeObserver::gatherObservations()
+{
+ m_activeObservations.clear();
szager1 2016/05/23 19:49:11 This should be either ASSERT(m_activeObservations-
atotic1 2016/05/25 00:15:53 ??? m_activeObservations->clear() does not compile
szager1 2016/06/02 20:52:40 It should never happen that gatherObservations() i
+
+ for (auto observation : m_observations) {
szager1 2016/05/23 19:49:11 I'm not sure if it makes a difference, but maybe i
atotic1 2016/05/25 00:15:53 auto& means you can't have a null. Done.
+ if (observation->hasResized())
+ m_activeObservations.append(*observation);
+ }
+}
+
+void ResizeObserver::deliverObservations()
+{
+ if (m_activeObservations.size() == 0)
+ return;
+
+ HeapVector<Member<ResizeObserverEntry>> entries;
+
+ for (size_t i = 0; i < m_activeObservations.size(); i++) {
szager1 2016/05/23 19:49:11 for (auto& observation : m_activeObservations)
atotic1 2016/05/25 00:15:53 Done.
+ auto observation = m_activeObservations.at(i);
+ auto entry = new ResizeObserverEntry(observation->target());
+ entries.append(entry);
+ observation->setBroadcastSize(entry->clientWidth(), entry->clientHeight());
+ }
+ m_callback->handleEvent(entries, this);
szager1 2016/05/23 19:49:11 m_activeObservations->clear();
atotic1 2016/05/25 00:15:53 Done.
+}
+
+ResizeObserver::ObservationList::iterator ResizeObserver::find(Element* target)
+{
+ for (ObservationList::iterator it = m_observations.begin(); it != m_observations.end(); ++it) {
+ if ((*it)->target() == target)
+ return it;
+ }
+
+ return m_observations.end();
+}
+
+DEFINE_TRACE(ResizeObserver)
+{
+ visitor->trace(m_callback);
+ visitor->trace(m_observations);
+ visitor->trace(m_activeObservations);
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698