Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Better handle UIButton drawable states (#3186)
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo authored Apr 17, 2020
1 parent 81a81dd commit c3a1149
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions app/src/common/shared/org/mozilla/vrbrowser/ui/views/UIButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public class UIButton extends AppCompatImageButton implements CustomUIButton {
private boolean mIsActive;
private boolean mIsNotification;
private ClipDrawable mClipDrawable;
private Drawable mDrawable;
private int mClipColor;

public UIButton(Context context, AttributeSet attrs) {
Expand All @@ -70,9 +69,6 @@ public UIButton(Context context, AttributeSet attrs, int defStyleAttr) {

TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.UIButton, defStyleAttr, 0);
mTintColorListRes = attributes.getResourceId(R.styleable.UIButton_tintColorList, 0);
if (mTintColorListRes != 0) {
setTintColorList(mTintColorListRes);
}
mBackground = attributes.getDrawable(R.styleable.UIButton_regularModeBackground);
mPrivateModeBackground = attributes.getDrawable(R.styleable.UIButton_privateModeBackground);
mActiveModeBackground = attributes.getDrawable(R.styleable.UIButton_activeModeBackground);
Expand All @@ -98,10 +94,12 @@ public UIButton(Context context, AttributeSet attrs, int defStyleAttr) {
}

if (mClipDrawable != null) {
Drawable[] layers = new Drawable[] { mDrawable, mClipDrawable };
Drawable[] layers = new Drawable[] { getDrawable(), mClipDrawable };
setImageDrawable(new LayerDrawable(layers));
mClipDrawable.setLevel(0);
mClipDrawable.setTint(R.color.azure);
}

if (mTintColorListRes != 0) {
setTintColorList(mTintColorListRes);
}

// Android >8 doesn't perform a click when long clicking in ImageViews even if long click is disabled
Expand Down Expand Up @@ -179,30 +177,25 @@ public boolean onHoverEvent(MotionEvent event) {
}

public void setTintColorList(int aColorListId) {
if (mDrawable == null) {
if (getDrawable() == null) {
return;
}
mTintColorList = getContext().getResources().getColorStateList(
aColorListId,
getContext().getTheme());
int color = mTintColorList.getColorForState(getDrawableState(), 0);
mDrawable.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
if (mClipDrawable != null) {
mClipDrawable.setColorFilter(new PorterDuffColorFilter(mClipColor, PorterDuff.Mode.MULTIPLY));
}
refreshDrawableState();
}

@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (mDrawable == null) {
return;
}
if (mTintColorList != null && mTintColorList.isStateful()) {
int color = mTintColorList.getColorForState(getDrawableState(), 0);
mDrawable.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
getDrawable().setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
if (mClipDrawable != null) {
mClipDrawable.setColorFilter(new PorterDuffColorFilter(mClipColor, PorterDuff.Mode.MULTIPLY));
} else {
setColorFilter(color);
}
}
}
Expand Down Expand Up @@ -368,8 +361,14 @@ public void setEnabled(boolean enabled) {

@Override
public void setImageDrawable(@Nullable Drawable drawable) {
super.setImageDrawable(drawable);
mDrawable = drawable;
Drawable image = drawable;
if (mClipDrawable != null) {
Drawable[] layers = new Drawable[] { drawable, mClipDrawable };
image = new LayerDrawable(layers);
mClipDrawable.setLevel(0);
mClipDrawable.setTint(getResources().getColor(R.color.azure, getContext().getTheme()));
}
super.setImageDrawable(image);
updateButtonColor();
}

Expand Down

0 comments on commit c3a1149

Please sign in to comment.