inspector: Stop using GtkTreeView for css nodes

Replace the css node tree with a GtkColumnView, using
the new gtk_css_node_observe_children api.
This commit is contained in:
Matthias Clasen 2022-10-07 15:50:48 -04:00
parent f8357512d0
commit 6815f3af6d
3 changed files with 298 additions and 198 deletions

View File

@ -26,23 +26,18 @@
#include "prop-editor.h"
#include "window.h"
#include "gtktreemodelcssnode.h"
#include "gtktreeview.h"
#include "gtklabel.h"
#include "gtkpopover.h"
#include "gtk/gtkwidgetprivate.h"
#include "gtkcssproviderprivate.h"
#include "gtkcssstylepropertyprivate.h"
#include "gtkcssstyleprivate.h"
#include "gtkcssvalueprivate.h"
#include "gtkcssselectorprivate.h"
#include "gtkliststore.h"
#include "gtksettings.h"
#include "gtktreeview.h"
#include "gtktreeselection.h"
#include "gtktypebuiltins.h"
#include "gtkstack.h"
#include "gtknoselection.h"
#include "gtksingleselection.h"
#include "gtkcolumnview.h"
#include "gtkcolumnviewcolumn.h"
@ -167,16 +162,6 @@ css_property_new (const char *name,
/* }}} */
enum {
COLUMN_NODE_NAME,
COLUMN_NODE_VISIBLE,
COLUMN_NODE_CLASSES,
COLUMN_NODE_ID,
COLUMN_NODE_STATE,
/* add more */
N_NODE_COLUMNS
};
enum
{
PROP_0,
@ -187,11 +172,10 @@ enum
struct _GtkInspectorCssNodeTreePrivate
{
GListStore *root_model;
GtkTreeListModel *node_model;
GtkSingleSelection *selection_model;
GtkWidget *node_tree;
GtkTreeModel *node_model;
GtkTreeViewColumn *node_name_column;
GtkTreeViewColumn *node_id_column;
GtkTreeViewColumn *node_classes_column;
GListStore *prop_model;
GtkWidget *prop_tree;
GtkCssNode *node;
@ -201,74 +185,21 @@ static GParamSpec *properties[N_PROPS] = { NULL, };
G_DEFINE_TYPE_WITH_PRIVATE (GtkInspectorCssNodeTree, gtk_inspector_css_node_tree, GTK_TYPE_BOX)
typedef struct {
GtkCssNode *node;
const char *prop_name;
GdkRectangle rect;
GtkInspectorCssNodeTree *cnt;
} NodePropEditor;
static void
show_node_prop_editor (NodePropEditor *npe)
{
GtkWidget *popover;
GtkWidget *editor;
popover = gtk_popover_new ();
gtk_widget_set_parent (popover, GTK_WIDGET (npe->cnt));
gtk_popover_set_pointing_to (GTK_POPOVER (popover), &npe->rect);
editor = gtk_inspector_prop_editor_new (G_OBJECT (npe->node), npe->prop_name, NULL);
gtk_popover_set_child (GTK_POPOVER (popover), editor);
gtk_popover_popup (GTK_POPOVER (popover));
g_signal_connect (popover, "unmap", G_CALLBACK (gtk_widget_unparent), NULL);
}
static void
row_activated (GtkTreeView *tv,
GtkTreePath *path,
GtkTreeViewColumn *col,
GtkInspectorCssNodeTree *cnt)
{
GtkTreeIter iter;
NodePropEditor npe;
npe.cnt = cnt;
if (col == cnt->priv->node_name_column)
npe.prop_name = "name";
else if (col == cnt->priv->node_id_column)
npe.prop_name = "id";
else if (col == cnt->priv->node_classes_column)
npe.prop_name = "classes";
else
return;
gtk_tree_model_get_iter (cnt->priv->node_model, &iter, path);
npe.node = gtk_tree_model_css_node_get_node_from_iter (GTK_TREE_MODEL_CSS_NODE (cnt->priv->node_model), &iter);
gtk_tree_view_get_cell_area (tv, path, col, &npe.rect);
gtk_tree_view_convert_bin_window_to_widget_coords (tv, npe.rect.x, npe.rect.y, &npe.rect.x, &npe.rect.y);
show_node_prop_editor (&npe);
}
static void
gtk_inspector_css_node_tree_set_node (GtkInspectorCssNodeTree *cnt,
GtkCssNode *node);
static void
selection_changed (GtkTreeSelection *selection, GtkInspectorCssNodeTree *cnt)
selection_changed (GtkSelectionModel *model,
GParamSpec *pspec,
GtkInspectorCssNodeTree *cnt)
{
GtkTreeIter iter;
GtkTreeListRow *row;
GtkCssNode *node;
if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
return;
row = gtk_single_selection_get_selected_item (cnt->priv->selection_model);
node = gtk_tree_list_row_get_item (row);
node = gtk_tree_model_css_node_get_node_from_iter (GTK_TREE_MODEL_CSS_NODE (cnt->priv->node_model), &iter);
gtk_inspector_css_node_tree_set_node (cnt, node);
}
@ -354,13 +285,7 @@ gtk_inspector_css_node_tree_class_init (GtkInspectorCssNodeTreeClass *klass)
gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/inspector/css-node-tree.ui");
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorCssNodeTree, node_tree);
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorCssNodeTree, node_name_column);
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorCssNodeTree, node_id_column);
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorCssNodeTree, node_classes_column);
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorCssNodeTree, prop_tree);
gtk_widget_class_bind_template_callback (widget_class, row_activated);
gtk_widget_class_bind_template_callback (widget_class, selection_changed);
}
static int
@ -411,47 +336,6 @@ format_state_flags (GtkStateFlags state)
return g_strdup ("");
}
static void
gtk_inspector_css_node_tree_get_node_value (GtkTreeModelCssNode *model,
GtkCssNode *node,
int column,
GValue *value)
{
char **strv;
char *s;
switch (column)
{
case COLUMN_NODE_NAME:
g_value_set_string (value, g_quark_to_string (gtk_css_node_get_name (node)));
break;
case COLUMN_NODE_VISIBLE:
g_value_set_boolean (value, gtk_css_node_get_visible (node));
break;
case COLUMN_NODE_CLASSES:
strv = gtk_css_node_get_classes (node);
strv_sort (strv);
s = g_strjoinv (" ", strv);
g_value_take_string (value, s);
g_strfreev (strv);
break;
case COLUMN_NODE_ID:
g_value_set_string (value, g_quark_to_string (gtk_css_node_get_id (node)));
break;
case COLUMN_NODE_STATE:
g_value_take_string (value, format_state_flags (gtk_css_node_get_state (node)));
break;
default:
g_assert_not_reached ();
break;
}
}
static void
setup_label (GtkSignalListItemFactory *factory,
GtkListItem *list_item)
@ -512,6 +396,206 @@ bind_location (GtkSignalListItemFactory *factory,
gtk_label_set_text (GTK_LABEL (label), property->location);
}
static GListModel *
create_model_for_node (gpointer object,
gpointer user_data)
{
return gtk_css_node_observe_children (GTK_CSS_NODE (object));
}
static void
setup_node_label (GtkSignalListItemFactory *factory,
GtkListItem *list_item)
{
GtkWidget *expander;
GtkWidget *label;
expander = gtk_tree_expander_new ();
label = gtk_editable_label_new ("");
gtk_editable_set_width_chars (GTK_EDITABLE (label), 5);
gtk_tree_expander_set_child (GTK_TREE_EXPANDER (expander), label);
gtk_list_item_set_child (list_item, expander);
}
static void
setup_editable_label (GtkSignalListItemFactory *factory,
GtkListItem *list_item)
{
GtkWidget *label;
label = gtk_editable_label_new ("");
gtk_editable_set_width_chars (GTK_EDITABLE (label), 5);
gtk_list_item_set_child (list_item, label);
}
static void
name_changed (GtkEditable *editable,
GParamSpec *pspec,
GtkCssNode *node)
{
gtk_css_node_set_name (node, g_quark_from_string (gtk_editable_get_text (editable)));
}
static void
bind_node_name (GtkSignalListItemFactory *factory,
GtkListItem *list_item)
{
GtkWidget *expander;
GtkWidget *label;
GtkTreeListRow *row;
GtkCssNode *node;
const char *text;
row = gtk_list_item_get_item (list_item);
node = gtk_tree_list_row_get_item (row);
expander = gtk_list_item_get_child (list_item);
gtk_tree_expander_set_list_row (GTK_TREE_EXPANDER (expander), row);
label = gtk_tree_expander_get_child (GTK_TREE_EXPANDER (expander));
text = g_quark_to_string (gtk_css_node_get_name (node));
if (!text)
text = "";
gtk_editable_set_text (GTK_EDITABLE (label), text);
g_signal_connect (label, "notify::text", G_CALLBACK (name_changed), node);
}
static void
unbind_node_name (GtkSignalListItemFactory *factory,
GtkListItem *list_item)
{
GtkWidget *expander;
GtkWidget *label;
GtkTreeListRow *row;
GtkCssNode *node;
row = gtk_list_item_get_item (list_item);
node = gtk_tree_list_row_get_item (row);
expander = gtk_list_item_get_child (list_item);
label = gtk_tree_expander_get_child (GTK_TREE_EXPANDER (expander));
g_signal_handlers_disconnect_by_func (label, G_CALLBACK (name_changed), node);
}
static void
id_changed (GtkEditable *editable,
GParamSpec *pspec,
GtkCssNode *node)
{
gtk_css_node_set_id (node, g_quark_from_string (gtk_editable_get_text (editable)));
}
static void
bind_node_id (GtkSignalListItemFactory *factory,
GtkListItem *list_item)
{
GtkWidget *label;
GtkTreeListRow *row;
GtkCssNode *node;
const char *text;
row = gtk_list_item_get_item (list_item);
node = gtk_tree_list_row_get_item (row);
label = gtk_list_item_get_child (list_item);
text = g_quark_to_string (gtk_css_node_get_id (node));
if (!text)
text = "";
gtk_editable_set_text (GTK_EDITABLE (label), text);
g_signal_connect (label, "notify::text", G_CALLBACK (id_changed), node);
}
static void
unbind_node_id (GtkSignalListItemFactory *factory,
GtkListItem *list_item)
{
GtkWidget *label;
GtkTreeListRow *row;
GtkCssNode *node;
row = gtk_list_item_get_item (list_item);
node = gtk_tree_list_row_get_item (row);
label = gtk_list_item_get_child (list_item);
g_signal_handlers_disconnect_by_func (label, G_CALLBACK (id_changed), node);
}
static void
classes_changed (GtkEditable *editable,
GParamSpec *pspec,
GtkCssNode *node)
{
const char *text;
char **classes;
text = gtk_editable_get_text (editable);
classes = g_strsplit (text, " ", -1);
gtk_css_node_set_classes (node, (const char **)classes);
g_strfreev (classes);
}
static void
bind_node_classes (GtkSignalListItemFactory *factory,
GtkListItem *list_item)
{
GtkWidget *label;
GtkTreeListRow *row;
GtkCssNode *node;
char **classes;
char *text;
row = gtk_list_item_get_item (list_item);
node = gtk_tree_list_row_get_item (row);
label = gtk_list_item_get_child (list_item);
classes = gtk_css_node_get_classes (node);
strv_sort (classes);
text = g_strjoinv (" ", classes);
gtk_editable_set_text (GTK_EDITABLE (label), text);
g_strfreev (classes);
g_free (text);
g_signal_connect (label, "notify::text", G_CALLBACK (classes_changed), node);
}
static void
unbind_node_classes (GtkSignalListItemFactory *factory,
GtkListItem *list_item)
{
GtkWidget *label;
GtkTreeListRow *row;
GtkCssNode *node;
row = gtk_list_item_get_item (list_item);
node = gtk_tree_list_row_get_item (row);
label = gtk_list_item_get_child (list_item);
g_signal_handlers_disconnect_by_func (label, G_CALLBACK (classes_changed), node);
}
static void
bind_node_state (GtkSignalListItemFactory *factory,
GtkListItem *list_item)
{
GtkWidget *label;
GtkTreeListRow *row;
GtkCssNode *node;
char *text;
row = gtk_list_item_get_item (list_item);
node = gtk_tree_list_row_get_item (row);
label = gtk_list_item_get_child (list_item);
text = format_state_flags (gtk_css_node_get_state (node));
gtk_label_set_text (GTK_LABEL (label), text);
g_free (text);
}
static void
gtk_inspector_css_node_tree_init (GtkInspectorCssNodeTree *cnt)
{
@ -527,15 +611,52 @@ gtk_inspector_css_node_tree_init (GtkInspectorCssNodeTree *cnt)
gtk_widget_init_template (GTK_WIDGET (cnt));
priv = cnt->priv;
priv->node_model = gtk_tree_model_css_node_new (gtk_inspector_css_node_tree_get_node_value,
N_NODE_COLUMNS,
G_TYPE_STRING,
G_TYPE_BOOLEAN,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING);
gtk_tree_view_set_model (GTK_TREE_VIEW (priv->node_tree), priv->node_model);
g_object_unref (priv->node_model);
priv->root_model = g_list_store_new (gtk_css_node_get_type ());
priv->node_model = gtk_tree_list_model_new (G_LIST_MODEL (priv->root_model),
FALSE, FALSE,
create_model_for_node,
NULL, NULL);
priv->selection_model = gtk_single_selection_new (G_LIST_MODEL (priv->node_model));
g_signal_connect (priv->selection_model, "notify::selected", G_CALLBACK (selection_changed), cnt);
gtk_column_view_set_model (GTK_COLUMN_VIEW (priv->node_tree), GTK_SELECTION_MODEL (priv->selection_model));
g_object_unref (priv->selection_model);
column = g_list_model_get_item (gtk_column_view_get_columns (GTK_COLUMN_VIEW (priv->node_tree)), 0);
factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK (setup_node_label), NULL);
g_signal_connect (factory, "bind", G_CALLBACK (bind_node_name), NULL);
g_signal_connect (factory, "unbind", G_CALLBACK (unbind_node_name), NULL);
gtk_column_view_column_set_factory (column, factory);
g_object_unref (factory);
g_object_unref (column);
column = g_list_model_get_item (gtk_column_view_get_columns (GTK_COLUMN_VIEW (priv->node_tree)), 1);
factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK (setup_editable_label), NULL);
g_signal_connect (factory, "bind", G_CALLBACK (bind_node_id), NULL);
g_signal_connect (factory, "unbind", G_CALLBACK (unbind_node_id), NULL);
gtk_column_view_column_set_factory (column, factory);
g_object_unref (factory);
g_object_unref (column);
column = g_list_model_get_item (gtk_column_view_get_columns (GTK_COLUMN_VIEW (priv->node_tree)), 2);
factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK (setup_editable_label), NULL);
g_signal_connect (factory, "bind", G_CALLBACK (bind_node_classes), NULL);
g_signal_connect (factory, "unbind", G_CALLBACK (unbind_node_classes), NULL);
gtk_column_view_column_set_factory (column, factory);
g_object_unref (factory);
g_object_unref (column);
column = g_list_model_get_item (gtk_column_view_get_columns (GTK_COLUMN_VIEW (priv->node_tree)), 3);
factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK (setup_label), NULL);
g_signal_connect (factory, "bind", G_CALLBACK (bind_node_state), NULL);
gtk_column_view_column_set_factory (column, factory);
g_object_unref (factory);
g_object_unref (column);
priv->prop_model = g_list_store_new (css_property_get_type ());
@ -543,9 +664,7 @@ gtk_inspector_css_node_tree_init (GtkInspectorCssNodeTree *cnt)
g_object_ref (gtk_column_view_get_sorter (GTK_COLUMN_VIEW (priv->prop_tree))));
selection_model = GTK_SELECTION_MODEL (gtk_no_selection_new (G_LIST_MODEL (sort_model)));
gtk_column_view_set_model (GTK_COLUMN_VIEW (priv->prop_tree), selection_model);
g_object_unref (selection_model);
column = g_list_model_get_item (gtk_column_view_get_columns (GTK_COLUMN_VIEW (priv->prop_tree)), 0);
@ -594,15 +713,13 @@ gtk_inspector_css_node_tree_set_object (GtkInspectorCssNodeTree *cnt,
{
GtkWidget *stack;
GtkStackPage *page;
GtkInspectorCssNodeTreePrivate *priv;
GtkCssNode *node, *root;
GtkTreePath *path;
GtkTreeIter iter;
GtkCssNode *root;
GList *nodes = NULL;
GList *l;
int i;
g_return_if_fail (GTK_INSPECTOR_IS_CSS_NODE_TREE (cnt));
priv = cnt->priv;
stack = gtk_widget_get_parent (GTK_WIDGET (cnt));
page = gtk_stack_get_page (GTK_STACK (stack), GTK_WIDGET (cnt));
@ -614,20 +731,37 @@ gtk_inspector_css_node_tree_set_object (GtkInspectorCssNodeTree *cnt,
g_object_set (page, "visible", TRUE, NULL);
root = node = gtk_widget_get_css_node (GTK_WIDGET (object));
root = gtk_widget_get_css_node (GTK_WIDGET (object));
nodes = g_list_prepend (nodes, root);
while (gtk_css_node_get_parent (root))
root = gtk_css_node_get_parent (root);
{
root = gtk_css_node_get_parent (root);
nodes = g_list_prepend (nodes, root);
}
gtk_tree_model_css_node_set_root_node (GTK_TREE_MODEL_CSS_NODE (priv->node_model), root);
g_list_store_remove_all (cnt->priv->root_model);
g_list_store_append (cnt->priv->root_model, root);
gtk_tree_model_css_node_get_iter_from_node (GTK_TREE_MODEL_CSS_NODE (priv->node_model), &iter, node);
path = gtk_tree_model_get_path (priv->node_model, &iter);
i = 0;
for (l = nodes; l; l = l->next)
{
GtkCssNode *node = l->data;
gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->node_tree), path);
gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->node_tree), path, NULL, FALSE);
gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->node_tree), path, NULL, TRUE, 0.5, 0.0);
for (; i < g_list_model_get_n_items (G_LIST_MODEL (cnt->priv->node_model)); i++)
{
GtkTreeListRow *row = g_list_model_get_item (G_LIST_MODEL (cnt->priv->node_model), i);
g_object_unref (row);
if (gtk_tree_list_row_get_item (row) == node)
{
gtk_tree_list_row_set_expanded (row, TRUE);
break;
}
}
}
gtk_tree_path_free (path);
gtk_single_selection_set_selected (cnt->priv->selection_model, i);
g_list_free (nodes);
}
static void
@ -698,10 +832,9 @@ gtk_inspector_css_node_tree_set_node (GtkInspectorCssNodeTree *cnt,
gtk_inspector_css_node_tree_unset_node (cnt);
priv->node = node;
if (node)
{
g_signal_connect (node, "style-changed", G_CALLBACK (gtk_inspector_css_node_tree_update_style_cb), cnt);
}
g_signal_connect (node, "style-changed", G_CALLBACK (gtk_inspector_css_node_tree_update_style_cb), cnt);
g_object_notify_by_pspec (G_OBJECT (cnt), properties[PROP_NODE]);
}

