class Gio::MenuModel


#GMenuModel 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 (ie whether it is stateful, and what kind of state it has) can influence the representation of the item.

The conceptual model of menus in #GMenuModel is hierarchical: sections and submenus are again represented by #GMenuModels. Menus themselves do not define their own roles. Rather, the role of a particular #GMenuModel 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:

An example menu # {#menu-example}

There are 8 "menus" visible in the screenshot: one menubar, two submenus and 5 sections:

The [example][menu-model] 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 # {#menu-model}

Notice that the separators visible in the [example][menu-example] appear nowhere in the [menu model][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 [GActionGroup exporter][gio-GActionGroup-exporter] and the [GMenuModel exporter][gio-GMenuModel-exporter] do for #GActionGroup and #GMenuModel. The client-side counterparts to make use of the exported information are #GDBusActionGroup and #GDBusMenuModel.

The API of #GMenuModel is very generic, with iterators for the attributes and links of an item, see g_menu_model_iterate_item_attributes() and g_menu_model_iterate_item_links(). The 'standard' attributes and link types have predefined names: %G_MENU_ATTRIBUTE_LABEL, %G_MENU_ATTRIBUTE_ACTION, %G_MENU_ATTRIBUTE_TARGET, %G_MENU_LINK_SECTION and %G_MENU_LINK_SUBMENU.

Items in a #GMenuModel 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 %G_MENU_ATTRIBUTE_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, 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:


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.

Direct Known Subclasses

Defined in:



Class Method Summary

Instance Method Summary

Instance methods inherited from class GObject::Object

==(other : self) ==, bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding bind_property, bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding bind_property_full, data(key : String) : Pointer(Void) | Nil data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, hash(hasher) hash, notify(property_name : String) : Nil notify, notify_by_pspec(pspec : GObject::ParamSpec) : Nil notify_by_pspec, notify_signal notify_signal, property(property_name : String, value : _) : Nil property, qdata(quark : UInt32) : Pointer(Void) | Nil qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void) | Nil) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void) | Nil steal_data, steal_qdata(quark : UInt32) : Pointer(Void) | Nil steal_qdata, thaw_notify : Nil thaw_notify, to_unsafe : Pointer(Void) to_unsafe, watch_closure(closure : GObject::Closure) : Nil watch_closure

Constructor methods inherited from class GObject::Object

cast(obj : GObject::Object) : self cast, new(pointer : Pointer(Void), transfer : GICrystal::Transfer)
, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv

Class methods inherited from class GObject::Object

cast?(obj : GObject::Object) : self | Nil cast?, compat_control(what : UInt64, data : Pointer(Void) | Nil) : UInt64 compat_control, g_type : UInt64 g_type, interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec interface_find_property, interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec) interface_list_properties

Macros inherited from class GObject::Object

previous_vfunc(*args) previous_vfunc, previous_vfunc!(*args) previous_vfunc!, signal(signature) signal

Constructor Detail

def #

Initialize a new MenuModel.

[View source]

Class Method Detail

def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.

[View source]

Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.

def hash(hasher) #
Description copied from class Reference

See Object#hash(hasher)

def is_mutable : Bool #

Queries if model is mutable.

An immutable #GMenuModel will never emit the #GMenuModel::items-changed signal. Consumers of the model may make optimisations accordingly.

[View source]
def item_attribute_value(item_index : Int32, attribute : String, expected_type : GLib::VariantType | Nil) : GLib::Variant | Nil #

Queries the item at position item_index in model for the attribute specified by attribute.

If expected_type is non-nil then it specifies the expected type of the attribute. If it is nil then any type will be accepted.

If the attribute exists and matches expected_type (or if the expected type is unspecified) then the value is returned.

If the attribute does not exist, or does not match the expected type then nil is returned.

[View source]
def item_link(item_index : Int32, link : String) : Gio::MenuModel | Nil #

Queries the item at position item_index in model for the link specified by link.

If the link exists, the linked #GMenuModel is returned. If the link does not exist, nil is returned.

[View source]
def items_changed(position : Int32, removed : Int32, added : Int32) : Nil #

Requests emission of the #GMenuModel::items-changed signal on model.

This function should never be called except by #GMenuModel subclasses. Any other calls to this function will very likely lead to a violation of the interface of the model.

The implementation should update its internal representation of the menu before emitting the signal. The implementation should further expect to receive queries about the new state of the menu (and particularly added menu items) while signal handlers are running.

The implementation must dispatch this call directly from a mainloop entry and not in response to calls -- particularly those from the #GMenuModel API. Said another way: the menu must not change while user code is running without returning to the mainloop.

[View source]
def items_changed_signal #

[View source]
def iterate_item_attributes(item_index : Int32) : Gio::MenuAttributeIter #

Creates a #GMenuAttributeIter to iterate over the attributes of the item at position item_index in model.

You must free the iterator with g_object_unref() when you are done.

[View source]
def iterate_item_links(item_index : Int32) : Gio::MenuLinkIter #

Creates a #GMenuLinkIter to iterate over the links of the item at position item_index in model.

You must free the iterator with g_object_unref() when you are done.

[View source]
def n_items : Int32 #

Query the number of items in model.

[View source]