[gnome-software/1131-featured-carousel: 7/21] plugins: Move key-colors-metadata plugin into GsApp code




commit d9d79f88a42b3ff209c620af7893dc95e1b281e5
Author: Philip Withnall <pwithnall endlessos org>
Date:   Fri Feb 12 18:03:47 2021 +0000

    plugins: Move key-colors-metadata plugin into GsApp code
    
    There’s no need for alternate implementations of the key-colors-metadata
    plugin, or for it to be disabled conditionally, so it’s more performant
    to move the plugin into the main library. That reduces the number of
    plugins to iterate over for each vfunc call.
    
    The key-colors-metadata code itself is unchanged, just moved into a
    helper method in `gs-common.c`, which code using CSS metadata from apps
    can call when needed.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 contrib/gnome-software.spec.in               |  1 -
 plugins/core/gs-plugin-key-colors-metadata.c | 88 ----------------------------
 plugins/core/meson.build                     | 13 ----
 src/gs-common.c                              | 52 ++++++++++++++++
 src/gs-common.h                              |  2 +
 src/gs-feature-tile.c                        |  5 +-
 src/gs-summary-tile.c                        |  4 +-
 src/gs-upgrade-banner.c                      |  4 +-
 8 files changed, 63 insertions(+), 106 deletions(-)
---
diff --git a/contrib/gnome-software.spec.in b/contrib/gnome-software.spec.in
index b98ce1bd8..21f68f464 100644
--- a/contrib/gnome-software.spec.in
+++ b/contrib/gnome-software.spec.in
@@ -161,7 +161,6 @@ desktop-file-validate %{buildroot}%{_datadir}/applications/*.desktop
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_hardcoded-blocklist.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_hardcoded-popular.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_icons.so
-%{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_key-colors-metadata.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_modalias.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_odrs.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_os-release.so
diff --git a/plugins/core/meson.build b/plugins/core/meson.build
index 193181851..fe9f453f8 100644
--- a/plugins/core/meson.build
+++ b/plugins/core/meson.build
@@ -13,19 +13,6 @@ shared_module(
   dependencies : plugin_libs,
 )
 
-shared_module(
-  'gs_plugin_key-colors-metadata',
-  sources : 'gs-plugin-key-colors-metadata.c',
-  include_directories : [
-    include_directories('../..'),
-    include_directories('../../lib'),
-  ],
-  install : true,
-  install_dir: plugin_dir,
-  c_args : cargs,
-  dependencies : plugin_libs,
-)
-
 shared_module(
   'gs_plugin_provenance',
   sources : 'gs-plugin-provenance.c',
diff --git a/src/gs-common.c b/src/gs-common.c
index dd1ba3f51..2258a2ad5 100644
--- a/src/gs-common.c
+++ b/src/gs-common.c
@@ -354,6 +354,58 @@ gs_utils_widget_css_parsing_error_cb (GtkCssProvider *provider,
                   error->message);
 }
 
+/**
+ * gs_utils_set_key_colors_in_css:
+ * @css: some CSS
+ * @app: a #GsApp to get the key colors from
+ *
+ * Replace placeholders in @css with the key colors from @app, returning a copy
+ * of the CSS with the key colors inlined as `rgb()` literals.
+ *
+ * The key color placeholders are of the form `@keycolor-XX@`, where `XX` is a
+ * two digit counter. The first counter (`00`) will be replaced with the first
+ * key color in @app, the second counter (`01`) with the second, etc.
+ *
+ * CSS may be %NULL, in which case %NULL is returned.
+ *
+ * Returns: (transfer full): a copy of @css with the key color placeholders
+ *     replaced, free with g_free()
+ * Since: 40
+ */
+gchar *
+gs_utils_set_key_colors_in_css (const gchar *css,
+                                GsApp       *app)
+{
+       GPtrArray *key_colors;
+       g_autoptr(GString) css_new = NULL;
+
+       if (css == NULL)
+               return NULL;
+
+       key_colors = gs_app_get_key_colors (app);
+
+       /* Do we not need to do any replacements? */
+       if (key_colors->len == 0 ||
+           g_strstr_len (css, -1, "@keycolor") == NULL)
+               return g_strdup (css);
+
+       /* replace key color values */
+       css_new = g_string_new (css);
+       for (guint j = 0; j < key_colors->len; j++) {
+               GdkRGBA *color = g_ptr_array_index (key_colors, j);
+               g_autofree gchar *key = NULL;
+               g_autofree gchar *value = NULL;
+               key = g_strdup_printf ("@keycolor-%02u@", j);
+               value = g_strdup_printf ("rgb(%.0f,%.0f,%.0f)",
+                                        color->red * 255.f,
+                                        color->green * 255.f,
+                                        color->blue * 255.f);
+               as_gstring_replace (css_new, key, value);
+       }
+
+       return g_string_free (g_steal_pointer (&css_new), FALSE);
+}
+
 /**
  * gs_utils_widget_set_css:
  * @widget: a widget
diff --git a/src/gs-common.h b/src/gs-common.h
index cbd6dc61c..bd9a82a63 100644
--- a/src/gs-common.h
+++ b/src/gs-common.h
@@ -33,6 +33,8 @@ void  gs_image_set_from_pixbuf                (GtkImage               *image,
                                                 const GdkPixbuf        *pixbuf);
 
 gboolean        gs_utils_is_current_desktop    (const gchar    *name);
+gchar          *gs_utils_set_key_colors_in_css (const gchar    *css,
+                                                GsApp          *app);
 void            gs_utils_widget_set_css        (GtkWidget      *widget,
                                                 GtkCssProvider **provider,
                                                 const gchar    *class_name,
diff --git a/src/gs-feature-tile.c b/src/gs-feature-tile.c
index ef3eb0ea6..9fb862f1a 100644
--- a/src/gs-feature-tile.c
+++ b/src/gs-feature-tile.c
@@ -71,8 +71,9 @@ gs_feature_tile_refresh (GsAppTile *self)
 
        if (tile->markup_cache != markup) {
                g_autoptr(GsCss) css = gs_css_new ();
-               if (markup != NULL)
-                       gs_css_parse (css, markup, NULL);
+               g_autofree gchar *modified_markup = gs_utils_set_key_colors_in_css (markup, app);
+               if (modified_markup != NULL)
+                       gs_css_parse (css, modified_markup, NULL);
                gs_utils_widget_set_css (GTK_WIDGET (tile), &tile->tile_provider, "feature-tile",
                                         gs_css_get_markup_for_id (css, "tile"));
                gs_utils_widget_set_css (tile->title, &tile->title_provider, "feature-tile-name",
diff --git a/src/gs-summary-tile.c b/src/gs-summary-tile.c
index 69b46557b..187f8871e 100644
--- a/src/gs-summary-tile.c
+++ b/src/gs-summary-tile.c
@@ -45,6 +45,7 @@ gs_summary_tile_refresh (GsAppTile *self)
        g_autofree gchar *name = NULL;
        const gchar *summary;
        const gchar *css;
+       g_autofree gchar *modified_css = NULL;
 
        if (app == NULL)
                return;
@@ -75,7 +76,8 @@ gs_summary_tile_refresh (GsAppTile *self)
 
        /* perhaps set custom css */
        css = gs_app_get_metadata_item (app, "GnomeSoftware::AppTile-css");
