webkit6/auto/context_menu.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// from webkit-gir-files
4// DO NOT EDIT
5
6use crate::{ContextMenuItem, ffi};
7use glib::translate::*;
8
9glib::wrapper! {
10 /// Represents the context menu in a [`WebView`][crate::WebView].
11 ///
12 /// [`ContextMenu`][crate::ContextMenu] represents a context menu containing
13 /// [`ContextMenuItem`][crate::ContextMenuItem]<!-- -->s in a [`WebView`][crate::WebView].
14 ///
15 /// When a [`WebView`][crate::WebView] is about to display the context menu, it
16 /// emits the [`context-menu`][struct@crate::WebView#context-menu] signal, which has the
17 /// [`ContextMenu`][crate::ContextMenu] as an argument. You can modify it, adding new
18 /// submenus that you can create with [`new()`][Self::new()], adding
19 /// new [`ContextMenuItem`][crate::ContextMenuItem]<!-- -->s with
20 /// [`prepend()`][Self::prepend()], [`append()`][Self::append()] or
21 /// [`insert()`][Self::insert()], maybe after having removed the
22 /// existing ones with [`remove_all()`][Self::remove_all()].
23 #[doc(alias = "WebKitContextMenu")]
24 pub struct ContextMenu(Object<ffi::WebKitContextMenu, ffi::WebKitContextMenuClass>);
25
26 match fn {
27 type_ => || ffi::webkit_context_menu_get_type(),
28 }
29}
30
31impl ContextMenu {
32 /// Creates a new [`ContextMenu`][crate::ContextMenu] object.
33 ///
34 /// Creates a new [`ContextMenu`][crate::ContextMenu] object to be used as a submenu of an existing
35 /// [`ContextMenu`][crate::ContextMenu]. The context menu of a [`WebView`][crate::WebView] is created by the view
36 /// and passed as an argument of [`context-menu`][struct@crate::WebView#context-menu] signal.
37 /// To add items to the menu use [`prepend()`][Self::prepend()],
38 /// [`append()`][Self::append()] or [`insert()`][Self::insert()].
39 /// See also [`with_items()`][Self::with_items()] to create a [`ContextMenu`][crate::ContextMenu] with
40 /// a list of initial items.
41 ///
42 /// # Returns
43 ///
44 /// The newly created [`ContextMenu`][crate::ContextMenu] object
45 #[doc(alias = "webkit_context_menu_new")]
46 pub fn new() -> ContextMenu {
47 assert_initialized_main_thread!();
48 unsafe { from_glib_full(ffi::webkit_context_menu_new()) }
49 }
50
51 /// Creates a new [`ContextMenu`][crate::ContextMenu] object with the given items.
52 ///
53 /// Creates a new [`ContextMenu`][crate::ContextMenu] object to be used as a submenu of an existing
54 /// [`ContextMenu`][crate::ContextMenu] with the given initial items.
55 /// See also [`new()`][Self::new()]
56 /// ## `items`
57 /// a `GList` of [`ContextMenuItem`][crate::ContextMenuItem]
58 ///
59 /// # Returns
60 ///
61 /// The newly created [`ContextMenu`][crate::ContextMenu] object
62 #[doc(alias = "webkit_context_menu_new_with_items")]
63 #[doc(alias = "new_with_items")]
64 pub fn with_items(items: &[ContextMenuItem]) -> ContextMenu {
65 assert_initialized_main_thread!();
66 unsafe {
67 from_glib_full(ffi::webkit_context_menu_new_with_items(
68 items.to_glib_none().0,
69 ))
70 }
71 }
72
73 /// Adds `item` at the end of the `self`.
74 /// ## `item`
75 /// the [`ContextMenuItem`][crate::ContextMenuItem] to add
76 #[doc(alias = "webkit_context_menu_append")]
77 pub fn append(&self, item: &ContextMenuItem) {
78 unsafe {
79 ffi::webkit_context_menu_append(self.to_glib_none().0, item.to_glib_none().0);
80 }
81 }
82
83 /// Gets the first item in the `self`.
84 ///
85 /// # Returns
86 ///
87 /// the first [`ContextMenuItem`][crate::ContextMenuItem] of `self`,
88 /// or [`None`] if the [`ContextMenu`][crate::ContextMenu] is empty.
89 #[doc(alias = "webkit_context_menu_first")]
90 pub fn first(&self) -> Option<ContextMenuItem> {
91 unsafe { from_glib_none(ffi::webkit_context_menu_first(self.to_glib_none().0)) }
92 }
93
94 /// Gets the [`gdk::Event`][crate::gdk::Event] that triggered the context menu. This function only returns a valid
95 /// [`gdk::Event`][crate::gdk::Event] when called for a [`ContextMenu`][crate::ContextMenu] passed to [`context-menu`][struct@crate::WebView#context-menu]
96 /// signal; in all other cases, [`None`] is returned.
97 ///
98 /// The returned [`gdk::Event`][crate::gdk::Event] is expected to be one of the following types:
99 /// `<itemizedlist>`
100 /// `<listitem>``<para>`
101 /// a `GdkEventButton` of type `GDK_BUTTON_PRESS` when the context menu was triggered with mouse.
102 /// `</para>``</listitem>`
103 /// `<listitem>``<para>`
104 /// a `GdkEventKey` of type `GDK_KEY_PRESS` if the keyboard was used to show the menu.
105 /// `</para>``</listitem>`
106 /// `<listitem>``<para>`
107 /// a generic [`gdk::Event`][crate::gdk::Event] of type `GDK_NOTHING` when the [`popup-menu`][struct@crate::gtk::Widget#popup-menu] signal was used to show the context menu.
108 /// `</para>``</listitem>`
109 /// `</itemizedlist>`
110 ///
111 /// # Returns
112 ///
113 /// the menu event or [`None`].
114 #[doc(alias = "webkit_context_menu_get_event")]
115 #[doc(alias = "get_event")]
116 pub fn event(&self) -> Option<gdk::Event> {
117 unsafe { from_glib_none(ffi::webkit_context_menu_get_event(self.to_glib_none().0)) }
118 }
119
120 /// Gets the item at the given position in the `self`.
121 /// ## `position`
122 /// the position of the item, counting from 0
123 ///
124 /// # Returns
125 ///
126 /// the [`ContextMenuItem`][crate::ContextMenuItem] at position `position` in `self`,
127 /// or [`None`] if the position is off the end of the `self`.
128 #[doc(alias = "webkit_context_menu_get_item_at_position")]
129 #[doc(alias = "get_item_at_position")]
130 pub fn item_at_position(&self, position: u32) -> Option<ContextMenuItem> {
131 unsafe {
132 from_glib_none(ffi::webkit_context_menu_get_item_at_position(
133 self.to_glib_none().0,
134 position,
135 ))
136 }
137 }
138
139 /// Returns the item list of `self`.
140 ///
141 /// # Returns
142 ///
143 /// a `GList` of
144 /// [`ContextMenuItem`][crate::ContextMenuItem]<!-- -->s
145 #[doc(alias = "webkit_context_menu_get_items")]
146 #[doc(alias = "get_items")]
147 pub fn items(&self) -> Vec<ContextMenuItem> {
148 unsafe {
149 FromGlibPtrContainer::from_glib_none(ffi::webkit_context_menu_get_items(
150 self.to_glib_none().0,
151 ))
152 }
153 }
154
155 /// Gets the length of the `self`.
156 ///
157 /// # Returns
158 ///
159 /// the number of [`ContextMenuItem`][crate::ContextMenuItem]<!-- -->s in `self`
160 #[doc(alias = "webkit_context_menu_get_n_items")]
161 #[doc(alias = "get_n_items")]
162 pub fn n_items(&self) -> u32 {
163 unsafe { ffi::webkit_context_menu_get_n_items(self.to_glib_none().0) }
164 }
165
166 /// Gets the position in view coordinates where the context menu was triggered.
167 ///
168 /// This function only returns valid coordinates when called for a [`ContextMenu`][crate::ContextMenu]
169 /// passed to [`context-menu`][struct@crate::WebView#context-menu] signal.
170 ///
171 /// # Returns
172 ///
173 /// [`true`] if valid position coordinates are available, [`false`] otherwise
174 ///
175 /// ## `x`
176 /// return location for the x coordinate
177 ///
178 /// ## `y`
179 /// return location for the y coordinate
180 #[cfg(feature = "v2_52")]
181 #[cfg_attr(docsrs, doc(cfg(feature = "v2_52")))]
182 #[doc(alias = "webkit_context_menu_get_position")]
183 #[doc(alias = "get_position")]
184 pub fn position(&self) -> Option<(i32, i32)> {
185 unsafe {
186 let mut x = std::mem::MaybeUninit::uninit();
187 let mut y = std::mem::MaybeUninit::uninit();
188 let ret = from_glib(ffi::webkit_context_menu_get_position(
189 self.to_glib_none().0,
190 x.as_mut_ptr(),
191 y.as_mut_ptr(),
192 ));
193 if ret {
194 Some((x.assume_init(), y.assume_init()))
195 } else {
196 None
197 }
198 }
199 }
200
201 /// Gets the user data of `self`.
202 ///
203 /// This function can be used from the UI Process to get user data previously set
204 /// from the Web Process with [`set_user_data()`][Self::set_user_data()].
205 ///
206 /// # Returns
207 ///
208 /// the user data of `self`, or [`None`] if `self` doesn't have user data
209 #[doc(alias = "webkit_context_menu_get_user_data")]
210 #[doc(alias = "get_user_data")]
211 pub fn user_data(&self) -> Option<glib::Variant> {
212 unsafe {
213 from_glib_none(ffi::webkit_context_menu_get_user_data(
214 self.to_glib_none().0,
215 ))
216 }
217 }
218
219 /// Inserts `item` into the `self` at the given position.
220 ///
221 /// If `position` is negative, or is larger than the number of items
222 /// in the [`ContextMenu`][crate::ContextMenu], the item is added on to the end of
223 /// the `self`. The first position is 0.
224 /// ## `item`
225 /// the [`ContextMenuItem`][crate::ContextMenuItem] to add
226 /// ## `position`
227 /// the position to insert the item
228 #[doc(alias = "webkit_context_menu_insert")]
229 pub fn insert(&self, item: &ContextMenuItem, position: i32) {
230 unsafe {
231 ffi::webkit_context_menu_insert(self.to_glib_none().0, item.to_glib_none().0, position);
232 }
233 }
234
235 /// Gets the last item in the `self`.
236 ///
237 /// # Returns
238 ///
239 /// the last [`ContextMenuItem`][crate::ContextMenuItem] of `self`,
240 /// or [`None`] if the [`ContextMenu`][crate::ContextMenu] is empty.
241 #[doc(alias = "webkit_context_menu_last")]
242 pub fn last(&self) -> Option<ContextMenuItem> {
243 unsafe { from_glib_none(ffi::webkit_context_menu_last(self.to_glib_none().0)) }
244 }
245
246 /// Moves `item` to the given position in the `self`.
247 ///
248 /// If `position` is negative, or is larger than the number of items
249 /// in the [`ContextMenu`][crate::ContextMenu], the item is added on to the end of
250 /// the `self`.
251 /// The first position is 0.
252 /// ## `item`
253 /// the [`ContextMenuItem`][crate::ContextMenuItem] to add
254 /// ## `position`
255 /// the new position to move the item
256 #[doc(alias = "webkit_context_menu_move_item")]
257 pub fn move_item(&self, item: &ContextMenuItem, position: i32) {
258 unsafe {
259 ffi::webkit_context_menu_move_item(
260 self.to_glib_none().0,
261 item.to_glib_none().0,
262 position,
263 );
264 }
265 }
266
267 /// Adds `item` at the beginning of the `self`.
268 /// ## `item`
269 /// the [`ContextMenuItem`][crate::ContextMenuItem] to add
270 #[doc(alias = "webkit_context_menu_prepend")]
271 pub fn prepend(&self, item: &ContextMenuItem) {
272 unsafe {
273 ffi::webkit_context_menu_prepend(self.to_glib_none().0, item.to_glib_none().0);
274 }
275 }
276
277 /// Removes `item` from the `self`.
278 ///
279 /// See also [`remove_all()`][Self::remove_all()] to remove all items.
280 /// ## `item`
281 /// the [`ContextMenuItem`][crate::ContextMenuItem] to remove
282 #[doc(alias = "webkit_context_menu_remove")]
283 pub fn remove(&self, item: &ContextMenuItem) {
284 unsafe {
285 ffi::webkit_context_menu_remove(self.to_glib_none().0, item.to_glib_none().0);
286 }
287 }
288
289 /// Removes all items of the `self`.
290 #[doc(alias = "webkit_context_menu_remove_all")]
291 pub fn remove_all(&self) {
292 unsafe {
293 ffi::webkit_context_menu_remove_all(self.to_glib_none().0);
294 }
295 }
296
297 /// Sets user data to `self`.
298 ///
299 /// This function can be used from a Web Process extension to set user data
300 /// that can be retrieved from the UI Process using [`user_data()`][Self::user_data()].
301 /// If the `user_data` [`glib::Variant`][struct@crate::glib::Variant] is floating, it is consumed.
302 #[doc(alias = "webkit_context_menu_set_user_data")]
303 pub fn set_user_data(&self, user_data: &glib::Variant) {
304 unsafe {
305 ffi::webkit_context_menu_set_user_data(
306 self.to_glib_none().0,
307 user_data.to_glib_none().0,
308 );
309 }
310 }
311}
312
313impl Default for ContextMenu {
314 fn default() -> Self {
315 Self::new()
316 }
317}