-
Notifications
You must be signed in to change notification settings - Fork 15k
/
dark_mode.cc
79 lines (60 loc) · 2.44 KB
/
dark_mode.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 2022 Microsoft Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE-CHROMIUM file.
#include "shell/browser/win/dark_mode.h"
#include <dwmapi.h> // DwmSetWindowAttribute()
#include "base/win/windows_version.h"
#include "electron/fuses.h"
#define DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 19
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
// This namespace contains code originally from
// https://github.com/microsoft/terminal
// governed by the MIT license and (c) Microsoft Corporation.
namespace {
// Use undocumented flags to set window theme on Windows 10
HRESULT TrySetWindowThemeOnWin10(HWND hWnd, bool dark) {
const BOOL isDarkMode = dark;
if (FAILED(DwmSetWindowAttribute(hWnd, DWMWA_USE_IMMERSIVE_DARK_MODE,
&isDarkMode, sizeof(isDarkMode)))) {
HRESULT result =
DwmSetWindowAttribute(hWnd, DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1,
&isDarkMode, sizeof(isDarkMode));
if (FAILED(result))
return result;
}
// Toggle the nonclient area active state to force a redraw (Win10 workaround)
HWND activeWindow = GetActiveWindow();
SendMessage(hWnd, WM_NCACTIVATE, hWnd != activeWindow, 0);
SendMessage(hWnd, WM_NCACTIVATE, hWnd == activeWindow, 0);
return S_OK;
}
// https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
HRESULT TrySetWindowTheme(HWND hWnd, bool dark) {
const BOOL isDarkMode = dark;
return DwmSetWindowAttribute(hWnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &isDarkMode,
sizeof(isDarkMode));
}
} // namespace
namespace electron {
namespace win {
bool IsDarkModeSupported() {
auto* os_info = base::win::OSInfo::GetInstance();
auto const version = os_info->version();
return version >= base::win::Version::WIN11 ||
(version >= base::win::Version::WIN10 &&
electron::fuses::IsWindows10ImmersiveDarkModeEnabled());
}
void SetDarkModeForWindow(HWND hWnd) {
ui::NativeTheme* theme = ui::NativeTheme::GetInstanceForNativeUi();
bool dark =
theme->ShouldUseDarkColors() && !theme->UserHasContrastPreference();
auto* os_info = base::win::OSInfo::GetInstance();
auto const version = os_info->version();
if (version >= base::win::Version::WIN11) {
TrySetWindowTheme(hWnd, dark);
} else {
TrySetWindowThemeOnWin10(hWnd, dark);
}
}
} // namespace win
} // namespace electron