[gnome-calendar/gbsneto/gtk4: 11/37] calendar-management/calendars-page: Port to GTK4




commit 5795ab19082fffb468da9043ee7d2379e5c88f5b
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Tue Jan 11 17:14:37 2022 -0300

    calendar-management/calendars-page: Port to GTK4

 src/gui/calendar-management/calendar-row.ui        | 55 +++++++----------
 src/gui/calendar-management/gcal-calendars-page.c  | 41 ++++++-------
 src/gui/calendar-management/gcal-calendars-page.ui | 33 ++++-------
 src/utils/gcal-utils.c                             | 69 +++++++++-------------
 src/utils/gcal-utils.h                             |  4 +-
 5 files changed, 80 insertions(+), 122 deletions(-)
---
diff --git a/src/gui/calendar-management/calendar-row.ui b/src/gui/calendar-management/calendar-row.ui
index b1893648..8b04770c 100644
--- a/src/gui/calendar-management/calendar-row.ui
+++ b/src/gui/calendar-management/calendar-row.ui
@@ -1,73 +1,62 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<!-- Generated with glade 3.18.1 -->
 <interface>
-  <requires lib="gtk+" version="3.16"/>
   <object class="GtkListBoxRow" id="row">
     <property name="width_request">100</property>
-    <property name="visible">True</property>
-    <property name="can_focus">True</property>
     <child>
       <object class="GtkGrid" id="grid">
-        <property name="visible">True</property>
-        <property name="can_focus">False</property>
-        <property name="border_width">12</property>
+        <property name="margin-top">12</property>
+        <property name="margin-bottom">12</property>
+        <property name="margin-start">12</property>
+        <property name="margin-end">12</property>
         <property name="column_spacing">6</property>
         <child>
           <object class="GtkImage" id="icon">
-            <property name="visible">True</property>
-            <property name="can_focus">False</property>
             <property name="pixel_size">24</property>
+            <layout>
+              <property name="column">0</property>
+              <property name="row">0</property>
+              <property name="row-span">2</property>
+            </layout>
             <style>
               <class name="calendar-color-image"/>
             </style>
           </object>
-          <packing>
-            <property name="left_attach">0</property>
-            <property name="top_attach">0</property>
-            <property name="height">2</property>
-          </packing>
         </child>
         <child>
           <object class="GtkLabel" id="subtitle">
-            <property name="visible">True</property>
-            <property name="can_focus">False</property>
             <property name="hexpand">True</property>
             <property name="xalign">0</property>
+            <layout>
+              <property name="column">1</property>
+              <property name="row">1</property>
+            </layout>
             <style>
               <class name="dim-label"/>
             </style>
           </object>
-          <packing>
-            <property name="left_attach">1</property>
-            <property name="top_attach">1</property>
-          </packing>
         </child>
         <child>
           <object class="GtkLabel" id="title">
-            <property name="visible">True</property>
-            <property name="can_focus">False</property>
             <property name="xalign">0</property>
+            <layout>
+              <property name="column">1</property>
+              <property name="row">0</property>
+            </layout>
           </object>
-          <packing>
-            <property name="left_attach">1</property>
-            <property name="top_attach">0</property>
-          </packing>
         </child>
 
         <!-- Visibility switch -->
         <child>
           <object class="GtkSwitch" id="switch">
-            <property name="visible">True</property>
-            <property name="can_focus">False</property>
             <property name="hexpand">True</property>
             <property name="halign">end</property>
             <property name="valign">center</property>
+            <layout>
+              <property name="column">2</property>
+              <property name="row">0</property>
+              <property name="row-span">2</property>
+            </layout>
           </object>
-          <packing>
-            <property name="left_attach">2</property>
-            <property name="top_attach">0</property>
-            <property name="height">2</property>
-          </packing>
         </child>
       </object>
     </child>
