Pass arguments to action by gtkmm activate_action

I have following code that defines an action with boolean argument expected in lambda function:

// window container
add_action(
    "update",
    [this](const bool & arg1 = true)
    {
        // ..
    }
);

But it does not work, when trying to pass arguments using following method:

// children widget
activate_action(
    "win.update",
    Glib::VariantBase {Glib::Variant<bool>::create(
        false
    )}
);

I cant simply pass the boolean argument as the array item,
so trying to use Glib::VariantBase and when activating, get following notice:

g_simple_action_activate: assertion 'simple->parameter_type == NULL ? parameter == NULL : (parameter != NULL && g_variant_is_of_type (parameter, simple->parameter_type))' failed

How to use add_action and activate_action with arguments properly, because I can’t find any documentation but clangd data tips only.

Found only this short instruction but it does not match my implementation:
https://gnome.pages.gitlab.gnome.org/gtkmm-documentation/sec-binding-extra-arguments.html

I think you should use either Gio::ActionMap::add_action_bool() or add_action_with_parameter()
instead of add_action(). add_action() creates an action without a parameter.
There are examples of add_action_bool() in

and examples/book/menus_and_toolbars/examplewindow.cc · master · GNOME / gtkmm-documentation · GitLab

1 Like

Well, just made it like that:

In window controller:

add_action_with_parameter(
    "open",
    Glib::VARIANT_TYPE_STRING, // or Glib::VariantType("s")
    [this](const Glib::VariantBase & parameter)
    {
        // By cast:
        // Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(parameter).get()

        // By type condition:
        // if (parameter.is_of_type(Glib::VARIANT_TYPE_STRING)) ..
    }
);

In target widget:

signal_activate_link().connect(
    [this](const Glib::ustring & URI) -> bool
    {
        // Open link URI
        activate_action(
            "win.open",
            Glib::Variant<Glib::ustring>::create(
                URI
            )
        );

        return true;
    },
    false // after
);

It’s raw fragment that works, maybe I’ll update it as looks super strange.

Now trying to pass multiple parameters using array,

add_action_with_parameter(
    "open",
    Glib::VARIANT_TYPE_ARRAY,
    // ...
GLib-CRITICAL **: 03:29:56.913: g_variant_type_is_subtype_of: assertion 'g_variant_type_check (type)' failed
GLib-CRITICAL **: 03:29:56.913: g_variant_get_type_string: assertion 'value != NULL' failed
GLib-ERROR **: 03:29:56.913: g_variant_new: expected GVariant of type '(bgav)' but received value has type '(null)'
Trace/breakpoint trap

Is it my mistake or bug?
Compiler say Glib::VARIANT_TYPE_ARRAY defined, the problem on application launch only