View File

@ -11,66 +11,34 @@
<property name="vexpand">1</property>
<property name="min-content-height">100</property>
<child>
<object class="GtkTreeView" id="node_tree">
<property name="search-column">0</property>
<property name="enable-search">0</property>
<property name="enable-grid-lines">vertical</property>
<signal name="row-activated" handler="row_activated"/>
<child internal-child="selection">
<object class="GtkTreeSelection">
<signal name="changed" handler="selection_changed"/>
</object>
</child>
<object class="GtkColumnView" id="node_tree">
<style>
<class name="data-table"/>
<class name="list"/>
</style>
<child>
<object class="GtkTreeViewColumn" id="node_name_column">
<object class="GtkColumnViewColumn">
<property name="resizable">1</property>
<property name="title" translatable="yes">Name</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">0</attribute>
<attribute name="sensitive">1</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="node_id_column">
<object class="GtkColumnViewColumn">
<property name="resizable">1</property>
<property name="title" translatable="yes">ID</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">3</attribute>
<attribute name="sensitive">1</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="node_classes_column">
<object class="GtkColumnViewColumn">
<property name="resizable">1</property>
<property name="title" translatable="yes">Style Classes</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">2</attribute>
<attribute name="sensitive">1</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="node_state_column">
<object class="GtkColumnViewColumn">
<property name="resizable">1</property>
<property name="expand">1</property>
<property name="title" translatable="yes">State</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">4</attribute>
<attribute name="sensitive">1</attribute>
</attributes>
</child>
</object>
</child>
</object>

View File

@ -14,7 +14,6 @@ inspector_sources = files(
'general.c',
'graphdata.c',
'graphrenderer.c',
'gtktreemodelcssnode.c',
'gtkdataviewer.c',
'highlightoverlay.c',
'init.c',