Skip to content

Commit 5b583c8

Browse files
trop[bot]mlaurencindeermicheldsanders11
authored
feat: add immersive dark mode on windows (#34549)
feat: add immersive dark mode on windows (#33624) * feat: add immersive dark mode * fix syntax and add header * add me * Update fuses.json5 * fix: redraw title bar on dark mode change * chore: SetWindowTheme doesn't seem to be needed * chore: separate out Win 10 dark mode implementation * final touches * final touches * chore: limit Win 10 to >= 20H1 and drop fuse * fix types * fix lint Co-authored-by: Micha Hanselmann <micha.hanselmann@gmail.com> Co-authored-by: David Sanders <dsanders11@ucsbalum.com> Co-authored-by: Michaela Laurencin <35157522+mlaurencin@users.noreply.github.com> Co-authored-by: Micha Hanselmann <micha.hanselmann@gmail.com> Co-authored-by: David Sanders <dsanders11@ucsbalum.com>
1 parent c20d6e5 commit 5b583c8

10 files changed

Lines changed: 52 additions & 179 deletions

File tree

BUILD.gn

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -727,14 +727,6 @@ source_set("electron_lib") {
727727

728728
sources += get_target_outputs(":electron_fuses")
729729

730-
if (is_win && enable_win_dark_mode_window_ui) {
731-
sources += [
732-
"shell/browser/win/dark_mode.cc",
733-
"shell/browser/win/dark_mode.h",
734-
]
735-
libs += [ "uxtheme.lib" ]
736-
}
737-
738730
if (allow_runtime_configurable_key_storage) {
739731
defines += [ "ALLOW_RUNTIME_CONFIGURABLE_KEY_STORAGE" ]
740732
}

buildflags/BUILD.gn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ buildflag_header("buildflags") {
1919
"ENABLE_ELECTRON_EXTENSIONS=$enable_electron_extensions",
2020
"ENABLE_BUILTIN_SPELLCHECKER=$enable_builtin_spellchecker",
2121
"ENABLE_PICTURE_IN_PICTURE=$enable_picture_in_picture",
22-
"ENABLE_WIN_DARK_MODE_WINDOW_UI=$enable_win_dark_mode_window_ui",
2322
"OVERRIDE_LOCATION_PROVIDER=$enable_fake_location_provider",
2423
]
2524
}

buildflags/buildflags.gni

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,4 @@ declare_args() {
3131

3232
# Enable Spellchecker support
3333
enable_builtin_spellchecker = true
34-
35-
# Undocumented Windows dark mode API
36-
enable_win_dark_mode_window_ui = false
3734
}

filenames.gni

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ filenames = {
105105
"shell/browser/ui/win/notify_icon.h",
106106
"shell/browser/ui/win/taskbar_host.cc",
107107
"shell/browser/ui/win/taskbar_host.h",
108+
"shell/browser/win/dark_mode.cc",
109+
"shell/browser/win/dark_mode.h",
108110
"shell/browser/win/scoped_hstring.cc",
109111
"shell/browser/win/scoped_hstring.h",
110112
"shell/common/api/electron_api_native_image_win.cc",

shell/browser/ui/win/electron_desktop_window_tree_host_win.cc

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@
77
#include "base/win/windows_version.h"
88
#include "electron/buildflags/buildflags.h"
99
#include "shell/browser/ui/views/win_frame_view.h"
10+
#include "shell/browser/win/dark_mode.h"
1011
#include "ui/base/win/hwnd_metrics.h"
1112
#include "ui/base/win/shell.h"
1213

13-
#if BUILDFLAG(ENABLE_WIN_DARK_MODE_WINDOW_UI)
14-
#include "shell/browser/win/dark_mode.h"
15-
#endif
16-
1714
namespace electron {
1815

1916
ElectronDesktopWindowTreeHostWin::ElectronDesktopWindowTreeHostWin(
@@ -29,14 +26,13 @@ bool ElectronDesktopWindowTreeHostWin::PreHandleMSG(UINT message,
2926
WPARAM w_param,
3027
LPARAM l_param,
3128
LRESULT* result) {
32-
#if BUILDFLAG(ENABLE_WIN_DARK_MODE_WINDOW_UI)
33-
if (message == WM_NCCREATE) {
34-
HWND const hwnd = GetAcceleratedWidget();
35-
auto const theme_source =
36-
ui::NativeTheme::GetInstanceForNativeUi()->theme_source();
37-
win::SetDarkModeForWindow(hwnd, theme_source);
29+
const bool dark_mode_supported = win::IsDarkModeSupported();
30+
if (dark_mode_supported && message == WM_NCCREATE) {
31+
win::SetDarkModeForWindow(GetAcceleratedWidget());
32+
ui::NativeTheme::GetInstanceForNativeUi()->AddObserver(this);
33+
} else if (dark_mode_supported && message == WM_DESTROY) {
34+
ui::NativeTheme::GetInstanceForNativeUi()->RemoveObserver(this);
3835
}
39-
#endif
4036

4137
return native_window_view_->PreHandleMSG(message, w_param, l_param, result);
4238
}
@@ -99,4 +95,9 @@ bool ElectronDesktopWindowTreeHostWin::GetClientAreaInsets(
9995
return false;
10096
}
10197

98+
void ElectronDesktopWindowTreeHostWin::OnNativeThemeUpdated(
99+
ui::NativeTheme* observed_theme) {
100+
win::SetDarkModeForWindow(GetAcceleratedWidget());
101+
}
102+
102103
} // namespace electron

shell/browser/ui/win/electron_desktop_window_tree_host_win.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
namespace electron {
1414

15-
class ElectronDesktopWindowTreeHostWin
16-
: public views::DesktopWindowTreeHostWin {
15+
class ElectronDesktopWindowTreeHostWin : public views::DesktopWindowTreeHostWin,
16+
public ::ui::NativeThemeObserver {
1717
public:
1818
ElectronDesktopWindowTreeHostWin(
1919
NativeWindowViews* native_window_view,
@@ -37,6 +37,9 @@ class ElectronDesktopWindowTreeHostWin
3737
bool GetClientAreaInsets(gfx::Insets* insets,
3838
HMONITOR monitor) const override;
3939

40+
// ui::NativeThemeObserver:
41+
void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override;
42+
4043
private:
4144
NativeWindowViews* native_window_view_; // weak ref
4245
};

shell/browser/win/dark_mode.cc

Lines changed: 30 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,178 +1,62 @@
1-
// Copyright (c) 2020 Microsoft Inc. All rights reserved.
1+
// Copyright (c) 2022 Microsoft Inc. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE-CHROMIUM file.
44

55
#include "shell/browser/win/dark_mode.h"
66

77
#include <dwmapi.h> // DwmSetWindowAttribute()
88

9-
#include "base/files/file_path.h"
10-
#include "base/scoped_native_library.h"
11-
#include "base/win/pe_image.h"
12-
#include "base/win/win_util.h"
139
#include "base/win/windows_version.h"
1410

11+
// This flag works since Win10 20H1 but is not documented until Windows 11
12+
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
13+
1514
// This namespace contains code originally from
16-
// https://github.com/ysc3839/win32-darkmode/
17-
// governed by the MIT license and (c) Richard Yu
15+
// https://github.com/microsoft/terminal
16+
// governed by the MIT license and (c) Microsoft Corporation.
1817
namespace {
1918

20-
// 1903 18362
21-
enum PreferredAppMode { Default, AllowDark, ForceDark, ForceLight, Max };
22-
23-
bool g_darkModeSupported = false;
24-
bool g_darkModeEnabled = false;
25-
DWORD g_buildNumber = 0;
26-
27-
enum WINDOWCOMPOSITIONATTRIB {
28-
WCA_USEDARKMODECOLORS = 26 // build 18875+
29-
};
30-
struct WINDOWCOMPOSITIONATTRIBDATA {
31-
WINDOWCOMPOSITIONATTRIB Attrib;
32-
PVOID pvData;
33-
SIZE_T cbData;
34-
};
35-
36-
using fnSetWindowCompositionAttribute =
37-
BOOL(WINAPI*)(HWND hWnd, WINDOWCOMPOSITIONATTRIBDATA*);
38-
fnSetWindowCompositionAttribute _SetWindowCompositionAttribute = nullptr;
39-
40-
bool IsHighContrast() {
41-
HIGHCONTRASTW highContrast = {sizeof(highContrast)};
42-
if (SystemParametersInfoW(SPI_GETHIGHCONTRAST, sizeof(highContrast),
43-
&highContrast, FALSE))
44-
return highContrast.dwFlags & HCF_HIGHCONTRASTON;
45-
return false;
46-
}
19+
// https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
20+
HRESULT TrySetWindowTheme(HWND hWnd, bool dark) {
21+
const BOOL isDarkMode = dark;
22+
HRESULT result = DwmSetWindowAttribute(hWnd, DWMWA_USE_IMMERSIVE_DARK_MODE,
23+
&isDarkMode, sizeof(isDarkMode));
4724

48-
void RefreshTitleBarThemeColor(HWND hWnd, bool dark) {
49-
LONG ldark = dark;
50-
if (g_buildNumber >= 20161) {
51-
// DWMA_USE_IMMERSIVE_DARK_MODE = 20
52-
DwmSetWindowAttribute(hWnd, 20, &ldark, sizeof dark);
53-
return;
54-
}
55-
if (g_buildNumber >= 18363) {
56-
auto data = WINDOWCOMPOSITIONATTRIBDATA{WCA_USEDARKMODECOLORS, &ldark,
57-
sizeof ldark};
58-
_SetWindowCompositionAttribute(hWnd, &data);
59-
return;
60-
}
61-
DwmSetWindowAttribute(hWnd, 0x13, &ldark, sizeof ldark);
62-
}
25+
if (FAILED(result))
26+
return result;
6327

64-
void InitDarkMode() {
65-
// confirm that we're running on a version of Windows
66-
// where the Dark Mode API is known
6728
auto* os_info = base::win::OSInfo::GetInstance();
68-
g_buildNumber = os_info->version_number().build;
6929
auto const version = os_info->version();
70-
if ((version < base::win::Version::WIN10_RS5) ||
71-
(version > base::win::Version::WIN10_20H1)) {
72-
return;
73-
}
74-
75-
// load "SetWindowCompositionAttribute", used in RefreshTitleBarThemeColor()
76-
_SetWindowCompositionAttribute =
77-
reinterpret_cast<decltype(_SetWindowCompositionAttribute)>(
78-
base::win::GetUser32FunctionPointer("SetWindowCompositionAttribute"));
79-
if (_SetWindowCompositionAttribute == nullptr) {
80-
return;
81-
}
82-
83-
// load the dark mode functions from uxtheme.dll
84-
// * RefreshImmersiveColorPolicyState()
85-
// * ShouldAppsUseDarkMode()
86-
// * AllowDarkModeForApp()
87-
// * SetPreferredAppMode()
88-
// * AllowDarkModeForApp() (build < 18362)
89-
// * SetPreferredAppMode() (build >= 18362)
90-
91-
base::NativeLibrary uxtheme =
92-
base::PinSystemLibrary(FILE_PATH_LITERAL("uxtheme.dll"));
93-
if (!uxtheme) {
94-
return;
95-
}
96-
auto ux_pei = base::win::PEImage(uxtheme);
97-
auto get_ux_proc_from_ordinal = [&ux_pei](int ordinal, auto* setme) {
98-
FARPROC proc = ux_pei.GetProcAddress(reinterpret_cast<LPCSTR>(ordinal));
99-
*setme = reinterpret_cast<decltype(*setme)>(proc);
100-
};
101-
102-
// ordinal 104
103-
using fnRefreshImmersiveColorPolicyState = VOID(WINAPI*)();
104-
fnRefreshImmersiveColorPolicyState _RefreshImmersiveColorPolicyState = {};
105-
get_ux_proc_from_ordinal(104, &_RefreshImmersiveColorPolicyState);
106-
107-
// ordinal 132
108-
using fnShouldAppsUseDarkMode = BOOL(WINAPI*)();
109-
fnShouldAppsUseDarkMode _ShouldAppsUseDarkMode = {};
110-
get_ux_proc_from_ordinal(132, &_ShouldAppsUseDarkMode);
111-
112-
// ordinal 135, in 1809
113-
using fnAllowDarkModeForApp = BOOL(WINAPI*)(BOOL allow);
114-
fnAllowDarkModeForApp _AllowDarkModeForApp = {};
115-
116-
// ordinal 135, in 1903
117-
typedef PreferredAppMode(WINAPI *
118-
fnSetPreferredAppMode)(PreferredAppMode appMode);
119-
fnSetPreferredAppMode _SetPreferredAppMode = {};
120-
121-
if (g_buildNumber < 18362) {
122-
get_ux_proc_from_ordinal(135, &_AllowDarkModeForApp);
123-
} else {
124-
get_ux_proc_from_ordinal(135, &_SetPreferredAppMode);
125-
}
12630

127-
// dark mode is supported iff we found the functions
128-
g_darkModeSupported = _RefreshImmersiveColorPolicyState &&
129-
_ShouldAppsUseDarkMode &&
130-
(_AllowDarkModeForApp || _SetPreferredAppMode);
131-
if (!g_darkModeSupported) {
132-
return;
31+
// Toggle the nonclient area active state to force a redraw (Win10 workaround)
32+
if (version < base::win::Version::WIN11) {
33+
HWND activeWindow = GetActiveWindow();
34+
SendMessage(hWnd, WM_NCACTIVATE, hWnd != activeWindow, 0);
35+
SendMessage(hWnd, WM_NCACTIVATE, hWnd == activeWindow, 0);
13336
}
13437

135-
// initial setup: allow dark mode to be used
136-
if (_AllowDarkModeForApp) {
137-
_AllowDarkModeForApp(true);
138-
} else if (_SetPreferredAppMode) {
139-
_SetPreferredAppMode(AllowDark);
140-
}
141-
_RefreshImmersiveColorPolicyState();
142-
143-
// check to see if dark mode is currently enabled
144-
g_darkModeEnabled = _ShouldAppsUseDarkMode() && !IsHighContrast();
38+
return S_OK;
14539
}
14640

14741
} // namespace
14842

14943
namespace electron {
15044

151-
void EnsureInitialized() {
152-
static bool initialized = false;
153-
if (!initialized) {
154-
initialized = true;
155-
::InitDarkMode();
156-
}
157-
}
45+
namespace win {
15846

159-
bool IsDarkPreferred(ui::NativeTheme::ThemeSource theme_source) {
160-
switch (theme_source) {
161-
case ui::NativeTheme::ThemeSource::kForcedLight:
162-
return false;
163-
case ui::NativeTheme::ThemeSource::kForcedDark:
164-
return g_darkModeSupported;
165-
case ui::NativeTheme::ThemeSource::kSystem:
166-
return g_darkModeEnabled;
167-
}
47+
bool IsDarkModeSupported() {
48+
auto* os_info = base::win::OSInfo::GetInstance();
49+
auto const version = os_info->version();
50+
51+
return version >= base::win::Version::WIN10_20H1;
16852
}
16953

170-
namespace win {
54+
void SetDarkModeForWindow(HWND hWnd) {
55+
ui::NativeTheme* theme = ui::NativeTheme::GetInstanceForNativeUi();
56+
bool dark =
57+
theme->ShouldUseDarkColors() && !theme->UserHasContrastPreference();
17158

172-
void SetDarkModeForWindow(HWND hWnd,
173-
ui::NativeTheme::ThemeSource theme_source) {
174-
EnsureInitialized();
175-
RefreshTitleBarThemeColor(hWnd, IsDarkPreferred(theme_source));
59+
TrySetWindowTheme(hWnd, dark);
17660
}
17761

17862
} // namespace win

shell/browser/win/dark_mode.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020 Microsoft Inc. All rights reserved.
1+
// Copyright (c) 2022 Microsoft Inc. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE-CHROMIUM file.
44

@@ -19,7 +19,8 @@ namespace electron {
1919

2020
namespace win {
2121

22-
void SetDarkModeForWindow(HWND hWnd, ui::NativeTheme::ThemeSource theme_source);
22+
bool IsDarkModeSupported();
23+
void SetDarkModeForWindow(HWND hWnd);
2324

2425
} // namespace win
2526

shell/common/api/features.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ bool IsPictureInPictureEnabled() {
5454
return BUILDFLAG(ENABLE_PICTURE_IN_PICTURE);
5555
}
5656

57-
bool IsWinDarkModeWindowUiEnabled() {
58-
return BUILDFLAG(ENABLE_WIN_DARK_MODE_WINDOW_UI);
59-
}
60-
6157
bool IsComponentBuild() {
6258
#if defined(COMPONENT_BUILD)
6359
return true;
@@ -84,7 +80,6 @@ void Initialize(v8::Local<v8::Object> exports,
8480
dict.SetMethod("isPictureInPictureEnabled", &IsPictureInPictureEnabled);
8581
dict.SetMethod("isComponentBuild", &IsComponentBuild);
8682
dict.SetMethod("isExtensionsEnabled", &IsExtensionsEnabled);
87-
dict.SetMethod("isWinDarkModeWindowUiEnabled", &IsWinDarkModeWindowUiEnabled);
8883
}
8984

9085
} // namespace

typings/internal-ambient.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ declare namespace NodeJS {
2727
isPictureInPictureEnabled(): boolean;
2828
isExtensionsEnabled(): boolean;
2929
isComponentBuild(): boolean;
30-
isWinDarkModeWindowUiEnabled(): boolean;
3130
}
3231

3332
interface IpcRendererBinding {

0 commit comments

Comments
 (0)