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.infobar; | 5 package org.chromium.chrome.browser.infobar; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.graphics.Bitmap; | 8 import android.graphics.Bitmap; |
9 import android.view.Gravity; | 9 import android.view.Gravity; |
10 import android.view.View; | 10 import android.view.View; |
11 import android.widget.ImageButton; | 11 import android.widget.ImageButton; |
12 import android.widget.ImageView; | 12 import android.widget.ImageView; |
13 import android.widget.LinearLayout; | 13 import android.widget.LinearLayout; |
14 | 14 |
15 import org.chromium.chrome.R; | 15 import org.chromium.chrome.R; |
16 | 16 |
17 /** | 17 /** |
18 * Lays out controls along a line, sandwiched between an (optional) icon and clo se button. | 18 * Lays out controls along a line, sandwiched between an (optional) icon and clo se button. |
19 * This should only be used by the {@link InfoBar} class, and is created when th e InfoBar subclass | 19 * This should only be used by the {@link InfoBar} class, and is created when th e InfoBar subclass |
20 * declares itself to be using a compact layout via {@link InfoBar#usesCompactLa yout}. | 20 * declares itself to be using a compact layout via {@link InfoBar#usesCompactLa yout}. |
21 */ | 21 */ |
22 public class InfoBarCompactLayout extends LinearLayout implements View.OnClickLi stener { | 22 public class InfoBarCompactLayout extends LinearLayout implements View.OnClickLi stener { |
23 private final InfoBarView mInfoBarView; | 23 private final InfoBarView mInfoBarView; |
24 private final int mCompactInfoBarSize; | 24 private final int mCompactInfoBarSize; |
25 private final int mMessagePadding; | |
25 private final View mCloseButton; | 26 private final View mCloseButton; |
26 | 27 |
27 InfoBarCompactLayout( | 28 InfoBarCompactLayout( |
28 Context context, InfoBarView infoBarView, int iconResourceId, Bitmap iconBitmap) { | 29 Context context, InfoBarView infoBarView, int iconResourceId, Bitmap iconBitmap) { |
29 super(context); | 30 super(context); |
30 mInfoBarView = infoBarView; | 31 mInfoBarView = infoBarView; |
31 mCompactInfoBarSize = | 32 mCompactInfoBarSize = |
32 context.getResources().getDimensionPixelOffset(R.dimen.infobar_c ompact_size); | 33 context.getResources().getDimensionPixelOffset(R.dimen.infobar_c ompact_size); |
34 mMessagePadding = | |
35 context.getResources().getDimensionPixelOffset(R.dimen.infobar_t ext_padding); | |
33 | 36 |
34 setOrientation(LinearLayout.HORIZONTAL); | 37 setOrientation(LinearLayout.HORIZONTAL); |
35 setGravity(Gravity.CENTER_VERTICAL); | 38 setGravity(Gravity.CENTER_VERTICAL); |
36 | 39 |
37 prepareIcon(iconResourceId, iconBitmap); | 40 prepareIcon(iconResourceId, iconBitmap); |
38 mCloseButton = prepareCloseButton(); | 41 mCloseButton = prepareCloseButton(); |
39 } | 42 } |
40 | 43 |
41 @Override | 44 @Override |
42 public void onClick(View view) { | 45 public void onClick(View view) { |
43 if (view.getId() == R.id.infobar_close_button) { | 46 if (view.getId() == R.id.infobar_close_button) { |
44 mInfoBarView.onCloseButtonClicked(); | 47 mInfoBarView.onCloseButtonClicked(); |
45 } else { | 48 } else { |
46 assert false; | 49 assert false; |
47 } | 50 } |
48 } | 51 } |
49 | 52 |
50 /** | 53 /** |
51 * Inserts a view before the close button. | 54 * Inserts a view before the close button. |
52 * @param view View to insert. | 55 * @param view View to insert. |
53 * @param weight Weight to assign to it. | 56 * @param weight Weight to assign to it. |
54 */ | 57 */ |
55 protected void addContent(View view, float weight) { | 58 protected void addContent(View view, float weight) { |
56 LinearLayout.LayoutParams params; | 59 LinearLayout.LayoutParams params; |
57 if (weight <= 0.0f) { | 60 if (weight <= 0.0f) { |
58 params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, mC ompactInfoBarSize); | 61 params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, mC ompactInfoBarSize); |
59 } else { | 62 } else { |
60 params = new LinearLayout.LayoutParams(0, mCompactInfoBarSize); | 63 params = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, weight); |
61 params.weight = weight; | |
62 } | 64 } |
65 view.setPadding(0, mMessagePadding, 0, mMessagePadding); | |
mdjones
2017/06/27 20:17:18
Why is this now needed? Does it affect the other c
wychen
2017/06/27 20:38:57
If the text fits in one line, the infobar is tall
| |
66 view.setMinimumHeight(mCompactInfoBarSize); | |
63 params.gravity = Gravity.BOTTOM; | 67 params.gravity = Gravity.BOTTOM; |
64 addView(view, indexOfChild(mCloseButton), params); | 68 addView(view, indexOfChild(mCloseButton), params); |
65 } | 69 } |
66 | 70 |
67 /** | 71 /** |
68 * Adds an icon to the start of the infobar, if the infobar requires one. | 72 * Adds an icon to the start of the infobar, if the infobar requires one. |
69 * @param iconResourceId Resource ID of the icon to use. | 73 * @param iconResourceId Resource ID of the icon to use. |
70 * @param iconBitmap Raw {@link Bitmap} to use instead of a resource. | 74 * @param iconBitmap Raw {@link Bitmap} to use instead of a resource. |
71 */ | 75 */ |
72 private void prepareIcon(int iconResourceId, Bitmap iconBitmap) { | 76 private void prepareIcon(int iconResourceId, Bitmap iconBitmap) { |
73 ImageView iconView = InfoBarLayout.createIconView(getContext(), iconReso urceId, iconBitmap); | 77 ImageView iconView = InfoBarLayout.createIconView(getContext(), iconReso urceId, iconBitmap); |
74 if (iconView != null) { | 78 if (iconView != null) { |
75 LinearLayout.LayoutParams iconParams = | 79 LinearLayout.LayoutParams iconParams = |
76 new LinearLayout.LayoutParams(mCompactInfoBarSize, mCompactI nfoBarSize); | 80 new LinearLayout.LayoutParams(mCompactInfoBarSize, mCompactI nfoBarSize); |
77 addView(iconView, iconParams); | 81 addView(iconView, iconParams); |
78 } | 82 } |
79 } | 83 } |
80 | 84 |
81 /** Adds a close button to the end of the infobar. */ | 85 /** Adds a close button to the end of the infobar. */ |
82 private View prepareCloseButton() { | 86 private View prepareCloseButton() { |
83 ImageButton closeButton = InfoBarLayout.createCloseButton(getContext()); | 87 ImageButton closeButton = InfoBarLayout.createCloseButton(getContext()); |
84 closeButton.setOnClickListener(this); | 88 closeButton.setOnClickListener(this); |
85 LinearLayout.LayoutParams closeParams = | 89 LinearLayout.LayoutParams closeParams = |
86 new LinearLayout.LayoutParams(mCompactInfoBarSize, mCompactInfoB arSize); | 90 new LinearLayout.LayoutParams(mCompactInfoBarSize, mCompactInfoB arSize); |
87 addView(closeButton, closeParams); | 91 addView(closeButton, closeParams); |
88 return closeButton; | 92 return closeButton; |
89 } | 93 } |
90 } | 94 } |
OLD | NEW |