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.contextmenu; | 5 package org.chromium.chrome.browser.contextmenu; |
6 | 6 |
7 import android.animation.Animator; | |
8 import android.animation.AnimatorListenerAdapter; | |
9 import android.animation.ValueAnimator; | |
10 import android.animation.ValueAnimator.AnimatorUpdateListener; | |
7 import android.content.Context; | 11 import android.content.Context; |
12 import android.graphics.Canvas; | |
13 import android.graphics.drawable.Drawable; | |
8 import android.support.v4.view.ViewPager; | 14 import android.support.v4.view.ViewPager; |
15 import android.support.v4.view.animation.LinearOutSlowInInterpolator; | |
9 import android.util.AttributeSet; | 16 import android.util.AttributeSet; |
10 import android.view.View; | 17 import android.view.View; |
11 | 18 |
19 import org.chromium.base.ApiCompatibilityUtils; | |
12 import org.chromium.chrome.R; | 20 import org.chromium.chrome.R; |
13 | 21 |
14 /** | 22 /** |
15 * When there is more than one view for the context menu to display, it wraps th e display in a view | 23 * When there is more than one view for the context menu to display, it wraps th e display in a view |
16 * pager. | 24 * pager. |
17 */ | 25 */ |
18 public class TabularContextMenuViewPager extends ViewPager { | 26 public class TabularContextMenuViewPager extends ViewPager { |
27 private static final int ANIMATION_DURATION_MS = 250; | |
28 | |
29 private ValueAnimator mAnimator; | |
30 private int mOldHeight; | |
31 private int mCanvasWidth; | |
32 private int mClipHeight; | |
33 | |
34 private int mDifferenceInHeight = 0; | |
35 private int mPreviousChildIndex = 1; | |
19 private final int mContextMenuMinimumPaddingPx = | 36 private final int mContextMenuMinimumPaddingPx = |
20 getResources().getDimensionPixelSize(R.dimen.context_menu_min_paddin g); | 37 getResources().getDimensionPixelSize(R.dimen.context_menu_min_paddin g); |
38 private final Drawable mDrawable = ApiCompatibilityUtils.getDrawable( | |
39 getResources(), R.drawable.white_with_rounded_corners); | |
21 | 40 |
22 public TabularContextMenuViewPager(Context context) { | 41 public TabularContextMenuViewPager(Context context) { |
23 super(context); | 42 super(context); |
43 mDrawable.mutate(); | |
24 } | 44 } |
25 | 45 |
26 public TabularContextMenuViewPager(Context context, AttributeSet attrs) { | 46 public TabularContextMenuViewPager(Context context, AttributeSet attrs) { |
27 super(context, attrs); | 47 super(context, attrs); |
48 mDrawable.mutate(); | |
28 } | 49 } |
29 | 50 |
30 /** | 51 /** |
31 * Used to show the full ViewPager dialog. Without this the dialog would hav e no height or | 52 * Used to show the full ViewPager dialog. Without this the dialog would hav e no height or |
32 * width. | 53 * width. |
33 */ | 54 */ |
34 @Override | 55 @Override |
35 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | 56 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
57 int tabHeight = 0; | |
36 int menuHeight = 0; | 58 int menuHeight = 0; |
37 int tabHeight = 0; | |
38 | |
39 // getCurrentItem() does not take into account the tab layout unlike get ChildCount(). | |
40 int currentItemsIndex = getCurrentItem() + 1; | |
41 | 59 |
42 // The width of the context menu is defined so that it leaves space betw een itself and the | 60 // The width of the context menu is defined so that it leaves space betw een itself and the |
43 // screen's edges. It is also bounded to a max size to prevent the menu from stretching | 61 // screen's edges. It is also bounded to a max size to prevent the menu from stretching |
44 // across a large display (e.g. a tablet screen). | 62 // across a large display (e.g. a tablet screen). |
45 int deviceWidthPx = getResources().getDisplayMetrics().widthPixels; | 63 int appWindowWidthPx = getResources().getDisplayMetrics().widthPixels; |
46 int contextMenuWidth = Math.min(deviceWidthPx - 2 * mContextMenuMinimumP addingPx, | 64 int contextMenuWidth = Math.min(appWindowWidthPx - 2 * mContextMenuMinim umPaddingPx, |
47 getResources().getDimensionPixelSize(R.dimen.context_menu_max_wi dth)); | 65 getResources().getDimensionPixelSize(R.dimen.context_menu_max_wi dth)); |
48 | 66 |
49 widthMeasureSpec = MeasureSpec.makeMeasureSpec(contextMenuWidth, Measure Spec.EXACTLY); | 67 widthMeasureSpec = MeasureSpec.makeMeasureSpec(contextMenuWidth, Measure Spec.EXACTLY); |
50 | 68 |
51 // The height of the context menu is calculated as the sum of: | 69 // getCurrentItem() does not take into account the tab layout unlike get ChildCount(). |
Theresa
2017/07/20 15:25:00
What does getCurrentItem() return, then?
Daniel Park
2017/07/20 18:55:19
Added more detail. Done.
| |
52 // 1. The tab bar's height, which is only visible when the context menu requires it | 70 int currentChildIndex = getCurrentItem() + 1; |
53 // (i.e. an ImageLink is clicked) | |
54 // 2. The height of the View being displayed for the current tab. | |
55 for (int i = 0; i < getChildCount(); i++) { | |
56 View child = getChildAt(i); | |
57 | 71 |
58 child.measure( | 72 // Handles the case where this is called while the pager is scrolling be tween views. |
Theresa
2017/07/20 15:25:00
nit: Update the comment to say what is done in thi
Daniel Park
2017/07/20 18:55:19
Done.
| |
59 widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec .UNSPECIFIED)); | 73 if (getScrollX() != 0 && getScrollX() != mCanvasWidth) { |
60 int measuredHeight = child.getMeasuredHeight(); | 74 heightMeasureSpec = MeasureSpec.makeMeasureSpec(mOldHeight, MeasureS pec.EXACTLY); |
75 } else { | |
76 // The height of the context menu is calculated as the sum of: | |
77 // 1. The tab bar's height, which is only visible when the context m enu requires it | |
78 // (i.e. an ImageLink is clicked) | |
79 // 2. The height of the View being displayed for the current tab. | |
80 for (int i = 0; i < getChildCount(); i++) { | |
81 View child = getChildAt(i); | |
82 child.measure( | |
83 widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, Measure Spec.UNSPECIFIED)); | |
84 int measuredHeight = child.getMeasuredHeight(); | |
61 | 85 |
62 // The ViewPager also considers the tab layout one of its children, and needs to be | 86 // The ViewPager also considers the tab layout one of its childr en, and needs to be |
63 // treated separately from getting the largest height. | 87 // treated separately from getting the largest height. |
64 if (child.getId() == R.id.tab_layout && child.getVisibility() != GON E) { | 88 if (child.getId() == R.id.tab_layout && child.getVisibility() != GONE) { |
65 tabHeight = measuredHeight; | 89 tabHeight = measuredHeight; |
66 } else if (i == currentItemsIndex) { | 90 } else if (i == currentChildIndex) { |
67 menuHeight = child.getMeasuredHeight(); | 91 menuHeight = child.getMeasuredHeight(); |
68 break; | 92 break; |
93 } | |
94 } | |
95 int fullHeight = menuHeight + tabHeight; | |
96 int appWindowHeightPx = getResources().getDisplayMetrics().heightPix els; | |
97 fullHeight = Math.min(fullHeight, appWindowHeightPx - 2 * mContextMe nuMinimumPaddingPx); | |
98 mDifferenceInHeight = fullHeight - mOldHeight; | |
99 | |
100 if (currentChildIndex == mPreviousChildIndex) { | |
101 // Handles the snapping and re-clipping of the view when its hei ght changes | |
102 // (i.e. an image finished loading or the link became fully visi ble). | |
Theresa
2017/07/20 15:25:00
nit: add "The pager will immediately snap to the n
Daniel Park
2017/07/20 18:55:19
Done.
| |
103 mClipHeight = fullHeight; | |
104 if (menuHeight != 0) mOldHeight = fullHeight; | |
105 heightMeasureSpec = MeasureSpec.makeMeasureSpec(fullHeight, Meas ureSpec.EXACTLY); | |
106 } else { | |
107 // Handles the case where the view pager has completely scrolled to a different | |
108 // child. | |
Theresa
2017/07/20 15:25:00
nit: add "The pager will animate to the height of
Daniel Park
2017/07/20 18:55:19
Done.
| |
109 initAnimator(); | |
110 heightMeasureSpec = MeasureSpec.makeMeasureSpec( | |
111 Math.max(mOldHeight, fullHeight), MeasureSpec.EXACTLY); | |
112 mAnimator.start(); | |
Theresa
2017/07/20 15:25:00
Should we start the animation after giving the sup
Daniel Park
2017/07/20 18:55:19
I prefer to keep it here since the results are the
Daniel Park
2017/07/20 21:42:36
Done.
| |
69 } | 113 } |
70 } | 114 } |
115 super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
116 mPreviousChildIndex = currentChildIndex; | |
117 } | |
71 | 118 |
72 // Cap the height of the context menu so that it fits on the screen with out touching the | 119 private void initAnimator() { |
73 // screen's edges. | 120 if (mAnimator != null) return; |
74 int fullHeight = menuHeight + tabHeight; | 121 mAnimator = ValueAnimator.ofFloat(0f, 1f); |
75 int deviceHeightPx = getResources().getDisplayMetrics().heightPixels; | 122 mAnimator.setDuration(ANIMATION_DURATION_MS); |
76 fullHeight = Math.min(fullHeight, deviceHeightPx - 2 * mContextMenuMinim umPaddingPx); | 123 mAnimator.setInterpolator(new LinearOutSlowInInterpolator()); |
124 mAnimator.addUpdateListener(new AnimatorUpdateListener() { | |
77 | 125 |
78 heightMeasureSpec = MeasureSpec.makeMeasureSpec(fullHeight, MeasureSpec. EXACTLY); | 126 @Override |
79 super.onMeasure(widthMeasureSpec, heightMeasureSpec); | 127 public void onAnimationUpdate(ValueAnimator animation) { |
128 float animatedValue = (float) animation.getAnimatedValue(); | |
129 // If the animation duration is long enough (e.g. 5x, 10x), anim atedValue will | |
130 // undesirably be set to 1f more than once. | |
Theresa
2017/07/20 15:25:00
We're not really doing anything in response to the
Daniel Park
2017/07/20 18:55:19
oops. Done.
| |
131 if (mDifferenceInHeight < 0) { | |
132 setTranslationY(animatedValue * -mDifferenceInHeight / 2); | |
133 } else { | |
134 setTranslationY((1 - animatedValue) * mDifferenceInHeight / 2); | |
135 } | |
136 mClipHeight = mOldHeight + (int) (mDifferenceInHeight * animated Value); | |
137 invalidate(); | |
138 } | |
139 }); | |
140 mAnimator.addListener(new AnimatorListenerAdapter() { | |
141 @Override | |
142 public void onAnimationEnd(Animator animation) { | |
143 mOldHeight = mClipHeight; | |
144 setTranslationY(0); | |
145 if (mDifferenceInHeight < 0) requestLayout(); | |
146 } | |
147 }); | |
148 } | |
149 | |
150 @Override | |
151 public void onDraw(Canvas canvas) { | |
152 mCanvasWidth = canvas.getWidth(); | |
153 int backgroundOffsetX = getScrollX(); | |
154 mDrawable.setBounds( | |
155 backgroundOffsetX, 0, canvas.getWidth() + backgroundOffsetX, mCl ipHeight); | |
156 mDrawable.draw(canvas); | |
157 boolean clipped = false; | |
Theresa
2017/07/20 15:25:00
nit: add a blank line above this one.
Daniel Park
2017/07/20 18:55:19
Done.
| |
158 if (mClipHeight != 0) { | |
159 canvas.save(); | |
160 canvas.clipRect(0, 0, mCanvasWidth, mClipHeight); | |
161 clipped = true; | |
162 } | |
163 super.onDraw(canvas); | |
Theresa
2017/07/20 15:25:00
nit: add blank lines around this super.onDraw() ca
Daniel Park
2017/07/20 18:55:19
Done.
| |
164 if (clipped) { | |
165 canvas.restore(); | |
166 } | |
80 } | 167 } |
81 } | 168 } |
OLD | NEW |