Skip to content

Commit

Permalink
feat(theme): arbitrary background color for dark theme
Browse files Browse the repository at this point in the history
Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
  • Loading branch information
oSumAtrIX committed Sep 16, 2022
1 parent 0032301 commit afd5502
Showing 1 changed file with 17 additions and 29 deletions.
Expand Up @@ -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
Expand All @@ -20,48 +23,33 @@ 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
}
}
}

return PatchResultSuccess()
}

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 }
}
}
}

0 comments on commit afd5502

Please sign in to comment.