[Windows] Merge SettingsPluginWin32 and superclass (flutter/engine#35019)

This merges SettingsPluginWin32 into SettingsPlugin.

With the removal of the UWP embedder, we can merge Win32-specific
implementation classes with their abstract superclasses where those
superclasses existed only to support both UWP and Win32.

No new tests since this is purely a restructuring of existing code
within the Win32 embedder, with no expected change in behaviour.

Issue: https://github.com/flutter/flutter/issues/108386
This commit is contained in:
Chris Bracken
2022-07-30 19:07:20 +01:00
committed by GitHub
parent 3a3177edb8
commit 75fbd93951
7 changed files with 107 additions and 193 deletions

View File

@@ -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

View File

@@ -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",

View File

@@ -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<SettingsPlugin>(messenger_wrapper_.get(),
task_runner_.get());
}
FlutterWindowsEngine::~FlutterWindowsEngine() {

View File

@@ -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<EventWatcher>([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<EventWatcher>([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

View File

@@ -5,10 +5,13 @@
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_SETTINGS_PLUGIN_H_
#define FLUTTER_SHELL_PLATFORM_WINDOWS_SETTINGS_PLUGIN_H_
#include <Windows.h>
#include <memory>
#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<SettingsPlugin> 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<BasicMessageChannel<rapidjson::Document>> channel_;
void WatchPreferredBrightnessChanged();
void WatchTextScaleFactorChanged();
HKEY preferred_brightness_reg_hkey_ = nullptr;
HKEY text_scale_factor_reg_hkey_ = nullptr;
std::unique_ptr<EventWatcher> preferred_brightness_changed_watcher_;
std::unique_ptr<EventWatcher> text_scale_factor_changed_watcher_;
TaskRunner* task_runner_;
SettingsPlugin(const SettingsPlugin&) = delete;
SettingsPlugin& operator=(const SettingsPlugin&) = delete;
};

View File

@@ -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> SettingsPlugin::Create(
BinaryMessenger* messenger,
TaskRunner* task_runner) {
return std::make_unique<SettingsPluginWin32>(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<EventWatcher>([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<EventWatcher>([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

View File

@@ -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 <Windows.h>
#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<EventWatcher> preferred_brightness_changed_watcher_;
std::unique_ptr<EventWatcher> 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_