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

Side by Side 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, 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 unified diff | Download patch | Annotate | Revision Log
« 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // 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.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 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.
6
7 import android.view.View;
8 import android.view.ViewTreeObserver;
9
10 import java.util.ArrayList;
11
12 /**
13 * Used to register listeners that can be notified of changes to the position of a view.
14 */
15 public class PositionObserver {
16 public interface Listener {
17 /**
18 * Called during predraw if the position of the underlying view has chan ged.
19 */
20 public void onPositionChanged(int positionX, int positionY);
21 }
22
23 private View mView;
24 // Absolute position of the container view relative to its parent window.
25 private int mPositionX, mPositionY;
26
27 private ArrayList<Listener> mListeners;
28 private ViewTreeObserver.OnPreDrawListener mPreDrawListener;
29
30 /**
31 * @param view The view to observe.
32 */
33 public PositionObserver(View view) {
34 mView = view;
35 mListeners = new ArrayList<Listener>();
36 updatePosition();
37 mPreDrawListener = new ViewTreeObserver.OnPreDrawListener() {
38 @Override
39 public boolean onPreDraw() {
40 if (updatePosition()) {
41 for (Listener l : mListeners) {
42 l.onPositionChanged(mPositionX, mPositionY);
43 }
44 }
45 return true;
46 }
47 };
48 }
49
50 /**
51 * @return The current x position of the observed view.
52 */
53 public int getPositionX() {
54 // 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
55 return getCurrentPosition()[0];
56 }
57
58 /**
59 * @return The current y position of the observed view.
60 */
61 public int getPositionY() {
62 // The stored position may be out-of-date. Get the real current position .
63 return getCurrentPosition()[1];
64 }
65
66 /**
67 * Register a listener to be called when the position of the underlying view changes.
68 */
69 public void addListener(Listener listener) {
70 if (mListeners.contains(listener)) {
71 return;
72 }
73
74 if (mListeners.isEmpty()) {
75 mView.getViewTreeObserver().addOnPreDrawListener(mPreDrawListener);
76 updatePosition();
77 }
78
79 mListeners.add(listener);
80 }
81
82 /**
83 * Remove a previously installed listener.
84 */
85 public void removeListener(Listener listener) {
86 if (!mListeners.contains(listener)) {
87 return;
88 }
89
90 mListeners.remove(listener);
91
92 if (mListeners.isEmpty()) {
93 mView.getViewTreeObserver().removeOnPreDrawListener(mPreDrawListener );
94 }
95 }
96
97 private int[] getCurrentPosition() {
98 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.
99 mView.getLocationInWindow(position);
100 return position;
101 }
102
103 private boolean updatePosition() {
104 int[] position = getCurrentPosition();
105 boolean positionChanged = mPositionX != position[0] || mPositionY != pos ition[1];
106 mPositionX = position[0];
107 mPositionY = position[1];
108 return positionChanged;
109 }
110 }
111
OLDNEW
« 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