diff --git a/src/gui/calendar-management/gcal-calendars-page.c 
b/src/gui/calendar-management/gcal-calendars-page.c
index ef712a38..52507b68 100644
--- a/src/gui/calendar-management/gcal-calendars-page.c
+++ b/src/gui/calendar-management/gcal-calendars-page.c
@@ -70,9 +70,9 @@ static GtkWidget*
 make_calendar_row (GcalCalendarsPage *self,
                    GcalCalendar      *calendar)
 {
+  g_autoptr (GdkPaintable) color_paintable = NULL;
   g_autofree gchar *parent_name = NULL;
   g_autoptr (GtkBuilder) builder = NULL;
-  cairo_surface_t *surface;
   const GdkRGBA *color;
   GcalManager *manager;
   GtkWidget *bottom_label;
@@ -95,9 +95,9 @@ make_calendar_row (GcalCalendarsPage *self,
 
   /* source color icon */
   color = gcal_calendar_get_color (calendar);
-  surface = get_circle_surface_from_color (color, 24);
+  color_paintable = get_circle_paintable_from_color (color, 24);
   icon = GTK_WIDGET (gtk_builder_get_object (builder, "icon"));
-  gtk_image_set_from_surface (GTK_IMAGE (icon), surface);
+  gtk_image_set_from_paintable (GTK_IMAGE (icon), color_paintable);
 
   /* source name label */
   top_label = GTK_WIDGET (gtk_builder_get_object (builder, "title"));
@@ -123,8 +123,6 @@ make_calendar_row (GcalCalendarsPage *self,
   bottom_label = GTK_WIDGET (gtk_builder_get_object (builder, "subtitle"));
   gtk_label_set_label (GTK_LABEL (bottom_label), parent_name);
 
-  g_clear_pointer (&surface, cairo_surface_destroy);
-
   gtk_size_group_add_widget (self->sizegroup, row);
 
   return row;
@@ -134,16 +132,15 @@ static void
 add_calendar (GcalCalendarsPage *self,
               GcalCalendar      *calendar)
 {
-  g_autoptr (GList) children = NULL;
+  GtkWidget *child;
   GtkWidget *row;
   ESource *source;
-  GList *l;
-
-  children = gtk_container_get_children (GTK_CONTAINER (self->listbox));
 
-  for (l = children; l; l = l->next)
+  for (child = gtk_widget_get_first_child (GTK_WIDGET (self->listbox));
+       child != NULL;
+       child = gtk_widget_get_next_sibling (child))
     {
-      if (g_object_get_data (l->data, "calendar") == calendar)
+      if (g_object_get_data (G_OBJECT (child), "calendar") == calendar)
         return;
     }
 
@@ -152,7 +149,7 @@ add_calendar (GcalCalendarsPage *self,
   row = make_calendar_row (self, calendar);
   g_object_set_data (G_OBJECT (row), "source", source);
   g_object_set_data (G_OBJECT (row), "calendar", calendar);
-  gtk_container_add (GTK_CONTAINER (self->listbox), row);
+  gtk_list_box_append (self->listbox, row);
 }
 
 
@@ -160,18 +157,17 @@ static void
 remove_calendar (GcalCalendarsPage *self,
                  GcalCalendar      *calendar)
 {
-  g_autoptr (GList) children = NULL;
-  GList *aux;
-
-  children = gtk_container_get_children (GTK_CONTAINER (self->listbox));
+  GtkWidget *child;
 
-  for (aux = children; aux != NULL; aux = aux->next)
+  for (child = gtk_widget_get_first_child (GTK_WIDGET (self->listbox));
+       child != NULL;
+       child = gtk_widget_get_next_sibling (child))
     {
-      GcalCalendar *row_calendar = g_object_get_data (G_OBJECT (aux->data), "calendar");
+      GcalCalendar *row_calendar = g_object_get_data (G_OBJECT (child), "calendar");
 
       if (row_calendar && row_calendar == calendar)
         {
-          gtk_widget_destroy (aux->data);
+          gtk_list_box_remove (self->listbox, child);
           break;
         }
     }
@@ -269,13 +265,12 @@ on_calendar_color_changed_cb (GcalCalendar *calendar,
                               GParamSpec   *pspec,
                               GtkImage     *icon)
 {
-  cairo_surface_t *surface;
+  g_autoptr (GdkPaintable) color_paintable = NULL;
   const GdkRGBA *color;
 
   color = gcal_calendar_get_color (calendar);
-  surface = get_circle_surface_from_color (color, 24);
-  gtk_image_set_from_surface (GTK_IMAGE (icon), surface);
-  g_clear_pointer (&surface, cairo_surface_destroy);
+  color_paintable = get_circle_paintable_from_color (color, 24);
+  gtk_image_set_from_paintable (GTK_IMAGE (icon), color_paintable);
 }
 
 static void
diff --git a/src/gui/calendar-management/gcal-calendars-page.ui 
b/src/gui/calendar-management/gcal-calendars-page.ui
index ed4c9a57..4defd6eb 100644
--- a/src/gui/calendar-management/gcal-calendars-page.ui
+++ b/src/gui/calendar-management/gcal-calendars-page.ui
@@ -1,51 +1,38 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <template class="GcalCalendarsPage" parent="GtkBox">
-    <property name="visible">True</property>
     <property name="orientation">vertical</property>
     <child>
       <object class="GtkOverlay">
-        <property name="visible">True</property>
 
         <child type="overlay">
           <object class="GtkRevealer" id="notification_revealer">
-            <property name="visible">True</property>
             <property name="halign">center</property>
             <property name="valign">start</property>
             <child>
               <object class="GtkFrame">
-                <property name="visible">True</property>
                 <child>
                   <object class="GtkBox">
-                    <property name="visible">True</property>
                     <property name="spacing">12</property>
                     <child>
                       <object class="GtkLabel" id="notification_label">
-                        <property name="visible">True</property>
                         <property name="use_markup">True</property>
                       </object>
                     </child>
                     <child>
                       <object class="GtkButton" id="notification_action_button">
-                        <property name="visible">True</property>
                         <property name="label" translatable="yes">Undo</property>
                         <signal name="clicked" handler="on_undo_remove_button_clicked_cb" 
object="GcalCalendarsPage" swapped="no"/>
                       </object>
                     </child>
                     <child>
                       <object class="GtkButton">
-                        <property name="visible">True</property>
-                        <property name="can_focus">True</property>
-                        <property name="receives_default">True</property>
-                        <property name="relief">none</property>
+                        <property name="icon-name">window-close-symbolic</property>
                         <property name="focus_on_click">False</property>
                         <signal name="clicked" handler="on_close_notification_button_clicked_cb" 
object="GcalCalendarsPage" swapped="no"/>
-                        <child>
-                          <object class="GtkImage">
-                            <property name="visible">True</property>
-                            <property name="icon_name">window-close-symbolic</property>
-                          </object>
-                        </child>
+                        <style>
+                          <class name="flat" />
+                        </style>
                       </object>
                     </child>
                   </object>
@@ -60,7 +47,6 @@
 
         <child>
           <object class="GtkScrolledWindow">
-            <property name="visible">True</property>
             <property name="hexpand">True</property>
             <property name="vexpand">True</property>
             <property name="hscrollbar_policy">never</property>
@@ -69,8 +55,10 @@
 
             <child>
               <object class="GtkListBox" id="listbox">
-                <property name="visible">True</property>
-                <property name="margin">24</property>
+                <property name="margin-top">24</property>
+                <property name="margin-bottom">24</property>
+                <property name="margin-start">24</property>
+                <property name="margin-end">24</property>
                 <property name="selection-mode">none</property>
                 <signal name="row-activated" handler="on_listbox_row_activated_cb" 
object="GcalCalendarsPage" swapped="no" />
                 <style>
@@ -80,12 +68,11 @@
 
                 <child>
                   <object class="GtkListBoxRow" id="add_calendar_row">
-                    <property name="visible">True</property>
 
                     <child>
                       <object class="GtkLabel">
-                        <property name="visible">True</property>
-                        <property name="expand">True</property>
+                        <property name="hexpand">True</property>
+                        <property name="vexpand">True</property>
                         <property name="xalign">0.5</property>
                         <property name="yalign">0.5</property>
                         <property name="label" translatable="yes">Add Calendar…</property>
diff --git a/src/utils/gcal-utils.c b/src/utils/gcal-utils.c
index 90778f54..6aa37fa1 100644
--- a/src/utils/gcal-utils.c
+++ b/src/utils/gcal-utils.c
@@ -132,39 +132,27 @@ gcal_get_month_name (gint i)
 }
 
 /**
- * gcal_get_surface_from_color:
+ * gcal_get_paintable_from_color:
  * @color: a #GdkRGBA
  * @size: the size of the surface
  *
  * Creates a squared surface filled with @color. The
  * surface is always @size x @size.
  *
- * Returns: (transfer full): a #cairo_surface_t
+ * Returns: (transfer full): a #GdkPaintable
  */
-cairo_surface_t*
-gcal_get_surface_from_color (const GdkRGBA *color,
-                             gint           size)
+GdkPaintable*
+gcal_get_paintable_from_color (const GdkRGBA *color,
+                               gint           size)
 {
-  cairo_surface_t *surface;
-  cairo_t *cr;
-
-  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, size, size);
-  cr = cairo_create (surface);
-
-  cairo_set_source_rgba (cr,
-                         color->red,
-                         color->green,
-                         color->blue,
-                         color->alpha);
-  cairo_rectangle (cr, 0, 0, size, size);
-  cairo_fill (cr);
-  cairo_destroy (cr);
-
-  return surface;
+  g_autoptr (GtkSnapshot) snapshot = gtk_snapshot_new ();
+
+  gtk_snapshot_append_color (snapshot, color, &GRAPHENE_RECT_INIT (0, 0, size, size));
+  return gtk_snapshot_to_paintable (snapshot, &GRAPHENE_SIZE_INIT (size, size));
 }
 
 /**
- * get_circle_surface_from_color:
+ * get_circle_paintable_from_color:
  * @color: a #GdkRGBA
  * @size: the size of the surface
  *
@@ -173,26 +161,25 @@ gcal_get_surface_from_color (const GdkRGBA *color,
  *
  * Returns: (transfer full): a #cairo_surface_t
  */
-cairo_surface_t*
-get_circle_surface_from_color (const GdkRGBA *color,
-                               gint           size)
+GdkPaintable*
+get_circle_paintable_from_color (const GdkRGBA *color,
+                                 gint           size)
 {
-  cairo_surface_t *surface;
-  cairo_t *cr;
-
-  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, size, size);
-  cr = cairo_create (surface);
-
-  cairo_set_source_rgba (cr,
-                         color->red,
-                         color->green,
-                         color->blue,
-                         color->alpha);
-  cairo_arc (cr, size / 2.0, size / 2.0, size / 2.0, 0., 2 * M_PI);
-  cairo_fill (cr);
-  cairo_destroy (cr);
-
-  return surface;
+  g_autoptr (GtkSnapshot) snapshot = NULL;
+  GskRoundedRect rect;
+
+  snapshot = gtk_snapshot_new ();
+
+  gtk_snapshot_push_rounded_clip (snapshot,
+                                  gsk_rounded_rect_init_from_rect (&rect,
+                                                                   &GRAPHENE_RECT_INIT (0, 0, size, size),
+                                                                   size / 2.0));
+
+  gtk_snapshot_append_color (snapshot, color, &GRAPHENE_RECT_INIT (0, 0, size, size));
+
+  gtk_snapshot_pop (snapshot);
+
+  return gtk_snapshot_to_paintable (snapshot, &GRAPHENE_SIZE_INIT (size, size));
 }
 
 /**
diff --git a/src/utils/gcal-utils.h b/src/utils/gcal-utils.h
index d2d3631d..05b27c56 100644
--- a/src/utils/gcal-utils.h
+++ b/src/utils/gcal-utils.h
@@ -49,10 +49,10 @@ gchar*               gcal_get_weekday                            (gint
 
 gchar*               gcal_get_month_name                         (gint                i);
 
-cairo_surface_t*     gcal_get_surface_from_color                 (const GdkRGBA      *color,
+GdkPaintable*        gcal_get_paintable_from_color               (const GdkRGBA      *color,
                                                                   gint                size);
 
-cairo_surface_t*     get_circle_surface_from_color               (const GdkRGBA      *color,
+GdkPaintable*        get_circle_paintable_from_color             (const GdkRGBA      *color,
                                                                   gint                size);
 
 void                 get_color_name_from_source                  (ESource            *source,


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