Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Linux] fix and test light vs. dark theme detection #32618

Merged
merged 2 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -2027,6 +2027,9 @@ FILE: ../../../flutter/shell/platform/linux/fl_event_channel.cc
FILE: ../../../flutter/shell/platform/linux/fl_event_channel_test.cc
FILE: ../../../flutter/shell/platform/linux/fl_gl_area.cc
FILE: ../../../flutter/shell/platform/linux/fl_gl_area.h
FILE: ../../../flutter/shell/platform/linux/fl_gnome_settings.cc
FILE: ../../../flutter/shell/platform/linux/fl_gnome_settings.h
FILE: ../../../flutter/shell/platform/linux/fl_gnome_settings_test.cc
FILE: ../../../flutter/shell/platform/linux/fl_json_message_codec.cc
FILE: ../../../flutter/shell/platform/linux/fl_json_message_codec_test.cc
FILE: ../../../flutter/shell/platform/linux/fl_json_method_codec.cc
Expand Down Expand Up @@ -2076,8 +2079,11 @@ FILE: ../../../flutter/shell/platform/linux/fl_renderer_gl.cc
FILE: ../../../flutter/shell/platform/linux/fl_renderer_gl.h
FILE: ../../../flutter/shell/platform/linux/fl_renderer_headless.cc
FILE: ../../../flutter/shell/platform/linux/fl_renderer_headless.h
FILE: ../../../flutter/shell/platform/linux/fl_settings.cc
FILE: ../../../flutter/shell/platform/linux/fl_settings.h
FILE: ../../../flutter/shell/platform/linux/fl_settings_plugin.cc
FILE: ../../../flutter/shell/platform/linux/fl_settings_plugin.h
FILE: ../../../flutter/shell/platform/linux/fl_settings_plugin_test.cc
FILE: ../../../flutter/shell/platform/linux/fl_standard_message_codec.cc
FILE: ../../../flutter/shell/platform/linux/fl_standard_message_codec_private.h
FILE: ../../../flutter/shell/platform/linux/fl_standard_message_codec_test.cc
Expand Down
13 changes: 13 additions & 0 deletions shell/platform/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ source_set("flutter_linux_sources") {
"fl_engine.cc",
"fl_event_channel.cc",
"fl_gl_area.cc",
"fl_gnome_settings.cc",
"fl_json_message_codec.cc",
"fl_json_method_codec.cc",
"fl_key_channel_responder.cc",
Expand All @@ -128,6 +129,7 @@ source_set("flutter_linux_sources") {
"fl_renderer.cc",
"fl_renderer_gl.cc",
"fl_renderer_headless.cc",
"fl_settings.cc",
"fl_settings_plugin.cc",
"fl_standard_message_codec.cc",
"fl_standard_method_codec.cc",
Expand Down Expand Up @@ -175,6 +177,13 @@ test_fixtures("flutter_linux_fixtures") {
fixtures = []
}

copy("flutter_linux_gschemas") {
testonly = true

sources = [ "testing/gschemas/ubuntu-20.04.compiled" ]
outputs = [ "$target_gen_dir/assets/{{source_name_part}}/gschemas.compiled" ]
}

executable("flutter_linux_unittests") {
testonly = true

Expand All @@ -186,6 +195,7 @@ executable("flutter_linux_unittests") {
"fl_dart_project_test.cc",
"fl_engine_test.cc",
"fl_event_channel_test.cc",
"fl_gnome_settings_test.cc",
"fl_json_message_codec_test.cc",
"fl_json_method_codec_test.cc",
"fl_key_channel_responder_test.cc",
Expand All @@ -197,6 +207,7 @@ executable("flutter_linux_unittests") {
"fl_method_response_test.cc",
"fl_pixel_buffer_texture_test.cc",
"fl_plugin_registrar_test.cc",
"fl_settings_plugin_test.cc",
"fl_standard_message_codec_test.cc",
"fl_standard_method_codec_test.cc",
"fl_string_codec_test.cc",
Expand All @@ -210,6 +221,7 @@ executable("flutter_linux_unittests") {
"testing/mock_epoxy.cc",
"testing/mock_plugin_registrar.cc",
"testing/mock_renderer.cc",
"testing/mock_settings.cc",
"testing/mock_signal_handler.cc",
"testing/mock_text_input_plugin.cc",
"testing/mock_texture_registrar.cc",
Expand All @@ -229,6 +241,7 @@ executable("flutter_linux_unittests") {

deps = [
":flutter_linux_fixtures",
":flutter_linux_gschemas",
":flutter_linux_sources",
"//flutter/runtime:libdart",
"//flutter/shell/platform/embedder:embedder_headers",
Expand Down
3 changes: 2 additions & 1 deletion shell/platform/linux/fl_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,9 @@ gboolean fl_engine_start(FlEngine* self, GError** error) {

setup_locales(self);

g_autoptr(FlSettings) settings = fl_settings_new();
self->settings_plugin = fl_settings_plugin_new(self->binary_messenger);
fl_settings_plugin_start(self->settings_plugin);
fl_settings_plugin_start(self->settings_plugin, settings);

result = self->embedder_api.UpdateSemanticsEnabled(self->engine, TRUE);
if (result != kSuccess) {
Expand Down
160 changes: 160 additions & 0 deletions shell/platform/linux/fl_gnome_settings.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// 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/fl_gnome_settings.h"

#include <gio/gio.h>
#include <glib.h>

static constexpr char kDesktopInterfaceSchema[] = "org.gnome.desktop.interface";
static constexpr char kDesktopTextScalingFactorKey[] = "text-scaling-factor";
static constexpr char kDesktopClockFormatKey[] = "clock-format";
static constexpr char kDesktopGtkThemeKey[] = "gtk-theme";

static constexpr char kClockFormat12Hour[] = "12h";
static constexpr char kGtkThemeDarkSuffix[] = "-dark";
static constexpr char kInterfaceSettings[] = "interface-settings";

struct _FlGnomeSettings {
GObject parent_instance;

GSettings* interface_settings;
};

enum { PROP_0, PROP_INTERFACE_SETTINGS, PROP_LAST };

static void fl_gnome_settings_iface_init(FlSettingsInterface* iface);

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) {
FlGnomeSettings* self = FL_GNOME_SETTINGS(settings);

FlClockFormat clock_format = FL_CLOCK_FORMAT_24H;

if (self->interface_settings != nullptr) {
g_autofree gchar* value =
g_settings_get_string(self->interface_settings, kDesktopClockFormatKey);
if (g_strcmp0(value, kClockFormat12Hour) == 0) {
clock_format = FL_CLOCK_FORMAT_12H;
}
}
return clock_format;
}

static FlColorScheme fl_gnome_settings_get_color_scheme(FlSettings* settings) {
FlGnomeSettings* self = FL_GNOME_SETTINGS(settings);

FlColorScheme color_scheme = FL_COLOR_SCHEME_LIGHT;

if (self->interface_settings != nullptr) {
// check whether org.gnome.desktop.interface.gtk-theme ends with "-dark"
g_autofree gchar* value =
g_settings_get_string(self->interface_settings, kDesktopGtkThemeKey);
if (g_str_has_suffix(value, kGtkThemeDarkSuffix)) {
color_scheme = FL_COLOR_SCHEME_DARK;
}
}
return color_scheme;
}

static gdouble fl_gnome_settings_get_text_scaling_factor(FlSettings* settings) {
FlGnomeSettings* self = FL_GNOME_SETTINGS(settings);

gdouble scaling_factor = 1.0;

if (self->interface_settings != nullptr) {
scaling_factor = g_settings_get_double(self->interface_settings,
kDesktopTextScalingFactorKey);
}
return scaling_factor;
}

static void fl_gnome_settings_set_interface_settings(FlGnomeSettings* self,
GSettings* settings) {
g_return_if_fail(G_IS_SETTINGS(settings));

g_signal_connect_object(settings, "changed::clock-format",
G_CALLBACK(fl_settings_emit_changed), self,
G_CONNECT_SWAPPED);
g_signal_connect_object(settings, "changed::gtk-theme",
G_CALLBACK(fl_settings_emit_changed), self,
G_CONNECT_SWAPPED);
g_signal_connect_object(settings, "changed::text-scaling-factor",
G_CALLBACK(fl_settings_emit_changed), self,
G_CONNECT_SWAPPED);

self->interface_settings = G_SETTINGS(g_object_ref(settings));
}

static void fl_gnome_settings_set_property(GObject* object,
guint prop_id,
const GValue* value,
GParamSpec* pspec) {
FlGnomeSettings* self = FL_GNOME_SETTINGS(object);
switch (prop_id) {
case PROP_INTERFACE_SETTINGS:
fl_gnome_settings_set_interface_settings(
self, G_SETTINGS(g_value_get_object(value)));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}

static void fl_gnome_settings_dispose(GObject* object) {
FlGnomeSettings* self = FL_GNOME_SETTINGS(object);

g_clear_object(&self->interface_settings);

G_OBJECT_CLASS(fl_gnome_settings_parent_class)->dispose(object);
}

static void fl_gnome_settings_class_init(FlGnomeSettingsClass* klass) {
GObjectClass* object_class = G_OBJECT_CLASS(klass);
object_class->dispose = fl_gnome_settings_dispose;
object_class->set_property = fl_gnome_settings_set_property;

g_object_class_install_property(
object_class, PROP_INTERFACE_SETTINGS,
g_param_spec_object(
kInterfaceSettings, kInterfaceSettings, kDesktopInterfaceSchema,
g_settings_get_type(),
static_cast<GParamFlags>(G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS)));
}

static void fl_gnome_settings_iface_init(FlSettingsInterface* iface) {
iface->get_clock_format = fl_gnome_settings_get_clock_format;
iface->get_color_scheme = fl_gnome_settings_get_color_scheme;
iface->get_text_scaling_factor = fl_gnome_settings_get_text_scaling_factor;
}

static void fl_gnome_settings_init(FlGnomeSettings* self) {}

static GSettings* create_settings(const gchar* schema_id) {
GSettings* settings = nullptr;
GSettingsSchemaSource* source = g_settings_schema_source_get_default();
if (source != nullptr) {
g_autoptr(GSettingsSchema) schema =
g_settings_schema_source_lookup(source, schema_id, TRUE);
if (schema != nullptr) {
settings = g_settings_new_full(schema, nullptr, nullptr);
}
}
return settings;
}

FlSettings* fl_gnome_settings_new() {
g_autoptr(GSettings) interface_settings =
create_settings(kDesktopInterfaceSchema);
return FL_SETTINGS(g_object_new(fl_gnome_settings_get_type(),
kInterfaceSettings, interface_settings,
nullptr));
}
29 changes: 29 additions & 0 deletions shell/platform/linux/fl_gnome_settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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_FL_GNOME_SETTINGS_H_
#define FLUTTER_SHELL_PLATFORM_LINUX_FL_GNOME_SETTINGS_H_

#include "flutter/shell/platform/linux/fl_settings.h"

G_BEGIN_DECLS

G_DECLARE_FINAL_TYPE(FlGnomeSettings,
fl_gnome_settings,
FL,
GNOME_SETTINGS,
GObject);

/**
* fl_gnome_settings_new:
*
* Creates a new settings instance for GNOME.
*
* Returns: a new #FlSettings.
*/
FlSettings* fl_gnome_settings_new();

G_END_DECLS

#endif // FLUTTER_SHELL_PLATFORM_LINUX_FL_GNOME_SETTINGS_H_
112 changes: 112 additions & 0 deletions shell/platform/linux/fl_gnome_settings_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// 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/fl_gnome_settings.h"
#include "flutter/shell/platform/linux/testing/fl_test.h"
#include "flutter/shell/platform/linux/testing/mock_settings.h"
#include "flutter/shell/platform/linux/testing/mock_signal_handler.h"
#include "flutter/testing/testing.h"

#include <gio/gio.h>
#define G_SETTINGS_ENABLE_BACKEND
#include <gio/gsettingsbackend.h>

#include "gmock/gmock.h"
#include "gtest/gtest.h"

class FlGnomeSettingsTest : public ::testing::Test {
protected:
void SetUp() override {
// force _g_io_modules_ensure_extension_points_registered() to get called
g_settings_backend_get_default();
}
};

static GSettings* create_settings(const gchar* name, const gchar* schema_id) {
g_autofree gchar* path =
g_build_filename(flutter::testing::GetFixturesPath(), name, nullptr);
g_autoptr(GSettingsSchemaSource) source =
g_settings_schema_source_new_from_directory(path, nullptr, false,
nullptr);
g_autoptr(GSettingsSchema) schema =
g_settings_schema_source_lookup(source, schema_id, false);
g_autoptr(GSettingsBackend) backend = g_memory_settings_backend_new();
return g_settings_new_full(schema, backend, nullptr);
}

TEST_F(FlGnomeSettingsTest, ClockFormat) {
g_autoptr(GSettings) interface_settings =
create_settings("ubuntu-20.04", "org.gnome.desktop.interface");
g_settings_set_string(interface_settings, "clock-format", "24h");

g_autoptr(FlSettings) settings = FL_SETTINGS(
g_object_new(fl_gnome_settings_get_type(), "interface_settings",
interface_settings, nullptr));
EXPECT_EQ(fl_settings_get_clock_format(settings), FL_CLOCK_FORMAT_24H);

flutter::testing::MockSignalHandler settings_changed(settings, "changed");
EXPECT_SIGNAL(settings_changed).Times(1);

g_settings_set_string(interface_settings, "clock-format", "12h");
EXPECT_EQ(fl_settings_get_clock_format(settings), FL_CLOCK_FORMAT_12H);
}

TEST_F(FlGnomeSettingsTest, GtkTheme) {
g_autoptr(GSettings) interface_settings =
create_settings("ubuntu-20.04", "org.gnome.desktop.interface");
g_settings_set_string(interface_settings, "gtk-theme", "Yaru");

g_autoptr(FlSettings) settings = FL_SETTINGS(
g_object_new(fl_gnome_settings_get_type(), "interface_settings",
interface_settings, nullptr));
EXPECT_EQ(fl_settings_get_color_scheme(settings), FL_COLOR_SCHEME_LIGHT);

flutter::testing::MockSignalHandler settings_changed(settings, "changed");
EXPECT_SIGNAL(settings_changed).Times(1);

g_settings_set_string(interface_settings, "gtk-theme", "Yaru-dark");
EXPECT_EQ(fl_settings_get_color_scheme(settings), FL_COLOR_SCHEME_DARK);
}

TEST_F(FlGnomeSettingsTest, TextScalingFactor) {
g_autoptr(GSettings) interface_settings =
create_settings("ubuntu-20.04", "org.gnome.desktop.interface");
g_settings_set_double(interface_settings, "text-scaling-factor", 1.0);

g_autoptr(FlSettings) settings = FL_SETTINGS(
g_object_new(fl_gnome_settings_get_type(), "interface_settings",
interface_settings, nullptr));
EXPECT_EQ(fl_settings_get_text_scaling_factor(settings), 1.0);

flutter::testing::MockSignalHandler settings_changed(settings, "changed");
EXPECT_SIGNAL(settings_changed).Times(1);

g_settings_set_double(interface_settings, "text-scaling-factor", 1.5);
EXPECT_EQ(fl_settings_get_text_scaling_factor(settings), 1.5);
}

TEST_F(FlGnomeSettingsTest, SignalHandlers) {
g_autoptr(GSettings) interface_settings =
create_settings("ubuntu-20.04", "org.gnome.desktop.interface");

g_autoptr(FlSettings) settings = FL_SETTINGS(
g_object_new(fl_gnome_settings_get_type(), "interface_settings",
interface_settings, nullptr));
flutter::testing::MockSignalHandler settings_changed(settings, "changed");

EXPECT_SIGNAL(settings_changed).Times(3);

g_settings_set_string(interface_settings, "clock-format", "12h");
g_settings_set_string(interface_settings, "gtk-theme", "Yaru-dark");
g_settings_set_double(interface_settings, "text-scaling-factor", 1.5);

EXPECT_SIGNAL(settings_changed).Times(0);

g_clear_object(&settings);

// destroyed FlSettings object must have disconnected its signal handlers
g_settings_set_string(interface_settings, "clock-format", "24h");
g_settings_set_string(interface_settings, "gtk-theme", "Yaru");
g_settings_set_double(interface_settings, "text-scaling-factor", 2.0);
}
Loading