Set CEF to light or dark mode at runtime

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Set CEF to light or dark mode at runtime

Postby atp15 » Tue Jan 16, 2024 4:20 am

Is there a way to send a message to CEF to tell it whether to be in light mode or dark mode so I can tell it whether it should respond to `prefers-color-scheme: light` or `prefers-color-scheme: dark` css rules?

We're adding a dark mode to our application with three settings that can be changed at runtime - light, dark and automatic where it uses the Windows theme setting. I've found it works correctly when set to automatic as Chromium automatically picks up the setting from Window. Unfortunately, this means that if the user has chosen a mode different from their Windows settings, it doesn't match. It would be good if I could send a message to CEF to tell it the which setting to use.
atp15
Techie
 
Posts: 22
Joined: Thu Nov 17, 2016 5:25 am

Re: Set CEF to light or dark mode at runtime

Postby magreenblatt » Tue Jan 16, 2024 11:49 am

With Chrome runtime you can potentially configure this using themes. See https://support.google.com/chrome/answe ... ktop&oco=2

You would need to identify what preference that equates to (load https://tests/preferences in cefclient).
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Set CEF to light or dark mode at runtime

Postby magreenblatt » Tue Jan 16, 2024 12:48 pm

magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Set CEF to light or dark mode at runtime

Postby atp15 » Mon Jan 29, 2024 4:02 am

For now, as an alternative, is there an easy way I can loop through all the CEF windows in the application and inject a CSS class?
atp15
Techie
 
Posts: 22
Joined: Thu Nov 17, 2016 5:25 am

Re: Set CEF to light or dark mode at runtime

Postby KatrinaS » Mon Jan 29, 2024 6:58 am

Get the Frame(s) throw a bunch of styles / CSS class in a String at it, or replace the href of the stylesheet,, both using ExecuteJavaScript :)
Untested but along the lines of :
Code: Select all
document.styleSheets[0].href='somestyles.css';

Or inject :
Code: Select all
const astyleelement=document.createElement('style');blah;blah;blah;document.head.appendChild(astyleelement);

Or inject a script Element referring to an external JavaScript file containing a single function and say call it :
Code: Select all
myDarkMode();

And that way you remove the hardcoded styles from your code and only have to tweak the external Function,, but regardless of what you throw at it, it *should* just all update :)
KatrinaS
Mentor
 
Posts: 83
Joined: Tue Jul 04, 2023 6:30 pm

Re: Set CEF to light or dark mode at runtime

Postby atp15 » Mon Jan 29, 2024 9:09 am

Thanks for your reply.
What I was more wondering was does CEF provide a convenient way of getting all the CEF browser windows so I use a for loop and then ExecuteJavaScript on them?
atp15
Techie
 
Posts: 22
Joined: Thu Nov 17, 2016 5:25 am

Re: Set CEF to light or dark mode at runtime

Postby KatrinaS » Mon Jan 29, 2024 9:36 am

I should think so, seems like something in browser_host_base.cc seems to provide a list that can eventually give you individuals, something like anyway :)
KatrinaS
Mentor
 
Posts: 83
Joined: Tue Jul 04, 2023 6:30 pm

Re: Set CEF to light or dark mode at runtime

Postby KatrinaS » Mon Jan 29, 2024 3:00 pm

Oh. Of course if you are creating the Clients / Frames / abstract whatever's why aren't you keeping a list of um :D
KatrinaS
Mentor
 
Posts: 83
Joined: Tue Jul 04, 2023 6:30 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 213 guests