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

Unified Diff: base/android/java/src/org/chromium/base/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: Created 7 years, 3 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
« no previous file with comments | « no previous file | content/public/android/java/src/org/chromium/content/browser/ContentViewCore.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/android/java/src/org/chromium/base/PositionObserver.java
diff --git a/base/android/java/src/org/chromium/base/PositionObserver.java b/base/android/java/src/org/chromium/base/PositionObserver.java
new file mode 100644
index 0000000000000000000000000000000000000000..9efe2c49206d82a08961ba1675e54d15c7f6a3f9
--- /dev/null
+++ b/base/android/java/src/org/chromium/base/PositionObserver.java
@@ -0,0 +1,111 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
joth 2013/09/27 22:23:20 this yr
cjhopman 2013/10/04 18:07:29 Done.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.base;
joth 2013/09/27 22:23:20 This is only needed in content/browser so I don't
cjhopman 2013/10/04 18:07:29 Done.
+
+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 {
+ public interface Listener {
+ /**
+ * Called during predraw if the position of the underlying view has changed.
+ */
+ public void onPositionChanged(int positionX, int positionY);
+ }
+
+ private View mView;
+ // Absolute position of the container view relative to its parent window.
+ private int mPositionX, mPositionY;
+
+ private ArrayList<Listener> mListeners;
+ 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) {
+ 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.
joth 2013/09/27 22:23:20 the position will be correct during a frame draw,
cjhopman 2013/10/04 18:07:29 The preDrawListener is only registered on the view
+ return getCurrentPosition()[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.
+ return getCurrentPosition()[1];
+ }
+
+ /**
+ * Register a listener to be called when the position of the underlying view changes.
+ */
+ public void addListener(Listener listener) {
+ if (mListeners.contains(listener)) {
+ 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 int[] getCurrentPosition() {
+ int[] position = new int[2];
joth 2013/09/27 22:23:20 this will cause garbage allocations on every frame
cjhopman 2013/10/04 18:07:29 Done.
+ mView.getLocationInWindow(position);
+ return position;
+ }
+
+ private boolean updatePosition() {
+ int[] position = getCurrentPosition();
+ boolean positionChanged = mPositionX != position[0] || mPositionY != position[1];
+ mPositionX = position[0];
+ mPositionY = position[1];
+ return positionChanged;
+ }
+}
+
« no previous file with comments | « no previous file | content/public/android/java/src/org/chromium/content/browser/ContentViewCore.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698