[gtk/wip/otte/listmodel: 9/22] inspector: Make the render node tree use a ListBox



commit 4eea2669accc814b9f5ae2a2fec2829e88196e81
Author: Benjamin Otte <otte redhat com>
Date:   Wed Sep 5 05:53:33 2018 +0200

    inspector: Make the render node tree use a ListBox
    
    It used to be a treeview.

 gtk/gtkrendernodepaintable.c           |   8 +
 gtk/gtkrendernodepaintableprivate.h    |   2 +
 gtk/inspector/gtktreemodelrendernode.c | 671 ---------------------------------
 gtk/inspector/gtktreemodelrendernode.h |  77 ----
 gtk/inspector/meson.build              |   1 -
 gtk/inspector/recorder.c               | 329 ++++++++++++----
 gtk/inspector/recorder.ui              |  22 +-
 7 files changed, 270 insertions(+), 840 deletions(-)
---
diff --git a/gtk/gtkrendernodepaintable.c b/gtk/gtkrendernodepaintable.c
index 35fcd2f545..7ee40b138b 100644
--- a/gtk/gtkrendernodepaintable.c
+++ b/gtk/gtkrendernodepaintable.c
@@ -149,3 +149,11 @@ gtk_render_node_paintable_new (GskRenderNode         *node,
 
   return GDK_PAINTABLE (self);
 }
