ContextMenu

class ContextMenu(**properties: Any)

Superclasses: Object

Represents the context menu in a WebKitWebView.

ContextMenu represents a context menu containing ContextMenuItem`<!-- -->s in a ``WebKitWebView`.

When a WebKitWebView is about to display the context menu, it emits the WebKitWebView::context-menu signal, which has the ContextMenu as an argument. You can modify it, adding new submenus that you can create with new(), adding new prepend, append() or insert(), maybe after having removed the existing ones with remove_all().

Constructors

class ContextMenu
classmethod new() ContextMenu

Creates a new ContextMenu object.

Creates a new ContextMenu object to be used as a submenu of an existing ContextMenu. The context menu of a WebKitWebView is created by the view and passed as an argument of WebKitWebView::context-menu signal. To add items to the menu use prepend(), append() or insert(). See also new_with_items() to create a ContextMenu with a list of initial items.

classmethod new_with_items(items: list[ContextMenuItem]) ContextMenu

Creates a new ContextMenu object with the given items.

Creates a new ContextMenu object to be used as a submenu of an existing ContextMenu with the given initial items. See also new()

Parameters:

items – a List of ContextMenuItem

Methods

class ContextMenu
append(item: ContextMenuItem) None

Adds item at the end of the menu.

Parameters:

item – the ContextMenuItem to add

first() ContextMenuItem

Gets the first item in the menu.

get_event() Event

Gets the Event that triggered the context menu. This function only returns a valid Event when called for a ContextMenu passed to WebKitWebView::context-menu signal; in all other cases, None is returned.

The returned Event is expected to be one of the following types: <itemizedlist> <listitem><para> a GdkEventButton of type %GDK_BUTTON_PRESS when the context menu was triggered with mouse. </para></listitem> <listitem><para> a GdkEventKey of type %GDK_KEY_PRESS if the keyboard was used to show the menu. </para></listitem> <listitem><para> a generic Event of type %GDK_NOTHING when the Widget::popup-menu signal was used to show the context menu. </para></listitem> </itemizedlist>

Added in version 2.40.

get_item_at_position(position: int) ContextMenuItem

Gets the item at the given position in the menu.

Parameters:

position – the position of the item, counting from 0

get_items() list[ContextMenuItem]

Returns the item list of menu.

get_n_items() int

Gets the length of the menu.

get_user_data() Variant

Gets the user data of menu.

This function can be used from the UI Process to get user data previously set from the Web Process with set_user_data().

Added in version 2.8.

insert(item: ContextMenuItem, position: int) None

Inserts item into the menu at the given position.

If position is negative, or is larger than the number of items in the ContextMenu, the item is added on to the end of the menu. The first position is 0.

Parameters:
  • item – the ContextMenuItem to add

  • position – the position to insert the item

last() ContextMenuItem

Gets the last item in the menu.

move_item(item: ContextMenuItem, position: int) None

Moves item to the given position in the menu.

If position is negative, or is larger than the number of items in the ContextMenu, the item is added on to the end of the menu. The first position is 0.

Parameters:
  • item – the ContextMenuItem to add

  • position – the new position to move the item

prepend(item: ContextMenuItem) None

Adds item at the beginning of the menu.

Parameters:

item – the ContextMenuItem to add

remove(item: ContextMenuItem) None

Removes item from the menu.

See also remove_all() to remove all items.

Parameters:

item – the ContextMenuItem to remove

remove_all() None

Removes all items of the menu.

set_user_data(user_data: Variant) None

Sets user data to menu.

This function can be used from a Web Process extension to set user data that can be retrieved from the UI Process using get_user_data(). If the user_data Variant is floating, it is consumed.

Added in version 2.8.

Parameters:

user_data – a Variant