Skip to content

Commit

Permalink
fix(windows): fix TitleBar buttons does not display correctly #415
Browse files Browse the repository at this point in the history
  • Loading branch information
lijy91 committed May 18, 2024
1 parent 8fa23db commit 7260319
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions windows/window_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,26 @@
#define STATE_FULLSCREEN_ENTERED 3
#define STATE_DOCKED 4

#define DWMWA_USE_IMMERSIVE_DARK_MODE 19
/// Window attribute that enables dark mode window decorations.
///
/// Redefined in case the developer's machine has a Windows SDK older than
/// version 10.0.22000.0.
/// See:
/// https://docs.microsoft.com/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
#endif

constexpr const wchar_t kWindowClassName[] = L"FLUTTER_RUNNER_WIN32_WINDOW";

/// Registry key for app theme preference.
///
/// A value of 0 indicates apps should use dark mode. A non-zero or missing
/// value indicates apps should use light mode.
constexpr const wchar_t kGetPreferredBrightnessRegKey[] =
L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
constexpr const wchar_t kGetPreferredBrightnessRegValue[] =
L"AppsUseLightTheme";

#define APPBAR_CALLBACK WM_USER + 0x01;

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

void WindowManager::SetBrightness(const flutter::EncodableMap& args) {
std::string brightness =
std::get<std::string>(args.at(flutter::EncodableValue("brightness")));

const BOOL is_dark_mode = brightness == "dark";

HWND hWnd = GetMainWindow();
DwmSetWindowAttribute(hWnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &is_dark_mode,
sizeof(is_dark_mode));
DWORD light_mode;
DWORD light_mode_size = sizeof(light_mode);
LSTATUS result =
RegGetValue(HKEY_CURRENT_USER, kGetPreferredBrightnessRegKey,
kGetPreferredBrightnessRegValue, RRF_RT_REG_DWORD, nullptr,
&light_mode, &light_mode_size);

if (result == ERROR_SUCCESS) {
std::string brightness =
std::get<std::string>(args.at(flutter::EncodableValue("brightness")));
HWND hWnd = GetMainWindow();
BOOL enable_dark_mode = light_mode == 0 && brightness == "dark";
DwmSetWindowAttribute(hWnd, DWMWA_USE_IMMERSIVE_DARK_MODE,
&enable_dark_mode, sizeof(enable_dark_mode));
}
}

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

0 comments on commit 7260319

Please sign in to comment.