-       gs_utils_widget_set_css (GTK_WIDGET (tile), &tile->tile_provider, "summary-tile", css);
+       modified_css = gs_utils_set_key_colors_in_css (css, app);
+       gs_utils_widget_set_css (GTK_WIDGET (tile), &tile->tile_provider, "summary-tile", modified_css);
 
        accessible = gtk_widget_get_accessible (GTK_WIDGET (tile));
 
diff --git a/src/gs-upgrade-banner.c b/src/gs-upgrade-banner.c
index 7971d5974..70a19d029 100644
--- a/src/gs-upgrade-banner.c
+++ b/src/gs-upgrade-banner.c
@@ -265,6 +265,7 @@ gs_upgrade_banner_set_app (GsUpgradeBanner *self, GsApp *app)
 {
        GsUpgradeBannerPrivate *priv = gs_upgrade_banner_get_instance_private (self);
        const gchar *css;
+       g_autofree gchar *modified_css = NULL;
 
        g_return_if_fail (GS_IS_UPGRADE_BANNER (self));
        g_return_if_fail (GS_IS_APP (app) || app == NULL);
@@ -285,7 +286,8 @@ gs_upgrade_banner_set_app (GsUpgradeBanner *self, GsApp *app)
 
        /* perhaps set custom css */
        css = gs_app_get_metadata_item (app, "GnomeSoftware::UpgradeBanner-css");
-       gs_utils_widget_set_css (priv->box_upgrades, &priv->banner_provider, "upgrade-banner-custom", css);
+       modified_css = gs_utils_set_key_colors_in_css (css, app);
+       gs_utils_widget_set_css (priv->box_upgrades, &priv->banner_provider, "upgrade-banner-custom", 
modified_css);
 
        gs_upgrade_banner_refresh (self);
 }


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