| Flutter Linux Embedder |

fl_gnome_settings.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_gnome_settings.h"
6
7 #include <gio/gio.h>
8 #include <glib.h>
9
10 static constexpr char kDesktopInterfaceSchema[] = "org.gnome.desktop.interface";
11 static constexpr char kDesktopTextScalingFactorKey[] = "text-scaling-factor";
12 static constexpr char kDesktopClockFormatKey[] = "clock-format";
13 static constexpr char kDesktopGtkThemeKey[] = "gtk-theme";
14
15 static constexpr char kClockFormat12Hour[] = "12h";
16 static constexpr char kGtkThemeDarkSuffix[] = "-dark";
17 static constexpr char kInterfaceSettings[] = "interface-settings";
18
19 struct _FlGnomeSettings {
20 GObject parent_instance;
21
22 GSettings* interface_settings;
23 };
24
25 enum { kProp0, kPropInterfaceSettings, kPropLast };
26
27 static void fl_gnome_settings_iface_init(FlSettingsInterface* iface);
28
29 G_DEFINE_TYPE_WITH_CODE(FlGnomeSettings,
30 fl_gnome_settings,
31 G_TYPE_OBJECT,
32 G_IMPLEMENT_INTERFACE(fl_settings_get_type(),
33 fl_gnome_settings_iface_init))
34
35 static FlClockFormat fl_gnome_settings_get_clock_format(FlSettings* settings) {
36 FlGnomeSettings* self = FL_GNOME_SETTINGS(settings);
37
38 FlClockFormat clock_format = FL_CLOCK_FORMAT_24H;
39
40 if (self->interface_settings != nullptr) {
41 g_autofree gchar* value =
42 g_settings_get_string(self->interface_settings, kDesktopClockFormatKey);
43 if (g_strcmp0(value, kClockFormat12Hour) == 0) {
44 clock_format = FL_CLOCK_FORMAT_12H;
45 }
46 }
47 return clock_format;
48 }
49
50 static FlColorScheme fl_gnome_settings_get_color_scheme(FlSettings* settings) {
51 FlGnomeSettings* self = FL_GNOME_SETTINGS(settings);
52
53 FlColorScheme color_scheme = FL_COLOR_SCHEME_LIGHT;
54
55 if (self->interface_settings != nullptr) {
56 // check whether org.gnome.desktop.interface.gtk-theme ends with "-dark"
57 g_autofree gchar* value =
58 g_settings_get_string(self->interface_settings, kDesktopGtkThemeKey);
59 if (g_str_has_suffix(value, kGtkThemeDarkSuffix)) {
60 color_scheme = FL_COLOR_SCHEME_DARK;
61 }
62 }
63 return color_scheme;
64 }
65
66 static gboolean fl_gnome_settings_get_enable_animations(FlSettings* settings) {
67 return true;
68 }
69
70 static gboolean fl_gnome_settings_get_high_contrast(FlSettings* settings) {
71 return false;
72 }
73
74 static gdouble fl_gnome_settings_get_text_scaling_factor(FlSettings* settings) {
75 FlGnomeSettings* self = FL_GNOME_SETTINGS(settings);
76
77 gdouble scaling_factor = 1.0;
78
79 if (self->interface_settings != nullptr) {
80 scaling_factor = g_settings_get_double(self->interface_settings,
81 kDesktopTextScalingFactorKey);
82 }
83 return scaling_factor;
84 }
85
86 static void fl_gnome_settings_set_interface_settings(FlGnomeSettings* self,
87 GSettings* settings) {
88 g_return_if_fail(G_IS_SETTINGS(settings));
89
90 g_signal_connect_object(settings, "changed::clock-format",
91 G_CALLBACK(fl_settings_emit_changed), self,
92 G_CONNECT_SWAPPED);
93 g_signal_connect_object(settings, "changed::gtk-theme",
94 G_CALLBACK(fl_settings_emit_changed), self,
95 G_CONNECT_SWAPPED);
96 g_signal_connect_object(settings, "changed::text-scaling-factor",
97 G_CALLBACK(fl_settings_emit_changed), self,
98 G_CONNECT_SWAPPED);
99
100 self->interface_settings = G_SETTINGS(g_object_ref(settings));
101 }
102
103 static void fl_gnome_settings_set_property(GObject* object,
104 guint prop_id,
105 const GValue* value,
106 GParamSpec* pspec) {
107 FlGnomeSettings* self = FL_GNOME_SETTINGS(object);
108 switch (prop_id) {
109 case kPropInterfaceSettings:
110 fl_gnome_settings_set_interface_settings(
111 self, G_SETTINGS(g_value_get_object(value)));
112 break;
113 default:
114 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
115 break;
116 }
117 }
118
119 static void fl_gnome_settings_dispose(GObject* object) {
120 FlGnomeSettings* self = FL_GNOME_SETTINGS(object);
121
122 g_clear_object(&self->interface_settings);
123
124 G_OBJECT_CLASS(fl_gnome_settings_parent_class)->dispose(object);
125 }
126
127 static void fl_gnome_settings_class_init(FlGnomeSettingsClass* klass) {
128 GObjectClass* object_class = G_OBJECT_CLASS(klass);
129 object_class->dispose = fl_gnome_settings_dispose;
130 object_class->set_property = fl_gnome_settings_set_property;
131
132 g_object_class_install_property(
133 object_class, kPropInterfaceSettings,
134 g_param_spec_object(
135 kInterfaceSettings, kInterfaceSettings, kDesktopInterfaceSchema,
136 g_settings_get_type(),
137 static_cast<GParamFlags>(G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
138 G_PARAM_STATIC_STRINGS)));
139 }
140
141 static void fl_gnome_settings_iface_init(FlSettingsInterface* iface) {
142 iface->get_clock_format = fl_gnome_settings_get_clock_format;
143 iface->get_color_scheme = fl_gnome_settings_get_color_scheme;
144 iface->get_enable_animations = fl_gnome_settings_get_enable_animations;
145 iface->get_high_contrast = fl_gnome_settings_get_high_contrast;
146 iface->get_text_scaling_factor = fl_gnome_settings_get_text_scaling_factor;
147 }
148
149 static void fl_gnome_settings_init(FlGnomeSettings* self) {}
150
151 static GSettings* create_settings(const gchar* schema_id) {
152 GSettings* settings = nullptr;
153 GSettingsSchemaSource* source = g_settings_schema_source_get_default();
154 if (source != nullptr) {
155 g_autoptr(GSettingsSchema) schema =
156 g_settings_schema_source_lookup(source, schema_id, TRUE);
157 if (schema != nullptr) {
158 settings = g_settings_new_full(schema, nullptr, nullptr);
159 }
160 }
161 return settings;
162 }
163
164 FlSettings* fl_gnome_settings_new() {
165 g_autoptr(GSettings) interface_settings =
166 create_settings(kDesktopInterfaceSchema);
167 return FL_SETTINGS(g_object_new(fl_gnome_settings_get_type(),
168 kInterfaceSettings, interface_settings,
169 nullptr));
170 }
fl_gnome_settings_get_color_scheme
static FlColorScheme fl_gnome_settings_get_color_scheme(FlSettings *settings)
Definition: fl_gnome_settings.cc:50
fl_gnome_settings_get_text_scaling_factor
static gdouble fl_gnome_settings_get_text_scaling_factor(FlSettings *settings)
Definition: fl_gnome_settings.cc:74
kDesktopClockFormatKey
static constexpr char kDesktopClockFormatKey[]
Definition: fl_gnome_settings.cc:12
G_DEFINE_TYPE_WITH_CODE
G_DEFINE_TYPE_WITH_CODE(FlGnomeSettings, fl_gnome_settings, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(fl_settings_get_type(), fl_gnome_settings_iface_init)) static FlClockFormat fl_gnome_settings_get_clock_format(FlSettings *settings)
Definition: fl_gnome_settings.cc:29
kDesktopInterfaceSchema
static constexpr char kDesktopInterfaceSchema[]
Definition: fl_gnome_settings.cc:10
_FlGnomeSettings::parent_instance
GObject parent_instance
Definition: fl_gnome_settings.cc:20
fl_gnome_settings_iface_init
static void fl_gnome_settings_iface_init(FlSettingsInterface *iface)
Definition: fl_gnome_settings.cc:141
fl_gnome_settings_set_interface_settings
static void fl_gnome_settings_set_interface_settings(FlGnomeSettings *self, GSettings *settings)
Definition: fl_gnome_settings.cc:86
FL_CLOCK_FORMAT_12H
@ FL_CLOCK_FORMAT_12H
Definition: fl_settings.h:22
FL_CLOCK_FORMAT_24H
@ FL_CLOCK_FORMAT_24H
Definition: fl_settings.h:23
kPropLast
@ kPropLast
Definition: fl_gnome_settings.cc:25
fl_gnome_settings_get_high_contrast
static gboolean fl_gnome_settings_get_high_contrast(FlSettings *settings)
Definition: fl_gnome_settings.cc:70
fl_gnome_settings_new
FlSettings * fl_gnome_settings_new()
Definition: fl_gnome_settings.cc:164
fl_gnome_settings_class_init
static void fl_gnome_settings_class_init(FlGnomeSettingsClass *klass)
Definition: fl_gnome_settings.cc:127
fl_gnome_settings.h
FlColorScheme
FlColorScheme
Definition: fl_settings.h:33
kGtkThemeDarkSuffix
static constexpr char kGtkThemeDarkSuffix[]
Definition: fl_gnome_settings.cc:16
_FlGnomeSettings
Definition: fl_gnome_settings.cc:19
fl_settings_emit_changed
void fl_settings_emit_changed(FlSettings *self)
Definition: fl_settings.cc:50
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
kPropInterfaceSettings
@ kPropInterfaceSettings
Definition: fl_gnome_settings.cc:25
kInterfaceSettings
static constexpr char kInterfaceSettings[]
Definition: fl_gnome_settings.cc:17
create_settings
static GSettings * create_settings(const gchar *schema_id)
Definition: fl_gnome_settings.cc:151
self
GdkEvent FlView * self
Definition: fl_view.cc:100
_FlGnomeSettings::interface_settings
GSettings * interface_settings
Definition: fl_gnome_settings.cc:22
FL_COLOR_SCHEME_LIGHT
@ FL_COLOR_SCHEME_LIGHT
Definition: fl_settings.h:34
kDesktopGtkThemeKey
static constexpr char kDesktopGtkThemeKey[]
Definition: fl_gnome_settings.cc:13
kDesktopTextScalingFactorKey
static constexpr char kDesktopTextScalingFactorKey[]
Definition: fl_gnome_settings.cc:11
FL_COLOR_SCHEME_DARK
@ FL_COLOR_SCHEME_DARK
Definition: fl_settings.h:35
fl_gnome_settings_dispose
static void fl_gnome_settings_dispose(GObject *object)
Definition: fl_gnome_settings.cc:119
kProp0
@ kProp0
Definition: fl_gnome_settings.cc:25
fl_gnome_settings_set_property
static void fl_gnome_settings_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
Definition: fl_gnome_settings.cc:103
fl_gnome_settings_init
static void fl_gnome_settings_init(FlGnomeSettings *self)
Definition: fl_gnome_settings.cc:149
FlClockFormat
FlClockFormat
Definition: fl_settings.h:21
value
uint8_t value
Definition: fl_standard_message_codec.cc:41
kClockFormat12Hour
static constexpr char kClockFormat12Hour[]
Definition: fl_gnome_settings.cc:15
fl_gnome_settings_get_enable_animations
static gboolean fl_gnome_settings_get_enable_animations(FlSettings *settings)
Definition: fl_gnome_settings.cc:66