+
+GskRenderNode *
+gtk_render_node_paintable_get_render_node (GtkRenderNodePaintable *self)
+{
+  g_return_val_if_fail (GTK_IS_RENDER_NODE_PAINTABLE (self), NULL);
+
+  return self->node;
+}
diff --git a/gtk/gtkrendernodepaintableprivate.h b/gtk/gtkrendernodepaintableprivate.h
index 305eecffb6..7a2777338b 100644
--- a/gtk/gtkrendernodepaintableprivate.h
+++ b/gtk/gtkrendernodepaintableprivate.h
@@ -31,6 +31,8 @@ G_DECLARE_FINAL_TYPE (GtkRenderNodePaintable, gtk_render_node_paintable, GTK, RE
 GdkPaintable *  gtk_render_node_paintable_new   (GskRenderNode         *node,
                                                  const graphene_rect_t *bounds);
 
+GskRenderNode * gtk_render_node_paintable_get_render_node       (GtkRenderNodePaintable *self);
+
 G_END_DECLS
 
 #endif /* __GTK_RENDER_NODE_PAINTABLE_H__ */
diff --git a/gtk/inspector/meson.build b/gtk/inspector/meson.build
index 9184e14e5f..97bc8140bd 100644
--- a/gtk/inspector/meson.build
+++ b/gtk/inspector/meson.build
@@ -11,7 +11,6 @@ inspector_sources = files(
   'graphdata.c',
   'gtkstackcombo.c',
   'gtktreemodelcssnode.c',
-  'gtktreemodelrendernode.c',
   'highlightoverlay.c',
   'init.c',
   'inspect-button.c',
diff --git a/gtk/inspector/recorder.c b/gtk/inspector/recorder.c
index 9d72f8b9b9..c136758af0 100644
--- a/gtk/inspector/recorder.c
+++ b/gtk/inspector/recorder.c
@@ -26,7 +26,8 @@
 #include <gtk/gtkmessagedialog.h>
 #include <gtk/gtkpopover.h>
 #include <gtk/gtktogglebutton.h>
-#include <gtk/gtktreeselection.h>
+#include <gtk/gtktreelistmodel.h>
+#include <gtk/gtktreemodel.h>
 #include <gtk/gtktreeview.h>
 #include <gsk/gskrendererprivate.h>
 #include <gsk/gskrendernodeprivate.h>
@@ -35,8 +36,9 @@
 #include <glib/gi18n-lib.h>
 #include <gdk/gdktextureprivate.h>
 #include "gtk/gtkdebug.h"
+#include "gtk/gtkiconprivate.h"
+#include "gtk/gtkrendernodepaintableprivate.h"
 
-#include "gtktreemodelrendernode.h"
 #include "recording.h"
 #include "rendernodeview.h"
 #include "renderrecording.h"
@@ -45,11 +47,11 @@
 struct _GtkInspectorRecorderPrivate
 {
   GListModel *recordings;
-  GtkTreeModel *render_node_model;
+  GtkTreeListModel *render_node_model;
 
   GtkWidget *recordings_list;
   GtkWidget *render_node_view;
-  GtkWidget *render_node_tree;
+  GtkWidget *render_node_list;
   GtkWidget *render_node_save_button;
   GtkWidget *node_property_tree;
   GtkTreeModel *render_node_properties;
@@ -59,12 +61,6 @@ struct _GtkInspectorRecorderPrivate
   gboolean debug_nodes;
 };
 
-enum {
-  COLUMN_NODE_NAME,
-  /* add more */
-  N_NODE_COLUMNS
-};
-
 enum
 {
   PROP_0,
@@ -77,49 +73,130 @@ static GParamSpec *props[LAST_PROP] = { NULL, };
 
 G_DEFINE_TYPE_WITH_PRIVATE (GtkInspectorRecorder, gtk_inspector_recorder, GTK_TYPE_BIN)
 
-static void
-recordings_clear_all (GtkButton            *button,
-                      GtkInspectorRecorder *recorder)
+static GListModel *
+create_render_node_list_model (GskRenderNode **nodes,
+                               guint           n_nodes)
 {
-  GtkInspectorRecorderPrivate *priv = gtk_inspector_recorder_get_instance_private (recorder);
+  GListStore *store;
+  guint i;
 
-  g_list_store_remove_all (G_LIST_STORE (priv->recordings));
+  /* can't put render nodes into list models - they're not GObjects */
+  store = g_list_store_new (GDK_TYPE_PAINTABLE);
+
+  for (i = 0; i < n_nodes; i++)
+    {
+      graphene_rect_t bounds;
+
+      gsk_render_node_get_bounds (nodes[i], &bounds);
+      GdkPaintable *paintable = gtk_render_node_paintable_new (nodes[i], &bounds);
+      g_list_store_append (store, paintable);
+      g_object_unref (paintable);
+    }
+
+  return G_LIST_MODEL (store);
 }
 
-static void
-recordings_list_row_selected (GtkListBox           *box,
-                              GtkListBoxRow        *row,
-                              GtkInspectorRecorder *recorder)
+static GListModel *
+create_list_model_for_render_node (GskRenderNode *node)
 {
-  GtkInspectorRecorderPrivate *priv = gtk_inspector_recorder_get_instance_private (recorder);
-  GtkInspectorRecording *recording;
+  switch (gsk_render_node_get_node_type (node))
+    {
+    default:
+    case GSK_NOT_A_RENDER_NODE:
+      g_assert_not_reached ();
+      return NULL;
 
-  if (row)
-    recording = g_list_model_get_item (priv->recordings, gtk_list_box_row_get_index (row));
-  else
-    recording = NULL;
+    case GSK_CAIRO_NODE:
+    case GSK_TEXT_NODE:
+    case GSK_TEXTURE_NODE:
+    case GSK_COLOR_NODE:
+    case GSK_LINEAR_GRADIENT_NODE:
+    case GSK_REPEATING_LINEAR_GRADIENT_NODE:
+    case GSK_BORDER_NODE:
+    case GSK_INSET_SHADOW_NODE:
+    case GSK_OUTSET_SHADOW_NODE:
+      /* no children */
+      return NULL;
 
-  if (GTK_INSPECTOR_IS_RENDER_RECORDING (recording))
-    {
-      gtk_render_node_view_set_render_node (GTK_RENDER_NODE_VIEW (priv->render_node_view),
-                                            gtk_inspector_render_recording_get_node 
(GTK_INSPECTOR_RENDER_RECORDING (recording)));
-      gtk_render_node_view_set_clip_region (GTK_RENDER_NODE_VIEW (priv->render_node_view),
-                                            gtk_inspector_render_recording_get_clip_region 
(GTK_INSPECTOR_RENDER_RECORDING (recording)));
-      gtk_render_node_view_set_viewport (GTK_RENDER_NODE_VIEW (priv->render_node_view),
-                                         gtk_inspector_render_recording_get_area 
(GTK_INSPECTOR_RENDER_RECORDING (recording)));
-      gtk_tree_model_render_node_set_root_node (GTK_TREE_MODEL_RENDER_NODE (priv->render_node_model),
-                                                gtk_inspector_render_recording_get_node 
(GTK_INSPECTOR_RENDER_RECORDING (recording)));
-    }
-  else
-    {
-      gtk_render_node_view_set_render_node (GTK_RENDER_NODE_VIEW (priv->render_node_view), NULL);
-      gtk_tree_model_render_node_set_root_node (GTK_TREE_MODEL_RENDER_NODE (priv->render_node_model), NULL);
+    case GSK_OFFSET_NODE:
+      return create_render_node_list_model ((GskRenderNode *[1]) { gsk_offset_node_get_child (node) }, 1);
+
+    case GSK_TRANSFORM_NODE:
+      return create_render_node_list_model ((GskRenderNode *[1]) { gsk_transform_node_get_child (node) }, 1);
+
+    case GSK_OPACITY_NODE:
+      return create_render_node_list_model ((GskRenderNode *[1]) { gsk_opacity_node_get_child (node) }, 1);
+
+    case GSK_COLOR_MATRIX_NODE:
+      return create_render_node_list_model ((GskRenderNode *[1]) { gsk_color_matrix_node_get_child (node) }, 
1);
+
+    case GSK_BLUR_NODE:
+      return create_render_node_list_model ((GskRenderNode *[1]) { gsk_blur_node_get_child (node) }, 1);
+
+    case GSK_REPEAT_NODE:
+      return create_render_node_list_model ((GskRenderNode *[1]) { gsk_repeat_node_get_child (node) }, 1);
+
+    case GSK_CLIP_NODE:
+      return create_render_node_list_model ((GskRenderNode *[1]) { gsk_clip_node_get_child (node) }, 1);
+
+    case GSK_ROUNDED_CLIP_NODE:
+      return create_render_node_list_model ((GskRenderNode *[1]) { gsk_rounded_clip_node_get_child (node) }, 
1);
+
+    case GSK_SHADOW_NODE:
+      return create_render_node_list_model ((GskRenderNode *[1]) { gsk_shadow_node_get_child (node) }, 1);
+
+    case GSK_BLEND_NODE:
+      return create_render_node_list_model ((GskRenderNode *[2]) { gsk_blend_node_get_bottom_child (node), 
+                                                                   gsk_blend_node_get_top_child (node) }, 2);
+
+    case GSK_CROSS_FADE_NODE:
+      return create_render_node_list_model ((GskRenderNode *[2]) { gsk_cross_fade_node_get_start_child 
(node),
+                                                                   gsk_cross_fade_node_get_end_child (node) 
}, 2);
+
+    case GSK_CONTAINER_NODE:
+      {
+        GListStore *store;
+        guint i;
+
+        /* can't put render nodes into list models - they're not GObjects */
+        store = g_list_store_new (GDK_TYPE_PAINTABLE);
+
+        for (i = 0; i < gsk_container_node_get_n_children (node); i++)
+          {
+            GskRenderNode *child = gsk_container_node_get_child (node, i);
+            graphene_rect_t bounds;
+            GdkPaintable *paintable;
+
+            gsk_render_node_get_bounds (child, &bounds);
+            paintable = gtk_render_node_paintable_new (child, &bounds);
+            g_list_store_append (store, paintable);
+            g_object_unref (paintable);
+          }
+
+        return G_LIST_MODEL (store);
+      }
+
+    case GSK_DEBUG_NODE:
+      return create_render_node_list_model ((GskRenderNode *[1]) { gsk_debug_node_get_child (node) }, 1);
     }
+}
 
-  gtk_tree_view_expand_all (GTK_TREE_VIEW (priv->render_node_tree));
+static GListModel *
+create_list_model_for_render_node_paintable (gpointer paintable,
+                                             gpointer unused)
+{
+  GskRenderNode *node = gtk_render_node_paintable_get_render_node (paintable);
 
-  if (recording)
-    g_object_unref (recording);
+  return create_list_model_for_render_node (node);
+}
+
+static void
+recordings_clear_all (GtkButton            *button,
+                      GtkInspectorRecorder *recorder)
+{
+  GtkInspectorRecorderPrivate *priv = gtk_inspector_recorder_get_instance_private (recorder);
+
+  g_list_store_remove_all (G_LIST_STORE (priv->recordings));
 }
 
 static const char *
@@ -223,22 +300,107 @@ node_name (GskRenderNode *node)
     }
 }
 
+static GtkWidget *
+create_widget_for_render_node (gpointer row_item,
+                               gpointer unused)
+{
+  GdkPaintable *paintable;
+  GskRenderNode *node;
+  GtkWidget *row, *box, *child;
+  char *name;
+  guint depth;
+
+  paintable = gtk_tree_list_row_get_item (row_item);
+  node = gtk_render_node_paintable_get_render_node (GTK_RENDER_NODE_PAINTABLE (paintable));
+  row = gtk_list_box_row_new ();
+  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 3);
+  gtk_container_add (GTK_CONTAINER (row), box);
+
+  /* expander */
+  depth = gtk_tree_list_row_get_depth (row_item);
+  if (depth > 0)
+    {
+      child = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
+      gtk_widget_set_size_request (child, 16 * depth, 0);
+      gtk_container_add (GTK_CONTAINER (box), child);
+    }
+  if (gtk_tree_list_row_is_expandable (row_item))
+    {
+      GtkWidget *title, *arrow;
+
+      child = g_object_new (GTK_TYPE_BOX, "css-name", "expander", NULL);
+      
+      title = g_object_new (GTK_TYPE_TOGGLE_BUTTON, "css-name", "title", NULL);
+      gtk_button_set_relief (GTK_BUTTON (title), GTK_RELIEF_NONE);
+      g_object_bind_property (row_item, "expanded", title, "active", G_BINDING_BIDIRECTIONAL | 
G_BINDING_SYNC_CREATE);
+      gtk_container_add (GTK_CONTAINER (child), title);
+      g_object_set_data_full (G_OBJECT (row), "make-sure-its-not-unreffed", g_object_ref (row_item), 
g_object_unref);
+
+      arrow = gtk_icon_new ("arrow");
+      gtk_container_add (GTK_CONTAINER (title), arrow);
+    }
+  else
+    {
+      child = gtk_image_new (); /* empty whatever */
+    }
+  gtk_container_add (GTK_CONTAINER (box), child);
+
+  name = node_name (node);
+  child = gtk_label_new (name);
+  g_free (name);
+  gtk_container_add (GTK_CONTAINER (box), child);
+
+  g_object_unref (paintable);
+
+  return row;
+}
+
 static void
-render_node_list_get_value (GtkTreeModelRenderNode *model,
-                            GskRenderNode          *node,
-                            int                     column,
-                            GValue                 *value)
+recordings_list_row_selected (GtkListBox           *box,
+                              GtkListBoxRow        *row,
+                              GtkInspectorRecorder *recorder)
 {
-  switch (column)
+  GtkInspectorRecorderPrivate *priv = gtk_inspector_recorder_get_instance_private (recorder);
+  GtkInspectorRecording *recording;
+
+  if (row)
+    recording = g_list_model_get_item (priv->recordings, gtk_list_box_row_get_index (row));
+  else
+    recording = NULL;
+
+  g_clear_object (&priv->render_node_model);
+
+  if (GTK_INSPECTOR_IS_RENDER_RECORDING (recording))
     {
-    case COLUMN_NODE_NAME:
-      g_value_take_string (value, node_name (node));
-      break;
+      GListModel *root_model;
 
-    default:
-      g_assert_not_reached ();
-      break;
+      gtk_render_node_view_set_render_node (GTK_RENDER_NODE_VIEW (priv->render_node_view),
+                                            gtk_inspector_render_recording_get_node 
(GTK_INSPECTOR_RENDER_RECORDING (recording)));
+      gtk_render_node_view_set_clip_region (GTK_RENDER_NODE_VIEW (priv->render_node_view),
+                                            gtk_inspector_render_recording_get_clip_region 
(GTK_INSPECTOR_RENDER_RECORDING (recording)));
+      gtk_render_node_view_set_viewport (GTK_RENDER_NODE_VIEW (priv->render_node_view),
+                                         gtk_inspector_render_recording_get_area 
(GTK_INSPECTOR_RENDER_RECORDING (recording)));
+
+      root_model = create_list_model_for_render_node (gtk_inspector_render_recording_get_node 
(GTK_INSPECTOR_RENDER_RECORDING (recording)));
+      priv->render_node_model = gtk_tree_list_model_new (FALSE,
+                                                         root_model,
+                                                         TRUE,
+                                                         create_list_model_for_render_node_paintable,
+                                                         NULL, NULL);
+      g_object_unref (root_model);
     }
+  else
+    {
+      gtk_render_node_view_set_render_node (GTK_RENDER_NODE_VIEW (priv->render_node_view), NULL);
+    }
+
+  gtk_list_box_bind_model (GTK_LIST_BOX (priv->render_node_list),
+                           G_LIST_MODEL (priv->render_node_model),
+                           create_widget_for_render_node,
+                           NULL, NULL);
+
+  if (recording)
+    g_object_unref (recording);
 }
 
 static GdkTexture *
@@ -733,24 +895,46 @@ populate_render_node_properties (GtkListStore  *store,
     }
 }
 
