feat(theme): arbitrary background color for dark theme

Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
This commit is contained in:
oSumAtrIX 2022-09-16 02:10:19 +02:00
parent 29b8f9ee53
commit 08b48b2bb4

View File

@ -4,7 +4,10 @@ import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.impl.ResourceData
import app.revanced.patcher.patch.*
import app.revanced.patcher.patch.OptionsContainer
import app.revanced.patcher.patch.PatchOption
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patcher.patch.annotations.Patch
import app.revanced.patcher.patch.impl.ResourcePatch
@ -20,14 +23,19 @@ import org.w3c.dom.Element
@Version("0.0.1")
class ThemePatch : ResourcePatch() {
override fun execute(data: ResourceData): PatchResult {
val theme = Themes.of(theme!!) ?: return PatchResultError("Theme '$theme' not found.")
val backgroundColor = backgroundColor!!
data.xmlEditor["res/values/colors.xml"].use { editor ->
val resourcesNode = editor.file.getElementsByTagName("resources").item(0) as Element
for (i in 0 until resourcesNode.childNodes.length) {
val node = resourcesNode.childNodes.item(i) as? Element ?: continue
node.textContent = theme.apply(node.getAttribute("name")) ?: continue
node.textContent = when (node.getAttribute("name")) {
"yt_black1", "yt_black1_opacity95", "yt_black2", "yt_black3", "yt_black4",
"yt_status_bar_background_dark" -> backgroundColor
else -> continue
}
}
}
@ -35,33 +43,13 @@ class ThemePatch : ResourcePatch() {
}
companion object : OptionsContainer() {
var theme: String? by option(
PatchOption.StringListOption(
key = "theme",
default = Themes.Amoled.name,
options = Themes.names,
title = "Theme",
description = "Select a theme.",
required = true
var backgroundColor: String? by option(
PatchOption.StringOption(
key = "darkThemeBackgroundColor",
default = "@android:color/black",
title = "Background color for the dark theme",
description = "The background color of the dark theme. Can be a hex color or a resource reference.",
)
)
}
enum class Themes(val apply: (String) -> String?) {
Amoled({ attr ->
when (attr) {
"yt_black1", "yt_black1_opacity95", "yt_black2", "yt_black3", "yt_black4",
"yt_status_bar_background_dark" -> "@android:color/black"
"yt_selected_nav_label_dark" -> "#ffdf0000"
else -> null
}
});
companion object {
val names = values().map { it.name }
fun of(name: String) = values().firstOrNull { it.name == name }
}
}
}