[recipes] Redo our custom css handling



commit 38eabab55174157e6113ce15119371f0c8f65829
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Mar 4 10:04:01 2017 -0500

    Redo our custom css handling
    
    Instead of creating two CSS provider objects, just import
    the recipes.css from the cuisine.css, since we are rewriting
    cuisine.css at runtime anyway.
    
    At the same time, introduce separate recipes-light.css and
    recipes-dark.css files, so we can do color tweaks for the
    dark theme in the future.
    
    And reload the CSS when the gtk-application-prefer-dark-theme
    setting changes. This is not really a common occurrence, but
    it is easy to do and it helps for debugging the CSS with the
    GTK+ inspector.

 src/Makefile.am                  |    1 +
 src/gr-app.c                     |   82 ++++++++++++++++++++++++--------------
 src/gr-cuisine.c                 |    7 ++-
 src/gr-cuisine.h                 |    2 +-
 src/recipes-dark.css             |    1 +
 src/recipes-images.gresource.xml |    2 +
 src/recipes-light.css            |    1 +
 7 files changed, 63 insertions(+), 33 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 95e5b3f..9166175 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -8,6 +8,7 @@ gnome_recipes_CFLAGS = \
        -Wno-sign-compare               \
        -O0 -ggdb                       \
        $(RECIPES_CFLAGS)               \
+       -DG_LOG_DOMAIN=\"$(PACKAGE_NAME)\"      \
        -DPACKAGE_NAME=\"$(PACKAGE_NAME)\"      \
        -DPKGDATADIR=\"$(pkgdatadir)\"  \
        -DDATADIR=\"$(datadir)\"        \
diff --git a/src/gr-app.c b/src/gr-app.c
index 41a8677..ba450c5 100644
--- a/src/gr-app.c
+++ b/src/gr-app.c
@@ -41,6 +41,8 @@ struct _GrApp
         GrRecipeStore *store;
         GrShellSearchProvider *search_provider;
         GrRecipeExporter *exporter;
+
+        GtkCssProvider *css_provider;
 };
 
 G_DEFINE_TYPE (GrApp, gr_app, GTK_TYPE_APPLICATION)
@@ -53,6 +55,7 @@ gr_app_finalize (GObject *object)
 
         g_clear_object (&self->store);
         g_clear_object (&self->search_provider);
+        g_clear_object (&self->css_provider);
 
         G_OBJECT_CLASS (gr_app_parent_class)->finalize (object);
 }
@@ -212,13 +215,57 @@ static GActionEntry app_entries[] =
 };
 
 static void
+load_application_css (GrApp *app)
+{
+        gboolean dark;
+        const char *css_file;
+        const char *src_file;
+        const char *resource;
+        const char *path;
+        g_autofree char *css = NULL;
+
+        if (!app->css_provider) {
+                app->css_provider = gtk_css_provider_new ();
+                g_signal_connect_swapped (gtk_settings_get_default (), 
"notify::gtk-application-prefer-dark-theme",
+                                          G_CALLBACK (load_application_css), app);
+        }
+
+        g_object_get (gtk_settings_get_default (),
+                      "gtk-application-prefer-dark-theme", &dark,
+                      NULL);
+
+        if (dark) {
+                css_file = "recipes-dark.css";
+                src_file = "src/recipes-dark.css";
+                resource = "resource:///org/gnome/Recipes/recipes-dark.css";
+        }
+        else {
+                css_file = "recipes-light.css";
+                src_file = "src/recipes-light.css";
+                resource = "resource:///org/gnome/Recipes/recipes-light.css";
+        }
+
+        if (g_file_test (css_file, G_FILE_TEST_EXISTS))
+                path = css_file;
+        else if (g_file_test (src_file, G_FILE_TEST_EXISTS))
+                path = src_file;
+        else
+                path = resource;
+
+        g_message ("Loading application CSS from %s", path);
+
+        css = gr_cuisine_get_css (path);
+
+        gtk_css_provider_load_from_data (app->css_provider, css, -1, NULL);
+        gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
+                                                   GTK_STYLE_PROVIDER (app->css_provider),
+                                                   GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+}
+
+static void
 gr_app_startup (GApplication *app)
 {
         const gchar *quit_accels[2] = { "<Ctrl>Q", NULL };
-        g_autoptr(GtkCssProvider) css_provider = NULL;
-        g_autoptr(GFile) file = NULL;
-        g_autofree char *css = NULL;
-        const char *path;
 
         G_APPLICATION_CLASS (gr_app_parent_class)->startup (app);
 
@@ -239,32 +286,7 @@ gr_app_startup (GApplication *app)
                                                "app.quit",
                                                quit_accels);
 