+static GskRenderNode *
+get_selected_node (GtkInspectorRecorder *recorder)
+{
+  GtkInspectorRecorderPrivate *priv = gtk_inspector_recorder_get_instance_private (recorder);
+  GtkTreeListRow *row_item;
+  GtkListBoxRow *row;
+  GdkPaintable *paintable;
+  GskRenderNode *node;
+
+  row = gtk_list_box_get_selected_row (GTK_LIST_BOX (priv->render_node_list));
+  if (row == NULL)
+    return NULL;
+
+  row_item = g_list_model_get_item (G_LIST_MODEL (priv->render_node_model),
+                                    gtk_list_box_row_get_index (row));
+  paintable = gtk_tree_list_row_get_item (row_item);
+  node = gtk_render_node_paintable_get_render_node (GTK_RENDER_NODE_PAINTABLE (paintable));
+  g_object_unref (paintable);
+  g_object_unref (row_item);
+
+  return node;
+}
+
 static void
-render_node_list_selection_changed (GtkTreeSelection     *selection,
+render_node_list_selection_changed (GtkListBox           *list,
+                                    GtkListBoxRow        *row,
                                     GtkInspectorRecorder *recorder)
 {
   GtkInspectorRecorderPrivate *priv = gtk_inspector_recorder_get_instance_private (recorder);
   GskRenderNode *node;
-  GtkTreeIter iter;
 
-  if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
+  node = get_selected_node (recorder);
+  if (node == NULL)
     {
       gtk_widget_set_sensitive (priv->render_node_save_button, FALSE);
       return;
     }
 
   gtk_widget_set_sensitive (priv->render_node_save_button, TRUE);
-  node = gtk_tree_model_render_node_get_node_from_iter (GTK_TREE_MODEL_RENDER_NODE 
(priv->render_node_model), &iter);
   gtk_render_node_view_set_render_node (GTK_RENDER_NODE_VIEW (priv->render_node_view), node);
-
   populate_render_node_properties (GTK_LIST_STORE (priv->render_node_properties), node);
 }
 
@@ -800,17 +984,14 @@ static void
 render_node_save (GtkButton            *button,
                   GtkInspectorRecorder *recorder)
 {
-  GtkInspectorRecorderPrivate *priv = gtk_inspector_recorder_get_instance_private (recorder);
   GskRenderNode *node;
-  GtkTreeIter iter;
   GtkWidget *dialog;
   char *filename, *nodename;
 
-  if (!gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW 
(priv->render_node_tree)), NULL, &iter))
+  node = get_selected_node (recorder);
+  if (node == NULL)
     return;
 
