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

Unified Diff: content/public/android/java/src/org/chromium/content/browser/PositionObserver.java

Issue 24449007: [Android] Allow text handles to observe position of "parent" view (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 7 years, 2 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: content/public/android/java/src/org/chromium/content/browser/PositionObserver.java
diff --git a/content/public/android/java/src/org/chromium/content/browser/PositionObserver.java b/content/public/android/java/src/org/chromium/content/browser/PositionObserver.java
new file mode 100644
index 0000000000000000000000000000000000000000..c7b3db5ed20646861cb3d4d59e42328a43100876
--- /dev/null
+++ b/content/public/android/java/src/org/chromium/content/browser/PositionObserver.java
@@ -0,0 +1,106 @@
+// Copyright 2013 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.
+
+package org.chromium.content.browser;
+
+import android.view.View;
+import android.view.ViewTreeObserver;
+
+import java.util.ArrayList;
+
+/**
+ * Used to register listeners that can be notified of changes to the position of a view.
+ */
+public class PositionObserver implements PositionObserverInterface {
Ted C 2013/10/10 18:16:32 As for naming, I would call this ViewPositionObser
cjhopman 2013/10/10 21:35:23 I like that. Done.
+ private View mView;
+ // Absolute position of the container view relative to its parent window.
+ private int mPositionX, mPositionY;
Ted C 2013/10/10 18:16:32 why do we need to keep position x and y? can't we
cjhopman 2013/10/10 21:35:23 Done.
+
+ private final int[] mTempPos = new int[2];
+
+ private ArrayList<Listener> mListeners;
Ted C 2013/10/10 18:16:32 make this final to ensure it can't ever be set to
cjhopman 2013/10/10 21:35:23 Done.
+ private ViewTreeObserver.OnPreDrawListener mPreDrawListener;
+
+ /**
+ * @param view The view to observe.
+ */
+ public PositionObserver(View view) {
+ mView = view;
+ mListeners = new ArrayList<Listener>();
+ updatePosition();
+ mPreDrawListener = new ViewTreeObserver.OnPreDrawListener() {
+ @Override
+ public boolean onPreDraw() {
+ if (updatePosition()) {
+ for (Listener l : mListeners) {
Ted C 2013/10/10 18:16:32 this will allocate an Iterator on each predraw, wh
cjhopman 2013/10/10 21:35:23 Done.
+ l.onPositionChanged(mPositionX, mPositionY);
+ }
+ }
+ return true;
+ }
+ };
+ }
+
+ /**
+ * @return The current x position of the observed view.
+ */
+ public int getPositionX() {
+ // The stored position may be out-of-date. Get the real current position.
+ updateTempPosition();
+ return mTempPos[0];
+ }
+
+ /**
+ * @return The current y position of the observed view.
+ */
+ public int getPositionY() {
+ // The stored position may be out-of-date. Get the real current position.
+ updateTempPosition();
+ return mTempPos[1];
+ }
+
+ /**
+ * Register a listener to be called when the position of the underlying view changes.
+ */
+ public void addListener(Listener listener) {
+ if (mListeners.contains(listener)) {
Ted C 2013/10/10 18:16:32 the statement and condition all fit on one line, n
cjhopman 2013/10/10 21:35:23 Done.
+ return;
+ }
+
+ if (mListeners.isEmpty()) {
+ mView.getViewTreeObserver().addOnPreDrawListener(mPreDrawListener);
+ updatePosition();
+ }
+
+ mListeners.add(listener);
+ }
+
+ /**
+ * Remove a previously installed listener.
+ */
+ public void removeListener(Listener listener) {
+ if (!mListeners.contains(listener)) {
+ return;
+ }
+
+ mListeners.remove(listener);
+
+ if (mListeners.isEmpty()) {
+ mView.getViewTreeObserver().removeOnPreDrawListener(mPreDrawListener);
+ }
+ }
+
+ private void updateTempPosition() {
+ mView.getLocationInWindow(mTempPos);
+ }
+
+ private boolean updatePosition() {
+ updateTempPosition();
Ted C 2013/10/10 18:16:32 if we got rid of mPositionX and Y can we just keep
cjhopman 2013/10/10 21:35:23 So, the issue is that if there is a listener, we w
+ boolean positionChanged = mPositionX != mTempPos[0] || mPositionY != mTempPos[1];
+ mPositionX = position[0];
Ted C 2013/10/10 18:16:32 hmm...position doesn't seem to exist anywhere (lit
cjhopman 2013/10/10 21:35:23 Yeah, this must have been confusing.
+ mPositionY = position[1];
+ return positionChanged;
+ }
+}
+

Powered by Google App Engine
This is Rietveld 408576698