Class: Gtk::PopoverMenu

Inherits:
Popover
  • Object
show all
Defined in:
(unknown)

Overview

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

GtkPopoverMenu is meant to be used primarily with menu models, using [ctorGtk.PopoverMenu.new_from_model]. If you need to put other widgets such as a GtkSpinButton or a GtkSwitch into a popover, you can use [methodGtk.PopoverMenu.add_child].

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

Menu models

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 [methodGtk.Application.set_menubar].
  • "custom": a string used to match against the ID of a custom child added with [methodGtk.PopoverMenu.add_child], [methodGtk.PopoverMenuBar.add_child], 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

Menu items will also show accelerators, which are usually associated with actions via [methodGtk.Application.set_accels_for_action], [methodWidgetClass.add_binding_action] or [methodGtk.ShortcutController.add_shortcut].

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_ACCESSIBLE_ROLE_MENU role, and its items use the %GTK_ACCESSIBLE_ROLE_MENU_ITEM, %GTK_ACCESSIBLE_ROLE_MENU_ITEM_CHECKBOX or %GTK_ACCESSIBLE_ROLE_MENU_ITEM_RADIO roles, depending on the action they are connected to.

Instance Method Summary collapse

Methods inherited from Popover

#autohide, #autohide=, #autohide?, #cascade_popdown, #cascade_popdown=, #cascade_popdown?, #child, #child=, #default_widget, #default_widget=, #get_offset, #get_pointing_to, #has_arrow, #has_arrow=, #has_arrow?, #mnemonics_visible, #mnemonics_visible=, #mnemonics_visible?, #pointing_to, #pointing_to=, #pointing_to_raw, #popdown, #popup, #position, #position=, #present, #set_offset

Methods inherited from Widget

#accessible_role, #accessible_role=, #action_set_enabled, #activate, #activate_action, #activate_action_variant, #activate_default, #activate_signal, #activate_signal=, #activate_signal_from_name=, #add_binding, #add_binding_action, #add_binding_signal, #add_controller, #add_css_class, #add_events, #add_events_raw, #add_mnemonic_label, #add_shortcut, #add_tick_callback, #allocate, #allocated_baseline, #allocated_height, #allocated_width, #baseline, #bind_template_callback_full, #bind_template_child, #bind_template_child_full, #can_focus, #can_focus=, #can_focus?, #can_target, #can_target=, #can_target?, #child_focus, #child_visible, #child_visible=, #clipboard, #compute_bounds, #compute_expand, #compute_point, #compute_transform, #contains, #create_pango_context, #create_pango_layout, #css_classes, #css_classes=, #css_name, #css_name=, #cursor, #cursor=, #cursor_from_name=, default_direction, default_direction=, #direction, #direction=, #display, #dispose_template, #drag_check_threshold, #drag_dest_set, #drag_dest_set_raw, #drag_source_set, #drag_source_set_raw, #error_bell, #events, #events_raw, #first_child, #focus_child, #focus_child=, #focus_on_click, #focus_on_click=, #focus_on_click?, #focusable, #focusable=, #focusable?, #font_map, #font_map=, #font_options, #font_options=, #frame_clock, #get_allocation, #get_ancestor, #get_color, #get_preferred_size, #get_size, #get_size_request, #get_template_child, #grab_focus, #halign, #halign=, #has_css_class, #has_default, #has_default=, #has_default?, #has_focus, #has_focus=, #has_focus?, #has_tooltip, #has_tooltip=, #has_tooltip?, #has_visible_focus, have_template?, #height, #height_request, #height_request=, #hexpand, #hexpand=, #hexpand?, #hexpand_set, #hexpand_set=, #hexpand_set?, #hide, #in_destruction, #init_template, #insert_action_group, #insert_action_group_raw, #insert_after, #insert_before, #install_action, #install_property_action, #is_ancestor, #is_drawable, #is_focus, #is_sensitive, #is_visible, #keynav_failed, #last_child, #layout_manager, #layout_manager=, #layout_manager_type, #layout_manager_type=, #list_mnemonic_labels, #map, #mapped, #margin_bottom, #margin_bottom=, #margin_end, #margin_end=, #margin_start, #margin_start=, #margin_top, #margin_top=, #measure, #mnemonic_activate, #name, #name=, #native, #next_sibling, #observe_children, #observe_controllers, #opacity, #opacity=, #overflow, #overflow=, #pango_context, #parent, #parent=, #pick, #prev_sibling, #primary_clipboard, #query_action, #queue_allocate, #queue_draw, #queue_resize, #realize, #realized, #receives_default, #receives_default=, #receives_default?, #remove_controller, #remove_css_class, #remove_mnemonic_label, #remove_tick_callback, #render_icon_pixbuf, #render_icon_pixbuf_raw, #request_mode, #root, #root=, #scale_factor, #scale_factor=, #sensitive, #sensitive=, #sensitive?, #set_allocation, set_connect_func, set_connect_func_raw, #set_events, #set_events_raw, #set_size_request, #set_size_request_raw, #set_state_flags, #set_template, #set_template_raw, #settings, #should_layout, #show, #size_allocate, #snapshot_child, #state_flags, #style_context, #style_context_raw, #style_get_property, #style_get_property_raw, #template=, template_children, #template_from_resource=, #template_scope=, #tooltip_markup, #tooltip_markup=, #tooltip_text, #tooltip_text=, #translate_coordinates, #translate_coordinates_raw, #trigger_tooltip_query, #unmap, #unparent, #unrealize, #unset_state_flags, #valign, #valign=, #vexpand, #vexpand=, #vexpand?, #vexpand_set, #vexpand_set=, #vexpand_set?, #visible, #visible=, #visible?, #width, #width_request, #width_request=

Constructor Details

#initialize(model, flags) ⇒ Gtk::Widget

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 [methodGtk.Widget.insert_action_group] on the parent widget or on any of its parent widgets.

Parameters:

  • model (Gio::MenuModel)

    a GMenuModel

  • flags (Gtk::PopoverMenuFlags)

    flags that affect how the menu is created

Instance Method Details

#add_child(child, id) ⇒ Boolean

Adds a custom widget to a generated menu.

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

Parameters:

  • child (Gtk::Widget)

    the GtkWidget to add

  • id (String)

    the ID to insert child at

Returns:

  • (Boolean)

    true if id was found and the widget added

#flagsGtk::PopoverMenuFlags

The flags that popover 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.

Returns:

#flags=(flags) ⇒ Gtk::PopoverMenuFlags

The flags that popover 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:

Returns:

The model from which the menu is made.

Returns:

  • (Gio::MenuModel)

    menu-model

The model from which the menu is made.

Parameters:

  • menu_model (Gio::MenuModel)

Returns:

  • (Gio::MenuModel)

    menu-model

  • (Gio::MenuModel)

    menu-model

#remove_child(child) ⇒ Boolean

Removes a widget that has previously been added with [methodGtk.PopoverMenu.add_child()]

Parameters:

Returns:

  • (Boolean)

    true if the widget was removed

#visible_submenuString

The name of the visible submenu.

Returns:

  • (String)

    visible-submenu

#visible_submenu=(visible_submenu) ⇒ String

The name of the visible submenu.

Parameters:

  • visible_submenu (String)

Returns:

  • (String)

    visible-submenu

  • (String)

    visible-submenu