Skip to content

Commit

Permalink
feat(ui): full app wide material theming in dim mode replacing dark blue
Browse files Browse the repository at this point in the history
  • Loading branch information
crimera authored Apr 17, 2024
2 parents 35bd138 + ad15bff commit 52d7444
Showing 1 changed file with 61 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,40 @@ import java.io.FileWriter
import java.nio.file.Files

@Patch(
name = "Dynamic color",
description = "Replaces the default X (Formerly Twitter) Blue with the user's Material You palette.",
name = "Dynamic color - Monet",
description = "Replaces the default Blue accent with the user's Material You palette and Dim Theme with Full Material Design.",
compatiblePackages = [CompatiblePackage("com.twitter.android")],
)
@Suppress("unused")
object DynamicColorPatch : ResourcePatch() {
override fun execute(context: ResourceContext) {
// directories
val resDirectory = context["res"]
if (!resDirectory.isDirectory) throw PatchException("The res folder can not be found.")

val valuesV31Directory = resDirectory.resolve("values-v31")
if (!valuesV31Directory.isDirectory) Files.createDirectories(valuesV31Directory.toPath())

val valuesNightV31Directory = resDirectory.resolve("values-night-v31")
if (!valuesNightV31Directory.isDirectory) Files.createDirectories(valuesNightV31Directory.toPath())

listOf(valuesV31Directory, valuesNightV31Directory).forEach { it ->
val colorsXml = it.resolve("colors.xml")
val colorsXml = resDirectory.resolve("colors.xml")
val colorsDirectory = "res/values-v31/colors.xml"
val colorsNightV31Directory = "res/values-night-v31/colors.xml"
val stylesXml = resDirectory.resolve("styles.xml")
val stylesNightV31Directory = "res/values-night-v31/styles.xml"

if (!colorsXml.exists()) {
FileWriter(colorsXml).use {
it.write("<?xml version=\"1.0\" encoding=\"utf-8\"?><resources></resources>")
}
when {
!resDirectory.isDirectory -> throw PatchException("The res folder can not be found.")
!valuesV31Directory.isDirectory -> Files.createDirectories(valuesV31Directory.toPath())
!valuesNightV31Directory.isDirectory -> Files.createDirectories(
valuesNightV31Directory.toPath()
)
!stylesXml.exists() -> FileWriter(stylesXml).write(
"<?xml version=\"1.0\" encoding=\"utf-8\"?><resources></resources>"
)
!colorsXml.exists() -> listOf(valuesV31Directory, valuesNightV31Directory).forEach { _ ->
FileWriter(colorsXml).write(
"<?xml version=\"1.0\" encoding=\"utf-8\"?><resources></resources>"
)
}
}

context.xmlEditor["res/values-v31/colors.xml"].use { editor ->
context.xmlEditor[colorsDirectory].use { editor ->
val document = editor.file

mapOf(
Expand All @@ -57,7 +64,7 @@ object DynamicColorPatch : ResourcePatch() {
}
}

context.xmlEditor["res/values-night-v31/colors.xml"].use { editor ->
context.xmlEditor[colorsNightV31Directory].use { editor ->
val document = editor.file

mapOf(
Expand All @@ -76,5 +83,43 @@ object DynamicColorPatch : ResourcePatch() {
document.getElementsByTagName("resources").item(0).appendChild(colorElement)
}
}

/** fun fullMaterialDesign() { **/
// backward compatible, creates style into v31 res dir (A12+)
// replace parts of DimTheme with user's material 3 neutral palette
context.xmlEditor[stylesNightV31Directory].use { editor ->
val document = editor.file

val newStyle = document.createElement("style")
newStyle.setAttribute("name", "PaletteDim")
newStyle.setAttribute("parent", "@style/HorizonColorPaletteDark")

val styleItems = mapOf(
"abstractColorCellBackground" to "@color/material_dynamic_neutral10",
"abstractColorCellBackgroundTranslucent" to "@color/material_dynamic_neutral10",
"abstractColorDeepGray" to "#ff8899a6",
"abstractColorDivider" to "#ff38444d",
"abstractColorFadedGray" to "@color/material_dynamic_neutral10",
"abstractColorFaintGray" to "@color/material_dynamic_neutral10",
"abstractColorHighlightBackground" to "@color/material_dynamic_neutral20",
"abstractColorLightGray" to "#ff3d5466",
"abstractColorLink" to "@color/twitter_blue",
"abstractColorMediumGray" to "#ff6b7d8c",
"abstractColorText" to "@color/white",
"abstractColorUnread" to "#ff163043",
"abstractElevatedBackground" to "#ff1c2c3c",
"abstractElevatedBackgroundShadow" to "#1a15202b"
)

styleItems.forEach { (k, v) ->
val styleElement = document.createElement("item")

styleElement.setAttribute("name", k)
styleElement.textContent = v
newStyle.appendChild(styleElement)
}

document.getElementsByTagName("resources").item(0).appendChild(newStyle)
}
}
}

0 comments on commit 52d7444

Please sign in to comment.