-  node = gtk_tree_model_render_node_get_node_from_iter (GTK_TREE_MODEL_RENDER_NODE 
(priv->render_node_model), &iter);
-
   dialog = gtk_file_chooser_dialog_new ("",
                                         GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (recorder))),
                                         GTK_FILE_CHOOSER_ACTION_SAVE,
@@ -1017,6 +1198,17 @@ gtk_inspector_recorder_set_property (GObject      *object,
     }
 }
 
+static void
+gtk_inspector_recorder_dispose (GObject *object)
+{
+  GtkInspectorRecorder *recorder = GTK_INSPECTOR_RECORDER (object);
+  GtkInspectorRecorderPrivate *priv = gtk_inspector_recorder_get_instance_private (recorder);
+
+  g_clear_object (&priv->render_node_model);
+
+  G_OBJECT_CLASS (gtk_inspector_recorder_parent_class)->dispose (object);
+}
+
 static void
 gtk_inspector_recorder_class_init (GtkInspectorRecorderClass *klass)
 {
@@ -1025,6 +1217,7 @@ gtk_inspector_recorder_class_init (GtkInspectorRecorderClass *klass)
 
   object_class->get_property = gtk_inspector_recorder_get_property;
   object_class->set_property = gtk_inspector_recorder_set_property;
+  object_class->dispose = gtk_inspector_recorder_dispose;
 
   props[PROP_RECORDING] =
     g_param_spec_boolean ("recording",
@@ -1046,7 +1239,7 @@ gtk_inspector_recorder_class_init (GtkInspectorRecorderClass *klass)
   gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorRecorder, recordings);
   gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorRecorder, recordings_list);
   gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorRecorder, render_node_view);
