Class PopoverMenu

All Implemented Interfaces:
Accessible, Buildable, ConstraintTarget, Native, ShortcutManager, Proxy

@Generated("org.javagi.JavaGI") public class PopoverMenu extends Popover implements Accessible, Buildable, ConstraintTarget, Native, ShortcutManager

A subclass of GtkPopover that implements menu behavior.

An example GtkPopoverMenu

GtkPopoverMenu 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.PopoverMenu:flags controls this appearance.

GtkPopoverMenu is meant to be used primarily with menu models, using fromModel(MenuModel). If you need to put other widgets such as a GtkSpinButton or a GtkSwitch into a popover, you can use addChild(Widget, String).

For more dialog-like behavior, use a plain GtkPopover.

The XML format understood by GtkBuilder for GMenuModel 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 GtkBuilder 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 GtkBuilder 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 Application.setMenubar(MenuModel).
  • "custom": a string used to match against the ID of a custom child added with addChild(Widget, String), PopoverMenuBar.addChild(Widget, String), 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 GtkTextDirection 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
  • "gtk-macos-special": (macOS only, ignored by others) Add special meaning to a menu in the macOS menu bar. See Using GTK on Apple macOS.

Menu items will also show accelerators, which are usually associated with actions via Application.setAccelsForAction(String, String[]), WidgetClass.addBindingAction or ShortcutController.addShortcut(Shortcut).

Shortcuts and Gestures

GtkPopoverMenu supports the following keyboard shortcuts:

  • Space activates the default widget.

CSS Nodes

GtkPopoverMenu is just a subclass of GtkPopover 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

GtkPopoverMenu uses the Gtk.AccessibleRole.menu role, and its items use the Gtk.AccessibleRole.menu_item, Gtk.AccessibleRole.checkbox or Gtk.AccessibleRole.menu_item_radio roles, depending on the action they are connected to.

  • Constructor Details

    • PopoverMenu

      public PopoverMenu(MemorySegment address)
      Create a PopoverMenu instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • PopoverMenu

      public PopoverMenu()
      Create a new PopoverMenu.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the PopoverMenu class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected PopoverMenu asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class Popover
      Returns:
      the instance as if it were its parent type
    • fromModel

      public static PopoverMenu fromModel(@Nullable MenuModel model)

      Creates a GtkPopoverMenu and populates it according to model.

      The created buttons are connected to actions found in the GtkApplicationWindow to which the popover belongs - typically by means of being attached to a widget that is contained within the GtkApplicationWindows widget hierarchy.

      Actions can also be added using Widget.insertActionGroup(String, ActionGroup) on the menus attach widget or on any of its parent widgets.

      This function creates menus with sliding submenus. See fromModelFull(MenuModel, Set) for a way to control this.

      Parameters:
      model - a GMenuModel
      Returns:
      the new GtkPopoverMenu
    • fromModelFull

      public static PopoverMenu fromModelFull(MenuModel model, Set<PopoverMenuFlags> flags)

      Creates a GtkPopoverMenu and populates it according to model.

      The created buttons are connected to actions found in the action groups that are accessible from the parent widget. This includes the GtkApplicationWindow to which the popover belongs. Actions can also be added using Widget.insertActionGroup(String, ActionGroup) on the parent widget or on any of its parent widgets.

      Parameters:
      model - a GMenuModel
      flags - flags that affect how the menu is created
      Returns:
      the new GtkPopoverMenu
    • fromModelFull

      public static PopoverMenu fromModelFull(MenuModel model, PopoverMenuFlags... flags)

      Creates a GtkPopoverMenu and populates it according to model.

      The created buttons are connected to actions found in the action groups that are accessible from the parent widget. This includes the GtkApplicationWindow to which the popover belongs. Actions can also be added using Widget.insertActionGroup(String, ActionGroup) on the parent widget or on any of its parent widgets.

      Parameters:
      model - a GMenuModel
      flags - flags that affect how the menu is created
      Returns:
      the new GtkPopoverMenu
    • addChild

      public boolean addChild(Widget child, String id)

      Adds a custom widget to a generated menu.

      For this to work, the menu model of this PopoverMenu must have an item with a custom attribute that matches id.

      Parameters:
      child - the GtkWidget to add
      id - the ID to insert child at
      Returns:
      true if id was found and the widget added
    • getFlags

      public Set<PopoverMenuFlags> getFlags()
      Returns the flags that this PopoverMenu uses to create/display a menu from its model.
      Returns:
      the GtkPopoverMenuFlags
      Since:
      4.14
    • getMenuModel

      public @Nullable MenuModel getMenuModel()
      Returns the menu model used to populate the popover.
      Returns:
      the menu model of this PopoverMenu
    • removeChild

      public boolean removeChild(Widget child)
      Removes a widget that has previously been added with addChild(Widget, String)
      Parameters:
      child - the GtkWidget to remove
      Returns:
      true if the widget was removed
    • setFlags

      public void setFlags(Set<PopoverMenuFlags> flags)

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

      If a model is set and the flags change, contents are rebuilt, so if setting properties individually, set flags before model to avoid a redundant rebuild.

      Parameters:
      flags - a set of GtkPopoverMenuFlags
      Since:
      4.14
    • setFlags

      public void setFlags(PopoverMenuFlags... flags)

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

      If a model is set and the flags change, contents are rebuilt, so if setting properties individually, set flags before model to avoid a redundant rebuild.

      Parameters:
      flags - a set of GtkPopoverMenuFlags
      Since:
      4.14
    • setMenuModel

      public void setMenuModel(@Nullable MenuModel model)

      Sets a new menu model on popover.

      The existing contents of this PopoverMenu are removed, and the this PopoverMenu is populated with new contents according to model.

      Parameters:
      model - a GMenuModel
    • builder

      public static PopoverMenu.Builder<? extends PopoverMenu.Builder> builder()
      A PopoverMenu.Builder object constructs a PopoverMenu with the specified properties. Use the various set...() methods to set properties, and finish construction with PopoverMenu.Builder.build().
      Returns:
      the builder object