Commit 9f4eef6f authored by Theresa's avatar Theresa Committed by Commit Bot

Introduce TextViewCompoundDrawables widget for use in Simplified NTP

Add a new TextViewCompoundDrawables widget that supports explicitly
sizing and tinting compound drawables.

Update the "Simplified NTP" bookmarks and downloads buttons to use the
new widget.

BUG=836308

Change-Id: I7705135153cee9b053fb031bb69b6dbc636caee4
Reviewed-on: https://chromium-review.googlesource.com/1054556Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Reviewed-by: default avataragrieve <agrieve@chromium.org>
Commit-Queue: Theresa <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#557784}
parent 84baff3f
...@@ -246,6 +246,17 @@ public class ApiCompatibilityUtils { ...@@ -246,6 +246,17 @@ public class ApiCompatibilityUtils {
} }
} }
/**
* @see android.widget.TextView#getCompoundDrawablesRelative()
*/
public static Drawable[] getCompoundDrawablesRelative(TextView textView) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return textView.getCompoundDrawablesRelative();
} else {
return textView.getCompoundDrawables();
}
}
/** /**
* @see android.widget.TextView#setCompoundDrawablesRelative(Drawable, Drawable, Drawable, * @see android.widget.TextView#setCompoundDrawablesRelative(Drawable, Drawable, Drawable,
* Drawable) * Drawable)
......
...@@ -16,61 +16,49 @@ ...@@ -16,61 +16,49 @@
android:gravity="center" android:gravity="center"
android:orientation="horizontal" > android:orientation="horizontal" >
<!-- TODO(twellington): Change these to compound drawables once we have a widget that supports <FrameLayout
easily sizing compound drawables. -->
<LinearLayout
android:id="@+id/bookmarks_button" android:id="@+id/bookmarks_button"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_weight="1" android:layout_weight="1"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:minHeight="48dp" android:minHeight="48dp"
android:paddingTop="4dp" android:paddingTop="4dp"
android:paddingBottom="4dp" android:paddingBottom="4dp" >
android:gravity="center"
android:orientation="horizontal"
tools:ignore="UseCompoundDrawables" >
<ImageView <org.chromium.chrome.browser.widget.TextViewWithCompoundDrawables
android:layout_width="20sp"
android:layout_height="20sp"
android:contentDescription="@null"
android:layout_marginEnd="8dp"
android:scaleType="fitCenter"
android:src="@drawable/btn_star_filled"
chrome:tint="@color/black_alpha_54" />
<TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:drawablePadding="8dp"
android:drawableStart="@drawable/btn_star_filled"
android:text="@string/menu_bookmarks" android:text="@string/menu_bookmarks"
android:textAppearance="@style/BlackCaption" /> android:textAppearance="@style/BlackCaption"
</LinearLayout> chrome:drawableHeight="20sp"
chrome:drawableWidth="20sp"
chrome:chromeDrawableTint="@color/black_alpha_54" />
</FrameLayout>
<LinearLayout <FrameLayout
android:id="@+id/downloads_button" android:id="@+id/downloads_button"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_weight="1" android:layout_weight="1"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:minHeight="48dp" android:minHeight="48dp"
android:paddingTop="4dp" android:paddingTop="4dp"
android:paddingBottom="4dp" android:paddingBottom="4dp" >
android:gravity="center"
android:orientation="horizontal"
tools:ignore="UseCompoundDrawables" >
<ImageView
android:layout_width="20sp"
android:layout_height="20sp"
android:contentDescription="@null"
android:layout_marginEnd="8dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_file_download_white_24dp"
chrome:tint="@color/black_alpha_54" />
<TextView <org.chromium.chrome.browser.widget.TextViewWithCompoundDrawables
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:drawablePadding="8dp"
android:drawableStart="@drawable/ic_file_download_white_24dp"
android:text="@string/menu_downloads" android:text="@string/menu_downloads"
android:textAppearance="@style/BlackCaption" /> android:textAppearance="@style/BlackCaption"
</LinearLayout> chrome:drawableHeight="20sp"
chrome:drawableWidth="20sp"
chrome:chromeDrawableTint="@color/black_alpha_54" />
</FrameLayout>
</LinearLayout> </LinearLayout>
\ No newline at end of file
...@@ -81,4 +81,11 @@ ...@@ -81,4 +81,11 @@
<!-- Used to customize the width of the popup menu. --> <!-- Used to customize the width of the popup menu. -->
<attr name="menuWidth" format="reference|dimension" /> <attr name="menuWidth" format="reference|dimension" />
</declare-styleable> </declare-styleable>
<declare-styleable name="TextViewWithCompoundDrawables">
<attr name="drawableWidth" format="dimension"/>
<attr name="drawableHeight" format="dimension"/>
<!-- drawableTint wasn't added in Android until API level 23. -->
<attr name="chromeDrawableTint" format="color"/>
</declare-styleable>
</resources> </resources>
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.widget;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.chrome.R;
/**
* A {@link TextView} with support for explicitly sizing and tinting compound drawables.
*
* To specify the drawable size, use the {@code drawableWidth} and {@code drawableHeight}
* attributes. To specify the drawable tint, use the {@code chromeDrawableTint} attribute.
*/
public class TextViewWithCompoundDrawables extends AppCompatTextView {
private int mDrawableWidth;
private int mDrawableHeight;
private ColorStateList mDrawableTint;
public TextViewWithCompoundDrawables(Context context) {
this(context, null);
}
public TextViewWithCompoundDrawables(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TextViewWithCompoundDrawables(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (mDrawableTint != null) {
setDrawableTint(ApiCompatibilityUtils.getCompoundDrawablesRelative(this));
}
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
TypedArray array = context.obtainStyledAttributes(
attrs, R.styleable.TextViewWithCompoundDrawables, defStyleAttr, 0);
mDrawableWidth = array.getDimensionPixelSize(
R.styleable.TextViewWithCompoundDrawables_drawableWidth, -1);
mDrawableHeight = array.getDimensionPixelSize(
R.styleable.TextViewWithCompoundDrawables_drawableHeight, -1);
mDrawableTint = array.getColorStateList(
R.styleable.TextViewWithCompoundDrawables_chromeDrawableTint);
array.recycle();
if (mDrawableWidth <= 0 && mDrawableHeight <= 0 && mDrawableTint == null) return;
Drawable[] drawables = ApiCompatibilityUtils.getCompoundDrawablesRelative(this);
for (Drawable drawable : drawables) {
if (drawable == null) continue;
if (mDrawableWidth > 0 || mDrawableHeight > 0) {
Rect bounds = drawable.getBounds();
if (mDrawableWidth > 0) {
bounds.right = bounds.left + Math.round(mDrawableWidth);
}
if (mDrawableHeight > 0) {
bounds.bottom = bounds.top + Math.round(mDrawableHeight);
}
drawable.setBounds(bounds);
}
}
if (mDrawableTint != null) setDrawableTint(drawables);
ApiCompatibilityUtils.setCompoundDrawablesRelative(
this, drawables[0], drawables[1], drawables[2], drawables[3]);
}
private void setDrawableTint(Drawable[] drawables) {
for (Drawable drawable : drawables) {
if (drawable == null) continue;
drawable.setColorFilter(
mDrawableTint.getColorForState(getDrawableState(), 0), PorterDuff.Mode.SRC_IN);
}
}
}
...@@ -1433,6 +1433,7 @@ chrome_java_sources = [ ...@@ -1433,6 +1433,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/widget/RadioButtonLayout.java", "java/src/org/chromium/chrome/browser/widget/RadioButtonLayout.java",
"java/src/org/chromium/chrome/browser/widget/RadioButtonWithDescription.java", "java/src/org/chromium/chrome/browser/widget/RadioButtonWithDescription.java",
"java/src/org/chromium/chrome/browser/widget/RoundedIconGenerator.java", "java/src/org/chromium/chrome/browser/widget/RoundedIconGenerator.java",
"java/src/org/chromium/chrome/browser/widget/TextViewWithCompoundDrawables.java",
"java/src/org/chromium/chrome/browser/widget/ThumbnailDiskStorage.java", "java/src/org/chromium/chrome/browser/widget/ThumbnailDiskStorage.java",
"java/src/org/chromium/chrome/browser/widget/ThumbnailGenerator.java", "java/src/org/chromium/chrome/browser/widget/ThumbnailGenerator.java",
"java/src/org/chromium/chrome/browser/widget/ThumbnailGeneratorCallback.java", "java/src/org/chromium/chrome/browser/widget/ThumbnailGeneratorCallback.java",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment