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

[Linux] revise dark theme detection #25535

Merged
merged 4 commits into from
May 7, 2021
Merged
Changes from 2 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
35 changes: 26 additions & 9 deletions shell/platform/linux/fl_settings_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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

#include <gmodule.h>
#include <gtk/gtk.h>
#include <cstring>

#include "flutter/shell/platform/linux/public/flutter_linux/fl_basic_message_channel.h"
Expand All @@ -17,7 +19,6 @@ static constexpr char kPlatformBrightnessLight[] = "light";
static constexpr char kPlatformBrightnessDark[] = "dark";

static constexpr char kDesktopInterfaceSchema[] = "org.gnome.desktop.interface";
static constexpr char kDesktopGtkThemeKey[] = "gtk-theme";
static constexpr char kDesktopTextScalingFactorKey[] = "text-scaling-factor";
static constexpr char kDesktopClockFormatKey[] = "clock-format";
static constexpr char kClockFormat24Hour[] = "24h";
Expand All @@ -32,6 +33,27 @@ struct _FlSettingsPlugin {

G_DEFINE_TYPE(FlSettingsPlugin, fl_settings_plugin, G_TYPE_OBJECT)

static bool is_dark_color(GdkRGBA* color) {
return color->red < 0.5 && color->green < 0.5 && color->blue < 0.5;
}

static bool is_dark_theme() {
// GTK doesn't have a specific flag for dark themes, so we check if the
// style text color is light or dark
GList* windows = gtk_window_list_toplevels();
if (windows == nullptr) {
return false;
}

GtkWidget* window = GTK_WIDGET(windows->data);
g_list_free(windows);

GdkRGBA color;
GtkStyleContext* style = gtk_widget_get_style_context(window);
gtk_style_context_get_color(style, GTK_STATE_FLAG_NORMAL, &color);
return !is_dark_color(&color);
}

// Sends the current settings to the Flutter engine.
static void update_settings(FlSettingsPlugin* self) {
gdouble scaling_factor = 1.0;
Expand All @@ -44,15 +66,10 @@ static void update_settings(FlSettingsPlugin* self) {
g_autofree gchar* clock_format =
g_settings_get_string(self->interface_settings, kDesktopClockFormatKey);
always_use_24hr = g_strcmp0(clock_format, kClockFormat24Hour) == 0;
}

// GTK doesn't have a specific flag for dark themes, so we have some
// hard-coded themes for Ubuntu (Yaru) and GNOME (Adwaita).
g_autofree gchar* gtk_theme =
g_settings_get_string(self->interface_settings, kDesktopGtkThemeKey);
if (g_strcmp0(gtk_theme, "Yaru-dark") == 0 ||
g_strcmp0(gtk_theme, "Adwaita-dark") == 0) {
platform_brightness = kPlatformBrightnessDark;
}
if (is_dark_theme()) {
platform_brightness = kPlatformBrightnessDark;
}

g_autoptr(FlValue) message = fl_value_new_map();
Expand Down