8000 feat: honor `nativeTheme.themeSource = 'dark'` before creating BrowserWindow on Windows by ckerr · Pull Request #25373 · electron/electron · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: honor nativeTheme.themeSource = 'dark' before creating BrowserWindow on Windows #25373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 28, 2020

Conversation

ckerr
Copy link
Member
@ckerr ckerr commented Sep 8, 2020

Description of Change

Fixes #23479.

With this patch, setting nativeTheme.themeSource = 'dark' or 'light' before creating the window is honored and the window will be rendered with the correct colors when shown.

As a side-effect of this change, the setting themeSource dynamically now works on Linux too.

There is still a wart: Changing themeSource dynamically when the window is already shown isn't perfect: the property is set, but the window still has to be poked before the change takes effect, e.g. changing focus or minimizing/restoring the window. The usual programmatic ways of forcing a window refresh don't seem to work here; perhaps this is my error but let's see about getting guidance for the right recipe to use.

This PR adds a lot of scaffolding to pull in the undocumented Win32 dark mode APIs. Much of this ground is covered by https://github.com/ysc3839/win32-darkmode, which is license-compatible (MIT).

TODO:

  • Can the titlebar issue described above be fixed?
  • win\dark_mode.cc is really big. Audit it to see if any can be removed

CC @MarshallOfSound @electron/wg-releases

Checklist

Release Notes

Notes: Improved dark mode support on Windows.

@electron-cation electron-cation bot added the new-pr 🌱 PR opened recently label Sep 8, 2020
@ckerr ckerr marked this pull request as draft September 8, 2020 15:16
@MarshallOfSound MarshallOfSound changed the title fix: better support of dark mode on windows feat: better support of dark mode on windows Sep 8, 2020
@MarshallOfSound MarshallOfSound added the semver/minor backwards-compatible functionality label Sep 8, 2020
Copy link
Member
@MarshallOfSound MarshallOfSound left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a tricky one because it "fixes" a thing that I would argue isn't expected behavior. On Windows there are maybe 3 apps that do this and all of them are windows built-in. The private API this utilizes to do this is super hacky, apparently depends on exact versions / builds of Windows and in theory could break at any time.

If there isn't a public API for this maybe we should ask for one? It doesn't feel right adding this amount of private API complexity to do a thing that by most definitions isn't expected behavior (and speaking for one app in particular we'd want to opt out of)

@electron-cation electron-cation bot removed the new-pr 🌱 PR opened recently label Sep 9, 2020
@ckerr
Copy link
Member Author
ckerr commented Oct 2, 2020

@MarshallOfSound it looks like we'll be using undocumented API if we want this to work in the short term. I agree this is a lot of code for a small amount of functionality -- but at the same time, I think having this feature work is still worthwhile. What if I shave down all the code to only the minimum subset that we need, and then we see how large that patch is?

@MarshallOfSound
Copy link
Member

@ckerr I'm really not a fan of this much private API usage. If this were to land against my recommendations the minimum requirements would be:

  • We've done our damndest to reduce the private API usage by as much as possible, the smaller the surface area the better
  • The code should at least be vaguely readable, it's almost there atm but it's still a lot of _ and magic enums
  • This feature should 100% be disabled by default (opt-in) and no code path should call these APIs unless the individual app has opted in

@samuelmaddock
Copy link
Member

If this feature were to land, would it allow apps to forcefully apply dark mode? As opposed to applying it only when the user has chosen that OS theme.

Aside from the issue of private API usage, this would be a nice addition for apps which are primarily dark in aesthetics, but aren't using a frameless window.

@ckerr
Copy link
Member Author
ckerr commented Oct 13, 2020
  • We've done our damndest to reduce the private API usage by as much as possible, the smaller the surface area the better

