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.suggestions; | 5 package org.chromium.chrome.browser.suggestions; |
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.text.TextUtils; | 9 import android.text.TextUtils; |
10 import android.util.AttributeSet; | 10 import android.util.AttributeSet; |
11 import android.view.View; | 11 import android.view.View; |
12 import android.widget.FrameLayout; | 12 import android.widget.FrameLayout; |
13 | 13 |
14 import org.chromium.base.ApiCompatibilityUtils; | 14 import org.chromium.base.ApiCompatibilityUtils; |
15 import org.chromium.chrome.R; | 15 import org.chromium.chrome.R; |
16 import org.chromium.chrome.browser.util.MathUtils; | 16 import org.chromium.chrome.browser.util.MathUtils; |
17 | 17 |
18 /** | 18 /** |
19 * A layout that arranges tiles in a grid. | 19 * A layout that arranges tiles in a grid. |
20 */ | 20 */ |
21 public class TileGridLayout extends FrameLayout { | 21 public class TileGridLayout extends FrameLayout { |
22 private static final int MAX_COLUMNS = 4; | 22 private final int mVerticalSpacing; |
| 23 private final int mMinHorizontalSpacing; |
| 24 private final int mMaxHorizontalSpacing; |
| 25 private final int mMaxWidth; |
23 | 26 |
24 private int mVerticalSpacing; | 27 private int mMaxRows; |
| 28 private int mMaxColumns; |
25 private int mExtraVerticalSpacing; | 29 private int mExtraVerticalSpacing; |
26 private int mMinHorizontalSpacing; | |
27 private int mMaxHorizontalSpacing; | |
28 private int mMaxWidth; | |
29 private int mMaxRows; | |
30 | 30 |
31 /** | 31 /** |
| 32 * Constructor for inflating from XML. |
| 33 * |
32 * @param context The view context in which this item will be shown. | 34 * @param context The view context in which this item will be shown. |
33 * @param attrs The attributes of the XML tag that is inflating the view. | 35 * @param attrs The attributes of the XML tag that is inflating the view. |
34 */ | 36 */ |
35 public TileGridLayout(Context context, AttributeSet attrs) { | 37 public TileGridLayout(Context context, AttributeSet attrs) { |
36 super(context, attrs); | 38 super(context, attrs); |
37 | 39 |
38 Resources res = getResources(); | 40 Resources res = getResources(); |
39 mVerticalSpacing = res.getDimensionPixelOffset(R.dimen.tile_grid_layout_
vertical_spacing); | 41 mVerticalSpacing = res.getDimensionPixelOffset(R.dimen.tile_grid_layout_
vertical_spacing); |
40 mMinHorizontalSpacing = | 42 mMinHorizontalSpacing = |
41 res.getDimensionPixelOffset(R.dimen.tile_grid_layout_min_horizon
tal_spacing); | 43 res.getDimensionPixelOffset(R.dimen.tile_grid_layout_min_horizon
tal_spacing); |
42 mMaxHorizontalSpacing = | 44 mMaxHorizontalSpacing = |
43 res.getDimensionPixelOffset(R.dimen.tile_grid_layout_max_horizon
tal_spacing); | 45 res.getDimensionPixelOffset(R.dimen.tile_grid_layout_max_horizon
tal_spacing); |
44 mMaxWidth = res.getDimensionPixelOffset(R.dimen.tile_grid_layout_max_wid
th); | 46 mMaxWidth = res.getDimensionPixelOffset(R.dimen.tile_grid_layout_max_wid
th); |
45 } | 47 } |
46 | 48 |
47 /** | 49 /** |
48 * Sets the maximum number of rows to display. Any items that don't fit with
in these rows will | 50 * Sets the maximum number of rows to display. Any items that don't fit will
be hidden. |
49 * be hidden. | |
50 */ | 51 */ |
51 public void setMaxRows(int rows) { | 52 public void setMaxRows(int rows) { |
52 mMaxRows = rows; | 53 mMaxRows = rows; |
53 } | 54 } |
54 | 55 |
55 /** | 56 /** |
| 57 * Sets the maximum number of columns to display. Any items that don't fit w
ill be hidden. |
| 58 */ |
| 59 public void setMaxColumns(int columns) { |
| 60 mMaxColumns = columns; |
| 61 } |
| 62 |
| 63 /** |
56 * Sets the extra vertical spacing that must be used. It will be distributed
evenly above each | 64 * Sets the extra vertical spacing that must be used. It will be distributed
evenly above each |
57 * row. | 65 * row. |
58 */ | 66 */ |
59 public void setExtraVerticalSpacing(int spacing) { | 67 public void setExtraVerticalSpacing(int spacing) { |
60 if (mExtraVerticalSpacing == spacing) { | 68 if (mExtraVerticalSpacing == spacing) { |
61 return; | 69 return; |
62 } | 70 } |
63 mExtraVerticalSpacing = spacing; | 71 mExtraVerticalSpacing = spacing; |
64 | 72 |
65 // Clear the measure cache for this view and make sure it will be remeas
ured. | 73 // Clear the measure cache for this view and make sure it will be remeas
ured. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 measureChild(getChildAt(i), MeasureSpec.UNSPECIFIED, MeasureSpec.UNS
PECIFIED); | 106 measureChild(getChildAt(i), MeasureSpec.UNSPECIFIED, MeasureSpec.UNS
PECIFIED); |
99 } | 107 } |
100 | 108 |
101 // Determine the number of columns that will fit. | 109 // Determine the number of columns that will fit. |
102 int gridWidth = totalWidth - ApiCompatibilityUtils.getPaddingStart(this) | 110 int gridWidth = totalWidth - ApiCompatibilityUtils.getPaddingStart(this) |
103 - ApiCompatibilityUtils.getPaddingEnd(this); | 111 - ApiCompatibilityUtils.getPaddingEnd(this); |
104 int childHeight = getChildAt(0).getMeasuredHeight(); | 112 int childHeight = getChildAt(0).getMeasuredHeight(); |
105 int childWidth = getChildAt(0).getMeasuredWidth(); | 113 int childWidth = getChildAt(0).getMeasuredWidth(); |
106 int numColumns = MathUtils.clamp( | 114 int numColumns = MathUtils.clamp( |
107 (gridWidth + mMinHorizontalSpacing) / (childWidth + mMinHorizont
alSpacing), 1, | 115 (gridWidth + mMinHorizontalSpacing) / (childWidth + mMinHorizont
alSpacing), 1, |
108 MAX_COLUMNS); | 116 mMaxColumns); |
109 | 117 |
110 // Ensure column spacing isn't greater than mMaxHorizontalSpacing. | 118 // Ensure column spacing isn't greater than mMaxHorizontalSpacing. |
111 int gridWidthMinusColumns = Math.max(0, gridWidth - numColumns * childWi
dth); | 119 int gridWidthMinusColumns = Math.max(0, gridWidth - numColumns * childWi
dth); |
112 int gridSidePadding = gridWidthMinusColumns - mMaxHorizontalSpacing * (n
umColumns - 1); | 120 int gridSidePadding = gridWidthMinusColumns - mMaxHorizontalSpacing * (n
umColumns - 1); |
113 | 121 |
114 int gridStart = 0; | 122 int gridStart = 0; |
115 float horizontalSpacing; | 123 float horizontalSpacing; |
116 if (gridSidePadding > 0) { | 124 if (gridSidePadding > 0) { |
117 horizontalSpacing = mMaxHorizontalSpacing; | 125 horizontalSpacing = mMaxHorizontalSpacing; |
118 gridStart = gridSidePadding / 2; | 126 gridStart = gridSidePadding / 2; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 /** @return A tile view associated to the provided URL, or {@code null} if n
one is found. */ | 163 /** @return A tile view associated to the provided URL, or {@code null} if n
one is found. */ |
156 private TileView getTileView(String url) { | 164 private TileView getTileView(String url) { |
157 int childCount = getChildCount(); | 165 int childCount = getChildCount(); |
158 for (int i = 0; i < childCount; i++) { | 166 for (int i = 0; i < childCount; i++) { |
159 TileView tileView = (TileView) getChildAt(i); | 167 TileView tileView = (TileView) getChildAt(i); |
160 if (TextUtils.equals(url, tileView.getUrl())) return tileView; | 168 if (TextUtils.equals(url, tileView.getUrl())) return tileView; |
161 } | 169 } |
162 return null; | 170 return null; |
163 } | 171 } |
164 } | 172 } |
OLD | NEW |