fix(YouTube Music - Dark theme): Gradient is applied to the button of the action bar

This commit is contained in:
inotia00 2025-02-07 18:11:17 +09:00
parent 7eb0c06001
commit 6c37959f18

View File

@ -1,5 +1,7 @@
package app.revanced.extension.music.patches.utils; package app.revanced.extension.music.patches.utils;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -7,7 +9,6 @@ import android.widget.ImageView;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import app.revanced.extension.shared.utils.Logger;
import app.revanced.extension.shared.utils.ResourceUtils; import app.revanced.extension.shared.utils.ResourceUtils;
@SuppressWarnings("unused") @SuppressWarnings("unused")
@ -21,10 +22,10 @@ public class DrawableColorPatch {
// background colors // background colors
private static final Drawable headerGradient = private static final Drawable headerGradient =
ResourceUtils.getDrawable("revanced_header_gradient"); ResourceUtils.getDrawable("revanced_header_gradient");
private static final Drawable transparentDrawable =
new ColorDrawable(Color.TRANSPARENT);
private static final int blackColor = private static final int blackColor =
ResourceUtils.getColor("yt_black1"); ResourceUtils.getColor("yt_black1");
private static final int elementsContainerIdentifier =
ResourceUtils.getIdIdentifier("elements_container");
public static int getLithoColor(int colorValue) { public static int getLithoColor(int colorValue) {
return ArrayUtils.contains(DARK_COLORS, colorValue) return ArrayUtils.contains(DARK_COLORS, colorValue)
@ -40,25 +41,30 @@ public class DrawableColorPatch {
if (secondChildView instanceof ImageView gradientView) { if (secondChildView instanceof ImageView gradientView) {
// Album // Album
setHeaderGradient(viewGroup, gradientView); setHeaderGradient(gradientView);
} else if (secondChildView instanceof ViewGroup thirdChildView && } else if (secondChildView instanceof ViewGroup thirdChildView &&
thirdChildView.getChildCount() == 1 && thirdChildView.getChildCount() == 1 &&
thirdChildView.getChildAt(0) instanceof ImageView gradientView) { thirdChildView.getChildAt(0) instanceof ImageView gradientView) {
// Playlist // Playlist
setHeaderGradient(viewGroup, gradientView); setHeaderGradient(gradientView);
} }
}); });
} }
private static void setHeaderGradient(ViewGroup viewGroup, ImageView gradientView) { private static void setHeaderGradient(ImageView gradientView) {
// For some reason, it sometimes applies to other lithoViews. // headerGradient is litho, so this view is sometimes used elsewhere, like the button of the action bar.
// To prevent this, check the viewId before applying the gradient. // In order to prevent the gradient to be applied to the button of the action bar,
if (gradientView.getForeground() == null && headerGradient != null && viewGroup.getId() == elementsContainerIdentifier) { // Add a layout listener to the ImageView.
viewGroup.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); if (headerGradient != null && gradientView.getForeground() == null) {
gradientView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
Logger.printDebug(() -> "set header gradient.\nviewGroup measured width: " + viewGroup.getMeasuredWidth() + ", gradientView measured width: " + gradientView.getMeasuredWidth());
gradientView.setForeground(headerGradient); gradientView.setForeground(headerGradient);
gradientView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
if (gradientView.getParent() instanceof View view &&
view.getContentDescription() != null &&
gradientView.getForeground() == headerGradient
) {
gradientView.setForeground(transparentDrawable);
}
});
} }
} }
} }