diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 0721913cb86fa..1c8291fb821a9 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -2422,8 +2422,6 @@ FILE: ../../../flutter/shell/platform/windows/sequential_id_generator_unittests. FILE: ../../../flutter/shell/platform/windows/settings_plugin.cc FILE: ../../../flutter/shell/platform/windows/settings_plugin.h FILE: ../../../flutter/shell/platform/windows/settings_plugin_unittests.cc -FILE: ../../../flutter/shell/platform/windows/settings_plugin_win32.cc -FILE: ../../../flutter/shell/platform/windows/settings_plugin_win32.h FILE: ../../../flutter/shell/platform/windows/system_utils.cc FILE: ../../../flutter/shell/platform/windows/system_utils.h FILE: ../../../flutter/shell/platform/windows/system_utils_unittests.cc diff --git a/shell/platform/windows/BUILD.gn b/shell/platform/windows/BUILD.gn index a24bff2566867..3aa94757e4cff 100644 --- a/shell/platform/windows/BUILD.gn +++ b/shell/platform/windows/BUILD.gn @@ -88,8 +88,6 @@ source_set("flutter_windows_source") { "sequential_id_generator.h", "settings_plugin.cc", "settings_plugin.h", - "settings_plugin_win32.cc", - "settings_plugin_win32.h", "system_utils.cc", "system_utils.h", "task_runner.cc", diff --git a/shell/platform/windows/flutter_windows_engine.cc b/shell/platform/windows/flutter_windows_engine.cc index 8ac8edaf1d95e..20d7318aaceab 100644 --- a/shell/platform/windows/flutter_windows_engine.cc +++ b/shell/platform/windows/flutter_windows_engine.cc @@ -187,8 +187,8 @@ FlutterWindowsEngine::FlutterWindowsEngine(const FlutterProjectBundle& project) // Set up internal channels. // TODO: Replace this with an embedder.h API. See // https://github.com/flutter/flutter/issues/71099 - settings_plugin_ = - SettingsPlugin::Create(messenger_wrapper_.get(), task_runner_.get()); + settings_plugin_ = std::make_unique(messenger_wrapper_.get(), + task_runner_.get()); } FlutterWindowsEngine::~FlutterWindowsEngine() { diff --git a/shell/platform/windows/settings_plugin.cc b/shell/platform/windows/settings_plugin.cc index 3bca2b4438a6c..3d293c9134425 100644 --- a/shell/platform/windows/settings_plugin.cc +++ b/shell/platform/windows/settings_plugin.cc @@ -5,6 +5,7 @@ #include "flutter/shell/platform/windows/settings_plugin.h" #include "flutter/shell/platform/common/json_message_codec.h" +#include "flutter/shell/platform/windows/system_utils.h" namespace flutter { @@ -17,6 +18,14 @@ constexpr char kPlatformBrightness[] = "platformBrightness"; constexpr char kPlatformBrightnessDark[] = "dark"; constexpr char kPlatformBrightnessLight[] = "light"; + +constexpr wchar_t kGetPreferredBrightnessRegKey[] = + L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"; +constexpr wchar_t kGetPreferredBrightnessRegValue[] = L"AppsUseLightTheme"; + +constexpr wchar_t kGetTextScaleFactorRegKey[] = + L"Software\\Microsoft\\Accessibility"; +constexpr wchar_t kGetTextScaleFactorRegValue[] = L"TextScaleFactor"; } // namespace SettingsPlugin::SettingsPlugin(BinaryMessenger* messenger, @@ -45,4 +54,81 @@ void SettingsPlugin::SendSettings() { channel_->Send(settings); } +void SettingsPlugin::StartWatching() { + if (preferred_brightness_reg_hkey_ != nullptr) { + WatchPreferredBrightnessChanged(); + } + if (text_scale_factor_reg_hkey_ != nullptr) { + WatchTextScaleFactorChanged(); + } +} + +void SettingsPlugin::StopWatching() { + preferred_brightness_changed_watcher_ = nullptr; + text_scale_factor_changed_watcher_ = nullptr; +} + +bool SettingsPlugin::GetAlwaysUse24HourFormat() { + return Prefer24HourTime(GetUserTimeFormat()); +} + +float SettingsPlugin::GetTextScaleFactor() { + DWORD text_scale_factor; + DWORD text_scale_factor_size = sizeof(text_scale_factor); + LONG result = RegGetValue( + HKEY_CURRENT_USER, kGetTextScaleFactorRegKey, kGetTextScaleFactorRegValue, + RRF_RT_REG_DWORD, nullptr, &text_scale_factor, &text_scale_factor_size); + + if (result == 0) { + return text_scale_factor / 100.0; + } else { + // The current OS does not have text scale factor. + return 1.0; + } +} + +SettingsPlugin::PlatformBrightness SettingsPlugin::GetPreferredBrightness() { + DWORD use_light_theme; + DWORD use_light_theme_size = sizeof(use_light_theme); + LONG result = RegGetValue(HKEY_CURRENT_USER, kGetPreferredBrightnessRegKey, + kGetPreferredBrightnessRegValue, RRF_RT_REG_DWORD, + nullptr, &use_light_theme, &use_light_theme_size); + + if (result == 0) { + return use_light_theme ? SettingsPlugin::PlatformBrightness::kLight + : SettingsPlugin::PlatformBrightness::kDark; + } else { + // The current OS does not support dark mode. (Older Windows 10 or before + // Windows 10) + return SettingsPlugin::PlatformBrightness::kLight; + } +} + +void SettingsPlugin::WatchPreferredBrightnessChanged() { + preferred_brightness_changed_watcher_ = + std::make_unique([this]() { + task_runner_->PostTask([this]() { + SendSettings(); + WatchPreferredBrightnessChanged(); + }); + }); + + RegNotifyChangeKeyValue( + preferred_brightness_reg_hkey_, FALSE, REG_NOTIFY_CHANGE_LAST_SET, + preferred_brightness_changed_watcher_->GetHandle(), TRUE); +} + +void SettingsPlugin::WatchTextScaleFactorChanged() { + text_scale_factor_changed_watcher_ = std::make_unique([this]() { + task_runner_->PostTask([this]() { + SendSettings(); + WatchTextScaleFactorChanged(); + }); + }); + + RegNotifyChangeKeyValue( + text_scale_factor_reg_hkey_, FALSE, REG_NOTIFY_CHANGE_LAST_SET, + text_scale_factor_changed_watcher_->GetHandle(), TRUE); +} + } // namespace flutter diff --git a/shell/platform/windows/settings_plugin.h b/shell/platform/windows/settings_plugin.h index a54c09e061b1d..6491e642ef1f8 100644 --- a/shell/platform/windows/settings_plugin.h +++ b/shell/platform/windows/settings_plugin.h @@ -5,10 +5,13 @@ #ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_SETTINGS_PLUGIN_H_ #define FLUTTER_SHELL_PLATFORM_WINDOWS_SETTINGS_PLUGIN_H_ +#include + #include #include "flutter/shell/platform/common/client_wrapper/include/flutter/basic_message_channel.h" #include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" +#include "flutter/shell/platform/windows/event_watcher.h" #include "flutter/shell/platform/windows/task_runner.h" #include "rapidjson/document.h" @@ -24,36 +27,42 @@ class SettingsPlugin { virtual ~SettingsPlugin(); - static std::unique_ptr Create(BinaryMessenger* messenger, - TaskRunner* task_runner); - // Sends settings (e.g., platform brightness) to the engine. void SendSettings(); // Start watching settings changes and notify the engine of the update. - virtual void StartWatching() = 0; + virtual void StartWatching(); // Stop watching settings change. The `SettingsPlugin` destructor will call // this automatically. - virtual void StopWatching() = 0; + virtual void StopWatching(); protected: enum struct PlatformBrightness { kDark, kLight }; // Returns `true` if the user uses 24 hour time. - virtual bool GetAlwaysUse24HourFormat() = 0; + virtual bool GetAlwaysUse24HourFormat(); // Returns the user-preferred text scale factor. - virtual float GetTextScaleFactor() = 0; + virtual float GetTextScaleFactor(); // Returns the user-preferred brightness. - virtual PlatformBrightness GetPreferredBrightness() = 0; - - TaskRunner* task_runner_; + virtual PlatformBrightness GetPreferredBrightness(); private: std::unique_ptr> channel_; + void WatchPreferredBrightnessChanged(); + void WatchTextScaleFactorChanged(); + + HKEY preferred_brightness_reg_hkey_ = nullptr; + HKEY text_scale_factor_reg_hkey_ = nullptr; + + std::unique_ptr preferred_brightness_changed_watcher_; + std::unique_ptr text_scale_factor_changed_watcher_; + + TaskRunner* task_runner_; + SettingsPlugin(const SettingsPlugin&) = delete; SettingsPlugin& operator=(const SettingsPlugin&) = delete; }; diff --git a/shell/platform/windows/settings_plugin_win32.cc b/shell/platform/windows/settings_plugin_win32.cc deleted file mode 100644 index 3b2382cdbb336..0000000000000 --- a/shell/platform/windows/settings_plugin_win32.cc +++ /dev/null @@ -1,121 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "flutter/shell/platform/windows/settings_plugin_win32.h" - -#include "flutter/shell/platform/windows/system_utils.h" - -namespace flutter { - -namespace { -constexpr wchar_t kGetPreferredBrightnessRegKey[] = - L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"; -constexpr wchar_t kGetPreferredBrightnessRegValue[] = L"AppsUseLightTheme"; - -constexpr wchar_t kGetTextScaleFactorRegKey[] = - L"Software\\Microsoft\\Accessibility"; -constexpr wchar_t kGetTextScaleFactorRegValue[] = L"TextScaleFactor"; -} // namespace - -// static -std::unique_ptr SettingsPlugin::Create( - BinaryMessenger* messenger, - TaskRunner* task_runner) { - return std::make_unique(messenger, task_runner); -} - -SettingsPluginWin32::SettingsPluginWin32(BinaryMessenger* messenger, - TaskRunner* task_runner) - : SettingsPlugin(messenger, task_runner) { - 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_); -} - -SettingsPluginWin32::~SettingsPluginWin32() { - StopWatching(); - RegCloseKey(preferred_brightness_reg_hkey_); - RegCloseKey(text_scale_factor_reg_hkey_); -} - -void SettingsPluginWin32::StartWatching() { - if (preferred_brightness_reg_hkey_ != nullptr) { - WatchPreferredBrightnessChanged(); - } - if (text_scale_factor_reg_hkey_ != nullptr) { - WatchTextScaleFactorChanged(); - } -} - -void SettingsPluginWin32::StopWatching() { - preferred_brightness_changed_watcher_ = nullptr; - text_scale_factor_changed_watcher_ = nullptr; -} - -bool SettingsPluginWin32::GetAlwaysUse24HourFormat() { - return Prefer24HourTime(GetUserTimeFormat()); -} - -float SettingsPluginWin32::GetTextScaleFactor() { - DWORD text_scale_factor; - DWORD text_scale_factor_size = sizeof(text_scale_factor); - LONG result = RegGetValue( - HKEY_CURRENT_USER, kGetTextScaleFactorRegKey, kGetTextScaleFactorRegValue, - RRF_RT_REG_DWORD, nullptr, &text_scale_factor, &text_scale_factor_size); - - if (result == 0) { - return text_scale_factor / 100.0; - } else { - // The current OS does not have text scale factor. - return 1.0; - } -} - -SettingsPlugin::PlatformBrightness -SettingsPluginWin32::GetPreferredBrightness() { - DWORD use_light_theme; - DWORD use_light_theme_size = sizeof(use_light_theme); - LONG result = RegGetValue(HKEY_CURRENT_USER, kGetPreferredBrightnessRegKey, - kGetPreferredBrightnessRegValue, RRF_RT_REG_DWORD, - nullptr, &use_light_theme, &use_light_theme_size); - - if (result == 0) { - return use_light_theme ? SettingsPlugin::PlatformBrightness::kLight - : SettingsPlugin::PlatformBrightness::kDark; - } else { - // The current OS does not support dark mode. (Older Windows 10 or before - // Windows 10) - return SettingsPlugin::PlatformBrightness::kLight; - } -} - -void SettingsPluginWin32::WatchPreferredBrightnessChanged() { - preferred_brightness_changed_watcher_ = - std::make_unique([this]() { - task_runner_->PostTask([this]() { - SendSettings(); - WatchPreferredBrightnessChanged(); - }); - }); - - RegNotifyChangeKeyValue( - preferred_brightness_reg_hkey_, FALSE, REG_NOTIFY_CHANGE_LAST_SET, - preferred_brightness_changed_watcher_->GetHandle(), TRUE); -} - -void SettingsPluginWin32::WatchTextScaleFactorChanged() { - text_scale_factor_changed_watcher_ = std::make_unique([this]() { - task_runner_->PostTask([this]() { - SendSettings(); - WatchTextScaleFactorChanged(); - }); - }); - - RegNotifyChangeKeyValue( - text_scale_factor_reg_hkey_, FALSE, REG_NOTIFY_CHANGE_LAST_SET, - text_scale_factor_changed_watcher_->GetHandle(), TRUE); -} - -} // namespace flutter diff --git a/shell/platform/windows/settings_plugin_win32.h b/shell/platform/windows/settings_plugin_win32.h deleted file mode 100644 index 914b4a8b1053e..0000000000000 --- a/shell/platform/windows/settings_plugin_win32.h +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_SETTINGS_PLUGIN_WIN32_H_ -#define FLUTTER_SHELL_PLATFORM_WINDOWS_SETTINGS_PLUGIN_WIN32_H_ - -#include "flutter/shell/platform/windows/settings_plugin.h" - -#include - -#include "flutter/shell/platform/windows/event_watcher.h" - -namespace flutter { - -// A settings plugin implementation for win32. -class SettingsPluginWin32 : public SettingsPlugin { - public: - explicit SettingsPluginWin32(BinaryMessenger* messenger, - TaskRunner* task_runner); - - virtual ~SettingsPluginWin32(); - - // |SettingsPlugin| - void StartWatching() override; - - // |SettingsPlugin| - void StopWatching() override; - - protected: - // |SettingsPlugin| - bool GetAlwaysUse24HourFormat() override; - - // |SettingsPlugin| - float GetTextScaleFactor() override; - - // |SettingsPlugin| - PlatformBrightness GetPreferredBrightness() override; - - private: - void WatchPreferredBrightnessChanged(); - void WatchTextScaleFactorChanged(); - - HKEY preferred_brightness_reg_hkey_ = nullptr; - HKEY text_scale_factor_reg_hkey_ = nullptr; - - std::unique_ptr preferred_brightness_changed_watcher_; - std::unique_ptr text_scale_factor_changed_watcher_; - - SettingsPluginWin32(const SettingsPluginWin32&) = delete; - SettingsPluginWin32& operator=(const SettingsPluginWin32&) = delete; -}; - -} // namespace flutter - -#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_SETTINGS_PLUGIN_WIN32_H_