PopoverMenu

gtk.popover_menu.PopoverMenu is a subclass of gtk.popover.Popover that implements menu behavior.

An example GtkPopoverMenu

gtk.popover_menu.PopoverMenu treats its children like menus and allows switching between them. It can open submenus as traditional, nested submenus, or in a more touch-friendly sliding fashion. The property gtk.popover_menu.PopoverMenu.PopoverMenuFlags controls this appearance.

gtk.popover_menu.PopoverMenu is meant to be used primarily with menu models, using gtk.popover_menu.PopoverMenu.newFromModel. If you need to put other widgets such as a gtk.spin_button.SpinButton or a gtk.switch_.Switch into a popover, you can use gtk.popover_menu.PopoverMenu.addChild.

For more dialog-like behavior, use a plain gtk.popover.Popover.

The XML format understood by gtk.builder.Builder for gio.menu_model.MenuModel consists of a toplevel <menu> element, which contains one or more <item> elements. Each <item> element contains <attribute> and <link> elements with a mandatory name attribute. <link> elements have the same content model as <menu>. Instead of <link name="submenu"> or <link name="section">, you can use <submenu> or <section> elements.

<menu id='app-menu'>
  <section>
    <item>
      <attribute name='label' translatable='yes'>_New Window</attribute>
      <attribute name='action'>app.new</attribute>
    </item>
    <item>
      <attribute name='label' translatable='yes'>_About Sunny</attribute>
      <attribute name='action'>app.about</attribute>
    </item>
    <item>
      <attribute name='label' translatable='yes'>_Quit</attribute>
      <attribute name='action'>app.quit</attribute>
    </item>
  </section>
</menu>

Attribute values can be translated using gettext, like other gtk.builder.Builder content. <attribute> elements can be marked for translation with a translatable="yes" attribute. It is also possible to specify message context and translator comments, using the context and comments attributes. To make use of this, the gtk.builder.Builder must have been given the gettext domain to use.

The following attributes are used when constructing menu items:

  • "label": a user-visible string to display
  • "use-markup": whether the text in the menu item includes Pango markup
  • "action": the prefixed name of the action to trigger
  • "target": the parameter to use when activating the action
  • "icon" and "verb-icon": names of icons that may be displayed
  • "submenu-action": name of an action that may be used to track whether a submenu is open
  • "hidden-when": a string used to determine when the item will be hidden. Possible values include "action-disabled", "action-missing", "macos-menubar". This is mainly useful for exported menus, see gtk.application.Application.setMenubar.
  • "custom": a string used to match against the ID of a custom child added with gtk.popover_menu.PopoverMenu.addChild, gtk.popover_menu_bar.PopoverMenuBar.addChild, or in the ui file with <child type="ID">.

The following attributes are used when constructing sections:

  • "label": a user-visible string to use as section heading
  • "display-hint": a string used to determine special formatting for the section. Possible values include "horizontal-buttons", "circular-buttons" and "inline-buttons". They all indicate that section should be displayed as a horizontal row of buttons.
  • "text-direction": a string used to determine the gtk.types.TextDirection to use when "display-hint" is set to "horizontal-buttons". Possible values include "rtl", "ltr", and "none".

The following attributes are used when constructing submenus:

  • "label": a user-visible string to display
  • "icon": icon name to display

Menu items will also show accelerators, which are usually associated with actions via gtk.application.Application.setAccelsForAction, gtk.widget_class.WidgetClass.addBindingAction or gtk.shortcut_controller.ShortcutController.addShortcut.

CSS Nodes

gtk.popover_menu.PopoverMenu is just a subclass of gtk.popover.Popover that adds custom content to it, therefore it has the same CSS nodes. It is one of the cases that add a .menu style class to the main popover node.

Menu items have nodes with name button and class .model. If a section display-hint is set, the section gets a node box with class horizontal plus a class with the same text as the display hint. Note that said box may not be the direct ancestor of the item buttons. Thus, for example, to style items in an inline-buttons section, select .inline-buttons button.model. Other things that may be of interest to style in menus include label nodes.

