Skip to content

Commit

Permalink
plugins: add settings_plugin (#364)
Browse files Browse the repository at this point in the history
Related issue #2

Signed-off-by: Hidenori Matsubayashi <[email protected]>
  • Loading branch information
HidenoriMatsubayashi authored Aug 8, 2023
1 parent eb2687a commit 8d8ab0a
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 23 deletions.
1 change: 1 addition & 0 deletions cmake/build.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ set(ELINUX_COMMON_SRC
"src/flutter/shell/platform/linux_embedded/plugins/navigation_plugin.cc"
"src/flutter/shell/platform/linux_embedded/plugins/platform_plugin.cc"
"src/flutter/shell/platform/linux_embedded/plugins/platform_views_plugin.cc"
"src/flutter/shell/platform/linux_embedded/plugins/settings_plugin.cc"
"src/flutter/shell/platform/linux_embedded/plugins/text_input_plugin.cc"
"src/flutter/shell/platform/linux_embedded/surface/context_egl.cc"
"src/flutter/shell/platform/linux_embedded/surface/egl_utils.cc"
Expand Down
23 changes: 5 additions & 18 deletions src/flutter/shell/platform/linux_embedded/flutter_elinux_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,6 @@ FlutterELinuxEngine::FlutterELinuxEngine(const FlutterProjectBundle& project)
texture_registrar_ =
std::make_unique<FlutterELinuxTextureRegistrar>(this, gl_procs_);

// Set up internal channels.
// TODO: Replace this with an embedder.h API. See
// https:/flutter/flutter/issues/71099
settings_channel_ =
std::make_unique<BasicMessageChannel<rapidjson::Document>>(
messenger_wrapper_.get(), "flutter/settings",
&JsonMessageCodec::GetInstance());

vsync_waiter_ = std::make_unique<VsyncWaiter>();
}

Expand Down Expand Up @@ -285,7 +277,10 @@ bool FlutterELinuxEngine::RunWithEntrypoint(const char* entrypoint) {
return false;
}

SendSystemSettings();
// TODO: add theme initial value support.
view_->UpdateHighContrastEnabled(false);

SendSystemLocales();

return true;
}
Expand Down Expand Up @@ -389,7 +384,7 @@ void FlutterELinuxEngine::ReloadSystemFonts() {
embedder_api_.ReloadSystemFonts(engine_);
}

