Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.ntp; | 5 package org.chromium.chrome.browser.ntp; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.content.res.Resources; | 8 import android.content.res.Resources; |
| 9 import android.graphics.Rect; | |
| 10 import android.graphics.drawable.Drawable; | |
| 9 import android.util.AttributeSet; | 11 import android.util.AttributeSet; |
| 10 import android.view.View; | 12 import android.view.View; |
| 11 import android.widget.LinearLayout; | 13 import android.widget.LinearLayout; |
| 12 | 14 |
| 13 import org.chromium.base.ApiCompatibilityUtils; | |
| 14 import org.chromium.chrome.R; | 15 import org.chromium.chrome.R; |
| 15 | 16 |
| 16 /** | 17 /** |
| 17 * Layout for the new tab page. This positions the page elements in the correct vertical positions. | 18 * Layout for the new tab page. This positions the page elements in the correct vertical positions. |
| 18 * There are no separate phone and tablet UIs; this layout adapts based on the a vailable space. | 19 * There are no separate phone and tablet UIs; this layout adapts based on the a vailable space. |
| 19 */ | 20 */ |
| 20 public class NewTabPageLayout extends LinearLayout { | 21 public class NewTabPageLayout extends LinearLayout { |
| 21 | 22 |
| 22 // Space permitting, the spacers will grow from 0dp to the heights given bel ow. If there is | 23 // Space permitting, the spacers will grow from 0dp to the heights given bel ow. If there is |
| 23 // additional space, it will be distributed evenly between the top and botto m spacers. | 24 // additional space, it will be distributed evenly between the top and botto m spacers. |
| 24 private static final float TOP_SPACER_HEIGHT_DP = 44f; | 25 private static final float TOP_SPACER_HEIGHT_DP = 44f; |
| 25 private static final float MIDDLE_SPACER_HEIGHT_DP = 24f; | 26 private static final float MIDDLE_SPACER_HEIGHT_DP = 24f; |
| 26 private static final float BOTTOM_SPACER_HEIGHT_DP = 44f; | 27 private static final float BOTTOM_SPACER_HEIGHT_DP = 44f; |
| 27 private static final float TOTAL_SPACER_HEIGHT_DP = TOP_SPACER_HEIGHT_DP | 28 private static final float TOTAL_SPACER_HEIGHT_DP = TOP_SPACER_HEIGHT_DP |
| 28 + MIDDLE_SPACER_HEIGHT_DP + BOTTOM_SPACER_HEIGHT_DP; | 29 + MIDDLE_SPACER_HEIGHT_DP + BOTTOM_SPACER_HEIGHT_DP; |
| 29 | 30 |
| 30 private final int mTopSpacerIdealHeight; | 31 private final int mTopSpacerIdealHeight; |
| 31 private final int mMiddleSpacerIdealHeight; | 32 private final int mMiddleSpacerIdealHeight; |
| 32 private final int mBottomSpacerIdealHeight; | 33 private final int mBottomSpacerIdealHeight; |
| 33 private final int mTotalSpacerIdealHeight; | 34 private final int mTotalSpacerIdealHeight; |
| 34 private final int mMostVisitedLayoutBleed; | 35 private final int mMostVisitedLayoutBleed; |
| 35 private final int mPeekingCardHeight; | 36 private final int mPeekingCardHeight; |
| 36 private final int mTabStripHeight; | 37 private final int mTabStripHeight; |
| 37 | 38 |
| 38 private int mParentViewportHeight; | 39 private int mParentViewportHeight; |
| 40 private int mSearchboxViewShadowWidth; | |
| 39 | 41 |
| 40 private boolean mCardsUiEnabled; | 42 private boolean mCardsUiEnabled; |
| 41 private View mTopSpacer; // Spacer above search logo. | 43 private View mTopSpacer; // Spacer above search logo. |
| 42 private View mMiddleSpacer; // Spacer between toolbar and Most Likely. | 44 private View mMiddleSpacer; // Spacer between toolbar and Most Likely. |
| 43 private View mBottomSpacer; // Spacer below Most Likely. | 45 private View mBottomSpacer; // Spacer below Most Likely. |
| 44 | 46 |
| 45 // Separate spacer below Most Likely to add enough space so the user can scr oll with Most Likely | 47 // Separate spacer below Most Likely to add enough space so the user can scr oll with Most Likely |
| 46 // at the top of the screen. | 48 // at the top of the screen. |
| 47 private View mScrollCompensationSpacer; | 49 private View mScrollCompensationSpacer; |
| 48 | 50 |
| 49 private LogoView mSearchProviderLogoView; | 51 private LogoView mSearchProviderLogoView; |
| 50 private View mSearchBoxView; | 52 private View mSearchBoxView; |
| 51 private MostVisitedLayout mMostVisitedLayout; | 53 private MostVisitedLayout mMostVisitedLayout; |
| 52 | |
|
Theresa
2016/05/26 21:54:41
nit: please add this line back.
| |
| 53 /** | 54 /** |
| 54 * Constructor for inflating from XML. | 55 * Constructor for inflating from XML. |
| 55 */ | 56 */ |
| 56 public NewTabPageLayout(Context context, AttributeSet attrs) { | 57 public NewTabPageLayout(Context context, AttributeSet attrs) { |
| 57 super(context, attrs); | 58 super(context, attrs); |
| 58 Resources res = getResources(); | 59 Resources res = getResources(); |
| 59 float density = res.getDisplayMetrics().density; | 60 float density = res.getDisplayMetrics().density; |
| 60 mTopSpacerIdealHeight = Math.round(density * TOP_SPACER_HEIGHT_DP); | 61 mTopSpacerIdealHeight = Math.round(density * TOP_SPACER_HEIGHT_DP); |
| 61 mMiddleSpacerIdealHeight = Math.round(density * MIDDLE_SPACER_HEIGHT_DP) ; | 62 mMiddleSpacerIdealHeight = Math.round(density * MIDDLE_SPACER_HEIGHT_DP) ; |
| 62 mBottomSpacerIdealHeight = Math.round(density * BOTTOM_SPACER_HEIGHT_DP) ; | 63 mBottomSpacerIdealHeight = Math.round(density * BOTTOM_SPACER_HEIGHT_DP) ; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 mParentViewportHeight - mPeekingCardHeight - mTabStripHe ight); | 136 mParentViewportHeight - mPeekingCardHeight - mTabStripHe ight); |
| 136 // Call onMeasure to update mTopScaper's height. | 137 // Call onMeasure to update mTopScaper's height. |
| 137 super.onMeasure(widthMeasureSpec, heightMeasureSpec); | 138 super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 138 } | 139 } |
| 139 distributeExtraSpace(mTopSpacer.getMeasuredHeight()); | 140 distributeExtraSpace(mTopSpacer.getMeasuredHeight()); |
| 140 } | 141 } |
| 141 | 142 |
| 142 // Make the search box and logo as wide as the most visited items. | 143 // Make the search box and logo as wide as the most visited items. |
| 143 if (mMostVisitedLayout.getVisibility() != GONE) { | 144 if (mMostVisitedLayout.getVisibility() != GONE) { |
| 144 final int width = mMostVisitedLayout.getMeasuredWidth() - mMostVisit edLayoutBleed; | 145 final int width = mMostVisitedLayout.getMeasuredWidth() - mMostVisit edLayoutBleed; |
| 145 setMeasure(mSearchBoxView, width, mSearchBoxView.getMeasuredHeight() ); | 146 setMeasure(mSearchBoxView, width + mSearchboxViewShadowWidth, |
| 147 mSearchBoxView.getMeasuredHeight()); | |
| 146 setMeasure(mSearchProviderLogoView, width, mSearchProviderLogoView.g etMeasuredHeight()); | 148 setMeasure(mSearchProviderLogoView, width, mSearchProviderLogoView.g etMeasuredHeight()); |
| 147 } | 149 } |
| 148 } | 150 } |
| 149 | 151 |
| 150 /** | 152 /** |
| 151 * Calculate the vertical position of Most Visited. | 153 * Calculate the vertical position of Most Visited. |
| 152 * This method does not use mMostVisitedLayout.getTop(), so can be called in onMeasure. | 154 * This method does not use mMostVisitedLayout.getTop(), so can be called in onMeasure. |
| 153 */ | 155 */ |
| 154 private int calculateTopOfMostVisited() { | 156 private int calculateTopOfMostVisited() { |
| 155 // Manually add the heights (and margins) of all children above Most Vis ited. | 157 // Manually add the heights (and margins) of all children above Most Vis ited. |
| 156 int top = 0; | 158 int top = 0; |
| 157 int mostVisitedIndex = indexOfChild(mMostVisitedLayout); | 159 int mostVisitedIndex = indexOfChild(mMostVisitedLayout); |
| 158 for (int i = 0; i < mostVisitedIndex; i++) { | 160 for (int i = 0; i < mostVisitedIndex; i++) { |
| 159 View child = getChildAt(i); | 161 View child = getChildAt(i); |
| 160 MarginLayoutParams params = (MarginLayoutParams) child.getLayoutPara ms(); | 162 MarginLayoutParams params = (MarginLayoutParams) child.getLayoutPara ms(); |
| 161 | 163 |
| 162 if (child.getVisibility() != View.GONE) { | 164 if (child.getVisibility() != View.GONE) { |
| 163 top += params.topMargin + child.getMeasuredHeight() + params.bot tomMargin; | 165 top += params.topMargin + child.getMeasuredHeight() + params.bot tomMargin; |
| 164 } | 166 } |
| 165 } | 167 } |
| 166 top += ((MarginLayoutParams) mMostVisitedLayout.getLayoutParams()).topMa rgin; | 168 top += ((MarginLayoutParams) mMostVisitedLayout.getLayoutParams()).topMa rgin; |
| 167 return top; | 169 return top; |
| 168 } | 170 } |
| 169 | 171 |
| 170 /** | 172 /** |
| 171 * Set the search box style. Modify the search box to enable material when s nippets or | 173 * Set the search box style, adding a shadow if required. |
| 172 * material design is enabled. | |
| 173 */ | 174 */ |
| 174 private void setSearchBoxStyle() { | 175 private void setSearchBoxStyle() { |
| 175 if (NtpColorUtils.shouldUseMaterialColors() | 176 if (!NtpColorUtils.shouldUseMaterialColors()) return; |
| 176 && ApiCompatibilityUtils.setElevation(mSearchBoxView, | 177 |
| 177 getResources().getDimensionPixelSize(R.dimen.toolbar_ elevation))) { | 178 Drawable background = mSearchBoxView.getBackground(); |
|
Theresa
2016/05/26 17:47:26
Can the padding be added via xml? I'm hoping we ca
Bernhard Bauer
2016/05/26 19:43:57
The reason I'm doing this in code is that we still
Theresa
2016/05/26 21:54:41
Can you use defined dimens that represent the corr
Bernhard Bauer
2016/05/27 12:16:41
Done. Note however that keeping the exactly same p
| |
| 178 // Replace drawable with one that does not contain a border. | 179 Rect padding = new Rect(); |
| 179 mSearchBoxView.setBackgroundResource(R.drawable.bg_ntp_search_box_ma terial); | 180 background.getPadding(padding); |
| 180 } | 181 |
| 182 Resources resources = getContext().getResources(); | |
| 183 Drawable shadow = resources.getDrawable(R.drawable.textbox); | |
|
Theresa
2016/05/26 21:54:41
Resources.getDrawable() is deprecated. Use ApiComp
Bernhard Bauer
2016/05/27 12:16:41
Thanks! That code is gone now though.
| |
| 184 Rect shadowPadding = new Rect(); | |
| 185 shadow.getPadding(shadowPadding); | |
| 186 | |
| 187 // Adjust the margins to remove the padding. | |
|
Theresa
2016/05/26 21:54:41
Why does the padding need to be removed just to be
Bernhard Bauer
2016/05/27 12:16:41
Sorry, that comment should have been a bit more de
| |
| 188 MarginLayoutParams layoutParams = (MarginLayoutParams) mSearchBoxView.ge tLayoutParams(); | |
| 189 layoutParams.topMargin -= shadowPadding.top; | |
| 190 layoutParams.bottomMargin -= shadowPadding.bottom; | |
| 191 layoutParams.leftMargin -= shadowPadding.left; | |
| 192 layoutParams.rightMargin -= shadowPadding.right; | |
| 193 layoutParams.height += shadowPadding.bottom + shadowPadding.top; | |
| 194 // Width will be adjusted in onMeasure(); | |
| 195 mSearchboxViewShadowWidth = shadowPadding.left + shadowPadding.right; | |
| 196 | |
| 197 // Add the extra padding back. | |
| 198 mSearchBoxView.setBackground(shadow); | |
| 199 mSearchBoxView.setPadding( | |
| 200 padding.left + shadowPadding.left, | |
| 201 padding.top + shadowPadding.top, | |
| 202 padding.right + shadowPadding.right, | |
| 203 padding.bottom + shadowPadding.bottom); | |
| 181 } | 204 } |
| 182 | 205 |
| 183 /** | 206 /** |
| 184 * Distribute extra vertical space between the three spacer views. | 207 * Distribute extra vertical space between the three spacer views. |
| 185 * @param extraHeight The amount of extra space, in pixels. | 208 * @param extraHeight The amount of extra space, in pixels. |
| 186 */ | 209 */ |
| 187 private void distributeExtraSpace(int extraHeight) { | 210 private void distributeExtraSpace(int extraHeight) { |
| 188 int topSpacerHeight; | 211 int topSpacerHeight; |
| 189 int middleSpacerHeight; | 212 int middleSpacerHeight; |
| 190 int bottomSpacerHeight; | 213 int bottomSpacerHeight; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 212 | 235 |
| 213 /** | 236 /** |
| 214 * Convenience method to call |measure| on the given View with MeasureSpecs converted from the | 237 * Convenience method to call |measure| on the given View with MeasureSpecs converted from the |
| 215 * given dimensions (in pixels) with MeasureSpec.EXACTLY. | 238 * given dimensions (in pixels) with MeasureSpec.EXACTLY. |
| 216 */ | 239 */ |
| 217 private void setMeasure(View view, int widthPx, int heightPx) { | 240 private void setMeasure(View view, int widthPx, int heightPx) { |
| 218 view.measure(MeasureSpec.makeMeasureSpec(widthPx, MeasureSpec.EXACTLY), | 241 view.measure(MeasureSpec.makeMeasureSpec(widthPx, MeasureSpec.EXACTLY), |
| 219 MeasureSpec.makeMeasureSpec(heightPx, MeasureSpec.EXACTLY)); | 242 MeasureSpec.makeMeasureSpec(heightPx, MeasureSpec.EXACTLY)); |
| 220 } | 243 } |
| 221 } | 244 } |
| OLD | NEW |