chromiumembedded / cef

Chromium Embedded Framework (CEF). A simple framework for embedding Chromium-based browsers in other applications.
https://bitbucket.org/chromiumembedded/cef/
Other
3.28k stars 456 forks source link

chrome: Switch between light and dark theme at runtime #3610

Closed albianto closed 5 months ago

albianto commented 10 months ago

Is your feature request related to a problem? Please describe. We have a cross-platform application that has a runtime setting that lets the user switch between light and dark themes. The user can choose to either follow the OS setting or override it. We can force CEF to run in light or dark mode at launch with the force-light/dark command line flags. We cannot however set these after launch.

Describe the solution you'd like We would like to be able to change the active CEF theme at runtime. We'd love to have an API on CefBrowser that would let us do this.

magreenblatt commented 8 months ago

This would require pretty significant Chromium changes, and is therefore not something that we are likely to implement for CEF.

magreenblatt commented 8 months ago

With Chrome runtime you can configure this using themes (see here). That likely equates to the browser.theme.color_scheme (prefs::kBrowserColorScheme) preference that can be configured via CefPreferenceManager. Supported integer values are listed in ThemeService::BrowserColorScheme.

magreenblatt commented 8 months ago

Setting the preference directly during runtime may not be enough. It may be necessary to instead call ThemeService::SetBrowserColorScheme which sets the preference and then triggers notifications.

andrewpitts15 commented 8 months ago

Setting the preference directly during runtime may not be enough. It may be necessary to instead call ThemeService::SetBrowserColorScheme which sets the preference and then triggers notifications.

I've tried setting the browser.theme.color_scheme preference from within code and it is not working like suspected.

Is it possible to call into ThemeService from within CEF or is it functionality that would need to be added?

magreenblatt commented 8 months ago

Is it possible to call into ThemeService from within CEF or is it functionality that would need to be added?

It is possible from within CEF (with Chrome runtime) but it is not yet exposed to client applications.

andrewpitts15 commented 8 months ago

Is it possible to call into ThemeService from within CEF or is it functionality that would need to be added?

It is possible from within CEF (with Chrome runtime) but it is not yet exposed to client applications.

It would be really appreciated if this feature could be added. I guess for now, the best alternative is to inject javascript from the application that sets different body classes for light and dark modes rather than relying on "prefers-color-scheme".

magreenblatt commented 6 months ago

Related bug with Chrome not applying dynamic light/dark mode changes to web content: https://issues.chromium.org/issues/332328864

andrewpitts15 commented 5 months ago

@magreenblatt does this completed change now mean we can change the colour scheme web-content is rendered in or is it limited to the colour of the window chrome? E.g. Can we control whether "prefer-color-scheme: light" or "prefer-color-scheme: dark" is applied?

magreenblatt commented 5 months ago

Can we control whether "prefer-color-scheme: light" or "prefer-color-scheme: dark" is applied?

Yes, but only in combination with setting the OS or Chrome theme. It cannot be set independently.

albianto commented 3 months ago

@magreenblatt Thanks for working on this. Really appreciate the effort! I tested CefRequestContext::SetChromeColorScheme and I was wondering if there's a way to have the new color scheme be applied to an already loaded page. It works just fine after the page is reloaded.