|
28 | 28 | #define STATE_FULLSCREEN_ENTERED 3
|
29 | 29 | #define STATE_DOCKED 4
|
30 | 30 |
|
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"; |
32 | 51 |
|
33 | 52 | #define APPBAR_CALLBACK WM_USER + 0x01;
|
34 | 53 |
|
@@ -1001,14 +1020,21 @@ void WindowManager::SetOpacity(const flutter::EncodableMap& args) {
|
1001 | 1020 | }
|
1002 | 1021 |
|
1003 | 1022 | 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 | + } |
1012 | 1038 | }
|
1013 | 1039 |
|
1014 | 1040 | void WindowManager::SetIgnoreMouseEvents(const flutter::EncodableMap& args) {
|
|
0 commit comments