From 1c8472c1a44e63c96da7b7cb3873a2822635c18b Mon Sep 17 00:00:00 2001
From: Rune Lillesveen <futhark@chromium.org>
Date: Fri, 03 Dec 2021 12:28:22 +0100
Subject: [PATCH] Make dark theme Gtk affect preferred color scheme.

Selecting a dark Gtk theme gives a dark NativeThemeGtk and UI, but it
didn't affect the preferred color scheme on the web instance. Hence, the
prefers-color-scheme media query still matched 'light', not 'dark'.

Observe the ui native theme instance to propagate any theme changes to
the web instance. Code copied from the NativeThemeWin implementation.

This is a reland of:

https://chromium-review.googlesource.com/c/chromium/src/+/1774922

Bug: 998903
Change-Id: Id8147ecfbda1f3f5899a1e35b97f36382e63e899
---

diff --git a/ui/gtk/native_theme_gtk.cc b/ui/gtk/native_theme_gtk.cc
index 4afa399..fbaaf06e 100644
--- a/ui/gtk/native_theme_gtk.cc
+++ b/ui/gtk/native_theme_gtk.cc
@@ -96,6 +96,12 @@
 NativeThemeGtk::NativeThemeGtk()
     : NativeThemeBase(/*should_only_use_dark_colors=*/false,
                       ui::SystemTheme::kGtk) {
+  // Add the web native theme as an observer to stay in sync with dark mode,
+  // high contrast, and preferred color scheme changes.
+  color_scheme_observer_ =
+      std::make_unique<NativeTheme::ColorSchemeNativeThemeObserver>(
+          NativeTheme::GetInstanceForWeb());
+  AddObserver(color_scheme_observer_.get());
   OnThemeChanged(gtk_settings_get_default(), nullptr);
 }
 
diff --git a/ui/gtk/native_theme_gtk.h b/ui/gtk/native_theme_gtk.h
index 876a738..ad3b88d41 100644
--- a/ui/gtk/native_theme_gtk.h
+++ b/ui/gtk/native_theme_gtk.h
@@ -93,6 +93,11 @@
   mutable absl::optional<SkColor> color_cache_[kColorId_NumColors];
 
   ScopedCssProvider theme_css_override_;
+
+  // Used to notify the web native theme of changes to dark mode, high
+  // contrast, and preferred color scheme.
+  std::unique_ptr<NativeTheme::ColorSchemeNativeThemeObserver>
+      color_scheme_observer_;
 };
 
 }  // namespace gtk
