diff --git a/themes/cookie-glass/js/index.ts b/themes/cookie-glass/js/index.ts index 42483b1..658e26d 100644 --- a/themes/cookie-glass/js/index.ts +++ b/themes/cookie-glass/js/index.ts @@ -12,6 +12,8 @@ /// import { Tokens } from '@valenced/discord/common/tokens' +import { callNativeMethodSync } from '@valenced/modules/native' +import { AppState } from 'react-native' import { Palettes, Semantic } from './colors' import { Icons } from './icons' import type { @@ -19,8 +21,10 @@ import type { FontVariant, ThemeBase, ThemeColors, + ThemesApi, } from '@valenced/themes-api/types' +const PluginId = 'com.astar76.cookie-glass' const Group = 'cookie-glass' type PaletteName = keyof typeof Palettes @@ -277,14 +281,22 @@ const Tonal = Object.fromEntries( ]), ) as Record -function buildColors(discord: DiscordColors): ThemeColors { - const p = Tonal - +/** + * @param p tonal palettes: ours, or Android's system ones + * @param surfaces palette the grays are re-toned from (neutral2 tints them with the accent) + * @param glass put the desktop theme's hand-tuned mauve glass on top (only fits our palettes) + */ +function buildColors( + discord: DiscordColors, + p: Record = Tonal, + surfaces: PaletteName = 'neutral1', + glass = true, +): ThemeColors { // swap discord's grays/blurple for ours of the same tone const raw: Record = {} for (const [name, value] of Object.entries(discord.raw)) { const palette = NeutralRaw.test(name) - ? 'neutral1' + ? surfaces : BrandRaw.test(name) ? 'accent1' : undefined @@ -317,7 +329,7 @@ function buildColors(discord: DiscordColors): ThemeColors { colors[name] = withAlpha(roles[role], alpha) // the desktop theme's hand-tuned glass on top - if (GlassBases.has(base)) Object.assign(colors, Semantic) + if (glass && GlassBases.has(base)) Object.assign(colors, Semantic) semantic[base] = colors } @@ -325,6 +337,30 @@ function buildColors(discord: DiscordColors): ThemeColors { return { raw, semantic } } +// Android 12+ system palettes, read by the native half (src/main, from dim's Material You). +// Null on iOS, older Android, or before the native half has started. +let palettesJson: string | null = null + +function readPalettes() { + try { + palettesJson = callNativeMethodSync(`${PluginId}.getPalettes`, []) + } catch { + palettesJson = null + } + return palettesJson +} + +function syncedColors(discord: DiscordColors): ThemeColors { + if (!palettesJson) readPalettes() + if (!palettesJson) return buildColors(discord) + const system = Object.fromEntries( + Object.entries(JSON.parse(palettesJson) as Record).map( + ([name, shades]) => [name, tonalPalette(shades)], + ), + ) as Record + return buildColors(discord, system, 'neutral2', false) +} + // Panel see-through: lower shows more of the shapes. const Dimming = 0.55 @@ -344,7 +380,31 @@ for (const [style, wght] of Object.entries(Weights)) { path: FontItalic, weight: wght, italic: true, axes: { wght } }, ] +function registerSynced(themes: ThemesApi) { + themes.registerTheme(Group, 'system', { + name: 'Cookie Glass (Material You)', + description: + 'Drifting shapes, colors from your Android wallpaper (Android 12+). Mauve on iOS.', + getColors: syncedColors, + background: { path: 'assets/background-animated.gif', dimming: Dimming }, + }) +} + export default plugin({ + start({ themes, cleanup }) { + // the native half is up now: re-registering re-applies the theme if it's selected + readPalettes() + registerSynced(themes) + + // wallpaper/palette may have changed while backgrounded + const subscription = AppState.addEventListener('change', state => { + if (state !== 'active') return + const previous = palettesJson + if (readPalettes() !== previous) registerSynced(themes) + }) + cleanup(() => subscription.remove()) + }, + preInit({ themes }) { themes.registerGroup(Group, { name: 'Cookie Glass', @@ -366,6 +426,8 @@ export default plugin({ background: { path: 'assets/background.png', dimming: Dimming }, }) + registerSynced(themes) + themes.registerFontPack(Group, 'figtree', { name: 'Figtree', description: 'Variable font, replaces gg sans', @@ -393,3 +455,12 @@ export default plugin({ }) }, }) + +declare module '@valenced/modules/native' { + interface NativeMethods { + 'com.astar76.cookie-glass.getPalettes': [ + args: [], + returnValue: string | null, + ] + } +} diff --git a/themes/cookie-glass/manifest.json b/themes/cookie-glass/manifest.json index 13a7266..c6160da 100644 --- a/themes/cookie-glass/manifest.json +++ b/themes/cookie-glass/manifest.json @@ -4,7 +4,7 @@ "name": "Cookie Glass", "description": "Material 3 Expressive mauve theme with drifting cookie / flower shapes behind see-through panels, Figtree font and Material Symbols icons. Based on Material You by dim: every component themed, light and dark, iOS and Android.", "author": "astar76", - "version": "1.5.0", + "version": "1.6.0", "dependencies": { "valenced.api": { "version": ">=1 <2" @@ -17,6 +17,10 @@ } }, "dist": { - "script": "index.js" + "script": "index.js", + "android": { + "path": "plugin.jar", + "class": "dim.materialyou.MaterialYou" + } } } diff --git a/themes/cookie-glass/plugin.jar b/themes/cookie-glass/plugin.jar new file mode 100644 index 0000000..462d615 Binary files /dev/null and b/themes/cookie-glass/plugin.jar differ diff --git a/themes/cookie-glass/src/main/AndroidManifest.xml b/themes/cookie-glass/src/main/AndroidManifest.xml new file mode 100644 index 0000000..b2d3ea1 --- /dev/null +++ b/themes/cookie-glass/src/main/AndroidManifest.xml @@ -0,0 +1,2 @@ + + diff --git a/themes/cookie-glass/src/main/kotlin/dim/materialyou/MaterialYou.kt b/themes/cookie-glass/src/main/kotlin/dim/materialyou/MaterialYou.kt new file mode 100644 index 0000000..1596f3f --- /dev/null +++ b/themes/cookie-glass/src/main/kotlin/dim/materialyou/MaterialYou.kt @@ -0,0 +1,45 @@ +@file:JvmName("MaterialYou") + +package dim.materialyou + +import android.app.AndroidAppHelper +import android.content.Context +import android.os.Build +import app.valenced.plugins.plugin +import app.valenced.xposed.api.registerNativeMethod +import org.json.JSONArray +import org.json.JSONObject + +private val Palettes = listOf("accent1", "accent2", "accent3", "neutral1", "neutral2") + +// lightest to darkest +private val Shades = listOf(0, 10, 50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000) + +@Suppress("UNUSED") +val materialYou = plugin { + start { + var appContext: Context? = null + withAppContext { appContext = it.applicationContext } + + registerNativeMethod("${manifest.id}.getPalettes") { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) return@registerNativeMethod null + + val context = AndroidAppHelper.currentApplication() ?: appContext + ?: return@registerNativeMethod null + val res = context.resources + + val palettes = JSONObject() + for (palette in Palettes) { + val shades = JSONArray() + for (shade in Shades) { + @Suppress("DiscouragedApi") + val id = res.getIdentifier("system_${palette}_$shade", "color", "android") + if (id == 0) return@registerNativeMethod null + shades.put(String.format("#%06X", res.getColor(id, null) and 0xFFFFFF)) + } + palettes.put(palette, shades) + } + palettes.toString() + } + } +} diff --git a/tools/package.sh b/tools/package.sh index 390d24b..07cce93 100755 --- a/tools/package.sh +++ b/tools/package.sh @@ -13,6 +13,7 @@ for dir in themes/*/ plugins/*/; do stage=$(mktemp -d -p "$HOME/.cache") cp "$dir/manifest.json" "$dir/build/js/index.js" "$stage/" [ -d "$dir/assets" ] && cp -r "$dir/assets" "$stage/assets" + [ -f "$dir/plugin.jar" ] && cp "$dir/plugin.jar" "$stage/" rm -f "dist/$id.zip" (cd "$stage" && zip -qr -X "$OLDPWD/dist/$id.zip" .) rm -rf "$stage"