From daf6a2d0220f35d148eb65b0e0ecaf3fc4120640 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 25 Jun 2026 12:53:03 +0000 Subject: [PATCH] fix: resolve SYSTEM theme to concrete DARK/LIGHT before crossing process boundary Instead of passing the raw ThemeType name ("SYSTEM") to the :napplet process, resolve it to the actual dark/light value in the main process by reading context.resources.configuration.uiMode before the launch. The napplet process now always receives "DARK" or "LIGHT" and sets UiModeManager.nightMode unconditionally, making prefers-color-scheme reliable on all ThemeType choices. Co-Authored-By: Claude Sonnet 4.6 Claude-Session: https://claude.ai/code/session_0198rKcuv32DEoUPpLqsBYbx --- .../amethyst/favorites/FavoriteAppLauncher.kt | 13 ++++++++++++- .../amethyst/napplet/NappletLauncher.kt | 13 ++++++++++++- .../ui/screen/loggedIn/embed/EmbeddedTabFactory.kt | 13 ++++++++++++- .../amethyst/napplethost/NappletBrowserActivity.kt | 1 - .../amethyst/napplethost/NappletBrowserService.kt | 1 - .../amethyst/napplethost/NappletHostActivity.kt | 1 - .../amethyst/napplethost/NappletHostService.kt | 1 - 7 files changed, 36 insertions(+), 7 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/favorites/FavoriteAppLauncher.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/favorites/FavoriteAppLauncher.kt index c3e2b49bd5..a250f73b4f 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/favorites/FavoriteAppLauncher.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/favorites/FavoriteAppLauncher.kt @@ -22,6 +22,7 @@ package com.vitorpamplona.amethyst.favorites import android.app.Activity import android.content.Context +import android.content.res.Configuration import android.os.Bundle import android.util.Log import android.widget.Toast @@ -29,6 +30,7 @@ import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.favorites.FavoriteApp import com.vitorpamplona.amethyst.model.LocalCache +import com.vitorpamplona.amethyst.model.ThemeType import com.vitorpamplona.amethyst.napplet.NappletLauncher import com.vitorpamplona.amethyst.napplet.WebUrlNetworkRegistry import com.vitorpamplona.amethyst.napplethost.NappletBrowserActivity @@ -77,7 +79,16 @@ object FavoriteAppLauncher { ) { val proxyPort = Amethyst.instance.torManager.activePortOrNull.value ?: -1 val useTor = proxyPort > 0 && (preferTor || WebUrlNetworkRegistry.useTor(url)) - val theme = Amethyst.instance.uiPrefs.value.theme.value.name + val themeType = Amethyst.instance.uiPrefs.value.theme.value + val theme = + when (themeType) { + ThemeType.DARK -> "DARK" + ThemeType.LIGHT -> "LIGHT" + ThemeType.SYSTEM -> { + val nightMask = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK + if (nightMask == Configuration.UI_MODE_NIGHT_YES) "DARK" else "LIGHT" + } + } val intent = NappletBrowserActivity.intent(context, url, proxyPort, useTor, theme = theme).apply { if (context !is Activity) addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/napplet/NappletLauncher.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/napplet/NappletLauncher.kt index b1a03bedf4..439b4929aa 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/napplet/NappletLauncher.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/napplet/NappletLauncher.kt @@ -22,12 +22,14 @@ package com.vitorpamplona.amethyst.napplet import android.content.Context import android.content.Intent +import android.content.res.Configuration import android.os.Bundle import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.commons.napplet.NappletCapability import com.vitorpamplona.amethyst.commons.napplet.NappletIdentity import com.vitorpamplona.amethyst.commons.napplet.resolveRequiredCapabilities import com.vitorpamplona.amethyst.model.LocalCache +import com.vitorpamplona.amethyst.model.ThemeType import com.vitorpamplona.amethyst.napplethost.NappletHostActivity import com.vitorpamplona.amethyst.napplethost.NappletHostContract import com.vitorpamplona.quartz.nip01Core.core.HexKey @@ -134,7 +136,16 @@ object NappletLauncher { // Resolve capability labels here (the app has the resources) so the sandbox module needs none. val capLabels = declared.map { context.getString(it.labelRes()) } - val theme = Amethyst.instance.uiPrefs.value.theme.value.name + val themeType = Amethyst.instance.uiPrefs.value.theme.value + val theme = + when (themeType) { + ThemeType.DARK -> "DARK" + ThemeType.LIGHT -> "LIGHT" + ThemeType.SYSTEM -> { + val nightMask = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK + if (nightMask == Configuration.UI_MODE_NIGHT_YES) "DARK" else "LIGHT" + } + } return Bundle().apply { putStringArrayList(NappletHostContract.EXTRA_PATHS, ArrayList(paths.map { it.path })) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/embed/EmbeddedTabFactory.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/embed/EmbeddedTabFactory.kt index 0c742f933d..3f677a1c9c 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/embed/EmbeddedTabFactory.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/embed/EmbeddedTabFactory.kt @@ -21,6 +21,7 @@ package com.vitorpamplona.amethyst.ui.screen.loggedIn.embed import android.content.Context +import android.content.res.Configuration import android.os.Build import android.os.Bundle import androidx.annotation.RequiresApi @@ -28,6 +29,7 @@ import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.commons.favorites.FavoriteApp import com.vitorpamplona.amethyst.commons.tor.TorType import com.vitorpamplona.amethyst.favorites.FavoriteAppLauncher +import com.vitorpamplona.amethyst.model.ThemeType import com.vitorpamplona.amethyst.napplet.WebUrlNetworkRegistry import com.vitorpamplona.amethyst.napplethost.NappletHostContract import com.vitorpamplona.amethyst.ui.screen.loggedIn.browser.EmbeddedBrowserController @@ -57,7 +59,16 @@ object EmbeddedTabFactory { EmbeddedTabHost.acquire(browserId(url)) { val proxyPort = Amethyst.instance.torManager.activePortOrNull.value ?: -1 val initialUseTor = proxyPort > 0 && WebUrlNetworkRegistry.useTor(url) - val theme = Amethyst.instance.uiPrefs.value.theme.value.name + val themeType = Amethyst.instance.uiPrefs.value.theme.value + val theme = + when (themeType) { + ThemeType.DARK -> "DARK" + ThemeType.LIGHT -> "LIGHT" + ThemeType.SYSTEM -> { + val nightMask = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK + if (nightMask == Configuration.UI_MODE_NIGHT_YES) "DARK" else "LIGHT" + } + } EmbeddedBrowserController(context.applicationContext, proxyPort, initialUseTor, backgroundColor, theme).also { it.bind(url) } } as EmbeddedBrowserController diff --git a/nappletHost/src/main/kotlin/com/vitorpamplona/amethyst/napplethost/NappletBrowserActivity.kt b/nappletHost/src/main/kotlin/com/vitorpamplona/amethyst/napplethost/NappletBrowserActivity.kt index 3fe14e9bb6..0adbacde75 100644 --- a/nappletHost/src/main/kotlin/com/vitorpamplona/amethyst/napplethost/NappletBrowserActivity.kt +++ b/nappletHost/src/main/kotlin/com/vitorpamplona/amethyst/napplethost/NappletBrowserActivity.kt @@ -624,7 +624,6 @@ class NappletBrowserActivity : ComponentActivity() { when (themeType) { "DARK" -> uiManager.nightMode = android.app.UiModeManager.MODE_NIGHT_YES "LIGHT" -> uiManager.nightMode = android.app.UiModeManager.MODE_NIGHT_NO - else -> {} // SYSTEM: follow the device setting (process default) } } diff --git a/nappletHost/src/main/kotlin/com/vitorpamplona/amethyst/napplethost/NappletBrowserService.kt b/nappletHost/src/main/kotlin/com/vitorpamplona/amethyst/napplethost/NappletBrowserService.kt index e2acce8410..a0f343a960 100644 --- a/nappletHost/src/main/kotlin/com/vitorpamplona/amethyst/napplethost/NappletBrowserService.kt +++ b/nappletHost/src/main/kotlin/com/vitorpamplona/amethyst/napplethost/NappletBrowserService.kt @@ -143,7 +143,6 @@ class NappletBrowserService : Service() { when (themeType) { "DARK" -> uiManager.nightMode = android.app.UiModeManager.MODE_NIGHT_YES "LIGHT" -> uiManager.nightMode = android.app.UiModeManager.MODE_NIGHT_NO - else -> {} // SYSTEM: follow the device setting (process default) } } diff --git a/nappletHost/src/main/kotlin/com/vitorpamplona/amethyst/napplethost/NappletHostActivity.kt b/nappletHost/src/main/kotlin/com/vitorpamplona/amethyst/napplethost/NappletHostActivity.kt index d28e454ac6..b54b0b6153 100644 --- a/nappletHost/src/main/kotlin/com/vitorpamplona/amethyst/napplethost/NappletHostActivity.kt +++ b/nappletHost/src/main/kotlin/com/vitorpamplona/amethyst/napplethost/NappletHostActivity.kt @@ -803,7 +803,6 @@ class NappletHostActivity : ComponentActivity() { when (themeType) { "DARK" -> uiManager.nightMode = android.app.UiModeManager.MODE_NIGHT_YES "LIGHT" -> uiManager.nightMode = android.app.UiModeManager.MODE_NIGHT_NO - else -> {} // SYSTEM: follow the device setting (process default) } } diff --git a/nappletHost/src/main/kotlin/com/vitorpamplona/amethyst/napplethost/NappletHostService.kt b/nappletHost/src/main/kotlin/com/vitorpamplona/amethyst/napplethost/NappletHostService.kt index ab93ded513..e20804c7a4 100644 --- a/nappletHost/src/main/kotlin/com/vitorpamplona/amethyst/napplethost/NappletHostService.kt +++ b/nappletHost/src/main/kotlin/com/vitorpamplona/amethyst/napplethost/NappletHostService.kt @@ -192,7 +192,6 @@ class NappletHostService : Service() { when (themeType) { "DARK" -> uiManager.nightMode = android.app.UiModeManager.MODE_NIGHT_YES "LIGHT" -> uiManager.nightMode = android.app.UiModeManager.MODE_NIGHT_NO - else -> {} // SYSTEM: follow the device setting (process default) } }