@@ -19,7 +19,6 @@ static constexpr char kPlatformBrightnessLight[] = "light";
1919static constexpr char kPlatformBrightnessDark [] = " dark" ;
2020
2121static constexpr char kDesktopInterfaceSchema [] = " org.gnome.desktop.interface" ;
22- static constexpr char kDesktopGtkThemeKey [] = " gtk-theme" ;
2322static constexpr char kDesktopTextScalingFactorKey [] = " text-scaling-factor" ;
2423static constexpr char kDesktopClockFormatKey [] = " clock-format" ;
2524static constexpr char kClockFormat24Hour [] = " 24h" ;
@@ -34,24 +33,25 @@ struct _FlSettingsPlugin {
3433
3534G_DEFINE_TYPE (FlSettingsPlugin, fl_settings_plugin, G_TYPE_OBJECT)
3635
37- static bool is_dark_theme(GSettings* interface_settings) {
36+ static bool is_dark_color(GdkRGBA* color) {
37+ return color->red < 0.5 && color->green < 0.5 && color->blue < 0.5 ;
38+ }
39+
40+ static bool is_dark_theme () {
3841 // GTK doesn't have a specific flag for dark themes, so we check if the
39- // window text color is light or dark
40- GtkApplication* app = GTK_APPLICATION (g_application_get_default ());
41- GtkWidget* window = GTK_WIDGET (gtk_application_get_active_window (app));
42-
43- if (G_UNLIKELY (window == nullptr )) {
44- // As a fallback, check if the theme name contains 'dark'
45- g_autofree gchar* gtk_theme =
46- g_settings_get_string (interface_settings, kDesktopGtkThemeKey );
47- g_autofree gchar* gtk_theme_down = g_utf8_strdown (gtk_theme, -1 );
48- return strstr (gtk_theme_down, " dark" ) != nullptr ;
42+ // style text color is light or dark
43+ GList* windows = gtk_window_list_toplevels ();
44+ if (windows == nullptr ) {
45+ return false ;
4946 }
5047
48+ GtkWidget* window = GTK_WIDGET (windows->data );
49+ g_list_free (windows);
50+
5151 GdkRGBA color;
5252 GtkStyleContext* style = gtk_widget_get_style_context (window);
5353 gtk_style_context_get_color (style, GTK_STATE_FLAG_NORMAL, &color);
54- return color. red > 0.5 && color. blue > 0.5 && color. green > 0.5 ;
54+ return ! is_dark_color (& color) ;
5555}
5656
5757// Sends the current settings to the Flutter engine.
@@ -66,10 +66,10 @@ static void update_settings(FlSettingsPlugin* self) {
6666 g_autofree gchar* clock_format =
6767 g_settings_get_string (self->interface_settings , kDesktopClockFormatKey );
6868 always_use_24hr = g_strcmp0 (clock_format, kClockFormat24Hour ) == 0 ;
69+ }
6970
70- if (is_dark_theme (self->interface_settings )) {
71- platform_brightness = kPlatformBrightnessDark ;
72- }
71+ if (is_dark_theme ()) {
72+ platform_brightness = kPlatformBrightnessDark ;
7373 }
7474
7575 g_autoptr (FlValue) message = fl_value_new_map ();
0 commit comments