File 0001-theme-stop-adw-from-forcing-Adwaita-theme-on-you.patch of Package libadwaita

From afcc682b9a2fe5b2dfc09e734f43c9a41e01472f Mon Sep 17 00:00:00 2001
From: "Florian \"sp1rit\"​"  <sp1rit@national.shitposting.agency>
Date: Fri, 17 Sep 2021 20:59:12 +0000
Subject: [PATCH] theme: stop adw from forcing Adwaita theme on you
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Florian "sp1rit"​ <sp1rit@national.shitposting.agency>
Signed-off-by: Florian "sp1rit"​ <sp1rit@disroot.org>
---
 src/adw-main.c | 60 ++++++++++++++++++++++++++++++++------------------
 1 file changed, 39 insertions(+), 21 deletions(-)

diff --git a/src/adw-main.c b/src/adw-main.c
index 3086d55..2f82067 100644
--- a/src/adw-main.c
+++ b/src/adw-main.c
@@ -9,46 +9,64 @@
 #include <glib/gi18n-lib.h>
 #include <gtk/gtk.h>
 
+static bool adw_high_contrast = false;
+
 static int adw_initialized = FALSE;
+static GtkCssProvider *adw_provider = NULL;
 
-static gboolean
-is_high_contrast (void)
+static void
+get_style_with_priority (GdkDisplay *display, const char **style, int *priority)
 {
-  GdkDisplay *display = gdk_display_get_default ();
   g_auto (GValue) value = G_VALUE_INIT;
   const char *theme_name;
 
   g_value_init (&value, G_TYPE_STRING);
-  if (!gdk_display_get_setting (display, "gtk-theme-name", &value))
-    return FALSE;
+  if (!gdk_display_get_setting (display, "gtk-theme-name", &value)) {
+    *style = "light";
+    *priority = 100;
+    return;
+  }
 
   theme_name = g_value_get_string (&value);
 
-  /* TODO: HighContrast shouldn't be a theme */
-  return !g_strcmp0 (theme_name, "HighContrast") ||
-         !g_strcmp0 (theme_name, "HighContrastInverse");
+  // adw's method of checking hc themes is currently broken in the first place.
+  // If gtk theme is set to "HighContrast", theme_name will be Adwaita anyways.
+  //
+  // TODO: get user preference about high contrast via gsettings rather than an env var.
+  if (getenv("ADW_HC") != NULL)
+    adw_high_contrast = true;
+
+  *priority = adw_high_contrast ? 300 : 100;
+
+  if (g_str_has_suffix(theme_name, "-dark") || g_str_has_suffix(theme_name, "-Dark")) {
+    *style = adw_high_contrast ? "hc-dark" : "dark";
+  } else {
+    *style = adw_high_contrast ? "hc" : "light";
+  }
 }
 
+
 static void
 update_theme (void)
 {
-  GtkSettings *settings = gtk_settings_get_default ();
-  g_autofree char *new_theme_name = NULL;
-  gboolean prefer_dark_theme;
-  const char *variant;
+  const char *style;
+  int priority;
+  g_autofree char *theme_path;
+  GdkDisplay *default_displ = gdk_display_get_default();
 
-  g_object_get (settings,
-                "gtk-application-prefer-dark-theme", &prefer_dark_theme,
-                NULL);
+  get_style_with_priority(default_displ, &style, &priority);
+
+  if (adw_provider != NULL) {
+    gtk_style_context_remove_provider_for_display(default_displ, (GtkStyleProvider*)adw_provider);
+    g_object_unref(adw_provider);
+  }
+  adw_provider = gtk_css_provider_new();
 
-  if (is_high_contrast ())
-    variant = prefer_dark_theme ? "hc-dark" : "hc";
-  else
-    variant = prefer_dark_theme ? "dark" : "light";
+  theme_path = g_strdup_printf ("/org/gtk/libgtk/theme/Adwaita-%s/gtk.css", style);
 
-  new_theme_name = g_strdup_printf ("Adwaita-%s", variant);
+  gtk_css_provider_load_from_resource(adw_provider, theme_path);
 
-  g_object_set (settings, "gtk-theme-name", new_theme_name, NULL);
+  gtk_style_context_add_provider_for_display(default_displ, (GtkStyleProvider*)adw_provider, priority);
 }
 
 static void
-- 
2.33.0

openSUSE Build Service is sponsored by