diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_engine.cc b/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_engine.cc index 2463a84cf5..3172a1f0b8 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_engine.cc +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_engine.cc @@ -74,6 +74,10 @@ void FlutterEngine::ReloadSystemFonts() { FlutterDesktopEngineReloadSystemFonts(engine_); } +void FlutterEngine::ReloadPlatformBrightness() { + FlutterDesktopEngineReloadPlatformBrightness(engine_); +} + FlutterDesktopPluginRegistrarRef FlutterEngine::GetRegistrarForPlugin( const std::string& plugin_name) { if (!engine_) { diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_engine_unittests.cc b/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_engine_unittests.cc index e52fb336a3..15053b293a 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_engine_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_engine_unittests.cc @@ -49,6 +49,11 @@ class TestFlutterWindowsApi : public testing::StubFlutterWindowsApi { // |flutter::testing::StubFlutterWindowsApi| void EngineReloadSystemFonts() override { reload_fonts_called_ = true; } + // |flutter::testing::StubFlutterWindowsApi| + void EngineReloadPlatformBrightness() override { + reload_brightness_called_ = true; + } + bool create_called() { return create_called_; } bool run_called() { return run_called_; } @@ -57,6 +62,8 @@ class TestFlutterWindowsApi : public testing::StubFlutterWindowsApi { bool reload_fonts_called() { return reload_fonts_called_; } + bool reload_brightness_called() { return reload_brightness_called_; } + const std::vector& dart_entrypoint_arguments() { return dart_entrypoint_arguments_; } @@ -66,6 +73,7 @@ class TestFlutterWindowsApi : public testing::StubFlutterWindowsApi { bool run_called_ = false; bool destroy_called_ = false; bool reload_fonts_called_ = false; + bool reload_brightness_called_ = false; std::vector dart_entrypoint_arguments_; }; @@ -124,6 +132,18 @@ TEST(FlutterEngineTest, ReloadFonts) { EXPECT_TRUE(test_api->reload_fonts_called()); } +TEST(FlutterEngineTest, ReloadBrightness) { + testing::ScopedStubFlutterWindowsApi scoped_api_stub( + std::make_unique()); + auto test_api = static_cast(scoped_api_stub.stub()); + + FlutterEngine engine(DartProject(L"fake/project/path")); + engine.Run(); + + engine.ReloadPlatformBrightness(); + EXPECT_TRUE(test_api->reload_brightness_called()); +} + TEST(FlutterEngineTest, GetMessenger) { DartProject project(L"data"); testing::ScopedStubFlutterWindowsApi scoped_api_stub( diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_engine.h b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_engine.h index ee48643dac..464c233fa9 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_engine.h +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_engine.h @@ -58,6 +58,11 @@ class FlutterEngine : public PluginRegistry { // Win32 application). void ReloadSystemFonts(); + // Tells the engine that the platform brightness value has changed. Should be + // called by clients when OS-level theme changes happen (e.g., + // WM_DWMCOLORIZATIONCOLORCHANGED in a Win32 application). + void ReloadPlatformBrightness(); + // flutter::PluginRegistry: FlutterDesktopPluginRegistrarRef GetRegistrarForPlugin( const std::string& plugin_name) override; diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc b/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc index a829326a61..ae3b048de6 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc @@ -117,6 +117,13 @@ void FlutterDesktopEngineReloadSystemFonts(FlutterDesktopEngineRef engine) { } } +void FlutterDesktopEngineReloadPlatformBrightness( + FlutterDesktopEngineRef engine) { + if (s_stub_implementation) { + s_stub_implementation->EngineReloadPlatformBrightness(); + } +} + FlutterDesktopPluginRegistrarRef FlutterDesktopEngineGetPluginRegistrar( FlutterDesktopEngineRef engine, const char* plugin_name) { diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h b/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h index b795eab1a6..81d00ac820 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h @@ -65,6 +65,9 @@ class StubFlutterWindowsApi { // Called for FlutterDesktopEngineReloadSystemFonts. virtual void EngineReloadSystemFonts() {} + // Called for FlutterDesktopEngineReloadPlatformBrightness. + virtual void EngineReloadPlatformBrightness() {} + // Called for FlutterDesktopViewGetHWND. virtual HWND ViewGetHWND() { return reinterpret_cast(1); } diff --git a/engine/src/flutter/shell/platform/windows/flutter_window_win32_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_window_win32_unittests.cc index 0340e2fc55..84af45a610 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window_win32_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_window_win32_unittests.cc @@ -222,6 +222,7 @@ class MockWindowBindingHandlerDelegate : public WindowBindingHandlerDelegate { int, FlutterPointerDeviceKind, int32_t)); + MOCK_METHOD0(OnPlatformBrightnessChanged, void()); }; // A FlutterWindowsView that overrides the RegisterKeyboardHandlers function diff --git a/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.cc b/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.cc index fb74ca2cd2..29781837fd 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.cc @@ -156,6 +156,9 @@ void FlutterWindowWinUWP::SetEventHandlers() { window_.PointerWheelChanged( {this, &FlutterWindowWinUWP::OnPointerWheelChanged}); + ui_settings_.ColorValuesChanged( + {this, &FlutterWindowWinUWP::OnColorValuesChanged}); + // TODO(clarkezone) support mouse leave handling // https://github.com/flutter/flutter/issues/70199 @@ -355,6 +358,12 @@ void FlutterWindowWinUWP::OnCharacterReceived( } } +void FlutterWindowWinUWP::OnColorValuesChanged( + winrt::Windows::Foundation::IInspectable const&, + winrt::Windows::Foundation::IInspectable const&) { + binding_handler_delegate_->OnPlatformBrightnessChanged(); +} + bool FlutterWindowWinUWP::OnBitmapSurfaceUpdated(const void* allocation, size_t row_bytes, size_t height) { diff --git a/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.h b/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.h index 25f76f5fbd..235caf409f 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.h +++ b/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.h @@ -114,6 +114,11 @@ class FlutterWindowWinUWP : public WindowBindingHandler { winrt::Windows::Foundation::IInspectable const&, winrt::Windows::UI::Core::CharacterReceivedEventArgs const& args); + // Notifies current |WindowBindingHandlerDelegate| of color values changed + // events. + void OnColorValuesChanged(winrt::Windows::Foundation::IInspectable const&, + winrt::Windows::Foundation::IInspectable const&); + // Converts from logical point to physical X value. double GetPosX(winrt::Windows::UI::Core::PointerEventArgs const& args); @@ -153,6 +158,9 @@ class FlutterWindowWinUWP : public WindowBindingHandler { // SwapChain to the CoreWindow. winrt::Windows::UI::Composition::SpriteVisual render_target_{nullptr}; + // UISettings for observing the color change. + winrt::Windows::UI::ViewManagement::UISettings ui_settings_; + // GamepadCursorWinUWP object used to manage an emulated cursor visual driven // by gamepad. std::unique_ptr game_pad_cursor_{nullptr}; diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows.cc b/engine/src/flutter/shell/platform/windows/flutter_windows.cc index 9b8b7d8047..81bf1eb6a7 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows.cc @@ -107,6 +107,11 @@ void FlutterDesktopEngineReloadSystemFonts(FlutterDesktopEngineRef engine) { EngineFromHandle(engine)->ReloadSystemFonts(); } +void FlutterDesktopEngineReloadPlatformBrightness( + FlutterDesktopEngineRef engine) { + EngineFromHandle(engine)->ReloadPlatformBrightness(); +} + FlutterDesktopPluginRegistrarRef FlutterDesktopEngineGetPluginRegistrar( FlutterDesktopEngineRef engine, const char* plugin_name) { diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc index 9e2d287b2b..19fd30d417 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc @@ -391,6 +391,12 @@ void FlutterWindowsEngine::ReloadSystemFonts() { embedder_api_.ReloadSystemFonts(engine_); } +void FlutterWindowsEngine::ReloadPlatformBrightness() { + if (engine_) { + SendSystemSettings(); + } +} + void FlutterWindowsEngine::SendSystemSettings() { std::vector languages = GetPreferredLanguageInfo(); std::vector flutter_locales; @@ -413,9 +419,8 @@ void FlutterWindowsEngine::SendSystemSettings() { settings.AddMember("alwaysUse24HourFormat", Prefer24HourTime(GetUserTimeFormat()), allocator); settings.AddMember("textScaleFactor", 1.0, allocator); - // TODO: Implement dark mode support. - // https://github.com/flutter/flutter/issues/54612 - settings.AddMember("platformBrightness", "light", allocator); + settings.AddMember("platformBrightness", + Utf8FromUtf16(GetPreferredBrightness()), allocator); settings_channel_->Send(settings); } diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h index cca0ff47fa..40087c7c14 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h @@ -131,6 +131,9 @@ class FlutterWindowsEngine { // Informs the engine that the system font list has changed. void ReloadSystemFonts(); + // Informs the engine that the platform brightness has changed. + void ReloadPlatformBrightness(); + // Attempts to register the texture with the given |texture_id|. bool RegisterExternalTexture(int64_t texture_id); diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc index 554938bac4..ac4e0e5e13 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc @@ -249,6 +249,10 @@ void FlutterWindowsView::OnScroll(double x, device_id); } +void FlutterWindowsView::OnPlatformBrightnessChanged() { + SendPlatformBrightnessChanged(); +} + void FlutterWindowsView::OnCursorRectUpdated(const Rect& rect) { binding_handler_->OnCursorRectUpdated(rect); } @@ -465,6 +469,11 @@ void FlutterWindowsView::SendPointerEventWithData( } } +void FlutterWindowsView::SendPlatformBrightnessChanged() { + engine_->task_runner()->RunNowOrPostTask( + [this]() { engine_->ReloadPlatformBrightness(); }); +}; + bool FlutterWindowsView::MakeCurrent() { return engine_->surface_manager()->MakeCurrent(); } diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_view.h b/engine/src/flutter/shell/platform/windows/flutter_windows_view.h index 9f5fd7559f..a50e908ef3 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_view.h +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_view.h @@ -149,6 +149,9 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate, FlutterPointerDeviceKind device_kind, int32_t device_id) override; + // |WindowBindingHandlerDelegate| + void OnPlatformBrightnessChanged() override; + // |TextInputPluginDelegate| void OnCursorRectUpdated(const Rect& rect) override; @@ -284,6 +287,9 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate, void SendPointerEventWithData(const FlutterPointerEvent& event_data, PointerState* state); + // Reports platform brightness change to Flutter engine. + void SendPlatformBrightnessChanged(); + // Currently configured WindowsRenderTarget for this view used by // surface_manager for creation of render surfaces and bound to the physical // os window. diff --git a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h index 14f8ea464a..0aeb6fdc15 100644 --- a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h +++ b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h @@ -174,6 +174,9 @@ FlutterDesktopEngineProcessMessages(FlutterDesktopEngineRef engine); FLUTTER_EXPORT void FlutterDesktopEngineReloadSystemFonts( FlutterDesktopEngineRef engine); +FLUTTER_EXPORT void FlutterDesktopEngineReloadPlatformBrightness( + FlutterDesktopEngineRef engine); + // Returns the plugin registrar handle for the plugin with the given name. // // The name must be unique across the application. diff --git a/engine/src/flutter/shell/platform/windows/system_utils.h b/engine/src/flutter/shell/platform/windows/system_utils.h index 60137d964b..1d39cb8b25 100644 --- a/engine/src/flutter/shell/platform/windows/system_utils.h +++ b/engine/src/flutter/shell/platform/windows/system_utils.h @@ -12,6 +12,11 @@ namespace flutter { +namespace { +static constexpr wchar_t kPlatformBrightnessLight[] = L"light"; +static constexpr wchar_t kPlatformBrightnessDark[] = L"dark"; +} // namespace + // Components of a system language/locale. struct LanguageInfo { std::string language; @@ -37,6 +42,9 @@ std::wstring GetUserTimeFormat(); // Returns true if the time_format is set to use 24 hour time. bool Prefer24HourTime(std::wstring time_format); +// Returns the user-preferred brightness. +std::wstring GetPreferredBrightness(); + } // namespace flutter #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_SYSTEM_UTILS_H_ diff --git a/engine/src/flutter/shell/platform/windows/system_utils_unittests.cc b/engine/src/flutter/shell/platform/windows/system_utils_unittests.cc index 6fecfbcc11..00e5fd48fd 100644 --- a/engine/src/flutter/shell/platform/windows/system_utils_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/system_utils_unittests.cc @@ -88,5 +88,10 @@ TEST(SystemUtils, Prefer24HourTimeHandles24Hour) { EXPECT_TRUE(Prefer24HourTime(L"HH:mm:ss")); } +TEST(SystemUtils, GetPreferredBrightness) { + std::wstring brightness = GetPreferredBrightness(); + EXPECT_TRUE(brightness == L"light" || brightness == L"dark"); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/system_utils_win32.cc b/engine/src/flutter/shell/platform/windows/system_utils_win32.cc index 53f9c21532..23147296c1 100644 --- a/engine/src/flutter/shell/platform/windows/system_utils_win32.cc +++ b/engine/src/flutter/shell/platform/windows/system_utils_win32.cc @@ -106,4 +106,22 @@ bool Prefer24HourTime(std::wstring time_format) { return time_format.find(L"H") != std::wstring::npos; } +std::wstring GetPreferredBrightness() { + DWORD use_light_theme; + DWORD use_light_theme_size = sizeof(use_light_theme); + LONG result = RegGetValue( + HKEY_CURRENT_USER, + L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", + L"AppsUseLightTheme", RRF_RT_REG_DWORD, nullptr, &use_light_theme, + &use_light_theme_size); + + if (result == 0) { + return use_light_theme ? kPlatformBrightnessLight : kPlatformBrightnessDark; + } else { + // The current OS does not support dark mode. (Older Windows 10 or before + // Windows 10) + return kPlatformBrightnessLight; + } +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/system_utils_winuwp.cc b/engine/src/flutter/shell/platform/windows/system_utils_winuwp.cc index 05d16d8838..cdee09e30f 100644 --- a/engine/src/flutter/shell/platform/windows/system_utils_winuwp.cc +++ b/engine/src/flutter/shell/platform/windows/system_utils_winuwp.cc @@ -10,6 +10,8 @@ #include "third_party/cppwinrt/generated/winrt/Windows.Foundation.Collections.h" #include "third_party/cppwinrt/generated/winrt/Windows.System.UserProfile.h" +#include "third_party/cppwinrt/generated/winrt/Windows.UI.ViewManagement.h" +#include "third_party/cppwinrt/generated/winrt/Windows.UI.h" #include "flutter/shell/platform/windows/string_conversion.h" @@ -80,4 +82,16 @@ bool Prefer24HourTime(std::wstring time_format) { return time_format.find(L"H") != std::wstring::npos; } +std::wstring GetPreferredBrightness() { + winrt::Windows::UI::ViewManagement::UISettings ui_settings; + auto background_color = ui_settings.GetColorValue( + winrt::Windows::UI::ViewManagement::UIColorType::Background); + // Assuming that Windows return `Colors::Black` when being dark theme. + if (background_color == winrt::Windows::UI::Colors::Black()) { + return kPlatformBrightnessDark; + } else { + return kPlatformBrightnessLight; + } +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/window_binding_handler_delegate.h b/engine/src/flutter/shell/platform/windows/window_binding_handler_delegate.h index 5cf8fadd89..7bdedc9d4d 100644 --- a/engine/src/flutter/shell/platform/windows/window_binding_handler_delegate.h +++ b/engine/src/flutter/shell/platform/windows/window_binding_handler_delegate.h @@ -95,6 +95,9 @@ class WindowBindingHandlerDelegate { int scroll_offset_multiplier, FlutterPointerDeviceKind device_kind, int32_t device_id) = 0; + + // Notifies delegate that backing window has received brightness change event. + virtual void OnPlatformBrightnessChanged() = 0; }; } // namespace flutter