Accessibility

gtk.popover_menu.PopoverMenu uses the gtk.types.AccessibleRole.Menu role, and its items use the gtk.types.AccessibleRole.MenuItem, gtk.types.AccessibleRole.MenuItemCheckbox or gtk.types.AccessibleRole.MenuItemRadio roles, depending on the action they are connected to.

Members

Functions

addChild
bool addChild(gtk.widget.Widget child, string id)

Adds a custom widget to a generated menu.

getFlags
gtk.types.PopoverMenuFlags getFlags()

Returns the flags that popover uses to create/display a menu from its model.

getMenuModel
gio.menu_model.MenuModel getMenuModel()

Returns the menu model used to populate the popover.

removeChild
bool removeChild(gtk.widget.Widget child)

Removes a widget that has previously been added with gtk.popover_menu.PopoverMenu.addChild

setFlags
void setFlags(gtk.types.PopoverMenuFlags flags)

Sets the flags that popover uses to create/display a menu from its model.

setMenuModel
void setMenuModel(gio.menu_model.MenuModel model)

Sets a new menu model on popover.

Static functions

newFromModel
gtk.popover_menu.PopoverMenu newFromModel(gio.menu_model.MenuModel model)

Creates a gtk.popover_menu.PopoverMenu and populates it according to model.

newFromModelFull
gtk.popover_menu.PopoverMenu newFromModelFull(gio.menu_model.MenuModel model, gtk.types.PopoverMenuFlags flags)

Creates a gtk.popover_menu.PopoverMenu and populates it according to model.

Inherited Members

From Popover

getAutohide
bool getAutohide()

Returns whether the popover is modal.

getCascadePopdown
bool getCascadePopdown()

Returns whether the popover will close after a modal child is closed.

getChild
gtk.widget.Widget getChild()

Gets the child widget of popover.

getHasArrow
bool getHasArrow()

Gets whether this popover is showing an arrow pointing at the widget that it is relative to.

getMnemonicsVisible
bool getMnemonicsVisible()

Gets whether mnemonics are visible.

getOffset
void getOffset(int xOffset, int yOffset)

Gets the offset previous set with gtk.popover.Popover.setOffset.

getPointingTo
bool getPointingTo(gdk.rectangle.Rectangle rect)

Gets the rectangle that the popover points to.

getPosition
gtk.types.PositionType getPosition()

Returns the preferred position of popover.

popdown
void popdown()

Pops popover down.

popup
void popup()

Pops popover up.

present
void present()

Allocate a size for the gtk.popover.Popover.

setAutohide
void setAutohide(bool autohide)

Sets whether popover is modal.

setCascadePopdown
void setCascadePopdown(bool cascadePopdown)

If cascade_popdown is true, the popover will be closed when a child modal popover is closed.

setChild
void setChild(gtk.widget.Widget child)

Sets the child widget of popover.

setDefaultWidget
void setDefaultWidget(gtk.widget.Widget widget)

Sets the default widget of a gtk.popover.Popover.

setHasArrow
void setHasArrow(bool hasArrow)

Sets whether this popover should draw an arrow pointing at the widget it is relative to.

setMnemonicsVisible
void setMnemonicsVisible(bool mnemonicsVisible)

Sets whether mnemonics should be visible.

setOffset
void setOffset(int xOffset, int yOffset)

Sets the offset to use when calculating the position of the popover.

setPointingTo
void setPointingTo(gdk.rectangle.Rectangle rect)

Sets the rectangle that popover points to.

setPosition
void setPosition(gtk.types.PositionType position)

Sets the preferred position for popover to appear.

connectActivateDefault
ulong connectActivateDefault(T callback, Flag!"After" after)

Connect to ActivateDefault signal.

connectClosed
ulong connectClosed(T callback, Flag!"After" after)

Connect to Closed signal.