From fc23f4971c869927d04fae1e3c0951ebf8e0a7f9 Mon Sep 17 00:00:00 2001 From: moko256 Date: Thu, 8 Sep 2022 04:27:12 +0900 Subject: [PATCH] [Windows] Fix system theme integration. (flutter/engine#35902) The #35019 didn't merge SettingsPluginWin32's constructor and destructor. This PR adds them and fix SettingsPlugin to re-enable it to watch the theme changes. This PR is part of flutter/flutter#110700. No changes in the flutter/tests repo. --- .../shell/platform/windows/settings_plugin.cc | 17 ++++++++++++++++- .../shell/platform/windows/settings_plugin.h | 9 ++++++--- .../windows/settings_plugin_unittests.cc | 18 +++++++++++++++--- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/engine/src/flutter/shell/platform/windows/settings_plugin.cc b/engine/src/flutter/shell/platform/windows/settings_plugin.cc index 3d293c9134..2754516e10 100644 --- a/engine/src/flutter/shell/platform/windows/settings_plugin.cc +++ b/engine/src/flutter/shell/platform/windows/settings_plugin.cc @@ -36,7 +36,9 @@ SettingsPlugin::SettingsPlugin(BinaryMessenger* messenger, &JsonMessageCodec::GetInstance())), task_runner_(task_runner) {} -SettingsPlugin::~SettingsPlugin() = default; +SettingsPlugin::~SettingsPlugin() { + StopWatching(); +} void SettingsPlugin::SendSettings() { rapidjson::Document settings(rapidjson::kObjectType); @@ -55,6 +57,12 @@ void SettingsPlugin::SendSettings() { } void SettingsPlugin::StartWatching() { + RegOpenKeyEx(HKEY_CURRENT_USER, kGetPreferredBrightnessRegKey, + RRF_RT_REG_DWORD, KEY_NOTIFY, &preferred_brightness_reg_hkey_); + RegOpenKeyEx(HKEY_CURRENT_USER, kGetTextScaleFactorRegKey, RRF_RT_REG_DWORD, + KEY_NOTIFY, &text_scale_factor_reg_hkey_); + + // Start watching when the keys exist. if (preferred_brightness_reg_hkey_ != nullptr) { WatchPreferredBrightnessChanged(); } @@ -66,6 +74,13 @@ void SettingsPlugin::StartWatching() { void SettingsPlugin::StopWatching() { preferred_brightness_changed_watcher_ = nullptr; text_scale_factor_changed_watcher_ = nullptr; + + if (preferred_brightness_reg_hkey_ != nullptr) { + RegCloseKey(preferred_brightness_reg_hkey_); + } + if (text_scale_factor_reg_hkey_ != nullptr) { + RegCloseKey(text_scale_factor_reg_hkey_); + } } bool SettingsPlugin::GetAlwaysUse24HourFormat() { diff --git a/engine/src/flutter/shell/platform/windows/settings_plugin.h b/engine/src/flutter/shell/platform/windows/settings_plugin.h index 6491e642ef..2c663fc048 100644 --- a/engine/src/flutter/shell/platform/windows/settings_plugin.h +++ b/engine/src/flutter/shell/platform/windows/settings_plugin.h @@ -49,12 +49,15 @@ class SettingsPlugin { // Returns the user-preferred brightness. virtual PlatformBrightness GetPreferredBrightness(); + // Starts watching brightness changes. + virtual void WatchPreferredBrightnessChanged(); + + // Starts watching text scale factor changes. + virtual void WatchTextScaleFactorChanged(); + private: std::unique_ptr> channel_; - void WatchPreferredBrightnessChanged(); - void WatchTextScaleFactorChanged(); - HKEY preferred_brightness_reg_hkey_ = nullptr; HKEY text_scale_factor_reg_hkey_ = nullptr; diff --git a/engine/src/flutter/shell/platform/windows/settings_plugin_unittests.cc b/engine/src/flutter/shell/platform/windows/settings_plugin_unittests.cc index 3158e5a41c..2380f46017 100644 --- a/engine/src/flutter/shell/platform/windows/settings_plugin_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/settings_plugin_unittests.cc @@ -22,12 +22,12 @@ class MockSettingsPlugin : public SettingsPlugin { virtual ~MockSettingsPlugin() = default; // |SettingsPlugin| - MOCK_METHOD0(StartWatching, void()); - MOCK_METHOD0(StopWatching, void()); - MOCK_METHOD0(GetAlwaysUse24HourFormat, bool()); MOCK_METHOD0(GetTextScaleFactor, float()); MOCK_METHOD0(GetPreferredBrightness, PlatformBrightness()); + + MOCK_METHOD0(WatchPreferredBrightnessChanged, void()); + MOCK_METHOD0(WatchTextScaleFactorChanged, void()); }; } // namespace @@ -58,5 +58,17 @@ TEST(SettingsPluginTest, SendSettingsGetsSettings) { settings_plugin.SendSettings(); } +TEST(SettingsPluginTest, StartWatchingStartsWatchingChanges) { + TestBinaryMessenger messenger([](const std::string& channel, + const uint8_t* message, size_t message_size, + BinaryReply reply) {}); + ::testing::NiceMock settings_plugin(&messenger, nullptr); + + EXPECT_CALL(settings_plugin, WatchPreferredBrightnessChanged).Times(1); + EXPECT_CALL(settings_plugin, WatchTextScaleFactorChanged).Times(1); + + settings_plugin.StartWatching(); +} + } // namespace testing } // namespace flutter