Skip to content

Commit

Permalink
Merge pull request #589 from 4ster1sk/fix/titlebar-color
Browse files Browse the repository at this point in the history
[Windows] ウィンドウタイトルと各種ボタンがタイトルバーの色と同じになる問題の修正
  • Loading branch information
shiosyakeyakini-info authored Jun 22, 2024
2 parents abbb114 + 373ed7d commit 4175997
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 21 deletions.
18 changes: 16 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ class MyApp extends ConsumerStatefulWidget {
ConsumerState<MyApp> createState() => _MyAppState();
}

class _MyAppState extends ConsumerState<MyApp> with WindowListener {
class _MyAppState extends ConsumerState<MyApp> with WindowListener, WidgetsBindingObserver {
final _appRouter = AppRouter();
final isDesktop = Platform.isWindows || Platform.isMacOS || Platform.isLinux;

@override
void initState() {
if (isDesktop) {
WidgetsBinding.instance.addObserver(this);
windowManager.addListener(this);
unawaited(() async {
await ref.read(desktopSettingsRepositoryProvider).load();
Expand All @@ -59,6 +60,7 @@ class _MyAppState extends ConsumerState<MyApp> with WindowListener {
@override
void dispose() {
if (isDesktop) {
WidgetsBinding.instance.removeObserver(this);
windowManager.removeListener(this);
}
super.dispose();
Expand Down Expand Up @@ -92,6 +94,19 @@ class _MyAppState extends ConsumerState<MyApp> with WindowListener {
}
}

@override
Future<void> didChangePlatformBrightness() async {
super.didChangePlatformBrightness();
if (!isDesktop) return;

final brightness =
WidgetsBinding.instance.platformDispatcher.platformBrightness;

/// Set up window brightness follow system theme mode.
await WindowManager.instance.setBrightness(brightness);
await WindowManager.instance.setTitleBarStyle(TitleBarStyle.normal);
}

Future<void> _initWindow() async {
await windowManager.setPreventClose(true);
final config = ref.read(desktopSettingsRepositoryProvider).settings;
Expand All @@ -107,7 +122,6 @@ class _MyAppState extends ConsumerState<MyApp> with WindowListener {
final opt = WindowOptions(
size: size,
center: position == null,
backgroundColor: Colors.transparent,
skipTaskbar: false,
titleBarStyle: TitleBarStyle.normal,
);
Expand Down
1 change: 1 addition & 0 deletions windows/runner/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ target_compile_definitions(${BINARY_NAME} PRIVATE "NOMINMAX")
# Add dependency libraries and include directories. Add any application-specific
# dependencies here.
target_link_libraries(${BINARY_NAME} PRIVATE flutter flutter_wrapper_app)
target_link_libraries(${BINARY_NAME} PRIVATE "dwmapi.lib")
target_include_directories(${BINARY_NAME} PRIVATE "${CMAKE_SOURCE_DIR}")

# Run the Flutter tool portions of the build. This must not be removed.
Expand Down
9 changes: 9 additions & 0 deletions windows/runner/flutter_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ bool FlutterWindow::OnCreate() {
}
RegisterPlugins(flutter_controller_->engine());
SetChildContent(flutter_controller_->view()->GetNativeWindow());

flutter_controller_->engine()->SetNextFrameCallback([&]() {
});

// Flutter can complete the first frame before the "show window" callback is
// registered. The following call ensures a frame is pending to ensure the
// window is shown. It is a no-op if the first frame hasn't completed yet.
flutter_controller_->ForceRedraw();

return true;
}

Expand Down
4 changes: 2 additions & 2 deletions windows/runner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,

FlutterWindow window(project);
Win32Window::Point origin(10, 10);
Win32Window::Size size(1280, 720);
if (!window.CreateAndShow(L"miria", origin, size)) {
Win32Window::Size size(400, 700);
if (!window.Create(L"miria", origin, size)) {
return EXIT_FAILURE;
}
window.SetQuitOnClose(true);
Expand Down
9 changes: 5 additions & 4 deletions windows/runner/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
}
int target_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, nullptr, 0, nullptr, nullptr);
-1, nullptr, 0, nullptr, nullptr)
-1; // remove the trailing null character
int input_length = (int)wcslen(utf16_string);
std::string utf8_string;
if (target_length == 0 || target_length > utf8_string.max_size()) {
if (target_length <= 0 || target_length > utf8_string.max_size()) {
return utf8_string;
}
utf8_string.resize(target_length);
int converted_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, utf8_string.data(),
target_length, nullptr, nullptr);
input_length, utf8_string.data(), target_length, nullptr, nullptr);
if (converted_length == 0) {
return std::string();
}
Expand Down
53 changes: 48 additions & 5 deletions windows/runner/win32_window.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
#include "win32_window.h"

#include <dwmapi.h>
#include <flutter_windows.h>

#include "resource.h"

namespace {

/// 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";

// The number of Win32Window objects that currently exist.
static int g_active_window_count = 0;

Expand All @@ -31,8 +49,8 @@ void EnableFullDpiSupportIfAvailable(HWND hwnd) {
GetProcAddress(user32_module, "EnableNonClientDpiScaling"));
if (enable_non_client_dpi_scaling != nullptr) {
enable_non_client_dpi_scaling(hwnd);
FreeLibrary(user32_module);
}
FreeLibrary(user32_module);
}

} // namespace
Expand All @@ -42,7 +60,7 @@ class WindowClassRegistrar {
public:
~WindowClassRegistrar() = default;

// Returns the singleton registar instance.
// Returns the singleton registrar instance.
static WindowClassRegistrar* GetInstance() {
if (!instance_) {
instance_ = new WindowClassRegistrar();
Expand Down Expand Up @@ -102,9 +120,9 @@ Win32Window::~Win32Window() {
Destroy();
}

bool Win32Window::CreateAndShow(const std::wstring& title,
const Point& origin,
const Size& size) {
bool Win32Window::Create(const std::wstring& title,
const Point& origin,
const Size& size) {
Destroy();

const wchar_t* window_class =
Expand All @@ -127,9 +145,15 @@ bool Win32Window::CreateAndShow(const std::wstring& title,
return false;
}

UpdateTheme(window);

return OnCreate();
}

bool Win32Window::Show() {
return ShowWindow(window_handle_, SW_SHOWNORMAL);
}

// static
LRESULT CALLBACK Win32Window::WndProc(HWND const window,
UINT const message,
Expand Down Expand Up @@ -189,6 +213,10 @@ Win32Window::MessageHandler(HWND hwnd,
SetFocus(child_content_);
}
return 0;

case WM_DWMCOLORIZATIONCOLORCHANGED:
UpdateTheme(hwnd);
return 0;
}

return DefWindowProc(window_handle_, message, wparam, lparam);
Expand Down Expand Up @@ -244,3 +272,18 @@ bool Win32Window::OnCreate() {
void Win32Window::OnDestroy() {
// No-op; provided for subclasses.
}

void Win32Window::UpdateTheme(HWND const window) {
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) {
BOOL enable_dark_mode = light_mode == 0;
DwmSetWindowAttribute(window, DWMWA_USE_IMMERSIVE_DARK_MODE,
&enable_dark_mode, sizeof(enable_dark_mode));
}
}
20 changes: 12 additions & 8 deletions windows/runner/win32_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ class Win32Window {
Win32Window();
virtual ~Win32Window();

// Creates and shows a win32 window with |title| and position and size using
// Creates a win32 window with |title| that is positioned and sized using
// |origin| and |size|. New windows are created on the default monitor. Window
// sizes are specified to the OS in physical pixels, hence to ensure a
// consistent size to will treat the width height passed in to this function
// as logical pixels and scale to appropriate for the default monitor. Returns
// true if the window was created successfully.
bool CreateAndShow(const std::wstring& title,
const Point& origin,
const Size& size);
// consistent size this function will scale the inputted width and height as
// as appropriate for the default monitor. The window is invisible until
// |Show| is called. Returns true if the window was created successfully.
bool Create(const std::wstring& title, const Point& origin, const Size& size);

// Show the current window. Returns true if the window was successfully shown.
bool Show();

// Release OS resources associated with window.
void Destroy();
Expand Down Expand Up @@ -76,7 +77,7 @@ class Win32Window {
// OS callback called by message pump. Handles the WM_NCCREATE message which
// is passed when the non-client area is being created and enables automatic
// non-client DPI scaling so that the non-client area automatically
// responsponds to changes in DPI. All other messages are handled by
// responds to changes in DPI. All other messages are handled by
// MessageHandler.
static LRESULT CALLBACK WndProc(HWND const window,
UINT const message,
Expand All @@ -86,6 +87,9 @@ class Win32Window {
// Retrieves a class instance pointer for |window|
static Win32Window* GetThisFromHandle(HWND const window) noexcept;

// Update the window frame's theme to match the system theme.
static void UpdateTheme(HWND const window);

bool quit_on_close_ = false;

// window handle for top level window.
Expand Down

0 comments on commit 4175997

Please sign in to comment.