Package: gio

Class g-menu-model

Superclasses

g-object, common-lisp:standard-object, common-lisp:t

Documented Subclasses

Direct Slots

None

Details

The g-menu-model class represents the contents of a menu - an ordered list of menu items. The items are associated with actions, which can be activated through them. Items can be grouped in sections, and may have submenus associated with them. Both items and sections usually have some representation data, such as labels or icons. The type of the associated action, i.e. whether it is stateful, and what kind of state it has, can influence the representation of the item.

The conceptual model of menus in a g-menu-model object is hierarchical: sections and submenus are again represented by g-menu-model objects. Menus themselves do not define their own roles. Rather, the role of a particular g-menu-model object is defined by the item that references it, or, in the case of the 'root' menu, is defined by the context in which it is used.

As an example, consider the visible portions of this menu.



There are 8 "menus" visible in the screenshot: one menubar, two submenus and 5 sections:
  • the toplevel menubar, containing 4 items
  • the View submenu, containing 3 sections
  • the first section of the View submenu, containing 2 items
  • the second section of the View submenu, containing 1 item
  • the final section of the View submenu, containing 1 item
  • the Highlight Mode submenu, containing 2 sections
  • the Sources section, containing 2 items
  • the Markup section, containing 2 items
The example illustrates the conceptual connection between these 8 menus. Each large block in the figure represents a menu and the smaller blocks within the large block represent items in that menu. Some items contain references to other menus.

A menu example



Notice that the separators visible in the example appear nowhere in the menu model. This is because separators are not explicitly represented in the menu model. Instead, a separator is inserted between any two non-empty sections of a menu. Section items can have labels just like any other item. In that case, a display system may show a section header instead of a separator.

The motivation for this abstract model of application controls is that modern user interfaces tend to make these controls available outside the application. Examples include global menus, jumplists, dash boards, etc. To support such uses, it is necessary to 'export' information about actions and their representation in menus, which is exactly what the g-action-group exporter and the g-menu-model exporter do for g-action-group and g-menu-model objects. The client-side counterparts to make use of the exported information are GDBusActionGroup and GDBusMenuModel.

The API of the g-menu-model class is very generic, with iterators for the attributes and links of an item, see the functions g-menu-model-iterate-item-attributes and g-menu-model-iterate-item-links. The 'standard' attributes and link types have names: "label", "action", "target", "section", and "submenu".

Items in a g-menu-model object represent active controls if they refer to an action that can get activated when the user interacts with the menu item. The reference to the action is encoded by the string ID in the "action" attribute. An action ID uniquely identifies an action in an action group. Which action group(s) provide actions depends on the context in which the menu model is used. E.g. when the model is exported as the application menu of a gtk-application instance, actions can be application-wide or window specific, and thus come from two different action groups. By convention, the application-wide actions have names that start with "app.", while the names of window specific actions start with "win.".

While a wide variety of stateful actions is possible, the following is the minimum that is expected to be supported by all users of exported menu information:
  • an action with no parameter type and no state
  • an action with no parameter type and boolean state
  • an action with string parameter type and string state
Stateless.
A stateless action typically corresponds to an ordinary menu item. Selecting such a menu item will activate the action, with no parameter.

Boolean State.
An action with a boolean state will most typically be used with a "toggle" or "switch" menu item. The state can be set directly, but activating the action, with no parameter, results in the state being toggled. Selecting a toggle menu item will activate the action. The menu item should be rendered as "checked" when the state is true.

String Parameter and State.
Actions with string parameters and state will most typically be used to represent an enumerated choice over the items available for a group of radio menu items. Activating the action with a string parameter is equivalent to setting that parameter as the state. Radio menu items, in addition to being associated with the action, will have a target value. Selecting that menu item will result in activation of the action with the target value as the parameter. The menu item should be rendered as "selected" when the state of the action is equal to the target value of the menu item.

Signal Details

The "items-changed" signal
 lambda (model position removed added)   :run-last      
Emitted when a change has occured to the menu. The only changes that can occur to a menu is that items are removed or added. Items may not change, except by being removed and added back in the same location. The signal is capable of describing both of those changes at the same time.

The signal means that starting at the index position, removed items were removed and added items were added in their place. If removed is zero then only items were added. If added is zero then only items were removed.

As an example, if the menu contains items a, b, c, d, in that order, and the signal (2, 1, 3) occurs then the new composition of the menu will be a, b, _, _, _, d, with each _ representing some new item.

Signal handlers may query the model, particularly the added items, and expect to see the results of the modification that is being reported. The signal is emitted after the modification.
model
The g-menu-model object that is changing.
position
An integer with the position of the change.
removed
An integer with the number of items removed.
added
An integer with the number of items added.
 

Inherited Slot Access Functions

See also

2021-8-16