Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 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.content.browser; | |
| 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 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.
| |
| 16 private View mView; | |
| 17 // Absolute position of the container view relative to its parent window. | |
| 18 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.
| |
| 19 | |
| 20 private final int[] mTempPos = new int[2]; | |
| 21 | |
| 22 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.
| |
| 23 private ViewTreeObserver.OnPreDrawListener mPreDrawListener; | |
| 24 | |
| 25 /** | |
| 26 * @param view The view to observe. | |
| 27 */ | |
| 28 public PositionObserver(View view) { | |
| 29 mView = view; | |
| 30 mListeners = new ArrayList<Listener>(); | |
| 31 updatePosition(); | |
| 32 mPreDrawListener = new ViewTreeObserver.OnPreDrawListener() { | |
| 33 @Override | |
| 34 public boolean onPreDraw() { | |
| 35 if (updatePosition()) { | |
| 36 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.
| |
| 37 l.onPositionChanged(mPositionX, mPositionY); | |
| 38 } | |
| 39 } | |
| 40 return true; | |
| 41 } | |
| 42 }; | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * @return The current x position of the observed view. | |
| 47 */ | |
| 48 public int getPositionX() { | |
| 49 // The stored position may be out-of-date. Get the real current position . | |
| 50 updateTempPosition(); | |
| 51 return mTempPos[0]; | |
| 52 } | |
| 53 | |
| 54 /** | |
| 55 * @return The current y position of the observed view. | |
| 56 */ | |
| 57 public int getPositionY() { | |
| 58 // The stored position may be out-of-date. Get the real current position . | |
| 59 updateTempPosition(); | |
| 60 return mTempPos[1]; | |
| 61 } | |
| 62 | |
| 63 /** | |
| 64 * Register a listener to be called when the position of the underlying view changes. | |
| 65 */ | |
| 66 public void addListener(Listener listener) { | |
| 67 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.
| |
| 68 return; | |
| 69 } | |
| 70 | |
| 71 if (mListeners.isEmpty()) { | |
| 72 mView.getViewTreeObserver().addOnPreDrawListener(mPreDrawListener); | |
| 73 updatePosition(); | |
| 74 } | |
| 75 | |
| 76 mListeners.add(listener); | |
| 77 } | |
| 78 | |
| 79 /** | |
| 80 * Remove a previously installed listener. | |
| 81 */ | |
| 82 public void removeListener(Listener listener) { | |
| 83 if (!mListeners.contains(listener)) { | |
| 84 return; | |
| 85 } | |
| 86 | |
| 87 mListeners.remove(listener); | |
| 88 | |
| 89 if (mListeners.isEmpty()) { | |
| 90 mView.getViewTreeObserver().removeOnPreDrawListener(mPreDrawListener ); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 private void updateTempPosition() { | |
| 95 mView.getLocationInWindow(mTempPos); | |
| 96 } | |
| 97 | |
| 98 private boolean updatePosition() { | |
| 99 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
| |
| 100 boolean positionChanged = mPositionX != mTempPos[0] || mPositionY != mTe mpPos[1]; | |
| 101 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.
| |
| 102 mPositionY = position[1]; | |
| 103 return positionChanged; | |
| 104 } | |
| 105 } | |
| 106 | |
| OLD | NEW |