| Flutter Linux Embedder |

fl_settings_plugin.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "flutter/shell/platform/linux/fl_settings_plugin.h"
6
7 #include <gmodule.h>
8
9 #include "flutter/shell/platform/embedder/embedder.h"
10 #include "flutter/shell/platform/linux/fl_engine_private.h"
11 #include "flutter/shell/platform/linux/public/flutter_linux/fl_basic_message_channel.h"
12 #include "flutter/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h"
13 #include "flutter/shell/platform/linux/public/flutter_linux/fl_json_message_codec.h"
14
15 static constexpr char kChannelName[] = "flutter/settings";
16 static constexpr char kTextScaleFactorKey[] = "textScaleFactor";
17 static constexpr char kAlwaysUse24HourFormatKey[] = "alwaysUse24HourFormat";
18 static constexpr char kPlatformBrightnessKey[] = "platformBrightness";
19 static constexpr char kPlatformBrightnessLight[] = "light";
20 static constexpr char kPlatformBrightnessDark[] = "dark";
21
22 struct _FlSettingsPlugin {
23 GObject parent_instance;
24
25 FlBasicMessageChannel* channel;
26 FlEngine* engine;
27 FlSettings* settings;
28 };
29
30 G_DEFINE_TYPE(FlSettingsPlugin, fl_settings_plugin, G_TYPE_OBJECT)
31
32 static const gchar* to_platform_brightness(FlColorScheme color_scheme) {
33 switch (color_scheme) {
34 case FL_COLOR_SCHEME_LIGHT:
35 return kPlatformBrightnessLight;
36 case FL_COLOR_SCHEME_DARK:
37 return kPlatformBrightnessDark;
38 default:
39 g_return_val_if_reached(nullptr);
40 }
41 }
42
43 // Sends the current settings to the Flutter engine.
44 static void update_settings(FlSettingsPlugin* self) {
45 FlClockFormat clock_format = fl_settings_get_clock_format(self->settings);
46 FlColorScheme color_scheme = fl_settings_get_color_scheme(self->settings);
47 gdouble scaling_factor = fl_settings_get_text_scaling_factor(self->settings);
48
49 g_autoptr(FlValue) message = fl_value_new_map();
50 fl_value_set_string_take(message, kTextScaleFactorKey,
51 fl_value_new_float(scaling_factor));
52 fl_value_set_string_take(
53 message, kAlwaysUse24HourFormatKey,
54 fl_value_new_bool(clock_format == FL_CLOCK_FORMAT_24H));
55 fl_value_set_string_take(
56 message, kPlatformBrightnessKey,
57 fl_value_new_string(to_platform_brightness(color_scheme)));
58 fl_basic_message_channel_send(self->channel, message, nullptr, nullptr,
59 nullptr);
60
61 if (self->engine != nullptr) {
62 int32_t flags = 0;
63 if (!fl_settings_get_enable_animations(self->settings)) {
64 flags |= kFlutterAccessibilityFeatureDisableAnimations;
65 }
66 if (fl_settings_get_high_contrast(self->settings)) {
67 flags |= kFlutterAccessibilityFeatureHighContrast;
68 }
69 fl_engine_update_accessibility_features(self->engine, flags);
70 }
71 }
72
73 static void fl_settings_plugin_dispose(GObject* object) {
74 FlSettingsPlugin* self = FL_SETTINGS_PLUGIN(object);
75
76 g_clear_object(&self->channel);
77 g_clear_object(&self->settings);
78
79 if (self->engine != nullptr) {
80 g_object_remove_weak_pointer(G_OBJECT(self),
81 reinterpret_cast<gpointer*>(&(self->engine)));
82 self->engine = nullptr;
83 }
84
85 G_OBJECT_CLASS(fl_settings_plugin_parent_class)->dispose(object);
86 }
87
88 static void fl_settings_plugin_class_init(FlSettingsPluginClass* klass) {
89 G_OBJECT_CLASS(klass)->dispose = fl_settings_plugin_dispose;
90 }
91
92 static void fl_settings_plugin_init(FlSettingsPlugin* self) {}
93
94 FlSettingsPlugin* fl_settings_plugin_new(FlEngine* engine) {
95 g_return_val_if_fail(FL_IS_ENGINE(engine), nullptr);
96
97 FlSettingsPlugin* self =
98 FL_SETTINGS_PLUGIN(g_object_new(fl_settings_plugin_get_type(), nullptr));
99
100 self->engine = engine;
101 g_object_add_weak_pointer(G_OBJECT(self),
102 reinterpret_cast<gpointer*>(&(self->engine)));
103
104 FlBinaryMessenger* messenger = fl_engine_get_binary_messenger(engine);
105 g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
106 self->channel = fl_basic_message_channel_new(messenger, kChannelName,
107 FL_MESSAGE_CODEC(codec));
108
109 return self;
110 }
111
112 void fl_settings_plugin_start(FlSettingsPlugin* self, FlSettings* settings) {
113 g_return_if_fail(FL_IS_SETTINGS_PLUGIN(self));
114 g_return_if_fail(FL_IS_SETTINGS(settings));
115
116 self->settings = FL_SETTINGS(g_object_ref(settings));
117 g_signal_connect_object(settings, "changed", G_CALLBACK(update_settings),
118 self, G_CONNECT_SWAPPED);
119
120 update_settings(self);
121 }
fl_settings_plugin.h
fl_engine_update_accessibility_features
void fl_engine_update_accessibility_features(FlEngine *self, int32_t flags)
Definition: fl_engine.cc:916
fl_settings_get_color_scheme
FlColorScheme fl_settings_get_color_scheme(FlSettings *self)
Definition: fl_settings.cc:34
fl_value_set_string_take
G_MODULE_EXPORT void fl_value_set_string_take(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:650
fl_basic_message_channel_send
G_MODULE_EXPORT void fl_basic_message_channel_send(FlBasicMessageChannel *self, FlValue *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
Definition: fl_basic_message_channel.cc:226
_FlSettingsPlugin
Definition: fl_settings_plugin.cc:22
FL_CLOCK_FORMAT_24H
@ FL_CLOCK_FORMAT_24H
Definition: fl_settings.h:24
to_platform_brightness
static const gchar * to_platform_brightness(FlColorScheme color_scheme)
Definition: fl_settings_plugin.cc:32
fl_value_new_bool
G_MODULE_EXPORT FlValue * fl_value_new_bool(bool value)
Definition: fl_value.cc:255
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
fl_json_message_codec_new
G_MODULE_EXPORT FlJsonMessageCodec * fl_json_message_codec_new()
Definition: fl_json_message_codec.cc:306
fl_json_message_codec.h
fl_settings_get_enable_animations
gboolean fl_settings_get_enable_animations(FlSettings *self)
Definition: fl_settings.cc:38
flags
FlutterSemanticsFlag flags
Definition: fl_accessible_node.cc:105
update_settings
static void update_settings(FlSettingsPlugin *self)
Definition: fl_settings_plugin.cc:44
fl_basic_message_channel.h
fl_engine_get_binary_messenger
G_MODULE_EXPORT FlBinaryMessenger * fl_engine_get_binary_messenger(FlEngine *self)
Definition: fl_engine.cc:894
kChannelName
static constexpr char kChannelName[]
Definition: fl_settings_plugin.cc:15
fl_settings_plugin_start
void fl_settings_plugin_start(FlSettingsPlugin *self, FlSettings *settings)
Definition: fl_settings_plugin.cc:112
kPlatformBrightnessLight
static constexpr char kPlatformBrightnessLight[]
Definition: fl_settings_plugin.cc:19
kPlatformBrightnessDark
static constexpr char kPlatformBrightnessDark[]
Definition: fl_settings_plugin.cc:20
fl_settings_plugin_dispose
static void fl_settings_plugin_dispose(GObject *object)
Definition: fl_settings_plugin.cc:73
FlColorScheme
FlColorScheme
Definition: fl_settings.h:35
fl_value_new_map
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:366
fl_settings_get_high_contrast
gboolean fl_settings_get_high_contrast(FlSettings *self)
Definition: fl_settings.cc:42
fl_binary_messenger.h
_FlSettingsPlugin::settings
FlSettings * settings
Definition: fl_settings_plugin.cc:27
kPlatformBrightnessKey
static constexpr char kPlatformBrightnessKey[]
Definition: fl_settings_plugin.cc:18
fl_settings_plugin_class_init
static void fl_settings_plugin_class_init(FlSettingsPluginClass *klass)
Definition: fl_settings_plugin.cc:88
_FlSettingsPlugin::channel
FlBasicMessageChannel * channel
Definition: fl_settings_plugin.cc:25
fl_engine_private.h
G_DEFINE_TYPE
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
Definition: fl_basic_message_channel.cc:37
_FlSettingsPlugin::engine
FlEngine * engine
Definition: fl_settings_plugin.cc:26
g_object_add_weak_pointer
g_object_add_weak_pointer(G_OBJECT(self), reinterpret_cast< gpointer * >(&self->engine))
fl_settings_get_text_scaling_factor
gdouble fl_settings_get_text_scaling_factor(FlSettings *self)
Definition: fl_settings.cc:46
kTextScaleFactorKey
static constexpr char kTextScaleFactorKey[]
Definition: fl_settings_plugin.cc:16
FL_COLOR_SCHEME_LIGHT
@ FL_COLOR_SCHEME_LIGHT
Definition: fl_settings.h:37
FL_COLOR_SCHEME_DARK
@ FL_COLOR_SCHEME_DARK
Definition: fl_settings.h:38
fl_value_new_float
G_MODULE_EXPORT FlValue * fl_value_new_float(double value)
Definition: fl_value.cc:269
engine
FlEngine * engine
Definition: fl_view_accessible.cc:26
_FlSettingsPlugin::parent_instance
GObject parent_instance
Definition: fl_settings_plugin.cc:23
fl_basic_message_channel_new
G_MODULE_EXPORT FlBasicMessageChannel * fl_basic_message_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMessageCodec *codec)
Definition: fl_basic_message_channel.cc:154
kAlwaysUse24HourFormatKey
static constexpr char kAlwaysUse24HourFormatKey[]
Definition: fl_settings_plugin.cc:17
fl_settings_plugin_init
static void fl_settings_plugin_init(FlSettingsPlugin *self)
Definition: fl_settings_plugin.cc:92
FlClockFormat
FlClockFormat
Definition: fl_settings.h:21
fl_settings_get_clock_format
FlClockFormat fl_settings_get_clock_format(FlSettings *self)
Definition: fl_settings.cc:30
fl_settings_plugin_new
FlSettingsPlugin * fl_settings_plugin_new(FlEngine *engine)
Definition: fl_settings_plugin.cc:94
fl_value_new_string
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276