Repository navigation
feat: add color scheme support for desktop WebViews - #67
Open
martinkade wants to merge 1 commit into
Open
martinkade wants to merge 1 commit into
martinkade wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add desktop color-scheme control for WebView content
Rationale
None of the three desktop backends previously exposed any way to influence whether loaded content
renders light or dark:
afterward. WKWebView resolves
prefers-color-scheme/color-schemepartly from the hostingNSView's effective appearance, so content relying on the UA's own default dark colors (rather
than setting every color explicitly) never reliably rendered dark.
ICoreWebView2Profile::put_PreferredColorScheme(Auto/Light/Dark) - but nothing reached it.WebKitWebViewAPI exists upstream either; the only lever is theprocess/display-wide
GtkSettings:gtk-application-prefer-dark-themeproperty, which WebKitGTKconsults when deciding what to report for
prefers-color-scheme.DesktopWebSettingshad no field for this at all. Practical effect: a consuming app's dark themetoggle had no way to also darken loaded HTML content (a mail body viewer, a rich-text editor, ...)
on desktop - unlike Android, where
WebSettingsCompat.setAlgorithmicDarkeningAllowedalreadycovers exactly this case.
What changed
WebViewColorSchemeenum (SYSTEM/LIGHT/DARK), commonMain.DesktopWebSettings.colorScheme- applied once at WebView creation, like every other field onthat settings class. Defaults to
SYSTEM, so no behavior change for existing callers.IWebView.setColorScheme(scheme)- new method to change it afterward without recreating theWebView. Default no-op on the interface; only the desktop
DesktopWebViewoverrides it. Android/iOS/Wasm don't need it - their engines already resolve
prefers-color-schemefrom the systemtheme correctly on their own.
a new
setColorSchemeon the concreteNativeWebViewsubclass:navigation.m,WebKitMacOsBridge.nativeSetAppearance): sets the WKWebView's ownNSAppearance(.aqua/.darkAqua/nilfor system).navigation.cpp,WebView2WindowsBridge.nativeSetPreferredColorScheme): callsICoreWebView2Profile::put_PreferredColorSchemeviaICoreWebView2_13::get_Profile.navigation.c,WebKitLinuxBridge.nativeSetPreferDarkTheme): setsgtk-application-prefer-dark-themeon the widget's ownGtkSettings. This is display-wide,not per-WebView - there is no narrower API upstream, so it also affects other GTK widgets'
theme rendering on that display. Called out in the native implementation's own doc comment.
Testing / verification status
./gradlew :webview-compose:buildNativeMacos. Not yet exercised in a running app window (novisual e2e case added for this yet - see below).
ICoreWebView2_13/ICoreWebView2Profile, available since SDK 1.0.1264.42, well below the 1.0.2210.55 this projectvendors) but not locally compiled - no Windows/MSVC toolchain available where this was
written. Needs a real Windows build (CI matrix or manual) before merging.
build before merging.
dev.nucleusframework.webview.setting.*/dev.nucleusframework.webview.web.*unittests pass unaffected (
./gradlew :webview-compose:jvmTest).Steps to verify
./gradlew :webview-compose:buildNativeLinux/buildNativeMacos/buildNativeWindowsoneach OS.
./gradlew :e2e-desktop:runon each OS, with a manual check that a WebView loading plain HTMLwith no
color-schemeof its own switches between light/dark for eachWebViewColorSchemevalue (no automated visual case added yet - worth a follow-up in
e2e-shared'svisualsuite/*catalog).Known limitations / follow-ups
desktop app, but worth documenting prominently in the public API doc if this ships.
until CI (or a manual build) confirms both.