Skip to content

Commit 7260319

Browse files
committed
fix(windows): fix TitleBar buttons does not display correctly #415
1 parent 8fa23db commit 7260319

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

windows/window_manager.cpp

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,26 @@
2828
#define STATE_FULLSCREEN_ENTERED 3
2929
#define STATE_DOCKED 4
3030

31-
#define DWMWA_USE_IMMERSIVE_DARK_MODE 19
31+
/// Window attribute that enables dark mode window decorations.
32+
///
33+
/// Redefined in case the developer's machine has a Windows SDK older than
34+
/// version 10.0.22000.0.
35+
/// See:
36+
/// https://docs.microsoft.com/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
37+
#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
38+
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
39+
#endif
40+
41+
constexpr const wchar_t kWindowClassName[] = L"FLUTTER_RUNNER_WIN32_WINDOW";
42+
43+
/// Registry key for app theme preference.
44+
///
45+
/// A value of 0 indicates apps should use dark mode. A non-zero or missing
46+
/// value indicates apps should use light mode.
47+
constexpr const wchar_t kGetPreferredBrightnessRegKey[] =
48+
L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
49+
constexpr const wchar_t kGetPreferredBrightnessRegValue[] =
50+
L"AppsUseLightTheme";
3251

3352
#define APPBAR_CALLBACK WM_USER + 0x01;
3453

@@ -1001,14 +1020,21 @@ void WindowManager::SetOpacity(const flutter::EncodableMap& args) {
10011020
}
10021021

10031022
void WindowManager::SetBrightness(const flutter::EncodableMap& args) {
1004-
std::string brightness =
1005-
std::get<std::string>(args.at(flutter::EncodableValue("brightness")));
1006-
1007-
const BOOL is_dark_mode = brightness == "dark";
1008-
1009-
HWND hWnd = GetMainWindow();
1010-
DwmSetWindowAttribute(hWnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &is_dark_mode,
1011-
sizeof(is_dark_mode));
1023+
DWORD light_mode;
1024+
DWORD light_mode_size = sizeof(light_mode);
1025+
LSTATUS result =
1026+
RegGetValue(HKEY_CURRENT_USER, kGetPreferredBrightnessRegKey,
1027+
kGetPreferredBrightnessRegValue, RRF_RT_REG_DWORD, nullptr,
1028+
&light_mode, &light_mode_size);
1029+
1030+
if (result == ERROR_SUCCESS) {
1031+
std::string brightness =
1032+
std::get<std::string>(args.at(flutter::EncodableValue("brightness")));
1033+
HWND hWnd = GetMainWindow();
1034+
BOOL enable_dark_mode = light_mode == 0 && brightness == "dark";
1035+
DwmSetWindowAttribute(hWnd, DWMWA_USE_IMMERSIVE_DARK_MODE,
1036+
&enable_dark_mode, sizeof(enable_dark_mode));
1037+
}
10121038
}
10131039

10141040
void WindowManager::SetIgnoreMouseEvents(const flutter::EncodableMap& args) {

0 commit comments

Comments
 (0)