Windows: Add SettingsPlugin to add theme change support in win32 (flutter/engine#29303)

* Windows: Add SettingsPlugin to separate sending and watching

* remove OnColorValuesChanged

* remove functions and test to handle brightness change from top-level window message

* add settings plugin test

* add files to licenses

* fix license

* use std::optional

* treat error

* fix comment based on code review
This commit is contained in:
moko256
2022-04-29 17:10:24 +09:00
committed by GitHub
parent e4cb095cf7
commit efd03ad7ee
28 changed files with 537 additions and 154 deletions

View File

@@ -2227,6 +2227,8 @@ FILE: ../../../flutter/shell/platform/windows/display_helper_winuwp.h
FILE: ../../../flutter/shell/platform/windows/dpi_utils_win32.cc
FILE: ../../../flutter/shell/platform/windows/dpi_utils_win32.h
FILE: ../../../flutter/shell/platform/windows/dpi_utils_win32_unittests.cc
FILE: ../../../flutter/shell/platform/windows/event_watcher_win32.cc
FILE: ../../../flutter/shell/platform/windows/event_watcher_win32.h
FILE: ../../../flutter/shell/platform/windows/external_texture_gl.cc
FILE: ../../../flutter/shell/platform/windows/external_texture_gl.h
FILE: ../../../flutter/shell/platform/windows/flutter_key_map.cc
@@ -2283,6 +2285,13 @@ FILE: ../../../flutter/shell/platform/windows/public/flutter_windows.h
FILE: ../../../flutter/shell/platform/windows/sequential_id_generator.cc
FILE: ../../../flutter/shell/platform/windows/sequential_id_generator.h
FILE: ../../../flutter/shell/platform/windows/sequential_id_generator_unittests.cc
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/settings_plugin_winuwp.cc
FILE: ../../../flutter/shell/platform/windows/settings_plugin_winuwp.h
FILE: ../../../flutter/shell/platform/windows/system_utils.h
FILE: ../../../flutter/shell/platform/windows/system_utils_unittests.cc
FILE: ../../../flutter/shell/platform/windows/system_utils_win32.cc

View File

@@ -79,6 +79,8 @@ source_set("flutter_windows_source") {
"platform_handler.h",
"sequential_id_generator.cc",
"sequential_id_generator.h",
"settings_plugin.cc",
"settings_plugin.h",
"system_utils.h",
"task_runner.cc",
"task_runner.h",
@@ -107,6 +109,8 @@ source_set("flutter_windows_source") {
"game_pad_winuwp.h",
"platform_handler_winuwp.cc",
"platform_handler_winuwp.h",
"settings_plugin_winuwp.cc",
"settings_plugin_winuwp.h",
"system_utils_winuwp.cc",
"task_runner_winuwp.cc",
"task_runner_winuwp.h",
@@ -117,6 +121,8 @@ source_set("flutter_windows_source") {
"accessibility_bridge_delegate_win32.h",
"dpi_utils_win32.cc",
"dpi_utils_win32.h",
"event_watcher_win32.cc",
"event_watcher_win32.h",
"flutter_platform_node_delegate_win32.cc",
"flutter_platform_node_delegate_win32.h",
"flutter_window_win32.cc",
@@ -124,6 +130,8 @@ source_set("flutter_windows_source") {
"flutter_windows_win32.cc",
"platform_handler_win32.cc",
"platform_handler_win32.h",
"settings_plugin_win32.cc",
"settings_plugin_win32.h",
"system_utils_win32.cc",
"task_runner_win32.cc",
"task_runner_win32.h",
@@ -226,6 +234,7 @@ executable("flutter_windows_unittests") {
# "flutter_project_bundle_unittests.cc", //TODO failing due to switches test failing. Blocked on https://github.com/flutter/flutter/issues/74153
# "flutter_windows_engine_unittests.cc", //TODO failing to send / receive platform message get plugins working first. Blocked on https://github.com/flutter/flutter/issues/74155
"sequential_id_generator_unittests.cc",
"settings_plugin_unittests.cc",
"system_utils_unittests.cc",
"task_runner_unittests.cc",
"testing/engine_modifier.h",

View File

@@ -74,10 +74,6 @@ void FlutterEngine::ReloadSystemFonts() {
FlutterDesktopEngineReloadSystemFonts(engine_);
}
void FlutterEngine::ReloadPlatformBrightness() {
FlutterDesktopEngineReloadPlatformBrightness(engine_);
}
FlutterDesktopPluginRegistrarRef FlutterEngine::GetRegistrarForPlugin(
const std::string& plugin_name) {
if (!engine_) {

View File

@@ -49,11 +49,6 @@ 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_; }
@@ -62,8 +57,6 @@ class TestFlutterWindowsApi : public testing::StubFlutterWindowsApi {
bool reload_fonts_called() { return reload_fonts_called_; }
bool reload_brightness_called() { return reload_brightness_called_; }
const std::vector<std::string>& dart_entrypoint_arguments() {
return dart_entrypoint_arguments_;
}
@@ -73,7 +66,6 @@ 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<std::string> dart_entrypoint_arguments_;
};
@@ -132,18 +124,6 @@ TEST(FlutterEngineTest, ReloadFonts) {
EXPECT_TRUE(test_api->reload_fonts_called());
}
TEST(FlutterEngineTest, ReloadBrightness) {
testing::ScopedStubFlutterWindowsApi scoped_api_stub(
std::make_unique<TestFlutterWindowsApi>());
auto test_api = static_cast<TestFlutterWindowsApi*>(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(

View File

@@ -117,13 +117,6 @@ void FlutterDesktopEngineReloadSystemFonts(FlutterDesktopEngineRef engine) {
}
}
void FlutterDesktopEngineReloadPlatformBrightness(
FlutterDesktopEngineRef engine) {
if (s_stub_implementation) {
s_stub_implementation->EngineReloadPlatformBrightness();
}
}
FlutterDesktopPluginRegistrarRef FlutterDesktopEngineGetPluginRegistrar(
FlutterDesktopEngineRef engine,
const char* plugin_name) {

View File

@@ -65,9 +65,6 @@ class StubFlutterWindowsApi {
// Called for FlutterDesktopEngineReloadSystemFonts.
virtual void EngineReloadSystemFonts() {}
// Called for FlutterDesktopEngineReloadPlatformBrightness.
virtual void EngineReloadPlatformBrightness() {}
// Called for FlutterDesktopViewGetHWND.
virtual HWND ViewGetHWND() { return reinterpret_cast<HWND>(1); }

View File

@@ -0,0 +1,34 @@
// 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/event_watcher_win32.h"
namespace flutter {
EventWatcherWin32::EventWatcherWin32(std::function<void()> callback)
: callback_(callback) {
handle_ = CreateEvent(NULL, TRUE, FALSE, NULL);
RegisterWaitForSingleObject(&handle_for_wait_, handle_, &CallbackForWait,
reinterpret_cast<void*>(this), INFINITE,
WT_EXECUTEONLYONCE | WT_EXECUTEINWAITTHREAD);
}
EventWatcherWin32::~EventWatcherWin32() {
UnregisterWait(handle_for_wait_);
CloseHandle(handle_);
}
HANDLE EventWatcherWin32::GetHandle() {
return handle_;
}
// static
VOID CALLBACK EventWatcherWin32::CallbackForWait(PVOID context, BOOLEAN) {
EventWatcherWin32* self = reinterpret_cast<EventWatcherWin32*>(context);
ResetEvent(self->handle_);
self->callback_();
}
} // namespace flutter

View File

@@ -0,0 +1,37 @@
// 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_EVENT_WATCHER_WIN32_H_
#define FLUTTER_SHELL_PLATFORM_WINDOWS_EVENT_WATCHER_WIN32_H_
#include <Windows.h>
#include <functional>
namespace flutter {
// A win32 `HANDLE` wrapper for use as a one-time callback.
class EventWatcherWin32 {
public:
explicit EventWatcherWin32(std::function<void()> callback);
~EventWatcherWin32();
// Returns `HANDLE`, which can be used to register an event listener.
HANDLE GetHandle();
private:
static VOID CALLBACK CallbackForWait(PVOID context, BOOLEAN);
std::function<void()> callback_;
HANDLE handle_;
HANDLE handle_for_wait_;
EventWatcherWin32(const EventWatcherWin32&) = delete;
EventWatcherWin32& operator=(const EventWatcherWin32&) = delete;
};
} // namespace flutter
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_EVENT_WATCHER_WIN32_H_

View File

@@ -166,9 +166,6 @@ 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
@@ -383,12 +380,6 @@ 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) {

View File

@@ -118,11 +118,6 @@ 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);
@@ -168,9 +163,6 @@ 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<GamepadCursorWinUWP> game_pad_cursor_{nullptr};

View File

@@ -107,11 +107,6 @@ void FlutterDesktopEngineReloadSystemFonts(FlutterDesktopEngineRef engine) {
EngineFromHandle(engine)->ReloadSystemFonts();
}
void FlutterDesktopEngineReloadPlatformBrightness(
FlutterDesktopEngineRef engine) {
EngineFromHandle(engine)->ReloadPlatformBrightness();
}
FlutterDesktopPluginRegistrarRef FlutterDesktopEngineGetPluginRegistrar(
FlutterDesktopEngineRef engine,
const char* plugin_name) {

View File

@@ -10,13 +10,10 @@
#include "flutter/fml/platform/win/wstring_conversion.h"
#include "flutter/shell/platform/common/client_wrapper/binary_messenger_impl.h"
#include "flutter/shell/platform/common/client_wrapper/include/flutter/basic_message_channel.h"
#include "flutter/shell/platform/common/json_message_codec.h"
#include "flutter/shell/platform/common/path_utils.h"
#include "flutter/shell/platform/windows/flutter_windows_view.h"
#include "flutter/shell/platform/windows/system_utils.h"
#include "flutter/shell/platform/windows/task_runner.h"
#include "third_party/rapidjson/include/rapidjson/document.h"
#if defined(WINUWP)
#include "flutter/shell/platform/windows/accessibility_bridge_delegate_winuwp.h"
@@ -197,10 +194,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_channel_ =
std::make_unique<BasicMessageChannel<rapidjson::Document>>(
messenger_wrapper_.get(), "flutter/settings",
&JsonMessageCodec::GetInstance());
settings_plugin_ =
SettingsPlugin::Create(messenger_wrapper_.get(), task_runner_.get());
}
FlutterWindowsEngine::~FlutterWindowsEngine() {
@@ -341,7 +336,10 @@ bool FlutterWindowsEngine::RunWithEntrypoint(const char* entrypoint) {
kFlutterEngineDisplaysUpdateTypeStartup,
displays.data(), displays.size());
SendSystemSettings();
SendSystemLocales();
settings_plugin_->StartWatching();
settings_plugin_->SendSettings();
return true;
}
@@ -485,13 +483,7 @@ void FlutterWindowsEngine::ReloadSystemFonts() {
embedder_api_.ReloadSystemFonts(engine_);
}
void FlutterWindowsEngine::ReloadPlatformBrightness() {
if (engine_) {
SendSystemSettings();
}
}
void FlutterWindowsEngine::SendSystemSettings() {
void FlutterWindowsEngine::SendSystemLocales() {
std::vector<LanguageInfo> languages = GetPreferredLanguageInfo();
std::vector<FlutterLocale> flutter_locales;
flutter_locales.reserve(languages.size());
@@ -507,16 +499,6 @@ void FlutterWindowsEngine::SendSystemSettings() {
[](const auto& arg) -> const auto* { return &arg; });
embedder_api_.UpdateLocales(engine_, flutter_locale_list.data(),
flutter_locale_list.size());
rapidjson::Document settings(rapidjson::kObjectType);
auto& allocator = settings.GetAllocator();
settings.AddMember("alwaysUse24HourFormat",
Prefer24HourTime(GetUserTimeFormat()), allocator);
settings.AddMember("textScaleFactor", 1.0, allocator);
settings.AddMember("platformBrightness",
fml::WideStringToUtf8(GetPreferredBrightness()),
allocator);
settings_channel_->Send(settings);
}
bool FlutterWindowsEngine::RegisterExternalTexture(int64_t texture_id) {

View File

@@ -20,6 +20,7 @@
#include "flutter/shell/platform/windows/flutter_project_bundle.h"
#include "flutter/shell/platform/windows/flutter_windows_texture_registrar.h"
#include "flutter/shell/platform/windows/public/flutter_windows.h"
#include "flutter/shell/platform/windows/settings_plugin.h"
#include "flutter/shell/platform/windows/task_runner.h"
#include "flutter/shell/platform/windows/window_state.h"
#include "third_party/rapidjson/include/rapidjson/document.h"
@@ -164,9 +165,6 @@ 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);
@@ -198,11 +196,11 @@ class FlutterWindowsEngine {
// Allows swapping out embedder_api_ calls in tests.
friend class EngineModifier;
// Sends system settings (e.g., locale) to the engine.
// Sends system locales to the engine.
//
// Should be called just after the engine is run, and after any relevant
// system changes.
void SendSystemSettings();
void SendSystemLocales();
// The handle to the embedder.h engine instance.
FLUTTER_API_SYMBOL(FlutterEngine) engine_ = nullptr;
@@ -243,8 +241,8 @@ class FlutterWindowsEngine {
// May be nullptr if ANGLE failed to initialize.
std::unique_ptr<AngleSurfaceManager> surface_manager_;
// The MethodChannel used for communication with the Flutter engine.
std::unique_ptr<BasicMessageChannel<rapidjson::Document>> settings_channel_;
// The settings plugin.
std::unique_ptr<SettingsPlugin> settings_plugin_;
// Callbacks to be called when the engine (and thus the plugin registrar) is
// being destroyed.

View File

@@ -236,10 +236,6 @@ void FlutterWindowsView::OnScroll(double x,
device_id);
}
void FlutterWindowsView::OnPlatformBrightnessChanged() {
SendPlatformBrightnessChanged();
}
void FlutterWindowsView::OnUpdateSemanticsEnabled(bool enabled) {
engine_->UpdateSemanticsEnabled(enabled);
}
@@ -479,11 +475,6 @@ void FlutterWindowsView::SendPointerEventWithData(
}
}
void FlutterWindowsView::SendPlatformBrightnessChanged() {
engine_->task_runner()->RunNowOrPostTask(
[this]() { engine_->ReloadPlatformBrightness(); });
};
bool FlutterWindowsView::MakeCurrent() {
return engine_->surface_manager()->MakeCurrent();
}

View File

@@ -156,9 +156,6 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate,
FlutterPointerDeviceKind device_kind,
int32_t device_id) override;
// |WindowBindingHandlerDelegate|
void OnPlatformBrightnessChanged() override;
// |WindowBindingHandlerDelegate|
virtual void OnUpdateSemanticsEnabled(bool enabled) override;
@@ -309,9 +306,6 @@ 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.

View File

@@ -174,9 +174,6 @@ 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.

View File

@@ -0,0 +1,48 @@
// 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.h"
#include "flutter/shell/platform/common/json_message_codec.h"
namespace flutter {
namespace {
constexpr char kChannelName[] = "flutter/settings";
constexpr char kAlwaysUse24HourFormat[] = "alwaysUse24HourFormat";
constexpr char kTextScaleFactor[] = "textScaleFactor";
constexpr char kPlatformBrightness[] = "platformBrightness";
constexpr char kPlatformBrightnessDark[] = "dark";
constexpr char kPlatformBrightnessLight[] = "light";
} // namespace
SettingsPlugin::SettingsPlugin(BinaryMessenger* messenger,
TaskRunner* task_runner)
: channel_(std::make_unique<BasicMessageChannel<rapidjson::Document>>(
messenger,
kChannelName,
&JsonMessageCodec::GetInstance())),
task_runner_(task_runner) {}
SettingsPlugin::~SettingsPlugin() = default;
void SettingsPlugin::SendSettings() {
rapidjson::Document settings(rapidjson::kObjectType);
auto& allocator = settings.GetAllocator();
settings.AddMember(kAlwaysUse24HourFormat, GetAlwaysUse24HourFormat(),
allocator);
settings.AddMember(kTextScaleFactor, GetTextScaleFactor(), allocator);
if (GetPreferredBrightness() == PlatformBrightness::kDark) {
settings.AddMember(kPlatformBrightness, kPlatformBrightnessDark, allocator);
} else {
settings.AddMember(kPlatformBrightness, kPlatformBrightnessLight,
allocator);
}
channel_->Send(settings);
}
} // namespace flutter

View File

@@ -0,0 +1,63 @@
// 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_H_
#define FLUTTER_SHELL_PLATFORM_WINDOWS_SETTINGS_PLUGIN_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/task_runner.h"
#include "rapidjson/document.h"
namespace flutter {
// Abstract settings plugin.
//
// Used to look up and notify Flutter of user-configured system settings.
// These are typically set in the control panel.
class SettingsPlugin {
public:
explicit SettingsPlugin(BinaryMessenger* messenger, TaskRunner* task_runner);
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;
// Stop watching settings change. The `SettingsPlugin` destructor will call
// this automatically.
virtual void StopWatching() = 0;
protected:
enum struct PlatformBrightness { kDark, kLight };
// Returns `true` if the user uses 24 hour time.
virtual bool GetAlwaysUse24HourFormat() = 0;
// Returns the user-preferred text scale factor.
virtual float GetTextScaleFactor() = 0;
// Returns the user-preferred brightness.
virtual PlatformBrightness GetPreferredBrightness() = 0;
TaskRunner* task_runner_;
private:
std::unique_ptr<BasicMessageChannel<rapidjson::Document>> channel_;
SettingsPlugin(const SettingsPlugin&) = delete;
SettingsPlugin& operator=(const SettingsPlugin&) = delete;
};
} // namespace flutter
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_SETTINGS_PLUGIN_H_

View File

@@ -0,0 +1,62 @@
// 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.h"
#include "flutter/shell/platform/windows/task_runner.h"
#include "flutter/shell/platform/windows/testing/test_binary_messenger.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace flutter {
namespace testing {
namespace {
class MockSettingsPlugin : public SettingsPlugin {
public:
explicit MockSettingsPlugin(BinaryMessenger* messenger,
TaskRunner* task_runner)
: SettingsPlugin(messenger, task_runner) {}
virtual ~MockSettingsPlugin() = default;
// |SettingsPlugin|
MOCK_METHOD0(StartWatching, void());
MOCK_METHOD0(StopWatching, void());
MOCK_METHOD0(GetAlwaysUse24HourFormat, bool());
MOCK_METHOD0(GetTextScaleFactor, float());
MOCK_METHOD0(GetPreferredBrightness, PlatformBrightness());
};
} // namespace
TEST(SettingsPluginTest, SendSettingsSendsMessage) {
bool message_is_sent = false;
TestBinaryMessenger messenger(
[&message_is_sent](const std::string& channel, const uint8_t* message,
size_t message_size,
BinaryReply reply) { message_is_sent = true; });
::testing::NiceMock<MockSettingsPlugin> settings_plugin(&messenger, nullptr);
settings_plugin.SendSettings();
EXPECT_TRUE(message_is_sent);
}
TEST(SettingsPluginTest, SendSettingsGetsSettings) {
TestBinaryMessenger messenger([](const std::string& channel,
const uint8_t* message, size_t message_size,
BinaryReply reply) {});
::testing::NiceMock<MockSettingsPlugin> settings_plugin(&messenger, nullptr);
EXPECT_CALL(settings_plugin, GetAlwaysUse24HourFormat).Times(1);
EXPECT_CALL(settings_plugin, GetTextScaleFactor).Times(1);
EXPECT_CALL(settings_plugin, GetPreferredBrightness).Times(1);
settings_plugin.SendSettings();
}
} // namespace testing
} // namespace flutter

View File

@@ -0,0 +1,86 @@
// 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";
} // 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_);
}
SettingsPluginWin32::~SettingsPluginWin32() {
StopWatching();
RegCloseKey(preferred_brightness_reg_hkey_);
}
void SettingsPluginWin32::StartWatching() {
if (preferred_brightness_reg_hkey_ != NULL) {
WatchPreferredBrightnessChanged();
}
}
void SettingsPluginWin32::StopWatching() {
preferred_brightness_changed_watcher_ = nullptr;
}
bool SettingsPluginWin32::GetAlwaysUse24HourFormat() {
return Prefer24HourTime(GetUserTimeFormat());
}
float SettingsPluginWin32::GetTextScaleFactor() {
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<EventWatcherWin32>([this]() {
task_runner_->PostTask([this]() {
SendSettings();
WatchPreferredBrightnessChanged();
});
});
RegNotifyChangeKeyValue(
preferred_brightness_reg_hkey_, FALSE, REG_NOTIFY_CHANGE_LAST_SET,
preferred_brightness_changed_watcher_->GetHandle(), TRUE);
}
} // namespace flutter

View File

@@ -0,0 +1,54 @@
// 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_win32.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();
HKEY preferred_brightness_reg_hkey_ = NULL;
std::unique_ptr<EventWatcherWin32> preferred_brightness_changed_watcher_{
nullptr};
SettingsPluginWin32(const SettingsPluginWin32&) = delete;
SettingsPluginWin32& operator=(const SettingsPluginWin32&) = delete;
};
} // namespace flutter
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_SETTINGS_PLUGIN_WIN32_H_

View File

@@ -0,0 +1,67 @@
// 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_winuwp.h"
#include "flutter/shell/platform/windows/system_utils.h"
#include "third_party/cppwinrt/generated/winrt/base.h"
namespace flutter {
// static
std::unique_ptr<SettingsPlugin> SettingsPlugin::Create(
BinaryMessenger* messenger,
TaskRunner* task_runner) {
return std::make_unique<SettingsPluginWinUwp>(messenger, task_runner);
}
SettingsPluginWinUwp::SettingsPluginWinUwp(BinaryMessenger* messenger,
TaskRunner* task_runner)
: SettingsPlugin(messenger, task_runner) {}
SettingsPluginWinUwp::~SettingsPluginWinUwp() {
StopWatching();
}
void SettingsPluginWinUwp::StartWatching() {
color_values_changed_ = ui_settings_.ColorValuesChanged(
{this, &SettingsPluginWinUwp::OnColorValuesChanged});
}
void SettingsPluginWinUwp::StopWatching() {
if (color_values_changed_.has_value()) {
ui_settings_.ColorValuesChanged(color_values_changed_.value());
color_values_changed_ = std::nullopt;
}
}
bool SettingsPluginWinUwp::GetAlwaysUse24HourFormat() {
return Prefer24HourTime(GetUserTimeFormat());
}
float SettingsPluginWinUwp::GetTextScaleFactor() {
return 1.0;
}
SettingsPlugin::PlatformBrightness
SettingsPluginWinUwp::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 SettingsPlugin::PlatformBrightness::kDark;
} else {
return SettingsPlugin::PlatformBrightness::kLight;
}
}
void SettingsPluginWinUwp::OnColorValuesChanged(
winrt::Windows::Foundation::IInspectable const&,
winrt::Windows::Foundation::IInspectable const&) {
task_runner_->RunNowOrPostTask([this]() { SendSettings(); });
}
} // namespace flutter

View File

@@ -0,0 +1,56 @@
// 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_WINUWP_H_
#define FLUTTER_SHELL_PLATFORM_WINDOWS_SETTINGS_PLUGIN_WINUWP_H_
#include "flutter/shell/platform/windows/settings_plugin.h"
#include <memory>
#include <optional>
#include "third_party/cppwinrt/generated/winrt/Windows.Foundation.h"
#include "third_party/cppwinrt/generated/winrt/Windows.UI.ViewManagement.h"
namespace flutter {
// A settings plugin implementation for UWP.
class SettingsPluginWinUwp : public SettingsPlugin {
public:
explicit SettingsPluginWinUwp(BinaryMessenger* messenger,
TaskRunner* task_runner);
virtual ~SettingsPluginWinUwp();
// |SettingsPlugin|
void StartWatching() override;
// |SettingsPlugin|
void StopWatching() override;
protected:
// |SettingsPlugin|
bool GetAlwaysUse24HourFormat() override;
// |SettingsPlugin|
float GetTextScaleFactor() override;
// |SettingsPlugin|
PlatformBrightness GetPreferredBrightness() override;
private:
void OnColorValuesChanged(winrt::Windows::Foundation::IInspectable const&,
winrt::Windows::Foundation::IInspectable const&);
winrt::Windows::UI::ViewManagement::UISettings ui_settings_;
std::optional<winrt::event_token> color_values_changed_ = std::nullopt;
SettingsPluginWinUwp(const SettingsPluginWinUwp&) = delete;
SettingsPluginWinUwp& operator=(const SettingsPluginWinUwp&) = delete;
};
} // namespace flutter
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_SETTINGS_PLUGIN_WINUWP_H_

View File

@@ -12,11 +12,6 @@
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;
@@ -42,9 +37,6 @@ 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_

View File

@@ -88,10 +88,5 @@ 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

View File

@@ -106,22 +106,4 @@ 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

View File

@@ -10,8 +10,6 @@
#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/fml/platform/win/wstring_conversion.h"
@@ -82,16 +80,4 @@ 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

View File

@@ -102,9 +102,6 @@ class WindowBindingHandlerDelegate {
FlutterPointerDeviceKind device_kind,
int32_t device_id) = 0;
// Notifies delegate that backing window has received brightness change event.
virtual void OnPlatformBrightnessChanged() = 0;
// Notifies delegate that the Flutter semantics tree should be enabled or
// disabled.
virtual void OnUpdateSemanticsEnabled(bool enabled) = 0;