Commit e781ae76 authored by mcarare's avatar mcarare Committed by mergify[bot]
Browse files

For #10571: Add optional icon tint for Toolbar Button.

parent 2ca324e0
......@@ -11,6 +11,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.VisibleForTesting
import androidx.annotation.VisibleForTesting.PRIVATE
......@@ -373,16 +374,27 @@ class BrowserToolbar @JvmOverloads constructor(
* @param visible Lambda that returns true or false to indicate whether this button should be shown.
* @param background A custom (stateful) background drawable resource to be used.
* @param padding a custom [Padding] for this Button.
* @param iconTintColorResource Optional ID of color resource to tint the icon.
* @param listener Callback that will be invoked whenever the button is pressed
*/
@Suppress("LongParameterList")
open class Button(
imageDrawable: Drawable,
contentDescription: String,
visible: () -> Boolean = { true },
@DrawableRes background: Int = 0,
val padding: Padding = DEFAULT_PADDING,
@ColorRes iconTintColorResource: Int = NO_ID,
listener: () -> Unit
) : Toolbar.ActionButton(imageDrawable, contentDescription, visible, background, padding, listener)
) : Toolbar.ActionButton(
imageDrawable,
contentDescription,
visible,
background,
padding,
iconTintColorResource,
listener
)
/**
* An action button with two states, selected and unselected. When the button is pressed, the
......
......@@ -26,6 +26,7 @@ dependencies {
implementation Dependencies.androidx_annotation
implementation Dependencies.androidx_appcompat
implementation Dependencies.androidx_core_ktx
api project(':support-base')
implementation project(':support-ktx')
......
......@@ -6,14 +6,17 @@ package mozilla.components.concept.toolbar
import android.graphics.drawable.Drawable
import android.view.View
import android.view.View.NO_ID
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import androidx.annotation.ColorRes
import androidx.annotation.Dimension
import androidx.annotation.Dimension.DP
import androidx.annotation.DrawableRes
import androidx.appcompat.widget.AppCompatImageButton
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.content.ContextCompat
import mozilla.components.support.base.android.Padding
import mozilla.components.support.ktx.android.content.res.resolveAttribute
import mozilla.components.support.ktx.android.view.setPadding
......@@ -228,20 +231,24 @@ interface Toolbar {
* @param contentDescription The content description to use.
* @param visible Lambda that returns true or false to indicate whether this button should be shown.
* @param padding A optional custom padding.
* @param iconTintColorResource Optional ID of color resource to tint the icon.
* @param listener Callback that will be invoked whenever the button is pressed
*/
@Suppress("LongParameterList")
open class ActionButton(
val imageDrawable: Drawable? = null,
val contentDescription: String,
override val visible: () -> Boolean = { true },
private val background: Int = 0,
private val padding: Padding? = null,
@ColorRes val iconTintColorResource: Int = ViewGroup.NO_ID,
private val listener: () -> Unit
) : Action {
override fun createView(parent: ViewGroup): View = AppCompatImageButton(parent.context).also { imageButton ->
imageButton.setImageDrawable(imageDrawable)
imageButton.contentDescription = contentDescription
imageButton.setTintResource(iconTintColorResource)
imageButton.setOnClickListener { listener.invoke() }
@DrawableRes
......@@ -445,3 +452,9 @@ interface Toolbar {
NONE
}
}
private fun AppCompatImageButton.setTintResource(@ColorRes tintColorResource: Int) {
if (tintColorResource != NO_ID) {
imageTintList = ContextCompat.getColorStateList(context, tintColorResource)
}
}
Supports Markdown
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