Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.widget; | 5 package org.chromium.chrome.browser.widget; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.graphics.Color; | 8 import android.graphics.Color; |
| 9 import android.graphics.Rect; | 9 import android.graphics.Rect; |
| 10 import android.graphics.drawable.ColorDrawable; | 10 import android.graphics.drawable.ColorDrawable; |
| 11 import android.support.v4.view.animation.LinearOutSlowInInterpolator; | 11 import android.support.v4.view.animation.LinearOutSlowInInterpolator; |
| 12 import android.view.MotionEvent; | 12 import android.view.MotionEvent; |
| 13 import android.view.View; | 13 import android.view.View; |
| 14 import android.view.View.OnLayoutChangeListener; | 14 import android.view.View.OnLayoutChangeListener; |
| 15 import android.view.ViewGroup; | 15 import android.view.ViewGroup; |
| 16 import android.view.ViewGroup.LayoutParams; | 16 import android.view.ViewGroup.LayoutParams; |
| 17 import android.view.Window; | 17 import android.view.Window; |
| 18 import android.view.animation.Animation; | 18 import android.view.animation.Animation; |
| 19 import android.view.animation.Animation.AnimationListener; | 19 import android.view.animation.Animation.AnimationListener; |
| 20 import android.view.animation.ScaleAnimation; | 20 import android.view.animation.ScaleAnimation; |
| 21 | 21 |
| 22 import org.chromium.chrome.browser.contextmenu.TabularContextMenuViewPager; | 22 import org.chromium.chrome.browser.contextmenu.TabularContextMenuViewPager; |
| 23 import org.chromium.content.browser.RenderCoordinates; | 23 import org.chromium.content.browser.RenderCoordinates; |
| 24 | 24 |
| 25 import javax.annotation.Nullable; | |
| 26 | |
| 25 /** | 27 /** |
| 26 * ContextMenuDialog is a subclass of AlwaysDismissedDialog that ensures that th e proper scale | 28 * ContextMenuDialog is a subclass of AlwaysDismissedDialog that ensures that th e proper scale |
| 27 * animation is played upon calling {@link #show()} and {@link #dismiss()}. | 29 * animation is played upon calling {@link #show()} and {@link #dismiss()}. |
| 28 */ | 30 */ |
| 29 public class ContextMenuDialog extends AlwaysDismissedDialog { | 31 public class ContextMenuDialog extends AlwaysDismissedDialog { |
| 30 private static final long ENTER_ANIMATION_DURATION_MS = 250; | 32 private static final long ENTER_ANIMATION_DURATION_MS = 250; |
| 31 // Exit animation duration should be set to 60% of the enter animation durat ion. | 33 // Exit animation duration should be set to 60% of the enter animation durat ion. |
| 32 private static final long EXIT_ANIMATION_DURATION_MS = 150; | 34 private static final long EXIT_ANIMATION_DURATION_MS = 150; |
| 33 | 35 |
| 34 private final Activity mActivity; | 36 private final Activity mActivity; |
| 35 private final View mContentView; | 37 private final View mContentView; |
| 36 private final float mTouchPointXPx; | 38 private final float mTouchPointXPx; |
| 37 private final float mTouchPointYPx; | 39 private final float mTouchPointYPx; |
| 38 private final RenderCoordinates mRenderCoordinates; | 40 private final RenderCoordinates mRenderCoordinates; |
| 39 | 41 |
| 40 private float mContextMenuSourceXPx; | 42 private float mContextMenuSourceXPx; |
| 41 private float mContextMenuSourceYPx; | 43 private float mContextMenuSourceYPx; |
| 42 private int mContextMenuFirstLocationYPx; | 44 private int mContextMenuFirstLocationYPx; |
| 43 | 45 |
| 44 /** | 46 /** |
| 45 * Creates an instance of the ContextMenuDialog. | 47 * Creates an instance of the ContextMenuDialog. |
| 46 * @param ownerActivity The activity in which the dialog should run | 48 * @param ownerActivity The activity in which the dialog should run |
| 47 * @param theme A style resource describing the theme to use for the window, or {@code 0} to use | 49 * @param theme A style resource describing the theme to use for the window, or {@code 0} to use |
| 48 * the default dialog theme | 50 * the default dialog theme |
| 49 * @param touchPointXPx The x-coordinate of the touch that triggered the con text menu. | 51 * @param touchPointXPx The x-coordinate of the touch that triggered the con text menu. |
| 50 * @param touchPointYPx The y-coordinate of the touch that triggered the con text menu. | 52 * @param touchPointYPx The y-coordinate of the touch that triggered the con text menu. |
| 51 * @param contentView The The {@link TabularContextMenuViewPager} to display on the dialog. | 53 * @param contentView The The {@link TabularContextMenuViewPager} to display on the dialog. |
| 52 * @param renderCoordinates The render coordinates to get the y offset of th e window. | 54 * @param renderCoordinates The render coordinates to get the y offset of th e window. This could |
| 55 * be null if ContentViewCore is not available. | |
| 53 */ | 56 */ |
| 54 public ContextMenuDialog(Activity ownerActivity, int theme, float touchPoint XPx, | 57 public ContextMenuDialog(Activity ownerActivity, int theme, float touchPoint XPx, |
| 55 float touchPointYPx, View contentView, RenderCoordinates renderCoord inates) { | 58 float touchPointYPx, View contentView, @Nullable RenderCoordinates r enderCoordinates) { |
| 56 super(ownerActivity, theme); | 59 super(ownerActivity, theme); |
| 57 mActivity = ownerActivity; | 60 mActivity = ownerActivity; |
| 58 mTouchPointXPx = touchPointXPx; | 61 mTouchPointXPx = touchPointXPx; |
| 59 mTouchPointYPx = touchPointYPx; | 62 mTouchPointYPx = touchPointYPx; |
| 60 mContentView = contentView; | 63 mContentView = contentView; |
| 61 mRenderCoordinates = renderCoordinates; | 64 mRenderCoordinates = renderCoordinates; |
| 62 } | 65 } |
| 63 | 66 |
| 64 @Override | 67 @Override |
| 65 public void show() { | 68 public void show() { |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 88 }); | 91 }); |
| 89 super.show(); | 92 super.show(); |
| 90 } | 93 } |
| 91 | 94 |
| 92 private void startEnterAnimation() { | 95 private void startEnterAnimation() { |
| 93 Rect rectangle = new Rect(); | 96 Rect rectangle = new Rect(); |
| 94 Window window = mActivity.getWindow(); | 97 Window window = mActivity.getWindow(); |
| 95 window.getDecorView().getWindowVisibleDisplayFrame(rectangle); | 98 window.getDecorView().getWindowVisibleDisplayFrame(rectangle); |
| 96 | 99 |
| 97 float xOffsetPx = rectangle.left; | 100 float xOffsetPx = rectangle.left; |
| 98 float yOffsetPx = rectangle.top + mRenderCoordinates.getContentOffsetYPi x(); | 101 float contentOffsetYPx = |
| 102 mRenderCoordinates != null ? mRenderCoordinates.getContentOffset YPix() : 0; | |
| 103 float yOffsetPx = rectangle.top + contentOffsetYPx; | |
| 99 | 104 |
| 100 int[] currentLocationOnScreenPx = new int[2]; | 105 int[] currentLocationOnScreenPx = new int[2]; |
| 101 mContentView.getLocationOnScreen(currentLocationOnScreenPx); | 106 mContentView.getLocationOnScreen(currentLocationOnScreenPx); |
| 102 | 107 |
| 103 mContextMenuFirstLocationYPx = currentLocationOnScreenPx[1]; | 108 mContextMenuFirstLocationYPx = currentLocationOnScreenPx[1]; |
| 104 | 109 |
| 105 mContextMenuSourceXPx = mTouchPointXPx - currentLocationOnScreenPx[0] + xOffsetPx; | 110 mContextMenuSourceXPx = mTouchPointXPx - currentLocationOnScreenPx[0] + xOffsetPx; |
| 106 mContextMenuSourceYPx = mTouchPointYPx - currentLocationOnScreenPx[1] + yOffsetPx; | 111 mContextMenuSourceYPx = mTouchPointYPx - currentLocationOnScreenPx[1] + yOffsetPx; |
| 107 | 112 |
| 108 Animation animation = getScaleAnimation(true, mContextMenuSourceXPx, mCo ntextMenuSourceYPx); | 113 Animation animation = getScaleAnimation(true, mContextMenuSourceXPx, mCo ntextMenuSourceYPx); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 * @param pivotY The Y coordinate of the point about which the object is bei ng scaled, specified | 157 * @param pivotY The Y coordinate of the point about which the object is bei ng scaled, specified |
| 153 * as an absolute number where 0 is the top edge. | 158 * as an absolute number where 0 is the top edge. |
| 154 * @return Returns the scale animation for the context menu. | 159 * @return Returns the scale animation for the context menu. |
| 155 */ | 160 */ |
| 156 public Animation getScaleAnimation(boolean isEnterAnimation, float pivotX, f loat pivotY) { | 161 public Animation getScaleAnimation(boolean isEnterAnimation, float pivotX, f loat pivotY) { |
| 157 float fromX = isEnterAnimation ? 0f : 1f; | 162 float fromX = isEnterAnimation ? 0f : 1f; |
| 158 float toX = isEnterAnimation ? 1f : 0f; | 163 float toX = isEnterAnimation ? 1f : 0f; |
| 159 float fromY = fromX; | 164 float fromY = fromX; |
| 160 float toY = toX; | 165 float toY = toX; |
| 161 | 166 |
| 162 ScaleAnimation animation = new ScaleAnimation( | 167 ScaleAnimation animation; |
| 163 fromX, toX, fromY, toY, Animation.ABSOLUTE, pivotX, Animation.AB SOLUTE, pivotY); | 168 if (mRenderCoordinates == null) { |
| 169 animation = new ScaleAnimation(fromX, toX, fromY, toY, Animation.REL ATIVE_TO_SELF, 0.5f, | |
| 170 Animation.RELATIVE_TO_SELF, 0.5f); | |
|
Theresa
2017/06/22 23:33:41
I think ideally we would skip calculating the pivo
| |
| 171 } else { | |
| 172 animation = new ScaleAnimation( | |
| 173 fromX, toX, fromY, toY, Animation.ABSOLUTE, pivotX, Animatio n.ABSOLUTE, pivotY); | |
| 174 } | |
| 164 | 175 |
| 165 long duration = isEnterAnimation ? ENTER_ANIMATION_DURATION_MS : EXIT_AN IMATION_DURATION_MS; | 176 long duration = isEnterAnimation ? ENTER_ANIMATION_DURATION_MS : EXIT_AN IMATION_DURATION_MS; |
| 166 | 177 |
| 167 animation.setDuration(duration); | 178 animation.setDuration(duration); |
| 168 animation.setInterpolator(new LinearOutSlowInInterpolator()); | 179 animation.setInterpolator(new LinearOutSlowInInterpolator()); |
| 169 return animation; | 180 return animation; |
| 170 } | 181 } |
| 171 } | 182 } |
| OLD | NEW |