electron: [Bug]: nativeTheme API not working on Fedora Workstation 36 / Gnome 42
Preflight Checklist
- I have read the Contributing Guidelines for this project.
- I agree to follow the Code of Conduct that this project adheres to.
- I have searched the issue tracker for a bug report that matches the one I want to file, without success.
Electron Version
17.1.2
What operating system are you using?
Other Linux
Operating System Version
Fedora Linux 36 (Workstation Edition Prerelease)
What arch are you using?
x64
Last Known Working Electron version
No response
Expected Behavior
nativeTheme should reflect the light/dark scheme configured in the OS
Actual Behavior
nativeTheme.shouldUseDarkColors
is always false.
Testcase Gist URL
No response
Additional Information
From https://github.com/microsoft/vscode/issues/146804
git clone https://github.com/aeschli/electron-theme-test.git
cd electron-theme-test
,npm i && npm start
- a simple electron window opens
- switch between light and dark in the OS
- the window should change the menu bar colors and the terminal should print
shouldUseDarkColors
matching the OS scheme.
Terminal output
> burly-rate-begin-iqsp1@1.0.0 start
> electron-forge start
✔ Checking your system
✔ Locating Application
✔ Preparing native dependencies
✔ Launching Application
shouldUseDarkColors: false, shouldUseHighContrastColors: false, shouldUseInvertedColorScheme: false
shouldUseDarkColors: false, shouldUseHighContrastColors: false, shouldUseInvertedColorScheme: false
libva error: vaGetDriverNameByIndex() failed with unknown libva error, driver_name = (null)
[4553:0406/110408.146875:ERROR:sandbox_linux.cc(377)] InitializeSandbox() called with multiple threads in process gpu-process.
[4553:0406/110412.166946:ERROR:gl_surface_presentation_helper.cc(260)] GetVSyncParametersIfAvailable() failed for 1 times!
[4553:0406/110449.772046:ERROR:gl_surface_presentation_helper.cc(260)] GetVSyncParametersIfAvailable() failed for 2 times!
[4553:0406/110449.783890:ERROR:gl_surface_presentation_helper.cc(260)] GetVSyncParametersIfAvailable() failed for 3 times!
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 38
- Comments: 27 (4 by maintainers)
The upstream issue in Chromium has been fixed today, at long last. They have implemented a new
DarkModeManagerLinux
view that monitors the FreeDesktop settings portal, and falls back to GTK/QT theme monitoring for systems where the portal is not supported. As far as I can see, this means that the current workaround for GTK themes in the nativeTheme API of Electron is no longer needed.@q234rty is correct. Chromium 114 did fix the prefers-color-scheme query for newer distros that implement the XDG Settings portal. You can test it yourself by using a suitable desktop environment (e.g. Fedora 38 / GNOME 44), downloading the latest Chrome/Chromium, opening this site, and toggling the dark mode setting in the desktop environment’s settings. The style of the page should change immediately.
However, since Electron 25 still uses the aforementioned workaround, which is designed for older Linux distributions and older Chromium, the
nativeTheme
API cannot possibly work as expected for newer systems that rely solely on the Settings portal, since Electron overrides Chromium’s (now correct) current behaviour.Reproduction steps
I have updated the reproduction repository example by @aeschli.
Prerequisities: use Fedora 38 / GNOME 44
What should happen
shouldUseDarkColors: false
will be printed in the terminalshouldUseDarkColors: true
will be printed in the terminalWhat actually happens
The window has black text on a white background at all times, and does not respond to the system’s preference. Only
shouldUseDarkColors: false
is printed to the terminal.Here’s also an example screenshot of the erroneous behaviour. As can be seen, the dark preference is enabled in the Fedora 38 system, but the Electron window does not reflect this.
Fix
To fix this, Electron must remove the previously mentioned workaround. Chromium now handles the
prefers-color-scheme
media query in a backwards-compatible manner, which means that it should work for both older distros/apps (that rely on the selected GTK/Qt theme) and newer distros/apps (that rely on the XDG Settings portal preference). Electron no longer needs to provide special handling for Linux distros in thenativeTheme
API.In this context, “old distros” are basically any distros that still rely on GTK themes for light/dark preference. This includes GNOME 41 or older (example: Ubuntu 18.04) and all currently supported Plasma releases (example: Kubuntu 22.04).
GNOME implemented the XDG Settings portal in GNOME 42 . Distros that have 42 or a newer version are “new distros” in this context (examples: Fedora 36+, Ubuntu 22.04).
(Note: KDE technically implemented the XDG Settings portal in Plasma 5.24. However, if you toggle between light and dark appearance in the system settings, it currently still changes the GTK theme under the hood.)
@codebytere Can you please reopen the issue, as this is still unresolved and reproducible on the latest Electron? This was inadvertently closed as a duplicate a few months ago. You marked 36040 as a duplicate of this issue (33635), so this issue should have stayed open as it is the earliest report of the problem.
Same issue on Fedora 37 beta with Gnome 43.
@codebytere Can this issue be reopened?
I tested on Fedora Linux 38 (Workstation Edition), Gnome 44.3, Wayland Electron 26.1.0, Node.js 18.16.1, Chromium 116.0.5845.97
git clone https://github.com/aeschli/electron-theme-test.git
cd electron-theme-test, npm i && npm start
Expected:
shouldUseDarkColors: false
when the OS uses a light color scheme, orshouldUseDarkColors: true
if the OS color scheme is darkActual:
Always
shouldUseDarkColors: false,
is printed, even when the OS color scheme is dark.In the OS settings, switch between light and dark while the window is still open
Expected:
Actual:
Test running under Wayland:
cd electron-theme-test, npm i && npm start-wayland
Same problem.
This ticket was actually filed by a Google developer. Freedesktop color-scheme is a recent addition, so it’s not that surprising that developers are not aware of it.
It is nice to see an interest from a Chromium developer in improving the situation of dark mode control for Linux users. Maybe there’s hope.
There is now an unified way to get the current dark mode in Linux by querying or watching with DBus org.freedesktop.appearance color-scheme. Works in both latest KDE and Gnome 42+. You can check it out with
Dark mode in Gnome 42+ can be set with Settings -> Appearance -> Style. In KDE 5.24+ it takes effect when you change the UI colors from Settings -> Appearance -> Colors.
Some links about it: About Gnome implementation: https://blogs.gnome.org/alexm/2021/10/04/dark-style-preference/ The setting in xdg-desktop-portal: https://github.com/flatpak/xdg-desktop-portal/blob/main/data/org.freedesktop.portal.Settings.xml Firefox already supports it: https://bugzilla.mozilla.org/show_bug.cgi?id=1734934
Here are the possible values:
@codebytere I believe you closed this issue by accident. Please reopen it
This issue is #33635. Was this one closed by accident?
I’m not able to reproduce this when running the test repo (Electron 17.1.2) on Ubuntu 22.04, which like Fedora 36 is also running GNOME 42. Wayland graphics. When I load System settings, go to Appearance, and click “Light” or “Dark”, Electron’s theme updates correctly along with the rest of the desktop.
I’ll try installing Fedora 36 to see if that changes things.
As an aside, I do see that the console log is triggered twice each time I toggle between light and dark:
I’m mentioning it here since It’s possible that this is relevant to the bug and that second event is needed to make Electron’s theme change. It’s also possible that, this is a red herring, that the testcase registered for the event twice, or that Electron is generating unnecessary duplicate events 👀 :
I’ll try installing Fedora 36 to see if that helps in reproducing this issue.
@codebytere Giving this another try. Can you please re-open this issue? It was closed by accident.
The work around posted here worked for me:
Gnome Tweaks -> Appearance -> Themes, Legacy Application -> Select Dark theme (Adwaita-dark)
I have also figured out for myself a way to script this:
The first line is how Fedora 36 seems to “generally” handle dark mode, while the second covers those edge cases like vscode / electron.
Combining with https://github.com/electron/electron/issues/33635
@remlse But this will not follow the system theme change. Although it’s possible to write a script that would listen to the DBus theme change event and change it globally.
New install of Fedora 36 and I see this issue as well. Specifically with VS Code 1.67.2. Came here from: https://github.com/microsoft/vscode/issues/146804
Problem solved in Chinese Communist Party ways.@bash Night Theme Switcher can do this, too. But it’s somewhat cumbersome: Its main purpose is to switch to dark mode automatically, and you can’t turn this off officially. If you don’t want to have an automatic schedule, you can set the night schedule to 5:00 to 5:00 or similar, so it’s effectively always light mode, or you can set the night schedule to 5:01 to 5:00, so it’s effectively always dark-mode.
It’s apparent that we won’t be getting a real fix…
Note: the video doesn’t work (here in my Firefox). “Video can’t be played because the file is corrupt.”
I also use a ready-made extension for GNOME 42 which allows to force a theme switch for legacy applications (this is disabled by default in the extension settings) https://gitlab.com/rmnvgr/nightthemeswitcher-gnome-shell-extension I definitely recommend it to those who want to have real theme switching in GNOME 42 like in macOS.