-        css_provider = gtk_css_provider_new ();
-        if (g_file_test ("recipes.css", G_FILE_TEST_EXISTS)) {
-                path = "recipes.css";
-                file = g_file_new_for_path (path);
-        }
-        else if (g_file_test ("src/recipes.css", G_FILE_TEST_EXISTS)) {
-                path = "src/recipes.css";
-                file = g_file_new_for_path (path);
-        }
-        else {
-                path = "resource:///org/gnome/Recipes/recipes.css";
-                file = g_file_new_for_uri (path);
-        }
-        g_message ("Load CSS from: %s", path);
-        gtk_css_provider_load_from_file (css_provider, file, NULL);
-        gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
-                                                   GTK_STYLE_PROVIDER (css_provider),
-                                                   GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
-        g_object_unref (css_provider);
-
-        css_provider = gtk_css_provider_new ();
-        css = gr_cuisine_get_css ();
-        gtk_css_provider_load_from_data (css_provider, css, -1, NULL);
-        gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
-                                                   GTK_STYLE_PROVIDER (css_provider),
-                                                   GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+        load_application_css (GR_APP (app));
 }
 
 static void
diff --git a/src/gr-cuisine.c b/src/gr-cuisine.c
index d6a364b..0972acc 100644
--- a/src/gr-cuisine.c
+++ b/src/gr-cuisine.c
@@ -156,7 +156,7 @@ gr_cuisine_get_data (const char  *name,
 }
 
 char *
-gr_cuisine_get_css (void)
+gr_cuisine_get_css (const char *import_url)
 {
         g_autoptr(GFile) file = NULL;
         const char *path;
@@ -176,10 +176,13 @@ gr_cuisine_get_css (void)
                 path = "resource:///org/gnome/Recipes/cuisine.css";
                 file = g_file_new_for_uri (path);
         }
-        g_message ("Load CSS from: %s", path);
+
         g_file_load_contents (file, NULL, &css, NULL, NULL, NULL);
 
         s = g_string_new ("");
+
+        g_string_append_printf (s, "@import url(\"%s\");\n", import_url);
+
         p = css;
         while (1) {
                 q = strstr (p, "@pkgdatadir@");
diff --git a/src/gr-cuisine.h b/src/gr-cuisine.h
index 4561372..668c7e7 100644
--- a/src/gr-cuisine.h
+++ b/src/gr-cuisine.h
@@ -29,6 +29,6 @@ void         gr_cuisine_get_data  (const char  *name,
                                    const char **title,
                                    const char **full_title,
                                    const char **description);
-char        *gr_cuisine_get_css   (void);
+char        *gr_cuisine_get_css   (const char  *import_url);
 
 G_END_DECLS
diff --git a/src/recipes-dark.css b/src/recipes-dark.css
new file mode 100644
index 0000000..d0c9a30
--- /dev/null
+++ b/src/recipes-dark.css
@@ -0,0 +1 @@
+@import url("resource:///org/gnome/Recipes/recipes.css");
diff --git a/src/recipes-images.gresource.xml b/src/recipes-images.gresource.xml
index 8f8f3af..96d0ff6 100644
--- a/src/recipes-images.gresource.xml
+++ b/src/recipes-images.gresource.xml
@@ -3,6 +3,8 @@
   <gresource prefix="/org/gnome/Recipes">
     <file>org.gnome.Recipes-symbolic.symbolic.png</file>
     <file>recipes.css</file>
+    <file>recipes-light.css</file>
+    <file>recipes-dark.css</file>
     <file>cuisine.css</file>
     <file>icons/16x16/apps/garlic-content-symbolic.symbolic.png</file>
     <file>icons/24x24/apps/garlic-content-symbolic.symbolic.png</file>
diff --git a/src/recipes-light.css b/src/recipes-light.css
new file mode 100644
index 0000000..d0c9a30
--- /dev/null
+++ b/src/recipes-light.css
@@ -0,0 +1 @@
+@import url("resource:///org/gnome/Recipes/recipes.css");


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]