Is the value always the same for a given menu item?
If yes, a normal non-stateful SimpleAction will do the trick.
Stateful actions is intended for cases where activate signal is not enough but you also want to track changes of a value (i.e. the action “state”).
In menus, it’s typically a checkbox, which can be shown checked/unchecked, depending on the action’s state.
No, the value is different,
in fact, that’s “Append new tab” action for browser app
It used in global menu (with default values tuple) but also could be activated somewhere from content controller (e.g. on middle click on link, or as the additional context menu for tab, where new tab position depends of current position (stored in state))
I want to apply it values once, so can just use Variant, not state, but with Variant it does not work in global menu, because it model does not implement Variant but State only.
So I manage it state manually - set and reset every time
def on_newtab(action, param):
if param is not None:
url = param.unpack()
open_tab(url)
# Create simple action, with string parameter
action = Gio.SimpleAction.new('newtab', GLib.VariantType('s'))
action.connect('activate', on_newtab)
# Add menu item, with "about:blank" as URL
menu.append(_("New Tab"), "notebook.newtab('about:blank')")
This way, you can either activate the action manually with widget.activate_action('mygroup.newtab', "https://gnome.org/")
or with the menu item, using the default “about:blank” URL.