[gnome-control-center/gbsneto/gtk4: 7/35] panel-list: Port to GTK4



commit ccba3b2462b3328d71b738f0b53f0951a7515464
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Tue Oct 19 20:59:49 2021 -0300

    panel-list: Port to GTK4
    
     - Inherit AdwBin since GtkStack is a final class now
     - Replace GtkContainer APIs by their widget-specific ones

 shell/cc-panel-list.c  |  54 ++++++------
 shell/cc-panel-list.h  |   2 +-
 shell/cc-panel-list.ui | 234 ++++++++++++++++++++++---------------------------
 3 files changed, 134 insertions(+), 156 deletions(-)
---
diff --git a/shell/cc-panel-list.c b/shell/cc-panel-list.c
index c5532ba5b..579b3561f 100644
--- a/shell/cc-panel-list.c
+++ b/shell/cc-panel-list.c
@@ -40,11 +40,12 @@ typedef struct
 
 struct _CcPanelList
 {
-  GtkStack            parent;
+  AdwBin              parent;
 
   GtkWidget          *privacy_listbox;
   GtkWidget          *main_listbox;
   GtkWidget          *search_listbox;
+  GtkStack           *stack;
 
   /* When clicking on Details or Devices row, show it
    * automatically select the first panel of the list.
@@ -62,7 +63,7 @@ struct _CcPanelList
   GHashTable         *id_to_search_data;
 };
 
-G_DEFINE_TYPE (CcPanelList, cc_panel_list, GTK_TYPE_STACK)
+G_DEFINE_TYPE (CcPanelList, cc_panel_list, ADW_TYPE_BIN)
 
 enum
 {
@@ -101,7 +102,7 @@ get_widget_from_view (CcPanelList     *self,
       return self->search_listbox;
 
     case CC_PANEL_LIST_WIDGET:
-      return gtk_stack_get_child_by_name (GTK_STACK (self), "custom-widget");
+      return gtk_stack_get_child_by_name (self->stack, "custom-widget");
 
     default:
       return NULL;
@@ -184,13 +185,13 @@ switch_to_view (CcPanelList     *self,
   should_crossfade = view == CC_PANEL_LIST_SEARCH ||
                      self->previous_view == CC_PANEL_LIST_SEARCH;
 
-  gtk_stack_set_transition_type (GTK_STACK (self),
+  gtk_stack_set_transition_type (self->stack,
                                  should_crossfade ? GTK_STACK_TRANSITION_TYPE_CROSSFADE :
                                                     GTK_STACK_TRANSITION_TYPE_SLIDE_LEFT_RIGHT);
 
   visible_child = get_widget_from_view (self, view);
 
-  gtk_stack_set_visible_child (GTK_STACK (self), visible_child);
+  gtk_stack_set_visible_child (self->stack, visible_child);
 
   /* For non-search views, make sure the displayed panel matches the
    * newly selected row
@@ -283,22 +284,21 @@ row_data_new (CcPanelCategory     category,
 
   /* Setup the row */
   grid = gtk_grid_new ();
-  gtk_widget_show (grid);
   gtk_widget_set_hexpand (grid, TRUE);
-  gtk_container_set_border_width (GTK_CONTAINER (grid), 12);
+  gtk_widget_set_margin_top (grid, 12);
+  gtk_widget_set_margin_bottom (grid, 12);
+  gtk_widget_set_margin_start (grid, 12);
+  gtk_widget_set_margin_end (grid, 12);
   gtk_grid_set_column_spacing (GTK_GRID (grid), 12);
 
   /* Icon */
-  image = gtk_image_new_from_icon_name (icon, GTK_ICON_SIZE_BUTTON);
+  image = gtk_image_new_from_icon_name (icon);
   gtk_style_context_add_class (gtk_widget_get_style_context (image), "sidebar-icon");
 
   gtk_grid_attach (GTK_GRID (grid), image, 0, 0, 1, 1);
 
-  gtk_widget_show (image);
-
   /* Name label */
   label = gtk_label_new (name);
-  gtk_widget_show (label);
   gtk_label_set_xalign (GTK_LABEL (label), 0.0);
   gtk_widget_set_hexpand (label, TRUE);
   gtk_grid_attach (GTK_GRID (grid), label, 1, 0, 1, 1);
@@ -308,14 +308,14 @@ row_data_new (CcPanelCategory     category,
   gtk_label_set_xalign (GTK_LABEL (label), 0.0);
   gtk_widget_set_hexpand (label, TRUE);
   gtk_label_set_max_width_chars (GTK_LABEL (label), 25);
-  gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
+  gtk_label_set_wrap (GTK_LABEL (label), TRUE);
+  gtk_widget_hide (label);
 
   if (has_sidebar)
     {
-      image = gtk_image_new_from_icon_name ("go-next-symbolic", GTK_ICON_SIZE_BUTTON);
+      image = gtk_image_new_from_icon_name ("go-next-symbolic");
       gtk_style_context_add_class (gtk_widget_get_style_context (image), "sidebar-icon");
       gtk_grid_attach (GTK_GRID (grid), image, 2, 0, 1, 1);
-      gtk_widget_show (image);
     }
 
   gtk_style_context_add_class (gtk_widget_get_style_context (label), "dim-label");
@@ -323,8 +323,7 @@ row_data_new (CcPanelCategory     category,
 
   data->description_label = label;
 
-  gtk_container_add (GTK_CONTAINER (data->row), grid);
-  gtk_widget_show (data->row);
+  gtk_list_box_row_set_child (GTK_LIST_BOX_ROW (data->row), grid);
 
   g_object_set_data_full (G_OBJECT (data->row), "data", data, (GDestroyNotify) row_data_free);
 
@@ -577,7 +576,7 @@ row_activated_cb (GtkWidget     *listbox,
    */
   if (g_strcmp0 (data->id, self->current_panel_id) == 0 &&
       self->previous_view != CC_PANEL_LIST_SEARCH &&
-      gtk_stack_get_child_by_name (GTK_STACK (self), "custom-widget") != NULL)
+      gtk_stack_get_child_by_name (self->stack, "custom-widget") != NULL)
     {
       CC_TRACE_MSG ("Switching to panel widget");
 
@@ -600,8 +599,8 @@ search_row_activated_cb (GtkWidget     *listbox,
                          CcPanelList   *self)
 {
   GtkWidget *real_listbox;
+  GtkWidget *child;
   RowData *data;
-  GList *children, *l;
 
   CC_ENTRY;
 
@@ -613,13 +612,13 @@ search_row_activated_cb (GtkWidget     *listbox,
     real_listbox = self->main_listbox;
 
   /* Select the correct row */
-  children = gtk_container_get_children (GTK_CONTAINER (real_listbox));
-
-  for (l = children; l != NULL; l = l->next)
+  for (child = gtk_widget_get_first_child (real_listbox);
+       child != NULL;
+       child = gtk_widget_get_next_sibling (child))
     {
       RowData *real_row_data;
 
-      real_row_data = g_object_get_data (l->data, "data");
+      real_row_data = g_object_get_data (G_OBJECT (child), "data");
 
       /*
        * The main listbox has the Details & Devices rows, and neither
@@ -643,8 +642,6 @@ search_row_activated_cb (GtkWidget     *listbox,
         }
     }
 
-  g_list_free (children);
-
   CC_EXIT;
 }
 
@@ -781,6 +778,7 @@ cc_panel_list_class_init (CcPanelListClass *klass)
   gtk_widget_class_bind_template_child (widget_class, CcPanelList, privacy_row);
   gtk_widget_class_bind_template_child (widget_class, CcPanelList, main_listbox);
   gtk_widget_class_bind_template_child (widget_class, CcPanelList, search_listbox);
+  gtk_widget_class_bind_template_child (widget_class, CcPanelList, stack);
 
   gtk_widget_class_bind_template_callback (widget_class, row_activated_cb);
   gtk_widget_class_bind_template_callback (widget_class, search_row_activated_cb);
@@ -947,13 +945,13 @@ cc_panel_list_add_panel (CcPanelList        *self,
   gtk_widget_set_visible (data->row, visibility == CC_PANEL_VISIBLE);
 
   listbox = get_listbox_from_category (self, category);
-  gtk_container_add (GTK_CONTAINER (listbox), data->row);
+  gtk_list_box_append (GTK_LIST_BOX (listbox), data->row);
 
   /* And add to the search listbox too */
   search_data = row_data_new (category, id, title, description, keywords, icon, visibility, has_sidebar);
   gtk_widget_set_visible (search_data->row, visibility != CC_PANEL_HIDDEN);
 
-  gtk_container_add (GTK_CONTAINER (self->search_listbox), search_data->row);
+  gtk_list_box_append (GTK_LIST_BOX (self->search_listbox), search_data->row);
 
   g_hash_table_insert (self->id_to_data, data->id, data);
   g_hash_table_insert (self->id_to_search_data, search_data->id, search_data);
@@ -1076,11 +1074,11 @@ cc_panel_list_add_sidebar_widget (CcPanelList *self,
 
   previous = get_widget_from_view (self, CC_PANEL_LIST_WIDGET);
   if (previous)
-    gtk_container_remove (GTK_CONTAINER (self), previous);
+    gtk_stack_remove (self->stack, previous);
 
   if (widget)
     {
-      gtk_stack_add_named (GTK_STACK (self), widget, "custom-widget");
+      gtk_stack_add_named (self->stack, widget, "custom-widget");
       switch_to_view (self, CC_PANEL_LIST_WIDGET);
     }
 }
diff --git a/shell/cc-panel-list.h b/shell/cc-panel-list.h
index 8d7324d16..39becac0e 100644
--- a/shell/cc-panel-list.h
+++ b/shell/cc-panel-list.h
@@ -37,7 +37,7 @@ typedef enum
 
 #define CC_TYPE_PANEL_LIST (cc_panel_list_get_type())
 
-G_DECLARE_FINAL_TYPE (CcPanelList, cc_panel_list, CC, PANEL_LIST, GtkStack)
+G_DECLARE_FINAL_TYPE (CcPanelList, cc_panel_list, CC, PANEL_LIST, AdwBin)
 
 GtkWidget*           cc_panel_list_new                           (void);
 
diff --git a/shell/cc-panel-list.ui b/shell/cc-panel-list.ui
index 06ef5a406..e136cec41 100644
--- a/shell/cc-panel-list.ui
+++ b/shell/cc-panel-list.ui
@@ -1,147 +1,127 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <requires lib="gtk+" version="3.20"/>
-  <template class="CcPanelList" parent="GtkStack">
-    <property name="visible">True</property>
-    <property name="can_focus">False</property>
-    <property name="vhomogeneous">False</property>
-    <property name="hhomogeneous">True</property>
-    <property name="transition_type">slide-left-right</property>
+  <template class="CcPanelList" parent="AdwBin">
+
     <child>
-      <object class="GtkListBox" id="main_listbox">
-        <property name="visible">True</property>
-        <property name="can_focus">False</property>
-        <signal name="row-activated" handler="row_activated_cb" object="CcPanelList" swapped="no" />
+      <object class="GtkStack" id="stack">
+        <property name="vhomogeneous">False</property>
+        <property name="hhomogeneous">True</property>
+        <property name="transition_type">slide-left-right</property>
+
         <child>
-          <object class="GtkListBoxRow" id="privacy_row">
-            <property name="visible">False</property>
-            <property name="can_focus">True</property>
-            <child>
-              <object class="GtkBox">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="border_width">12</property>
-                <property name="spacing">12</property>
+          <object class="GtkStackPage">
+            <property name="name">main</property>
+            <property name="child">
+              <object class="GtkListBox" id="main_listbox">
+                <signal name="row-activated" handler="row_activated_cb" object="CcPanelList" swapped="no" />
                 <child>
-                  <object class="GtkImage">
-                    <property name="visible">True</property>
-                    <property name="can_focus">False</property>
-                    <property name="icon_name">preferences-system-privacy-symbolic</property>
-                    <style>
-                      <class name="sidebar-icon" />
-                    </style>
+                  <object class="GtkListBoxRow" id="privacy_row">
+                    <property name="visible">False</property>
+                    <child>
+                      <object class="GtkBox">
+                        <property name="margin-start">12</property>
+                        <property name="margin-end">12</property>
+                        <property name="margin-top">12</property>
+                        <property name="margin-bottom">12</property>
+                        <property name="spacing">12</property>
+                        <child>
+                          <object class="GtkImage">
+                            <property name="icon_name">preferences-system-privacy-symbolic</property>
+                            <style>
+                              <class name="sidebar-icon" />
+                            </style>
+                          </object>
+                        </child>
+                        <child>
+                          <object class="GtkLabel">
+                            <property name="hexpand">True</property>
+                            <property name="label" translatable="yes">Privacy</property>
+                            <property name="xalign">0</property>
+                          </object>
+                        </child>
+                        <child>
+                          <object class="GtkImage">
+                            <property name="icon_name">go-next-symbolic</property>
+                            <style>
+                              <class name="sidebar-icon" />
+                            </style>
+                          </object>
+                        </child>
+                      </object>
+                    </child>
                   </object>
-                  <packing>
-                    <property name="expand">False</property>
-                    <property name="fill">True</property>
-                    <property name="position">0</property>
-                  </packing>
-                </child>
-                <child>
-                  <object class="GtkLabel">
-                    <property name="visible">True</property>
-                    <property name="can_focus">False</property>
-                    <property name="hexpand">True</property>
-                    <property name="label" translatable="yes">Privacy</property>
-                    <property name="xalign">0</property>
-                  </object>
-                  <packing>
-                    <property name="expand">False</property>
-                    <property name="fill">True</property>
-                    <property name="position">1</property>
-                  </packing>
-                </child>
-                <child>
-                  <object class="GtkImage">
-                    <property name="visible">True</property>
-                    <property name="can_focus">False</property>
-                    <property name="icon_name">go-next-symbolic</property>
-                    <style>
-                      <class name="sidebar-icon" />
-                    </style>
-                  </object>
-                  <packing>
-                    <property name="expand">False</property>
-                    <property name="fill">True</property>
-                    <property name="position">2</property>
-                  </packing>
                 </child>
               </object>
-            </child>
+            </property>
           </object>
         </child>
-      </object>
-      <packing>
-        <property name="name">main</property>
-      </packing>
-    </child>
-    <child>
-      <object class="GtkListBox" id="privacy_listbox">
-        <property name="visible">True</property>
-        <property name="can_focus">False</property>
-        <signal name="row-activated" handler="row_activated_cb" object="CcPanelList" swapped="no" />
-      </object>
-      <packing>
-        <property name="name">privacy</property>
-        <property name="position">1</property>
-      </packing>
-    </child>
-    <child>
-      <object class="GtkListBox" id="search_listbox">
-        <property name="visible">True</property>
-        <property name="can_focus">False</property>
-        <signal name="row-activated" handler="search_row_activated_cb" object="CcPanelList" swapped="no" />
 
-        <!-- Placeholder -->
-        <child type="placeholder">
-          <object class="GtkBox" id="empty_search_placeholder">
-            <property name="visible">True</property>
-            <property name="can_focus">False</property>
-            <property name="halign">center</property>
-            <property name="valign">center</property>
-            <property name="expand">True</property>
-            <property name="border_width">18</property>
-            <property name="orientation">vertical</property>
-            <property name="spacing">6</property>
-            <child>
-              <object class="GtkImage">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="pixel_size">64</property>
-                <property name="icon_name">edit-find-symbolic</property>
-                <style>
-                  <class name="dim-label"/>
-                </style>
-              </object>
-            </child>
-            <child>
-              <object class="GtkLabel">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="label" translatable="yes">No results found</property>
-                <attributes>
-                  <attribute name="weight" value="bold"/>
-                  <attribute name="scale" value="1.44"/>
-                </attributes>
+        <child>
+          <object class="GtkStackPage">
+            <property name="name">privacy</property>
+            <property name="child">
+              <object class="GtkListBox" id="privacy_listbox">
+                <signal name="row-activated" handler="row_activated_cb" object="CcPanelList" swapped="no" />
               </object>
-            </child>
-            <child>
-              <object class="GtkLabel">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="label" translatable="yes">Try a different search</property>
-                <style>
-                  <class name="dim-label"/>
-                </style>
+            </property>
+          </object>
+        </child>
+
+        <child>
+          <object class="GtkStackPage">
+            <property name="name">search</property>
+            <property name="child">
+              <object class="GtkListBox" id="search_listbox">
+                <signal name="row-activated" handler="search_row_activated_cb" object="CcPanelList" 
swapped="no" />
+
+                <!-- Placeholder -->
+                <child type="placeholder">
+                  <object class="GtkBox" id="empty_search_placeholder">
+                    <property name="halign">center</property>
+                    <property name="valign">center</property>
+                    <property name="hexpand">True</property>
+                    <property name="vexpand">True</property>
+                    <property name="margin-start">18</property>
+                    <property name="margin-end">18</property>
+                    <property name="margin-top">18</property>
+                    <property name="margin-bottom">18</property>
+                    <property name="orientation">vertical</property>
+                    <property name="spacing">6</property>
+                    <child>
+                      <object class="GtkImage">
+                        <property name="pixel_size">64</property>
+                        <property name="icon_name">edit-find-symbolic</property>
+                        <style>
+                          <class name="dim-label"/>
+                        </style>
+                      </object>
+                    </child>
+                    <child>
+                      <object class="GtkLabel">
+                        <property name="label" translatable="yes">No results found</property>
+                        <attributes>
+                          <attribute name="weight" value="bold"/>
+                          <attribute name="scale" value="1.44"/>
+                        </attributes>
+                      </object>
+                    </child>
+                    <child>
+                      <object class="GtkLabel">
+                        <property name="label" translatable="yes">Try a different search</property>
+                        <style>
+                          <class name="dim-label"/>
+                        </style>
+                      </object>
+                    </child>
+                  </object>
+                </child>
               </object>
-            </child>
+            </property>
           </object>
         </child>
+
       </object>
-      <packing>
-        <property name="name">search</property>
-        <property name="position">3</property>
-      </packing>
     </child>
   </template>
 </interface>


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