I have a context-menu in my application and am trying to add a few entries with check boxes and radio buttons. I am defining the menu using a .ui file.
The menu is defined using a <menu/> element.
Can this be done? If so how? I was unable to find any information about it online.
Yes, it’s possible to define menu items bound to stateful and parametrised actions in UI definition files.
Check boxes are represented by stateful actions; you need to create your own stateful action instance and add it to the action map object—typically, the window or the application. You also need to update the action’s state whenever the action is activated. In Rust, this would look like:
self.add_action_entries([gio::ActionEntry::builder("toggle-action")
.state(initial_state.to_variant())
.activate(|this: &Application, action, _| {
let state = action.state().unwrap();
let action_state: bool = state.get().unwrap();
let new_state = !action_state; // toggle
action.set_state(new_state.to_variant());
// do something with the new state
})
.build()]);
In the UI definition file, you can now use the action:
I would love to, not sure how to go about it though.
In this particular case the issue was that it wasn’t clear to me, even after reading through the page on actions, that they influence how a menu items, defined by a model, are rendered. Ofc this might just be my personal biases at work here, or perhaps I didn’t pay enough attention when reading through the documentation.
Should I just mention all of this in a GLib issue and go from there?
Yes please! In particular it would be interesting to know which high level concepts weren’t clear, where you looked for them, and some ideas of what documentation you would have found helpful (and where).
The actual first place to look at would be the Actions page on the developers documentation website. Adding a proper description of stateful and parametrised actions, as well as code examples, would be great. The page has a link to the Git repository, as well as a “edit in the web browser” link—you’ll need an account on gitlab.gnome.org, and you’ll need to add an SSH key to it before you can edit the content directly from the web UI.
The GNOME developer docs are where we put anything that isn’t an API reference.
We probably want to put a link on the docs.gtk.org landing page, so that people are aware. Ideally, using a search engine should lead you in the right place already, but the Google-juice of the whole GNOME docs is kind of low.