inspector: Redo the action page

Instead of iterating action groups manually,
just show what the action muxer provides. This
gives an accurate view of what actions are
available in a widgets context, and saves us
the trouble of juggling prefixes manually.
This commit is contained in:
Matthias Clasen 2019-06-15 21:54:45 +00:00
parent 3b565d7f6e
commit 00c828e752
4 changed files with 54 additions and 110 deletions

View File

@ -29,7 +29,6 @@
struct _GtkInspectorActionEditorPrivate
{
GActionGroup *group;
gchar *prefix;
gchar *name;
gboolean enabled;
const GVariantType *parameter_type;
@ -319,7 +318,6 @@ finalize (GObject *object)
{
GtkInspectorActionEditor *r = GTK_INSPECTOR_ACTION_EDITOR (object);
g_free (r->priv->prefix);
g_free (r->priv->name);
g_object_unref (r->priv->sg);
if (r->priv->state_type)
@ -344,10 +342,6 @@ get_property (GObject *object,
g_value_set_object (value, r->priv->group);
break;
case PROP_PREFIX:
g_value_set_string (value, r->priv->prefix);
break;
case PROP_NAME:
g_value_set_string (value, r->priv->name);
break;
@ -376,18 +370,13 @@ set_property (GObject *object,
r->priv->group = g_value_get_object (value);
break;
case PROP_PREFIX:
g_free (r->priv->prefix);
r->priv->prefix = g_value_dup_string (value);
break;
case PROP_NAME:
g_free (r->priv->name);
r->priv->name = g_value_dup_string (value);
break;
case PROP_SIZEGROUP:
r->priv->sg = g_value_get_object (value);
r->priv->sg = g_value_dup_object (value);
break;
default:
@ -410,10 +399,6 @@ gtk_inspector_action_editor_class_init (GtkInspectorActionEditorClass *klass)
g_param_spec_object ("group", "Action Group", "The Action Group containing the action",
G_TYPE_ACTION_GROUP, G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
g_object_class_install_property (object_class, PROP_PREFIX,
g_param_spec_string ("prefix", "Prefix", "The action name prefix",
NULL, G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
g_object_class_install_property (object_class, PROP_NAME,
g_param_spec_string ("name", "Name", "The action name",
NULL, G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
@ -424,13 +409,11 @@ gtk_inspector_action_editor_class_init (GtkInspectorActionEditorClass *klass)
GtkWidget *
gtk_inspector_action_editor_new (GActionGroup *group,
const gchar *prefix,
const gchar *name,
GtkSizeGroup *activate)
{
return g_object_new (GTK_TYPE_INSPECTOR_ACTION_EDITOR,
"group", group,
"prefix", prefix,
"name", name,
"sizegroup", activate,
NULL);

View File

@ -49,7 +49,6 @@ G_BEGIN_DECLS
GType gtk_inspector_action_editor_get_type (void);
GtkWidget *gtk_inspector_action_editor_new (GActionGroup *group,
const gchar *prefix,
const gchar *name,
GtkSizeGroup *activate);

View File

@ -26,6 +26,7 @@
#include "gtktreeview.h"
#include "gtkliststore.h"
#include "gtkwidgetprivate.h"
#include "gtkactionmuxerprivate.h"
#include "gtkpopover.h"
#include "gtklabel.h"
#include "gtkstack.h"
@ -33,26 +34,15 @@
#include "gtkstylecontext.h"
#include "gtksizegroup.h"
enum
{
COLUMN_PREFIX,
COLUMN_NAME,
COLUMN_ENABLED,
COLUMN_PARAMETER,
COLUMN_STATE,
COLUMN_GROUP
};
struct _GtkInspectorActionsPrivate
{
GtkWidget *list;
GtkSizeGroup *prefix;
GtkSizeGroup *name;
GtkSizeGroup *enabled;
GtkSizeGroup *parameter;
GtkSizeGroup *state;
GtkSizeGroup *activate;
GHashTable *groups;
GActionGroup *group;
};
G_DEFINE_TYPE_WITH_PRIVATE (GtkInspectorActions, gtk_inspector_actions, GTK_TYPE_BOX)
@ -61,17 +51,12 @@ static void
gtk_inspector_actions_init (GtkInspectorActions *sl)
{
sl->priv = gtk_inspector_actions_get_instance_private (sl);
sl->priv->groups = g_hash_table_new_full (g_direct_hash,
g_direct_equal,
NULL,
g_free);
gtk_widget_init_template (GTK_WIDGET (sl));
}
static void
add_action (GtkInspectorActions *sl,
GActionGroup *group,
const gchar *prefix,
const gchar *name)
{
gboolean enabled;
@ -81,7 +66,7 @@ add_action (GtkInspectorActions *sl,
GtkWidget *row;
GtkWidget *label;
GtkWidget *box;
char *key = g_strconcat (prefix, ".", name, NULL);
char *key = g_strdup (name);
GtkWidget *editor;
enabled = g_action_group_get_action_enabled (group, name);
@ -99,12 +84,6 @@ add_action (GtkInspectorActions *sl,
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_container_add (GTK_CONTAINER (row), box);
label = gtk_label_new (prefix);
gtk_style_context_add_class (gtk_widget_get_style_context (label), "cell");
gtk_label_set_xalign (GTK_LABEL (label), 0);
gtk_size_group_add_widget (sl->priv->prefix, label);
gtk_container_add (GTK_CONTAINER (box), label);
label = gtk_label_new (name);
gtk_style_context_add_class (gtk_widget_get_style_context (label), "cell");
gtk_label_set_xalign (GTK_LABEL (label), 0);
@ -132,7 +111,7 @@ add_action (GtkInspectorActions *sl,
gtk_container_add (GTK_CONTAINER (box), label);
g_object_set_data (G_OBJECT (row), "state", label);
editor = gtk_inspector_action_editor_new (group, prefix, name, sl->priv->activate);
editor = gtk_inspector_action_editor_new (group, name, sl->priv->activate);
gtk_style_context_add_class (gtk_widget_get_style_context (editor), "cell");
gtk_container_add (GTK_CONTAINER (box), editor);
@ -143,27 +122,23 @@ add_action (GtkInspectorActions *sl,
static GtkWidget *
find_row (GtkInspectorActions *sl,
const char *prefix,
const char *action_name)
{
GtkWidget *row = NULL;
GtkWidget *widget;
char *key = g_strconcat (prefix, ".", action_name, NULL);
for (widget = gtk_widget_get_first_child (sl->priv->list);
widget;
widget = gtk_widget_get_next_sibling (widget))
{
const char *rkey = g_object_get_data (G_OBJECT (widget), "key");
if (g_str_equal (key, rkey))
const char *key = g_object_get_data (G_OBJECT (widget), "key");
if (g_str_equal (key, action_name))
{
row = widget;
break;
}
}
g_free (key);
return row;
}
@ -172,9 +147,7 @@ action_added_cb (GActionGroup *group,
const gchar *action_name,
GtkInspectorActions *sl)
{
const gchar *prefix;
prefix = g_hash_table_lookup (sl->priv->groups, group);
add_action (sl, group, prefix, action_name);
add_action (sl, group, action_name);
}
static void
@ -182,13 +155,11 @@ action_removed_cb (GActionGroup *group,
const gchar *action_name,
GtkInspectorActions *sl)
{
const gchar *prefix;
GtkWidget *row;
prefix = g_hash_table_lookup (sl->priv->groups, group);
row = find_row (sl, prefix, action_name);
row = find_row (sl, action_name);
if (row)
gtk_container_remove (GTK_CONTAINER (sl->priv->list), row);
gtk_widget_destroy (row);
}
static void
@ -197,13 +168,10 @@ action_enabled_changed_cb (GActionGroup *group,
gboolean enabled,
GtkInspectorActions *sl)
{
const gchar *prefix;
GtkWidget *row;
GtkWidget *label;
prefix = g_hash_table_lookup (sl->priv->groups, group);
row = find_row (sl, prefix, action_name);
row = find_row (sl, action_name);
label = GTK_WIDGET (g_object_get_data (G_OBJECT (row), "enabled"));
gtk_label_set_label (GTK_LABEL (label), enabled ? "+" : "-" );
}
@ -214,14 +182,11 @@ action_state_changed_cb (GActionGroup *group,
GVariant *state,
GtkInspectorActions *sl)
{
const gchar *prefix;
gchar *state_string;
GtkWidget *row;
GtkWidget *label;
prefix = g_hash_table_lookup (sl->priv->groups, group);
row = find_row (sl, prefix, action_name);
row = find_row (sl, action_name);
if (state)
state_string = g_variant_print (state, FALSE);
else
@ -231,39 +196,56 @@ action_state_changed_cb (GActionGroup *group,
g_free (state_string);
}
static void
connect_group (GActionGroup *group,
GtkInspectorActions *sl)
{
g_signal_connect (group, "action-added", G_CALLBACK (action_added_cb), sl);
g_signal_connect (group, "action-removed", G_CALLBACK (action_removed_cb), sl);
g_signal_connect (group, "action-enabled-changed", G_CALLBACK (action_enabled_changed_cb), sl);
g_signal_connect (group, "action-state-changed", G_CALLBACK (action_state_changed_cb), sl);
}
static void
disconnect_group (GActionGroup *group,
GtkInspectorActions *sl)
{
g_signal_handlers_disconnect_by_func (group, action_added_cb, sl);
g_signal_handlers_disconnect_by_func (group, action_removed_cb, sl);
g_signal_handlers_disconnect_by_func (group, action_enabled_changed_cb, sl);
g_signal_handlers_disconnect_by_func (group, action_state_changed_cb, sl);
}
static void
add_group (GtkInspectorActions *sl,
GtkStackPage *page,
GActionGroup *group,
const gchar *prefix)
GActionGroup *group)
{
gint i;
gchar **names;
g_object_set (page, "visible", TRUE, NULL);
g_signal_connect (group, "action-added", G_CALLBACK (action_added_cb), sl);
g_signal_connect (group, "action-removed", G_CALLBACK (action_removed_cb), sl);
g_signal_connect (group, "action-enabled-changed", G_CALLBACK (action_enabled_changed_cb), sl);
g_signal_connect (group, "action-state-changed", G_CALLBACK (action_state_changed_cb), sl);
g_hash_table_insert (sl->priv->groups, group, g_strdup (prefix));
connect_group (group, sl);
names = g_action_group_list_actions (group);
for (i = 0; names[i]; i++)
add_action (sl, group, prefix, names[i]);
add_action (sl, group, names[i]);
g_strfreev (names);
g_set_object (&sl->priv->group, group);
}
static void
disconnect_group (gpointer key, gpointer value, gpointer data)
remove_group (GtkInspectorActions *sl,
GtkStackPage *page,
GActionGroup *group)
{
GActionGroup *group = key;
GtkInspectorActions *sl = data;
g_object_set (page, "visible", FALSE, NULL);
g_signal_handlers_disconnect_by_func (group, action_added_cb, sl);
g_signal_handlers_disconnect_by_func (group, action_removed_cb, sl);
g_signal_handlers_disconnect_by_func (group, action_enabled_changed_cb, sl);
g_signal_handlers_disconnect_by_func (group, action_state_changed_cb, sl);
disconnect_group (group, sl);
g_set_object (&sl->priv->group, NULL);
}
void
@ -272,32 +254,25 @@ gtk_inspector_actions_set_object (GtkInspectorActions *sl,
{
GtkWidget *stack;
GtkStackPage *page;
GtkWidget *child;
stack = gtk_widget_get_parent (GTK_WIDGET (sl));
page = gtk_stack_get_page (GTK_STACK (stack), GTK_WIDGET (sl));
g_object_set (page, "visible", FALSE, NULL);
g_hash_table_foreach (sl->priv->groups, disconnect_group, sl);
g_hash_table_remove_all (sl->priv->groups);
remove_group (sl, page, sl->priv->group);
while ((child = gtk_widget_get_first_child (sl->priv->list)))
gtk_widget_destroy (child);
if (GTK_IS_APPLICATION (object))
add_group (sl, page, G_ACTION_GROUP (object), "app");
add_group (sl, page, G_ACTION_GROUP (object));
else if (GTK_IS_WIDGET (object))
{
const gchar **prefixes;
GActionGroup *group;
gint i;
GtkActionMuxer *muxer;
prefixes = gtk_widget_list_action_prefixes (GTK_WIDGET (object));
if (prefixes)
{
for (i = 0; prefixes[i]; i++)
{
group = gtk_widget_get_action_group (GTK_WIDGET (object), prefixes[i]);
add_group (sl, page, group, prefixes[i]);
}
g_free (prefixes);
}
muxer = _gtk_widget_get_action_muxer (GTK_WIDGET (object), FALSE);
if (muxer)
add_group (sl, page, G_ACTION_GROUP (muxer));
}
}
@ -308,7 +283,6 @@ gtk_inspector_actions_class_init (GtkInspectorActionsClass *klass)
gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/inspector/actions.ui");
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorActions, list);
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorActions, prefix);
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorActions, name);
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorActions, enabled);
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorActions, parameter);

View File

@ -20,12 +20,6 @@
<style>
<class name="header"/>
</style>
<child>
<object class="GtkLabel" id="prefix_heading">
<property name="label" translatable="yes">Prefix</property>
<property name="xalign">0</property>
</object>
</child>
<child>
<object class="GtkLabel" id="name_heading">
<property name="label" translatable="yes">Name</property>
@ -73,12 +67,6 @@
</object>
</child>
</template>
<object class="GtkSizeGroup" id="prefix">
<property name="mode">horizontal</property>
<widgets>
<widget name="prefix_heading"/>
</widgets>
</object>
<object class="GtkSizeGroup" id="name">
<property name="mode">horizontal</property>
<widgets>