Which activate_action() shall be chosen in GtkApplicationWindow?

GtkApplicationWindow inherits from GtkWidget and implements GActionGroup.
Both GtkWidget and GActionGroup have activate_action() functions in gtk4.

When GtkApplicationWindow is wrapped in C++ code in Gtk::ApplicationWindow
in gtkmm4, Gtk::ApplicationWindow inherits two activate_action() functions.
A plain call to activate_action() is ambiguous. The user (application programmer)
must choose Gio::ActionGroup::activate_action() or Gtk::Widget::activate_action().
Is either one (for instance Gtk::Widget::activate_action()) always acceptable?
Does the right choice depend on how the action was added?

The question started in https://gitlab.gnome.org/GNOME/gtkmm/-/issues/122.
I’m no longer sure my comment there is correct.

The gtk_widget_activate_action() function is a C convenience wrapper around gtk_widget_activate_action_variant() that does the g_variant_new() dance for you.

The gtk_widget_activate_action_variant() function will either call g_action_group_activate_action() on the widget, or will traverse the widget hierarchy until it hits a top level, and call g_action_group_activate_action() there. So, up to a point, GtkWidget.activate_action_variant() is mostly a wrapper around the interface method GActionGroup.activate_action().

The overloading of the method name is kind of unfortunate, but in general, both methods will end up calling the same implementation.

I made a test with an action, added by add_action(“paste”) in a Gtk::ApplicationWindow.
It creates a GSimpleAction and calls g_action_map_add_action().

The action can be activated with both versions of activate_action(), but they don’t behave
identically. To activate the action, you must call either Gio::ActionGroup::activate_action("paste")
or Gtk::Widget::activate_action("win.paste").

Yes, that’s expected: activate_action() on the action group only activates the action on that precise action group; this means that the action must be unprefixed when passed to g_action_group_activate_action().

The win prefix identifies an action inside the win action group, which is created automatically by an application window similarly to how actions added to a GApplication instance have an app prefix.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.