panel-list: Port to GTK4

- Inherit AdwBin since GtkStack is a final class now
 - Replace GtkContainer APIs by their widget-specific ones
This commit is contained in:
Georges Basile Stavracas Neto 2021-10-19 20:59:49 -03:00
parent 15a67221a0
commit 521ea3a635
3 changed files with 150 additions and 163 deletions

View file

@ -40,11 +40,12 @@ typedef struct
struct _CcPanelList struct _CcPanelList
{ {
GtkStack parent; AdwBin parent;
GtkWidget *privacy_listbox; GtkWidget *privacy_listbox;
GtkWidget *main_listbox; GtkWidget *main_listbox;
GtkWidget *search_listbox; GtkWidget *search_listbox;
GtkStack *stack;
/* When clicking on Details or Devices row, show it /* When clicking on Details or Devices row, show it
* automatically select the first panel of the list. * automatically select the first panel of the list.
@ -62,7 +63,7 @@ struct _CcPanelList
GHashTable *id_to_search_data; 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 enum
{ {
@ -101,7 +102,7 @@ get_widget_from_view (CcPanelList *self,
return self->search_listbox; return self->search_listbox;
case CC_PANEL_LIST_WIDGET: 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: default:
return NULL; return NULL;
@ -184,13 +185,13 @@ switch_to_view (CcPanelList *self,
should_crossfade = view == CC_PANEL_LIST_SEARCH || should_crossfade = view == CC_PANEL_LIST_SEARCH ||
self->previous_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 : should_crossfade ? GTK_STACK_TRANSITION_TYPE_CROSSFADE :
GTK_STACK_TRANSITION_TYPE_SLIDE_LEFT_RIGHT); GTK_STACK_TRANSITION_TYPE_SLIDE_LEFT_RIGHT);
visible_child = get_widget_from_view (self, view); 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 /* For non-search views, make sure the displayed panel matches the
* newly selected row * newly selected row
@ -283,22 +284,21 @@ row_data_new (CcPanelCategory category,
/* Setup the row */ /* Setup the row */
grid = gtk_grid_new (); grid = gtk_grid_new ();
gtk_widget_show (grid);
gtk_widget_set_hexpand (grid, TRUE); 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); gtk_grid_set_column_spacing (GTK_GRID (grid), 12);
/* Icon */ /* 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_style_context_add_class (gtk_widget_get_style_context (image), "sidebar-icon");
gtk_grid_attach (GTK_GRID (grid), image, 0, 0, 1, 1); gtk_grid_attach (GTK_GRID (grid), image, 0, 0, 1, 1);
gtk_widget_show (image);
/* Name label */ /* Name label */
label = gtk_label_new (name); label = gtk_label_new (name);
gtk_widget_show (label);
gtk_label_set_xalign (GTK_LABEL (label), 0.0); gtk_label_set_xalign (GTK_LABEL (label), 0.0);
gtk_widget_set_hexpand (label, TRUE); gtk_widget_set_hexpand (label, TRUE);
gtk_grid_attach (GTK_GRID (grid), label, 1, 0, 1, 1); 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_label_set_xalign (GTK_LABEL (label), 0.0);
gtk_widget_set_hexpand (label, TRUE); gtk_widget_set_hexpand (label, TRUE);
gtk_label_set_max_width_chars (GTK_LABEL (label), 25); 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) 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_style_context_add_class (gtk_widget_get_style_context (image), "sidebar-icon");
gtk_grid_attach (GTK_GRID (grid), image, 2, 0, 1, 1); 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"); 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; data->description_label = label;
gtk_container_add (GTK_CONTAINER (data->row), grid); gtk_list_box_row_set_child (GTK_LIST_BOX_ROW (data->row), grid);
gtk_widget_show (data->row);
g_object_set_data_full (G_OBJECT (data->row), "data", data, (GDestroyNotify) row_data_free); 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 && if (g_strcmp0 (data->id, self->current_panel_id) == 0 &&
self->previous_view != CC_PANEL_LIST_SEARCH && 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"); CC_TRACE_MSG ("Switching to panel widget");
@ -600,8 +599,8 @@ search_row_activated_cb (GtkWidget *listbox,
CcPanelList *self) CcPanelList *self)
{ {
GtkWidget *real_listbox; GtkWidget *real_listbox;
GtkWidget *child;
RowData *data; RowData *data;
GList *children, *l;
CC_ENTRY; CC_ENTRY;
@ -613,13 +612,13 @@ search_row_activated_cb (GtkWidget *listbox,
real_listbox = self->main_listbox; real_listbox = self->main_listbox;
/* Select the correct row */ /* Select the correct row */
children = gtk_container_get_children (GTK_CONTAINER (real_listbox)); for (child = gtk_widget_get_first_child (real_listbox);
child != NULL;
for (l = children; l != NULL; l = l->next) child = gtk_widget_get_next_sibling (child))
{ {
RowData *real_row_data; 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 * 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; 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, privacy_row);
gtk_widget_class_bind_template_child (widget_class, CcPanelList, main_listbox); 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, 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, row_activated_cb);
gtk_widget_class_bind_template_callback (widget_class, search_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); gtk_widget_set_visible (data->row, visibility == CC_PANEL_VISIBLE);
listbox = get_listbox_from_category (self, category); 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 */ /* And add to the search listbox too */
search_data = row_data_new (category, id, title, description, keywords, icon, visibility, has_sidebar); 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_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_data, data->id, data);
g_hash_table_insert (self->id_to_search_data, search_data->id, search_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); previous = get_widget_from_view (self, CC_PANEL_LIST_WIDGET);
if (previous) if (previous)
gtk_container_remove (GTK_CONTAINER (self), previous); gtk_stack_remove (self->stack, previous);
if (widget) 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); switch_to_view (self, CC_PANEL_LIST_WIDGET);
} }
} }

View file

@ -37,7 +37,7 @@ typedef enum
#define CC_TYPE_PANEL_LIST (cc_panel_list_get_type()) #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); GtkWidget* cc_panel_list_new (void);

View file

@ -1,147 +1,136 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<interface> <interface>
<requires lib="gtk+" version="3.20"/> <requires lib="gtk+" version="3.20"/>
<template class="CcPanelList" parent="GtkStack"> <template class="CcPanelList" parent="AdwBin">
<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>
<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" />
<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>
<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>
<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>
</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>
<child type="placeholder"> <object class="GtkStack" id="stack">
<object class="GtkBox" id="empty_search_placeholder"> <property name="vhomogeneous">False</property>
<property name="visible">True</property> <property name="hhomogeneous">True</property>
<property name="can_focus">False</property> <property name="transition_type">slide-left-right</property>
<property name="halign">center</property>
<property name="valign">center</property> <child>
<property name="expand">True</property> <object class="GtkStackPage">
<property name="border_width">18</property> <property name="name">main</property>
<property name="orientation">vertical</property> <property name="child">
<property name="spacing">6</property> <object class="GtkListBox" id="main_listbox">
<child> <signal name="row-activated" handler="row_activated_cb" object="CcPanelList" swapped="no" />
<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> <style>
<class name="dim-label"/> <class name="navigation-sidebar" />
</style> </style>
<child>
<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>
</child>
</object> </object>
</child> </property>
<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>
</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>
</object>
</child>
</object> </object>
</child> </child>
<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" />
<style>
<class name="navigation-sidebar" />
</style>
</object>
</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" />
<style>
<class name="navigation-sidebar" />
</style>
<!-- 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>
</property>
</object>
</child>
</object> </object>
<packing>
<property name="name">search</property>
<property name="position">3</property>
</packing>
</child> </child>
</template> </template>
</interface> </interface>