[gnome-builder] libide/tweaks: create page rows using GtkListBoxRow subclass



commit fb5244078ac17acdb6f467f6f0a6561505c49e8c
Author: Christian Hergert <chergert redhat com>
Date:   Tue Aug 2 19:05:16 2022 -0700

    libide/tweaks: create page rows using GtkListBoxRow subclass

 .../tweaks/ide-tweaks-panel-list-row-private.h     |  38 ++++++
 src/libide/tweaks/ide-tweaks-panel-list-row.c      | 135 +++++++++++++++++++++
 src/libide/tweaks/ide-tweaks-panel-list-row.ui     |  37 ++++++
 src/libide/tweaks/ide-tweaks-panel-list.c          |  34 +++++-
 src/libide/tweaks/libide-tweaks.gresource.xml      |   1 +
 src/libide/tweaks/meson.build                      |   1 +
 6 files changed, 245 insertions(+), 1 deletion(-)
---
diff --git a/src/libide/tweaks/ide-tweaks-panel-list-row-private.h 
b/src/libide/tweaks/ide-tweaks-panel-list-row-private.h
new file mode 100644
index 000000000..5119009ac
--- /dev/null
+++ b/src/libide/tweaks/ide-tweaks-panel-list-row-private.h
@@ -0,0 +1,38 @@
+/*
+ * ide-tweaks-panel-list-row-private.h
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+
+#include "ide-tweaks-item.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_TWEAKS_PANEL_LIST_ROW (ide_tweaks_panel_list_row_get_type())
+
+G_DECLARE_FINAL_TYPE (IdeTweaksPanelListRow, ide_tweaks_panel_list_row, IDE, TWEAKS_PANEL_LIST_ROW, 
GtkListBoxRow)
+
+IdeTweaksItem *ide_tweaks_panel_list_row_get_item (IdeTweaksPanelListRow *self);
+void           ide_tweaks_panel_list_row_set_item (IdeTweaksPanelListRow *self,
+                                                   IdeTweaksItem         *item);
+
+G_END_DECLS
diff --git a/src/libide/tweaks/ide-tweaks-panel-list-row.c b/src/libide/tweaks/ide-tweaks-panel-list-row.c
new file mode 100644
index 000000000..9b94cac09
--- /dev/null
+++ b/src/libide/tweaks/ide-tweaks-panel-list-row.c
@@ -0,0 +1,135 @@
+/*
+ * ide-tweaks-panel-list-row.c
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "ide-tweaks-panel-list-row"
+
+#include "config.h"
+
+#include "ide-tweaks-panel-list-row-private.h"
+
+struct _IdeTweaksPanelListRow
+{
+  GtkListBoxRow  parent_instance;
+  IdeTweaksItem *item;
+};
+
+G_DEFINE_FINAL_TYPE (IdeTweaksPanelListRow, ide_tweaks_panel_list_row, GTK_TYPE_LIST_BOX_ROW)
+
+enum {
+  PROP_0,
+  PROP_ITEM,
+  N_PROPS
+};
+
+static GParamSpec *properties [N_PROPS];
+
+static void
+ide_tweaks_panel_list_row_dispose (GObject *object)
+{
+  IdeTweaksPanelListRow *self = (IdeTweaksPanelListRow *)object;
+
+  g_clear_object (&self->item);
+
+  G_OBJECT_CLASS (ide_tweaks_panel_list_row_parent_class)->dispose (object);
+}
+
+static void
+ide_tweaks_panel_list_row_get_property (GObject    *object,
+                                        guint       prop_id,
+                                        GValue     *value,
+                                        GParamSpec *pspec)
+{
+  IdeTweaksPanelListRow *self = IDE_TWEAKS_PANEL_LIST_ROW (object);
+
+  switch (prop_id)
+    {
+    case PROP_ITEM:
+      g_value_set_object (value, ide_tweaks_panel_list_row_get_item (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_tweaks_panel_list_row_set_property (GObject      *object,
+                                        guint         prop_id,
+                                        const GValue *value,
+                                        GParamSpec   *pspec)
+{
+  IdeTweaksPanelListRow *self = IDE_TWEAKS_PANEL_LIST_ROW (object);
+
+  switch (prop_id)
+    {
+    case PROP_ITEM:
+      ide_tweaks_panel_list_row_set_item (self, g_value_get_object (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_tweaks_panel_list_row_class_init (IdeTweaksPanelListRowClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  object_class->dispose = ide_tweaks_panel_list_row_dispose;
+  object_class->get_property = ide_tweaks_panel_list_row_get_property;
+  object_class->set_property = ide_tweaks_panel_list_row_set_property;
+
+  properties [PROP_ITEM] =
+    g_param_spec_object ("item", NULL, NULL,
+                         IDE_TYPE_TWEAKS_ITEM,
+                         (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+
+  gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/libide-tweaks/ide-tweaks-panel-list-row.ui");
+}
+
+static void
+ide_tweaks_panel_list_row_init (IdeTweaksPanelListRow *self)
+{
+  gtk_widget_init_template (GTK_WIDGET (self));
+}
+
+IdeTweaksItem *
+ide_tweaks_panel_list_row_get_item (IdeTweaksPanelListRow *self)
+{
+  g_return_val_if_fail (IDE_IS_TWEAKS_PANEL_LIST_ROW (self), NULL);
+
+  return self->item;
+}
+
+void
+ide_tweaks_panel_list_row_set_item (IdeTweaksPanelListRow *self,
+                                    IdeTweaksItem         *item)
+{
+  g_return_if_fail (IDE_IS_TWEAKS_PANEL_LIST_ROW (self));
+  g_return_if_fail (!item || IDE_IS_TWEAKS_ITEM (item));
+
+  if (g_set_object (&self->item, item))
+    g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ITEM]);
+}
diff --git a/src/libide/tweaks/ide-tweaks-panel-list-row.ui b/src/libide/tweaks/ide-tweaks-panel-list-row.ui
new file mode 100644
index 000000000..949aabfd5
--- /dev/null
+++ b/src/libide/tweaks/ide-tweaks-panel-list-row.ui
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="IdeTweaksPanelListRow" parent="GtkListBoxRow">
+    <child>
+      <object class="GtkBox">
+        <property name="spacing">12</property>
+        <property name="margin-start">12</property>
+        <property name="margin-end">12</property>
+        <property name="margin-top">12</property>
+        <property name="margin-bottom">12</property>
+        <child>
+          <object class="GtkImage" id="image">
+            <property name="valign">baseline</property>
+            <binding name="icon-name">
+              <lookup name="icon-name" type="IdeTweaksPage">
+                <lookup name="item">IdeTweaksPanelListRow</lookup>
+              </lookup>
+            </binding>
+          </object>
+        </child>
+        <child>
+          <object class="GtkLabel" id="label">
+            <property name="valign">baseline</property>
+            <property name="xalign">0</property>
+            <property name="hexpand">true</property>
+            <property name="ellipsize">end</property>
+            <binding name="label">
+              <lookup name="title" type="IdeTweaksPage">
+                <lookup name="item">IdeTweaksPanelListRow</lookup>
+              </lookup>
+            </binding>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/libide/tweaks/ide-tweaks-panel-list.c b/src/libide/tweaks/ide-tweaks-panel-list.c
index 4a0b0a369..53816084b 100644
--- a/src/libide/tweaks/ide-tweaks-panel-list.c
+++ b/src/libide/tweaks/ide-tweaks-panel-list.c
@@ -26,6 +26,7 @@
 #include "ide-tweaks-model-private.h"
 #include "ide-tweaks-page.h"
 #include "ide-tweaks-panel-list-private.h"
+#include "ide-tweaks-panel-list-row-private.h"
 #include "ide-tweaks-section.h"
 #include "ide-tweaks-subpage.h"
 
@@ -48,6 +49,31 @@ G_DEFINE_FINAL_TYPE (IdeTweaksPanelList, ide_tweaks_panel_list, ADW_TYPE_BIN)
 
 static GParamSpec *properties [N_PROPS];
 
+static void
+ide_tweaks_panel_list_header_func (IdeTweaksPanelListRow *row,
+                                   IdeTweaksPanelListRow *before,
+                                   gpointer               user_data)
+{
+  GtkWidget *header = NULL;
+  IdeTweaksItem *before_item = NULL;
+  IdeTweaksItem *row_item;
+
+  g_assert (!before || IDE_IS_TWEAKS_PANEL_LIST_ROW (before));
+  g_assert (IDE_IS_TWEAKS_PANEL_LIST_ROW (row));
+
+  if (before != NULL)
+    before_item = ide_tweaks_panel_list_row_get_item (before);
+  row_item = ide_tweaks_panel_list_row_get_item (row);
+
+  if (IDE_IS_TWEAKS_PAGE (before_item) &&
+      IDE_IS_TWEAKS_PAGE (row_item) &&
+      ide_tweaks_page_get_section (IDE_TWEAKS_PAGE (before_item)) !=
+      ide_tweaks_page_get_section (IDE_TWEAKS_PAGE (row_item)))
+    header = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
+
+  gtk_list_box_row_set_header (GTK_LIST_BOX_ROW (row), header);
+}
+
 static void
 ide_tweaks_panel_list_dispose (GObject *object)
 {
@@ -119,6 +145,10 @@ static void
 ide_tweaks_panel_list_init (IdeTweaksPanelList *self)
 {
   gtk_widget_init_template (GTK_WIDGET (self));
+
+  gtk_list_box_set_header_func (self->list_box,
+                                (GtkListBoxUpdateHeaderFunc)ide_tweaks_panel_list_header_func,
+                                NULL, NULL);
 }
 
 GtkWidget *
@@ -147,7 +177,9 @@ static GtkWidget *
 ide_tweaks_panel_list_create_row_cb (gpointer item,
                                      gpointer user_data)
 {
-  return g_object_new (GTK_TYPE_LABEL, "label", "TODO: ", NULL);
+  return g_object_new (IDE_TYPE_TWEAKS_PANEL_LIST_ROW,
+                       "item", item,
+                       NULL);
 }
 
 static IdeTweaksItemVisitResult
diff --git a/src/libide/tweaks/libide-tweaks.gresource.xml b/src/libide/tweaks/libide-tweaks.gresource.xml
index 87955e1a8..12070f107 100644
--- a/src/libide/tweaks/libide-tweaks.gresource.xml
+++ b/src/libide/tweaks/libide-tweaks.gresource.xml
@@ -3,6 +3,7 @@
   <gresource prefix="/org/gnome/libide-tweaks">
     <file preprocess="xml-stripblanks">ide-tweaks-panel.ui</file>
     <file preprocess="xml-stripblanks">ide-tweaks-panel-list.ui</file>
+    <file preprocess="xml-stripblanks">ide-tweaks-panel-list-row.ui</file>
     <file preprocess="xml-stripblanks">ide-tweaks-window.ui</file>
   </gresource>
 </gresources>
diff --git a/src/libide/tweaks/meson.build b/src/libide/tweaks/meson.build
index a063fdda6..43df28eb0 100644
--- a/src/libide/tweaks/meson.build
+++ b/src/libide/tweaks/meson.build
@@ -44,6 +44,7 @@ libide_tweaks_private_sources = [
   'ide-tweaks-model.c',
   'ide-tweaks-panel.c',
   'ide-tweaks-panel-list.c',
+  'ide-tweaks-panel-list-row.c',
 ]
 
 #


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