Windows: Add dark theme support. (flutter/engine#28131)

This PR adds the dark theme support for both windows and winuwp engine, based on flutter/flutter#54612.

On Win32: using a registry value that is not documented. (I tested on Windows 10.0.19043.1165)
On UWP: using default background color. (I couldn't find WinRT API that returns dark/light directly, without xaml package.)

Flutter PR for win32: flutter/flutter#88520.
This PR will close flutter/flutter#54612.
This PR will complete one item in flutter/flutter#70214.
This commit is contained in:
moko256
2021-09-29 03:03:18 +09:00
committed by GitHub
parent 5bddc10646
commit b8809dde77
19 changed files with 139 additions and 3 deletions

View File

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

View File

@@ -49,6 +49,11 @@ 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_; }
@@ -57,6 +62,8 @@ 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_;
}
@@ -66,6 +73,7 @@ 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_;
};
@@ -124,6 +132,18 @@ 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

@@ -58,6 +58,11 @@ class FlutterEngine : public PluginRegistry {
// Win32 application).
void ReloadSystemFonts();
// Tells the engine that the platform brightness value has changed. Should be
// called by clients when OS-level theme changes happen (e.g.,
// WM_DWMCOLORIZATIONCOLORCHANGED in a Win32 application).
void ReloadPlatformBrightness();
// flutter::PluginRegistry:
FlutterDesktopPluginRegistrarRef GetRegistrarForPlugin(
const std::string& plugin_name) override;

View File

@@ -117,6 +117,13 @@ 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,6 +65,9 @@ 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

@@ -222,6 +222,7 @@ class MockWindowBindingHandlerDelegate : public WindowBindingHandlerDelegate {
int,
FlutterPointerDeviceKind,
int32_t));
MOCK_METHOD0(OnPlatformBrightnessChanged, void());
};
// A FlutterWindowsView that overrides the RegisterKeyboardHandlers function

View File

@@ -156,6 +156,9 @@ 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
@@ -355,6 +358,12 @@ 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

@@ -114,6 +114,11 @@ 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);
@@ -153,6 +158,9 @@ 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,6 +107,11 @@ void FlutterDesktopEngineReloadSystemFonts(FlutterDesktopEngineRef engine) {
EngineFromHandle(engine)->ReloadSystemFonts();
}
void FlutterDesktopEngineReloadPlatformBrightness(
FlutterDesktopEngineRef engine) {
EngineFromHandle(engine)->ReloadPlatformBrightness();
}
FlutterDesktopPluginRegistrarRef FlutterDesktopEngineGetPluginRegistrar(
FlutterDesktopEngineRef engine,
const char* plugin_name) {

View File

@@ -391,6 +391,12 @@ void FlutterWindowsEngine::ReloadSystemFonts() {
embedder_api_.ReloadSystemFonts(engine_);
}
void FlutterWindowsEngine::ReloadPlatformBrightness() {
if (engine_) {
SendSystemSettings();
}
}
void FlutterWindowsEngine::SendSystemSettings() {
std::vector<LanguageInfo> languages = GetPreferredLanguageInfo();
std::vector<FlutterLocale> flutter_locales;
@@ -413,9 +419,8 @@ void FlutterWindowsEngine::SendSystemSettings() {
settings.AddMember("alwaysUse24HourFormat",
Prefer24HourTime(GetUserTimeFormat()), allocator);
settings.AddMember("textScaleFactor", 1.0, allocator);
// TODO: Implement dark mode support.
// https://github.com/flutter/flutter/issues/54612
settings.AddMember("platformBrightness", "light", allocator);
settings.AddMember("platformBrightness",
Utf8FromUtf16(GetPreferredBrightness()), allocator);
settings_channel_->Send(settings);
}

View File

@@ -131,6 +131,9 @@ 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);

View File

@@ -249,6 +249,10 @@ void FlutterWindowsView::OnScroll(double x,
device_id);
}
void FlutterWindowsView::OnPlatformBrightnessChanged() {
SendPlatformBrightnessChanged();
}
void FlutterWindowsView::OnCursorRectUpdated(const Rect& rect) {
binding_handler_->OnCursorRectUpdated(rect);
}
@@ -465,6 +469,11 @@ void FlutterWindowsView::SendPointerEventWithData(
}
}
void FlutterWindowsView::SendPlatformBrightnessChanged() {
engine_->task_runner()->RunNowOrPostTask(
[this]() { engine_->ReloadPlatformBrightness(); });
};
bool FlutterWindowsView::MakeCurrent() {
return engine_->surface_manager()->MakeCurrent();
}

View File

@@ -149,6 +149,9 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate,
FlutterPointerDeviceKind device_kind,
int32_t device_id) override;
// |WindowBindingHandlerDelegate|
void OnPlatformBrightnessChanged() override;
// |TextInputPluginDelegate|
void OnCursorRectUpdated(const Rect& rect) override;
@@ -284,6 +287,9 @@ 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,6 +174,9 @@ 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

@@ -12,6 +12,11 @@
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;
@@ -37,6 +42,9 @@ 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,5 +88,10 @@ 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,4 +106,22 @@ 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,6 +10,8 @@
#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/shell/platform/windows/string_conversion.h"
@@ -80,4 +82,16 @@ 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

@@ -95,6 +95,9 @@ class WindowBindingHandlerDelegate {
int scroll_offset_multiplier,
FlutterPointerDeviceKind device_kind,
int32_t device_id) = 0;
// Notifies delegate that backing window has received brightness change event.
virtual void OnPlatformBrightnessChanged() = 0;
};
} // namespace flutter