-  gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorRecorder, render_node_tree);
+  gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorRecorder, render_node_list);
   gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorRecorder, render_node_save_button);
   gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorRecorder, node_property_tree);
 
@@ -1070,12 +1263,6 @@ gtk_inspector_recorder_init (GtkInspectorRecorder *recorder)
                            recorder,
                            NULL);
 
-  priv->render_node_model = gtk_tree_model_render_node_new (render_node_list_get_value,
-                                                            N_NODE_COLUMNS,
-                                                            G_TYPE_STRING);
-  gtk_tree_view_set_model (GTK_TREE_VIEW (priv->render_node_tree), priv->render_node_model);
-  g_object_unref (priv->render_node_model);
-
   priv->render_node_properties = GTK_TREE_MODEL (gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_STRING, 
G_TYPE_BOOLEAN, GDK_TYPE_TEXTURE));
   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->node_property_tree), priv->render_node_properties);
   g_object_unref (priv->render_node_properties);
diff --git a/gtk/inspector/recorder.ui b/gtk/inspector/recorder.ui
index 6ea6f0244b..607c3d05b5 100644
--- a/gtk/inspector/recorder.ui
+++ b/gtk/inspector/recorder.ui
@@ -81,27 +81,9 @@
                       <class name="sidebar"/>
                     </style>
                     <child>
-                      <object class="GtkTreeView" id="render_node_tree">
+                      <object class="GtkListBox" id="render_node_list">
                         <property name="vexpand">1</property>
-                        <property name="search-column">0</property>
-                        <property name="enable-search">0</property>
-                        <property name="headers-visible">0</property>
-                        <child internal-child="selection">
-                          <object class="GtkTreeSelection">
-                            <signal name="changed" handler="render_node_list_selection_changed"/>
-                          </object>
-                        </child>
-                        <child>
-                          <object class="GtkTreeViewColumn" id="render_node_name_column">
-                            <property name="resizable">1</property>
-                            <child>
-                              <object class="GtkCellRendererText"/>
-                              <attributes>
-                                <attribute name="text">0</attribute>
-                              </attributes>
-                            </child>
-                          </object>
-                        </child>
+                        <signal name="row-selected" handler="render_node_list_selection_changed"/>
                       </object>
                     </child>
                   </object>


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