Skip to content

Commit

Permalink
fix(YouTube Music - Dark theme): Gradient is applied to the button of…
Browse files Browse the repository at this point in the history
… the action bar
  • Loading branch information
inotia00 authored and anddea committed Feb 8, 2025
1 parent 2f35f59 commit 4f0cf0f
Showing 1 changed file with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package app.revanced.extension.music.patches.utils;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import org.apache.commons.lang3.ArrayUtils;

import app.revanced.extension.shared.utils.Logger;
import app.revanced.extension.shared.utils.ResourceUtils;

@SuppressWarnings("unused")
Expand All @@ -21,10 +22,10 @@ public class DrawableColorPatch {
// background colors
private static final Drawable headerGradient =
ResourceUtils.getDrawable("revanced_header_gradient");
private static final Drawable transparentDrawable =
new ColorDrawable(Color.TRANSPARENT);
private static final int blackColor =
ResourceUtils.getColor("yt_black1");
private static final int elementsContainerIdentifier =
ResourceUtils.getIdIdentifier("elements_container");

public static int getLithoColor(int colorValue) {
return ArrayUtils.contains(DARK_COLORS, colorValue)
Expand All @@ -40,25 +41,30 @@ public static void setHeaderGradient(ViewGroup viewGroup) {

if (secondChildView instanceof ImageView gradientView) {
// Album
setHeaderGradient(viewGroup, gradientView);
setHeaderGradient(gradientView);
} else if (secondChildView instanceof ViewGroup thirdChildView &&
thirdChildView.getChildCount() == 1 &&
thirdChildView.getChildAt(0) instanceof ImageView gradientView) {
// Playlist
setHeaderGradient(viewGroup, gradientView);
setHeaderGradient(gradientView);
}
});
}

private static void setHeaderGradient(ViewGroup viewGroup, ImageView gradientView) {
// For some reason, it sometimes applies to other lithoViews.
// To prevent this, check the viewId before applying the gradient.
if (gradientView.getForeground() == null && headerGradient != null && viewGroup.getId() == elementsContainerIdentifier) {
viewGroup.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
gradientView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
Logger.printDebug(() -> "set header gradient.\nviewGroup measured width: " + viewGroup.getMeasuredWidth() + ", gradientView measured width: " + gradientView.getMeasuredWidth());

private static void setHeaderGradient(ImageView gradientView) {
// headerGradient is litho, so this view is sometimes used elsewhere, like the button of the action bar.
// In order to prevent the gradient to be applied to the button of the action bar,
// Add a layout listener to the ImageView.
if (headerGradient != null && gradientView.getForeground() == null) {
gradientView.setForeground(headerGradient);
gradientView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
if (gradientView.getParent() instanceof View view &&
view.getContentDescription() != null &&
gradientView.getForeground() == headerGradient
) {
gradientView.setForeground(transparentDrawable);
}
});
}
}
}
Expand Down

0 comments on commit 4f0cf0f

Please sign in to comment.