Skip to content

Commit

Permalink
[Windows] Merge SettingsPluginWin32 and superclass (flutter#35019)
Browse files Browse the repository at this point in the history
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: flutter#108386
  • Loading branch information
cbracken authored Jul 30, 2022
1 parent 953c434 commit 2555b16
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 193 deletions.
2 changes: 0 additions & 2 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions shell/platform/windows/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions shell/platform/windows/flutter_windows_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
86 changes: 86 additions & 0 deletions shell/platform/windows/settings_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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,
Expand Down Expand Up @@ -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
29 changes: 19 additions & 10 deletions shell/platform/windows/settings_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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;
};
Expand Down
121 changes: 0 additions & 121 deletions shell/platform/windows/settings_plugin_win32.cc

This file was deleted.

56 changes: 0 additions & 56 deletions shell/platform/windows/settings_plugin_win32.h

This file was deleted.

0 comments on commit 2555b16

Please sign in to comment.