I've removed everything not necessary to fix the titlebar issue reported in #23479. This revised patch only loads enough API to determine how to see if dark mode is supported, if it's allowed, and how to set it for newly-created windows. dark_mode.cc is now about 60% smaller despite having new comments to address the next point --

  • The code should at least be vaguely readable, it's almost there atm but it's still a lot of _ and magic enums

I've tried to eliminate confusing code where possible, added explanatory comments, limited variables' scope, and moved variables to be declared just before use. Looking up function pointers from the dlls is still verbose -- you have to declare the function type, then declare a variable, and then do the lookup -- but I've added some template helper code to make it more readable.

  • This feature should 100% be disabled by default (opt-in) and no code path should call these APIs unless the individual app has opted in

I'm not sure this is a good idea -- it doesn't seem productive to require users to opt-in to a bugfix? This fixes an existing code in our dark mode:

nativeTheme.themeSource = 'dark';
new BrowserWindow(...)
// windows' titlebar will not be dark

@ckerr
Copy link
Member Author
ckerr commented Oct 13, 2020

If this feature were to land, would it allow apps to forcefully apply dark mode? As opposed to applying it only when the user has chosen that OS theme.

Yes it would. The use case is literally setting nativeTheme.themeSource = 'dark'; before creating a window. :)

@ckerr ckerr marked this pull request as ready for review October 13, 2020 04:37
@ckerr ckerr changed the title feat: better support of dark mode on windows feat: honor nativeTheme.themeSource = 'dark' before creating BrowserWindow on Windows Oct 21, 2020
@ckerr ckerr requested a review from a team as a code owner October 24, 2020 20:53
@ckerr ckerr force-pushed the ckerr/master/win32-darkmode branch from c4b9e8e to 8e96c90 Compare October 25, 2020 19:29
@ckerr ckerr requested review from nornagon and a team as code owners October 25, 2020 19:29
@ckerr ckerr force-pushed the ckerr/master/win32-darkmode branch 2 times, most recently from fcca81c to d432f76 Compare October 26, 2020 20:40
Copy link
Member
@MarshallOfSound MarshallOfSound left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One missed replacement, other than that :shipit:

@ckerr ckerr removed the request for review from nornagon October 26, 2020 22:55
@ckerr
Copy link
Member Author
ckerr commented Oct 28, 2020

AssertionError: expected 'visible' to equal 'hidden'
at Context. (electron/spec-main/visibility-state-spec.ts:77:22)
at Context. (electron/spec-main/visibility-state-spec.ts:31:7)

mas CI failures are unrelated

@ckerr ckerr merged commit f489e30 into master Oct 28, 2020
@release-clerk
Copy link
release-clerk bot commented Oct 28, 2020

Release Notes Persisted

Improved dark mode support on Windows.

@ckerr ckerr deleted the ckerr/master/win32-darkmode branch October 28, 2020 20:00
@trop
Copy link
Contributor
trop bot commented Oct 28, 2020

I was unable to backport this PR to "10-x-y" cleanly;
you will need to perform this backport manually.

@trop trop bot removed the target/10-x-y label Oct 28, 2020
@trop
Copy link
Contributor
trop bot commented Oct 28, 2020

I was unable to backport this PR to "11-x-y" cleanly;
you will need to perform this backport manually.

ckerr added a commit that referenced this pull request Oct 28, 2020
ckerr added a commit that referenced this pull request Oct 28, 2020
@trop
Copy link
Contributor
trop bot commented Oct 28, 2020

@ckerr has manually backported this PR to "10-x-y", please check out #26237

@trop
Copy link
Contributor
trop bot commented Oct 28, 2020

@ckerr has manually backported this PR to "11-x-y", please check out #26238

jkleinsc pushed a commit that referenced this pull request Oct 29, 2020
zcbenz pushed a commit that referenced this pull request Nov 2, 2020
@gpetrov
Copy link
gpetrov commented Apr 23, 2021

Can we have this in production now without the flag?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Windows] Title bar does not respect dark mode
5 participants
0