void FlutterELinuxEngine::SendSystemSettings() {
void FlutterELinuxEngine::SendSystemLocales() {
auto languages = flutter::GetPreferredLanguageInfo();
auto flutter_locales = flutter::ConvertToFlutterLocale(languages);

Expand All @@ -405,14 +400,6 @@ void FlutterELinuxEngine::SendSystemSettings() {
if (result != kSuccess) {
ELINUX_LOG(ERROR) << "Failed to set up Flutter locales.";
}

rapidjson::Document settings(rapidjson::kObjectType);
auto& allocator = settings.GetAllocator();
// todo: Use set values instead of fixed values.
settings.AddMember("alwaysUse24HourFormat", true, allocator);
settings.AddMember("textScaleFactor", 1.0, allocator);
settings.AddMember("platformBrightness", "light", allocator);
settings_channel_->Send(settings);
}

bool FlutterELinuxEngine::RegisterExternalTexture(int64_t texture_id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ class FlutterELinuxEngine {
// Allows swapping out embedder_api_ calls in tests.
friend class EngineEmbedderApiModifier;

// 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;
Expand Down Expand Up @@ -172,9 +172,6 @@ class FlutterELinuxEngine {
// Resolved OpenGL functions used by external texture implementations.
GlProcs gl_procs_ = {};

// The MethodChannel used for communication with the Flutter engine.
std::unique_ptr<BasicMessageChannel<rapidjson::Document>> settings_channel_;

// A callback to be called when the engine (and thus the plugin registrar)
// is being destroyed.
FlutterDesktopOnPluginRegistrarDestroyed
Expand Down
12 changes: 12 additions & 0 deletions src/flutter/shell/platform/linux_embedded/flutter_elinux_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ void FlutterELinuxView::SetEngine(std::unique_ptr<FlutterELinuxEngine> engine) {

// Set up the system channel handlers.
auto internal_plugin_messenger = internal_plugin_registrar_->messenger();

// Set up internal channels.
// TODO: Replace this with an embedder.h API. See
// https:/flutter/flutter/issues/71099
keyboard_handler_ =
std::make_unique<flutter::KeyeventPlugin>(internal_plugin_messenger);
textinput_handler_ = std::make_unique<flutter::TextInputPlugin>(
Expand All @@ -74,6 +78,8 @@ void FlutterELinuxView::SetEngine(std::unique_ptr<FlutterELinuxEngine> engine) {
std::make_unique<flutter::NavigationPlugin>(internal_plugin_messenger);
platform_views_handler_ =
std::make_unique<flutter::PlatformViewsPlugin>(internal_plugin_messenger);
settings_handler_ = std::make_unique<flutter::SettingsPlugin>(
internal_plugin_messenger, binding_handler_.get());

PhysicalWindowBounds bounds = binding_handler_->GetPhysicalWindowBounds();
SendWindowMetrics(bounds.width, bounds.height,
Expand Down Expand Up @@ -498,4 +504,10 @@ std::pair<double, double> FlutterELinuxView::GetPointerRotation(double x_px,
}
return res;
}

void FlutterELinuxView::UpdateHighContrastEnabled(bool enabled) {
// TODO: add UpdateAccessibilityFeatures support
settings_handler_->UpdateHighContrastMode(enabled);
}

} // namespace flutter
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "flutter/shell/platform/linux_embedded/plugins/navigation_plugin.h"
#include "flutter/shell/platform/linux_embedded/plugins/platform_plugin.h"
#include "flutter/shell/platform/linux_embedded/plugins/platform_views_plugin.h"
#include "flutter/shell/platform/linux_embedded/plugins/settings_plugin.h"
#include "flutter/shell/platform/linux_embedded/plugins/text_input_plugin.h"
#include "flutter/shell/platform/linux_embedded/public/flutter_elinux.h"
#include "flutter/shell/platform/linux_embedded/public/flutter_platform_views.h"
Expand Down Expand Up @@ -144,6 +145,9 @@ class FlutterELinuxView : public WindowBindingHandlerDelegate {
void OnVsync(uint64_t frame_start_time_nanos,
uint64_t frame_target_time_nanos) override;

// |WindowBindingHandlerDelegate|
void UpdateHighContrastEnabled(bool enabled) override;

private:
// Struct holding the mouse state. The engine doesn't keep track of which
// mouse buttons have been pressed, so it's the embedding's responsibility.
Expand Down Expand Up @@ -290,6 +294,9 @@ class FlutterELinuxView : public WindowBindingHandlerDelegate {
// Handler for flutter/platform_views channel.
std::unique_ptr<flutter::PlatformViewsPlugin> platform_views_handler_;

// Handler for flutter/settings channel.
std::unique_ptr<flutter::SettingsPlugin> settings_handler_;

// Currently configured WindowBindingHandler for view.
std::unique_ptr<flutter::WindowBindingHandler> binding_handler_;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2023 Sony Corporation. All rights reserved.
// 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/linux_embedded/plugins/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,
WindowBindingHandler* delegate)
: channel_(std::make_unique<BasicMessageChannel<rapidjson::Document>>(
messenger,
kChannelName,
&JsonMessageCodec::GetInstance())),
delegate_(delegate) {}

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);
}

bool SettingsPlugin::GetAlwaysUse24HourFormat() {
// The current OS does not have 24 hour format factor.
return true;
}

float SettingsPlugin::GetTextScaleFactor() {
// The current OS does not have text scale factor.
return 1.0;
}

SettingsPlugin::PlatformBrightness SettingsPlugin::GetPreferredBrightness() {
// The current OS does not have brightness factor.
return PlatformBrightness::kLight;
}

void SettingsPlugin::UpdateHighContrastMode(bool is_high_contrast) {
is_high_contrast_ = is_high_contrast;
SendSettings();
}

} // namespace flutter
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2023 Sony Corporation. All rights reserved.
// 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_LINUX_EMBEDDED_PLUGINS_SETTINGS_PLUGIN_H_
#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_SETTINGS_PLUGIN_H_

#include <rapidjson/document.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/linux_embedded/window_binding_handler.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:
enum struct PlatformBrightness { kDark, kLight };

explicit SettingsPlugin(BinaryMessenger* messenger,
WindowBindingHandler* delegate);
~SettingsPlugin() = default;

// Sends settings (e.g., platform brightness) to the engine.
void SendSettings();

// Update the high contrast status of the system.
void UpdateHighContrastMode(bool is_high_contrast);

private:
// Returns `true` if the user uses 24 hour time.
bool GetAlwaysUse24HourFormat();

// Returns the user-preferred text scale factor.
float GetTextScaleFactor();

// Returns the user-preferred brightness.
PlatformBrightness GetPreferredBrightness();

bool is_high_contrast_ = false;

private:
std::unique_ptr<BasicMessageChannel<rapidjson::Document>> channel_;
WindowBindingHandler* delegate_;
};

} // namespace flutter

#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_SETTINGS_PLUGIN_H_
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ class WindowBindingHandlerDelegate {
// Typically called by currently configured WindowBindingHandler
virtual void OnVsync(uint64_t last_frame_time_nanos,
uint64_t vsync_interval_time_nanos) = 0;

// Update the status of the high contrast feature
virtual void UpdateHighContrastEnabled(bool enabled) = 0;
};

} // namespace flutter
Expand Down

0 comments on commit 8d8ab0a

Please sign in to comment.