[gnome-calendar/gbsneto/gtk4: 20/46] project: Adjust color rendering to use GdkPaintable




commit 2139f40e67658aebc6e91fd8a1f2182b3d7fe98a
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Fri Jan 14 11:16:36 2022 -0300

    project: Adjust color rendering to use GdkPaintable

 src/core/gcal-shell-search-provider.c           |  8 ++---
 src/gui/event-editor/gcal-event-editor-dialog.c | 23 ++++++-------
 src/gui/gcal-calendar-popover.c                 |  8 ++---
 src/gui/gcal-quick-add-popover.c                | 23 ++++++-------
 src/gui/importer/gcal-import-dialog.c           | 24 ++++++--------
 src/search/gcal-search-hit-event.c              | 12 +++----
 src/utils/gcal-utils.c                          | 43 +++++++++++++++++++++++++
 src/utils/gcal-utils.h                          |  2 ++
 8 files changed, 84 insertions(+), 59 deletions(-)
---
diff --git a/src/core/gcal-shell-search-provider.c b/src/core/gcal-shell-search-provider.c
index 8711da49..165436e8 100644
--- a/src/core/gcal-shell-search-provider.c
+++ b/src/core/gcal-shell-search-provider.c
@@ -220,9 +220,9 @@ get_result_metas_cb (GcalShellSearchProvider  *self,
   g_variant_builder_init (&abuilder, G_VARIANT_TYPE ("aa{sv}"));
   for (i = 0; i < g_strv_length (results); i++)
     {
+      g_autoptr (GdkPaintable) paintable = NULL;
       g_autoptr (GVariant) icon_variant = NULL;
       g_autoptr (GdkPixbuf) gicon = NULL;
-      cairo_surface_t *surface;
 
       uuid = results[i];
       event = g_hash_table_lookup (self->events, uuid);
@@ -231,8 +231,8 @@ get_result_metas_cb (GcalShellSearchProvider  *self,
       g_variant_builder_add (&builder, "{sv}", "id", g_variant_new_string (uuid));
       g_variant_builder_add (&builder, "{sv}", "name", g_variant_new_string (gcal_event_get_summary 
(event)));
 
-      surface = get_circle_surface_from_color (gcal_event_get_color (event), 96);
-      gicon = gdk_pixbuf_get_from_surface (surface, 0, 0, 96, 96);
+      paintable = get_circle_paintable_from_color (gcal_event_get_color (event), 96);
+      gicon = paintable_to_pixbuf (paintable);
       icon_variant = g_icon_serialize (G_ICON (gicon));
       g_variant_builder_add (&builder, "{sv}", "icon", icon_variant);
 
@@ -246,8 +246,6 @@ get_result_metas_cb (GcalShellSearchProvider  *self,
 
       g_variant_builder_add (&builder, "{sv}", "description", g_variant_new_string (desc));
       g_variant_builder_add_value (&abuilder, g_variant_builder_end (&builder));
-
-      g_clear_pointer (&surface, cairo_surface_destroy);
     }
   g_dbus_method_invocation_return_value (invocation, g_variant_new ("(aa{sv})", &abuilder));
 
diff --git a/src/gui/event-editor/gcal-event-editor-dialog.c b/src/gui/event-editor/gcal-event-editor-dialog.c
index e2f04234..69b1a82e 100644
--- a/src/gui/event-editor/gcal-event-editor-dialog.c
+++ b/src/gui/event-editor/gcal-event-editor-dialog.c
@@ -151,18 +151,18 @@ fill_sources_menu (GcalEventEditorDialog *self)
 
   for (aux = list; aux != NULL; aux = aux->next)
     {
+      g_autoptr (GdkPaintable) paintable = NULL;
       g_autoptr (GMenuItem) item = NULL;
       g_autoptr (GdkPixbuf) pix = NULL;
       GcalCalendar *calendar;
       const GdkRGBA *color;
-      cairo_surface_t *surface;
 
       calendar = GCAL_CALENDAR (aux->data);
 
       /* retrieve color */
       color = gcal_calendar_get_color (calendar);
-      surface = get_circle_surface_from_color (color, 16);
-      pix = gdk_pixbuf_get_from_surface (surface, 0, 0, 16, 16);
+      paintable = get_circle_paintable_from_color (color, 16);
+      pix = paintable_to_pixbuf (paintable);
 
       /* menu item */
       item = g_menu_item_new (gcal_calendar_get_name (calendar), "select-calendar");
@@ -180,8 +180,6 @@ fill_sources_menu (GcalEventEditorDialog *self)
         }
 
       g_menu_append_item (self->sources_menu, item);
-
-      g_clear_pointer (&surface, cairo_surface_destroy);
     }
 
   gtk_popover_bind_model (GTK_POPOVER (self->sources_popover), G_MENU_MODEL (self->sources_menu), "edit");
@@ -243,19 +241,17 @@ on_calendar_selected_action_cb (GSimpleAction *action,
 
       if (g_strcmp0 (gcal_calendar_get_id (calendar), uid) == 0)
         {
-          cairo_surface_t *surface;
+          g_autoptr (GdkPaintable) paintable = NULL;
           const GdkRGBA *color;
 
           /* retrieve color */
           color = gcal_calendar_get_color (calendar);
-          surface = get_circle_surface_from_color (color, 16);
-          gtk_image_set_from_surface (GTK_IMAGE (self->source_image), surface);
+          paintable = get_circle_paintable_from_color (color, 16);
+          gtk_image_set_from_paintable (GTK_IMAGE (self->source_image), paintable);
 
           self->selected_calendar = calendar;
 
           gtk_label_set_label (GTK_LABEL (self->subtitle_label), gcal_calendar_get_name (calendar));
-
-          g_clear_pointer (&surface, cairo_surface_destroy);
           break;
         }
     }
@@ -597,10 +593,10 @@ gcal_event_editor_dialog_set_event (GcalEventEditorDialog *self,
                                     GcalEvent             *event,
                                     gboolean               new_event)
 {
+  g_autoptr (GdkPaintable) paintable = NULL;
   g_autoptr (GcalEvent) cloned_event = NULL;
   GcalEventEditorFlags flags;
   GcalCalendar *calendar;
-  cairo_surface_t *surface;
   gint i;
 
   GCAL_ENTRY;
@@ -625,9 +621,8 @@ gcal_event_editor_dialog_set_event (GcalEventEditorDialog *self,
   fill_sources_menu (self);
 
   /* dialog titlebar's title & subtitle */
-  surface = get_circle_surface_from_color (gcal_event_get_color (cloned_event), 10);
-  gtk_image_set_from_surface (GTK_IMAGE (self->source_image), surface);
-  g_clear_pointer (&surface, cairo_surface_destroy);
+  paintable = get_circle_paintable_from_color (gcal_event_get_color (cloned_event), 10);
+  gtk_image_set_from_paintable (GTK_IMAGE (self->source_image), paintable);
 
   g_clear_pointer (&self->event_title_binding, g_binding_unbind);
   self->event_title_binding = g_object_bind_property (cloned_event,
diff --git a/src/gui/gcal-calendar-popover.c b/src/gui/gcal-calendar-popover.c
index de86f54a..1b5775df 100644
--- a/src/gui/gcal-calendar-popover.c
+++ b/src/gui/gcal-calendar-popover.c
@@ -59,9 +59,9 @@ static GParamSpec *properties [N_PROPS];
 static GtkWidget*
 make_calendar_row (GcalCalendar *calendar)
 {
+  g_autoptr (GdkPaintable) paintable = NULL;
   GtkWidget *label, *icon, *checkbox, *box, *row;
   GtkStyleContext *context;
-  cairo_surface_t *surface;
   const GdkRGBA *color;
 
   row = gtk_list_box_row_new ();
@@ -78,8 +78,8 @@ make_calendar_row (GcalCalendar *calendar)
 
   /* source color icon */
   color = gcal_calendar_get_color (calendar);
-  surface = get_circle_surface_from_color (color, 16);
-  icon = gtk_image_new_from_surface (surface);
+  paintable = get_circle_paintable_from_color (color, 16);
+  icon = gtk_image_new_from_paintable (paintable);
 
   gtk_style_context_add_class (gtk_widget_get_style_context (icon), "calendar-color-image");
 
@@ -106,8 +106,6 @@ make_calendar_row (GcalCalendar *calendar)
 
   gtk_widget_show_all (row);
 
-  g_clear_pointer (&surface, cairo_surface_destroy);
-
   return row;
 }
 
diff --git a/src/gui/gcal-quick-add-popover.c b/src/gui/gcal-quick-add-popover.c
index f245e64a..b6138834 100644
--- a/src/gui/gcal-quick-add-popover.c
+++ b/src/gui/gcal-quick-add-popover.c
@@ -70,7 +70,7 @@ static GtkWidget*
 create_calendar_row (GcalManager  *manager,
                      GcalCalendar *calendar)
 {
-  cairo_surface_t *surface;
+  g_autoptr (GdkPaintable) paintable = NULL;
   const GdkRGBA *color;
   GtkWidget *row, *box, *icon, *label, *selected_icon;
   gboolean read_only;
@@ -85,8 +85,8 @@ create_calendar_row (GcalManager  *manager,
 
   /* The icon with the source color */
   color = gcal_calendar_get_color (calendar);
-  surface = get_circle_surface_from_color (color, 16);
-  icon = gtk_image_new_from_surface (surface);
+  paintable = get_circle_paintable_from_color (color, 16);
+  icon = gtk_image_new_from_paintable (paintable);
 
   gtk_container_add (GTK_CONTAINER (box), icon);
 
@@ -135,7 +135,6 @@ create_calendar_row (GcalManager  *manager,
   gtk_widget_show (box);
   gtk_widget_show (row);
 
-  g_clear_pointer (&surface, cairo_surface_destroy);
   g_free (parent_name);
   g_free (tooltip);
 
@@ -176,7 +175,7 @@ static void
 select_row (GcalQuickAddPopover *self,
             GtkListBoxRow       *row)
 {
-  cairo_surface_t *surface;
+  g_autoptr (GdkPaintable) paintable = NULL;
   const GdkRGBA *color;
   GcalCalendar *calendar;
   GtkWidget *icon;
@@ -200,8 +199,8 @@ select_row (GcalQuickAddPopover *self,
   gtk_label_set_label (GTK_LABEL (self->calendar_name_label), gcal_calendar_get_name (calendar));
 
   color = gcal_calendar_get_color (calendar);
-  surface = get_circle_surface_from_color (color, 16);
-  gtk_image_set_from_surface (GTK_IMAGE (self->color_image), surface);
+  paintable = get_circle_paintable_from_color (color, 16);
+  gtk_image_set_from_paintable (GTK_IMAGE (self->color_image), paintable);
 
   /* Return to the events page */
   gtk_stack_set_visible_child_name (GTK_STACK (self->stack), "events");
@@ -209,8 +208,6 @@ select_row (GcalQuickAddPopover *self,
   /* Focus back the event Entry */
   if (gtk_widget_get_visible (GTK_WIDGET (self)))
     gtk_entry_grab_focus_without_selecting (GTK_ENTRY (self->summary_entry));
-
-  g_clear_pointer (&surface, cairo_surface_destroy);
 }
 
 static gint
@@ -536,7 +533,7 @@ on_calendar_changed (GcalManager         *manager,
                      GcalCalendar        *calendar,
                      GcalQuickAddPopover *self)
 {
-  cairo_surface_t *surface;
+  g_autoptr (GdkPaintable) paintable = NULL;
   const GdkRGBA *color;
   GtkWidget *row, *color_icon, *name_label;
   gboolean read_only;
@@ -567,10 +564,8 @@ on_calendar_changed (GcalManager         *manager,
 
   /* Setup the source color, in case it changed */
   color = gcal_calendar_get_color (calendar);
-  surface = get_circle_surface_from_color (color, 16);
-  gtk_image_set_from_surface (GTK_IMAGE (color_icon), surface);
-
-  g_clear_pointer (&surface, cairo_surface_destroy);
+  paintable = get_circle_paintable_from_color (color, 16);
+  gtk_image_set_from_paintable (GTK_IMAGE (color_icon), paintable);
 
   /* Also setup the row name, in case we just changed the source name */
   gtk_label_set_text (GTK_LABEL (name_label), gcal_calendar_get_name (calendar));
diff --git a/src/gui/importer/gcal-import-dialog.c b/src/gui/importer/gcal-import-dialog.c
index 12bf8ce5..aa7dcb78 100644
--- a/src/gui/importer/gcal-import-dialog.c
+++ b/src/gui/importer/gcal-import-dialog.c
@@ -109,21 +109,21 @@ static GtkWidget*
 create_calendar_row (GcalManager  *manager,
                      GcalCalendar *calendar)
 {
+  g_autoptr (GdkPaintable) paintable = NULL;
   g_autofree gchar *parent_name = NULL;
-  cairo_surface_t *surface;
   const GdkRGBA *color;
   GtkWidget *icon;
   GtkWidget *row;
 
   color = gcal_calendar_get_color (calendar);
-  surface = get_circle_surface_from_color (color, 16);
+  paintable = get_circle_paintable_from_color (color, 16);
   get_source_parent_name_color (manager,
                                 gcal_calendar_get_source (calendar),
                                 &parent_name,
                                 NULL);
 
   /* The icon with the source color */
-  icon = gtk_image_new_from_surface (surface);
+  icon = gtk_image_new_from_paintable (paintable);
   gtk_style_context_add_class (gtk_widget_get_style_context (icon), "calendar-color-image");
   gtk_widget_show (icon);
 
@@ -141,8 +141,6 @@ create_calendar_row (GcalManager  *manager,
   g_object_set_data_full (G_OBJECT (row), "calendar", g_object_ref (calendar), g_object_unref);
   g_object_set_data (G_OBJECT (row), "color-icon", icon);
 
-  g_clear_pointer (&surface, cairo_surface_destroy);
-
   return row;
 }
 
@@ -175,7 +173,7 @@ static void
 select_row (GcalImportDialog *self,
             GtkListBoxRow    *row)
 {
-  cairo_surface_t *surface;
+  g_autoptr (GdkPaintable) paintable = NULL;
   const GdkRGBA *color;
   GcalCalendar *calendar;
 
@@ -187,10 +185,8 @@ select_row (GcalImportDialog *self,
   gtk_label_set_label (self->calendar_name_label, gcal_calendar_get_name (calendar));
 
   color = gcal_calendar_get_color (calendar);
-  surface = get_circle_surface_from_color (color, 16);
-  gtk_image_set_from_surface (self->calendar_color_image, surface);
-
-  g_clear_pointer (&surface, cairo_surface_destroy);
+  paintable = get_circle_paintable_from_color (color, 16);
+  gtk_image_set_from_paintable (self->calendar_color_image, paintable);
 }
 
 static void
@@ -449,7 +445,7 @@ on_manager_calendar_changed_cb (GcalManager      *manager,
                                 GcalCalendar     *calendar,
                                 GcalImportDialog *self)
 {
-  cairo_surface_t *surface;
+  g_autoptr (GdkPaintable) paintable = NULL;
   const GdkRGBA *color;
   GtkWidget *row, *color_icon;
   gboolean read_only;
@@ -475,13 +471,11 @@ on_manager_calendar_changed_cb (GcalManager      *manager,
 
   /* Setup the source color, in case it changed */
   color = gcal_calendar_get_color (calendar);
-  surface = get_circle_surface_from_color (color, 16);
+  paintable = get_circle_paintable_from_color (color, 16);
   color_icon = g_object_get_data (G_OBJECT (row), "color-icon");
-  gtk_image_set_from_surface (GTK_IMAGE (color_icon), surface);
+  gtk_image_set_from_paintable (GTK_IMAGE (color_icon), paintable);
 
   gtk_list_box_invalidate_sort (GTK_LIST_BOX (self->calendars_listbox));
-
-  g_clear_pointer (&surface, cairo_surface_destroy);
 }
 
 static void
diff --git a/src/search/gcal-search-hit-event.c b/src/search/gcal-search-hit-event.c
index f4304f43..807c9c5a 100644
--- a/src/search/gcal-search-hit-event.c
+++ b/src/search/gcal-search-hit-event.c
@@ -71,12 +71,12 @@ set_event (GcalSearchHitEvent *self,
  * DzlSuggestion overrides
  */
 
-static cairo_surface_t*
-gcal_search_hit_event_get_icon_surface (DzlSuggestion *suggestion,
-                                        GtkWidget     *widget)
+static GdkPaintable*
+gcal_search_hit_event_get_paintable (DzlSuggestion *suggestion,
+                                     GtkWidget     *widget)
 {
+  g_autoptr (GdkPaintable) paintable = NULL;
   GcalSearchHitEvent *self;
-  cairo_surface_t *surface;
   const GdkRGBA *color;
   GcalCalendar *calendar;
 
@@ -84,12 +84,12 @@ gcal_search_hit_event_get_icon_surface (DzlSuggestion *suggestion,
   calendar = gcal_event_get_calendar (self->event);
 
   color = gcal_calendar_get_color (calendar);
-  surface = get_circle_surface_from_color (color, 16);
+  paintable = get_circle_paintable_from_color (color, 16);
 
   /* Inject our custom style class into the given widget */
   gtk_style_context_add_class (gtk_widget_get_style_context (widget), "calendar-color-image");
 
-  return surface;
+  return g_steal_pointer (&paintable);
 }
 
 
diff --git a/src/utils/gcal-utils.c b/src/utils/gcal-utils.c
index 6aa37fa1..977dd5ee 100644
--- a/src/utils/gcal-utils.c
+++ b/src/utils/gcal-utils.c
@@ -182,6 +182,49 @@ get_circle_paintable_from_color (const GdkRGBA *color,
   return gtk_snapshot_to_paintable (snapshot, &GRAPHENE_SIZE_INIT (size, size));
 }
 
+/**
+ * paintable_to_pixbuf:
+ * @color: a #GdkRGBA
+ * @size: the size of the surface
+ *
+ * Creates a circular surface filled with @color. The
+ * surface is always @size x @size.
+ *
+ * Returns: (transfer full): a #cairo_surface_t
+ */
+GdkPixbuf*
+paintable_to_pixbuf (GdkPaintable *paintable)
+{
+  g_autoptr (GtkSnapshot) snapshot = NULL;
+  g_autoptr (GskRenderNode) node = NULL;
+  g_autoptr (GskRenderer) renderer = NULL;
+  g_autoptr (GdkTexture) texture = NULL;
+  g_autoptr (GError) error = NULL;
+  graphene_rect_t viewport;
+
+  snapshot = gtk_snapshot_new ();
+  gdk_paintable_snapshot (paintable, snapshot,
+                          gdk_paintable_get_intrinsic_width (paintable),
+                          gdk_paintable_get_intrinsic_height (paintable));
+  node = gtk_snapshot_free_to_node (g_steal_pointer (&snapshot));
+
+  renderer = gsk_cairo_renderer_new ();
+  gsk_renderer_realize (renderer, NULL, &error);
+  if (error)
+    {
+      g_warning ("Couldn't realize Cairo renderer: %s", error->message);
+      return NULL;
+    }
+
+  viewport = GRAPHENE_RECT_INIT (0, 0,
+                                 gdk_paintable_get_intrinsic_width (paintable),
+                                 gdk_paintable_get_intrinsic_height (paintable));
+  texture = gsk_renderer_render_texture (renderer, node, &viewport);
+  gsk_renderer_unrealize (renderer);
+
+  return gdk_pixbuf_get_from_texture (texture);
+}
+
 /**
  * get_color_name_from_source:
  * @source: an #ESource
diff --git a/src/utils/gcal-utils.h b/src/utils/gcal-utils.h
index 05b27c56..d93040cf 100644
--- a/src/utils/gcal-utils.h
+++ b/src/utils/gcal-utils.h
@@ -55,6 +55,8 @@ GdkPaintable*        gcal_get_paintable_from_color               (const GdkRGBA
 GdkPaintable*        get_circle_paintable_from_color             (const GdkRGBA      *color,
                                                                   gint                size);
 
+GdkPixbuf*           paintable_to_pixbuf                         (GdkPaintable       *paintable);
+
 void                 get_color_name_from_source                  (ESource            *source,
                                                                   GdkRGBA            *out_color);
 


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