:right-sidebar: True
Popover
===================================================================
.. currentmodule:: gi.repository.Gtk
.. class:: Popover(**properties: ~typing.Any)
:no-contents-entry:
Superclasses: :class:`~gi.repository.Gtk.Widget`, :class:`~gi.repository.GObject.InitiallyUnowned`, :class:`~gi.repository.GObject.Object`
Subclasses: :class:`~gi.repository.Gtk.EmojiChooser`, :class:`~gi.repository.Gtk.PopoverMenu`
Implemented Interfaces: :class:`~gi.repository.Gtk.Accessible`, :class:`~gi.repository.Gtk.Buildable`, :class:`~gi.repository.Gtk.ConstraintTarget`, :class:`~gi.repository.Gtk.Native`, :class:`~gi.repository.Gtk.ShortcutManager`
``GtkPopover`` is a bubble-like context popup.
.. image:: https://docs.gtk.org/gtk4/popover.png
It is primarily meant to provide context-dependent information
or options. Popovers are attached to a parent widget. By default,
they point to the whole widget area, although this behavior can be
changed with :obj:`~gi.repository.Gtk.Popover.set_pointing_to`.
The position of a popover relative to the widget it is attached to
can also be changed with :obj:`~gi.repository.Gtk.Popover.set_position`
By default, ``GtkPopover`` performs a grab, in order to ensure input
events get redirected to it while it is shown, and also so the popover
is dismissed in the expected situations (clicks outside the popover,
or the Escape key being pressed). If no such modal behavior is desired
on a popover, :obj:`~gi.repository.Gtk.Popover.set_autohide` may be called on it to
tweak its behavior.
GtkPopover as menu replacement
------------------------------
``GtkPopover`` is often used to replace menus. The best way to do this
is to use the :obj:`~gi.repository.Gtk.PopoverMenu` subclass which supports being
populated from a ``GMenuModel`` with :obj:`~gi.repository.Gtk.PopoverMenu.new_from_model`.
.. code-block:: xml
:dedent:
horizontal-buttons
-
Cut
app.cut
edit-cut-symbolic
-
Copy
app.copy
edit-copy-symbolic
-
Paste
app.paste
edit-paste-symbolic
Shortcuts and Gestures
----------------------
``GtkPopover`` supports the following keyboard shortcuts:
- :kbd:`Escape` closes the popover.
- :kbd:`Alt` makes the mnemonics visible.
The following signals have default keybindings:
- :obj:`~gi.repository.Gtk.Popover.signals.activate_default`
CSS nodes
---------
.. code-block::
:dedent:
popover.background[.menu]
├── arrow
╰── contents
╰──
``GtkPopover`` has a main node with name ``popover``, an arrow with name ``arrow``,
and another node for the content named ``contents``. The ``popover`` node always
gets the ``.background`` style class. It also gets the ``.menu`` style class
if the popover is menu-like, e.g. is a :obj:`~gi.repository.Gtk.PopoverMenu`.
Particular uses of ``GtkPopover``, such as touch selection popups or
magnifiers in ``GtkEntry`` or ``GtkTextView`` get style classes like
``.touch-selection`` or ``.magnifier`` to differentiate from plain popovers.
When styling a popover directly, the ``popover`` node should usually
not have any background. The visible part of the popover can have
a shadow. To specify it in CSS, set the box-shadow of the ``contents`` node.
Note that, in order to accomplish appropriate arrow visuals, ``GtkPopover``
uses custom drawing for the ``arrow`` node. This makes it possible for the
arrow to change its shape dynamically, but it also limits the possibilities
of styling it using CSS. In particular, the ``arrow`` gets drawn over the
``content`` node's border and shadow, so they look like one shape, which
means that the border width of the ``content`` node and the ``arrow`` node should
be the same. The arrow also does not support any border shape other than
solid, no border-radius, only one border width (border-bottom-width is
used) and no box-shadow.
Constructors
------------
.. rst-class:: interim-class
.. class:: Popover
:no-index:
.. classmethod:: new() -> ~gi.repository.Gtk.Widget
Creates a new ``GtkPopover``.
Methods
-------
.. rst-class:: interim-class
.. class:: Popover
:no-index:
.. method:: get_autohide() -> bool
Returns whether the popover is modal.
See :obj:`~gi.repository.Gtk.Popover.set_autohide` for the
implications of this.
.. method:: get_cascade_popdown() -> bool
Returns whether the popover will close after a modal child is closed.
.. method:: get_child() -> ~gi.repository.Gtk.Widget | None
Gets the child widget of ``popover``.
.. method:: get_has_arrow() -> bool
Gets whether this popover is showing an arrow
pointing at the widget that it is relative to.
.. method:: get_mnemonics_visible() -> bool
Gets whether mnemonics are visible.
.. method:: get_offset() -> tuple[int, int]
Gets the offset previous set with :obj:`~gi.repository.Gtk.Popover.set_offset()`.
.. method:: get_pointing_to() -> tuple[bool, ~gi.repository.Gdk.Rectangle]
Gets the rectangle that the popover points to.
If a rectangle to point to has been set, this function will
return :const:`True` and fill in ``rect`` with such rectangle, otherwise
it will return :const:`False` and fill in ``rect`` with the parent
widget coordinates.
.. method:: get_position() -> ~gi.repository.Gtk.PositionType
Returns the preferred position of ``popover``.
.. method:: popdown() -> None
Pops ``popover`` down.
This may have the side-effect of closing a parent popover
as well. See :obj:`~gi.repository.Gtk.Popover.props.cascade_popdown`.
.. method:: popup() -> None
Pops ``popover`` up.
.. method:: present() -> None
Allocate a size for the ``GtkPopover``.
This function needs to be called in size-allocate by widgets
who have a ``GtkPopover`` as child. When using a layout manager,
this is happening automatically.
To make a popover appear on screen, use :obj:`~gi.repository.Gtk.Popover.popup`.
.. method:: set_autohide(autohide: bool) -> None
Sets whether ``popover`` is modal.
A modal popover will grab the keyboard focus on it when being
displayed. Focus will wrap around within the popover. Clicking
outside the popover area or pressing Esc will dismiss the popover.
Called this function on an already showing popup with a new
autohide value different from the current one, will cause the
popup to be hidden.
:param autohide: :const:`True` to dismiss the popover on outside clicks
.. method:: set_cascade_popdown(cascade_popdown: bool) -> None
If ``cascade_popdown`` is :const:`True`, the popover will be
closed when a child modal popover is closed.
If :const:`False`, ``popover`` will stay visible.
:param cascade_popdown: :const:`True` if the popover should follow a child closing
.. method:: set_child(child: ~gi.repository.Gtk.Widget | None = None) -> None
Sets the child widget of ``popover``.
:param child: the child widget
.. method:: set_default_widget(widget: ~gi.repository.Gtk.Widget | None = None) -> None
Sets the default widget of a ``GtkPopover``.
The default widget is the widget that’s activated when the user
presses Enter in a dialog (for example). This function sets or
unsets the default widget for a ``GtkPopover``.
:param widget: a child widget of ``popover`` to set as
the default, or :const:`None` to unset the default widget for the popover
.. method:: set_has_arrow(has_arrow: bool) -> None
Sets whether this popover should draw an arrow
pointing at the widget it is relative to.
:param has_arrow: :const:`True` to draw an arrow
.. method:: set_mnemonics_visible(mnemonics_visible: bool) -> None
Sets whether mnemonics should be visible.
:param mnemonics_visible: the new value
.. method:: set_offset(x_offset: int, y_offset: int) -> None
Sets the offset to use when calculating the position
of the popover.
These values are used when preparing the :obj:`~gi.repository.Gdk.PopupLayout`
for positioning the popover.
:param x_offset: the x offset to adjust the position by
:param y_offset: the y offset to adjust the position by
.. method:: set_pointing_to(rect: ~gi.repository.Gdk.Rectangle | None = None) -> None
Sets the rectangle that ``popover`` points to.
This is in the coordinate space of the ``popover`` parent.
:param rect: rectangle to point to
.. method:: set_position(position: ~gi.repository.Gtk.PositionType) -> None
Sets the preferred position for ``popover`` to appear.
If the ``popover`` is currently visible, it will be immediately
updated.
This preference will be respected where possible, although
on lack of space (eg. if close to the window edges), the
``GtkPopover`` may choose to appear on the opposite side.
:param position: preferred popover position
Properties
----------
.. rst-class:: interim-class
.. class:: Popover
:no-index:
.. attribute:: props.autohide
:type: bool
The type of the None singleton.
.. attribute:: props.cascade_popdown
:type: bool
The type of the None singleton.
.. attribute:: props.child
:type: ~gi.repository.Gtk.Widget
The type of the None singleton.
.. attribute:: props.default_widget
:type: ~gi.repository.Gtk.Widget
The type of the None singleton.
.. attribute:: props.has_arrow
:type: bool
The type of the None singleton.
.. attribute:: props.mnemonics_visible
:type: bool
The type of the None singleton.
.. attribute:: props.pointing_to
:type: ~gi.repository.Gdk.Rectangle
The type of the None singleton.
.. attribute:: props.position
:type: ~gi.repository.Gtk.PositionType
The type of the None singleton.
Signals
-------
.. rst-class:: interim-class
.. class:: Popover.signals
:no-index:
.. method:: activate_default() -> None
The type of the None singleton.
.. method:: closed() -> None
The type of the None singleton.
Virtual Methods
---------------
.. rst-class:: interim-class
.. class:: Popover
:no-index:
.. method:: do_activate_default() -> None
.. method:: do_closed() -> None
The type of the None singleton.
Fields
------
.. rst-class:: interim-class
.. class:: Popover
:no-index:
.. attribute:: parent