Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Windows] Use dark title bar on dark system theme #110615

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 2 commits into from
Sep 2, 2022

Conversation

loic-sharma
Copy link
Member

@loic-sharma loic-sharma commented Aug 30, 2022

Ideally, the app should follow the system theme:

  1. App is aware of the system theme - The app and its window frame should be able to detect whether the system theme is in dark mode
  2. React to system changes - If you switch your system theme to dark mode, the app and its window frame should also be able to switch

This change makes Flutter apps have a dark title bar if Windows's theme is set to dark mode. This does nothing on older versions of Windows - like Windows 7 - that don't support dark mode.

Making the app's content follow the system theme will be fixed in subsequent changes and is tracked by #110700

Fixes #107926

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@flutter-dashboard flutter-dashboard bot added d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos documentation c: contributor-productivity Team-specific productivity, code health, technical debt. tool Affects the "flutter" command-line tool. See also t: labels. labels Aug 30, 2022
#include <flutter_windows.h>

#include "resource.h"

namespace {

#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
#endif
Copy link
Member Author

@loic-sharma loic-sharma Aug 30, 2022

Choose a reason for hiding this comment

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

Without these manual defines, we would need to raise the Windows SDK requirement as this attribute was added in a recent version (1, 2). Developers that haven't updated to the latest Windows SDK may not have these defined in their version of dwmapi.h. This also affects our CI machines.

Also see: https://docs.microsoft.com/windows/apps/desktop/modernize/apply-windows-themes#enable-a-dark-mode-title-bar-for-win32-applications

Copy link
Member

@cbracken cbracken Sep 1, 2022

Choose a reason for hiding this comment

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

Add a TODO to remove this define when we bump our Windows SDK on the bots.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry since this is code that lives on the developer's machine, this is going to be dependent on which SDK they have installed. This is probably fine as-is, but add a comment about why we're defining it here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a comment!

@loic-sharma loic-sharma marked this pull request as ready for review August 30, 2022 22:20
@tgucio
Copy link
Contributor

tgucio commented Aug 31, 2022

Just tried this and there's a problem in that the window bar will be dark if the system is using dark mode, regardless of whether the Flutter app actually uses dark theme or not (many apps typically allow to override the system setting).

There's a fair question here.
Another interesting bit is that Windows 10 has two separate settings under Personalization -> Colors:

  • Choose your default Windows mode
  • Choose your default app mode

With the changes from this PR, with the 1st set to dark and the 2nd set to light, I was still getting a dark window bar in the Flutter app.

@@ -126,6 +131,9 @@ bool Win32Window::Create(const std::wstring& title,
return false;
}

BOOL honorDarkMode = TRUE;
DwmSetWindowAttribute(window, DWMWA_USE_IMMERSIVE_DARK_MODE, &honorDarkMode, sizeof(honorDarkMode));
Copy link
Contributor

Choose a reason for hiding this comment

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

Does setting this window attribute allow the Flutter app context to use the specified dark mode theme instead of its default if provided, or does it affect only the part of the window outside of the Flutter app content?

Copy link
Member Author

@loic-sharma loic-sharma Aug 31, 2022

Choose a reason for hiding this comment

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

Good questions! I added more details to the pull request's description to clarify.

does it affect only the part of the window outside of the Flutter app content?

Yup, that's correct.

Does setting this window attribute allow the Flutter app context to use the specified dark mode theme instead

This will be done in a future change. We'll need to respond to Windows settings change message in the top-level message handler. EDIT: It looks like this feature was implemented but later regressed. Fix is tracked by: #110700

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@loic-sharma
Copy link
Member Author

loic-sharma commented Aug 31, 2022

Just tried this and there's a problem in that the window bar will be dark if the system is using dark mode, regardless of whether the Flutter app actually uses dark theme or not (many apps typically allow to override the system setting).

That's correct. Flutter doesn't have the concept of an app theme that's shared between the Dart code and the underlying native code. This is likely something we'll want to add in the future, but it's out-of-scope for this pull request. Apps that don't want to follow the system theme will need to remove this code.

With the changes from this PR, with the 1st set to dark and the 2nd set to light, I was still getting a dark window bar in the Flutter app.

Thanks for the heads up! I'll mark this PR as a draft while I investigate this.

@loic-sharma loic-sharma marked this pull request as draft August 31, 2022 16:44
@loic-sharma loic-sharma marked this pull request as ready for review September 2, 2022 18:06
@loic-sharma
Copy link
Member Author

@tgucio @yaakovschectman @cbracken Please take another look! :)

Copy link
Member

@cbracken cbracken left a comment

Choose a reason for hiding this comment

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

LGTM stamp from a Japanese personal seal

@loic-sharma loic-sharma added the autosubmit Merge PR when tree becomes green via auto submit App label Sep 2, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 4, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/plugins that referenced this pull request Sep 4, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 4, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 4, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 5, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 5, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 5, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 5, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 6, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 6, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 6, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 6, 2022
@loic-sharma loic-sharma restored the windows_dark_title branch September 6, 2022 18:31
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 6, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App c: contributor-productivity Team-specific productivity, code health, technical debt. d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos tool Affects the "flutter" command-line tool. See also t: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dark theme in Windows 10 has a light title bar
5 participants