Skip to content

Commit 5f4262d

Browse files
committed
[Linux] resort to "-dark" suffix for detecting platform brightness
Some users have reported that toggling between light and dark system theme results in Flutter detecting the opposite theme. This is because the window text color, which is used to calculate the brightness, may not have been yet refreshed when the theme change signal is emitted. This commit replaces the unreliable (and untestable) window text color -based theme detection with the simpler alternative proposed in flutter#25535 to check for a "-dark" suffix in the theme name, which is used by convention in dark GTK theme names (Yaru vs. Yaru-dark, Adwaita vs. Adwaita-dark, Pop vs. Pop-dark, ...). Ref: flutter/flutter#101438
1 parent 9c4142c commit 5f4262d

File tree

1 file changed

+7
-55
lines changed

1 file changed

+7
-55
lines changed

shell/platform/linux/fl_settings_plugin.cc

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include "flutter/shell/platform/linux/fl_settings_plugin.h"
66

77
#include <gmodule.h>
8-
#include <gtk/gtk.h>
9-
#include <math.h>
108

119
#include "flutter/shell/platform/linux/public/flutter_linux/fl_basic_message_channel.h"
1210
#include "flutter/shell/platform/linux/public/flutter_linux/fl_json_message_codec.h"
@@ -21,9 +19,9 @@ static constexpr char kPlatformBrightnessDark[] = "dark";
2119
static constexpr char kDesktopInterfaceSchema[] = "org.gnome.desktop.interface";
2220
static constexpr char kDesktopTextScalingFactorKey[] = "text-scaling-factor";
2321
static constexpr char kDesktopClockFormatKey[] = "clock-format";
22+
static constexpr char kDesktopGtkThemeKey[] = "gtk-theme";
2423
static constexpr char kClockFormat24Hour[] = "24h";
25-
26-
enum class Brightness { Light, Dark };
24+
static constexpr char kGtkThemeDarkSuffix[] = "-dark";
2725

2826
struct _FlSettingsPlugin {
2927
GObject parent_instance;
@@ -37,54 +35,6 @@ struct _FlSettingsPlugin {
3735

3836
G_DEFINE_TYPE(FlSettingsPlugin, fl_settings_plugin, G_TYPE_OBJECT)
3937

40-
// The color brightness calculation has been adapted from theme_data.dart:
41-
// https://github.com/flutter/flutter/blob/8fe4cc79648a952f9c7e49a5248756c2ff98fa3b/packages/flutter/lib/src/material/theme_data.dart#L1470-L1488
42-
43-
// See <https://www.w3.org/TR/WCAG20/#relativeluminancedef>.
44-
static gdouble linearize_color_component(gdouble component) {
45-
if (component <= 0.03928) {
46-
return component / 12.92;
47-
}
48-
return pow((component + 0.055) / 1.055, 2.4);
49-
}
50-
51-
// See <https://en.wikipedia.org/wiki/Relative_luminance>.
52-
gdouble compute_luminance(GdkRGBA* color) {
53-
gdouble r = linearize_color_component(color->red);
54-
gdouble g = linearize_color_component(color->green);
55-
gdouble b = linearize_color_component(color->blue);
56-
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
57-
}
58-
59-
static Brightness estimate_brightness_for_color(GdkRGBA* color) {
60-
gdouble relative_luminance = compute_luminance(color);
61-
62-
// See <https://www.w3.org/TR/WCAG20/#contrast-ratiodef> and
63-
// <https://material.io/go/design-theming#color-color-palette>.
64-
const gdouble kThreshold = 0.15;
65-
if ((relative_luminance + 0.05) * (relative_luminance + 0.05) > kThreshold) {
66-
return Brightness::Light;
67-
}
68-
return Brightness::Dark;
69-
}
70-
71-
static bool is_dark_theme() {
72-
// GTK doesn't have a specific flag for dark themes, so we check if the
73-
// style text color is light or dark
74-
GList* windows = gtk_window_list_toplevels();
75-
if (windows == nullptr) {
76-
return false;
77-
}
78-
79-
GtkWidget* window = GTK_WIDGET(windows->data);
80-
g_list_free(windows);
81-
82-
GdkRGBA text_color;
83-
GtkStyleContext* style = gtk_widget_get_style_context(window);
84-
gtk_style_context_get_color(style, GTK_STATE_FLAG_NORMAL, &text_color);
85-
return estimate_brightness_for_color(&text_color) == Brightness::Light;
86-
}
87-
8838
// Sends the current settings to the Flutter engine.
8939
static void update_settings(FlSettingsPlugin* self) {
9040
gdouble scaling_factor = 1.0;
@@ -97,10 +47,12 @@ static void update_settings(FlSettingsPlugin* self) {
9747
g_autofree gchar* clock_format =
9848
g_settings_get_string(self->interface_settings, kDesktopClockFormatKey);
9949
always_use_24hr = g_strcmp0(clock_format, kClockFormat24Hour) == 0;
100-
}
10150

102-
if (is_dark_theme()) {
103-
platform_brightness = kPlatformBrightnessDark;
51+
g_autofree gchar* gtk_theme =
52+
g_settings_get_string(self->interface_settings, kDesktopGtkThemeKey);
53+
if (g_str_has_suffix(gtk_theme, kGtkThemeDarkSuffix)) {
54+
platform_brightness = kPlatformBrightnessDark;
55+
}
10456
}
10557

10658
g_autoptr(FlValue) message = fl_value_new_map();

0 commit comments

Comments
 (0)