Skip to main content

get_registry/common/protocols/xdg_shell/
xdg_popup.rs

1//! short-lived, popup surfaces for menus
2//!
3//! A popup surface is a short-lived, temporary surface. It can be used to
4//! implement for example menus, popovers, tooltips and other similar user
5//! interface concepts.
6//!
7//! A popup can be made to take an explicit grab. See xdg_popup.grab for
8//! details.
9//!
10//! When the popup is dismissed, a popup_done event will be sent out, and at
11//! the same time the surface will be unmapped. See the xdg_popup.popup_done
12//! event for details.
13//!
14//! Explicitly destroying the xdg_popup object will also dismiss the popup and
15//! unmap the surface. Clients that want to dismiss the popup when another
16//! surface of their own is clicked should dismiss the popup using the destroy
17//! request.
18//!
19//! A newly created xdg_popup will be stacked on top of all previously created
20//! xdg_popup surfaces associated with the same xdg_toplevel.
21//!
22//! The parent of an xdg_popup must be mapped (see the xdg_surface
23//! description) before the xdg_popup itself.
24//!
25//! The client must call wl_surface.commit on the corresponding wl_surface
26//! for the xdg_popup state to take effect.
27
28use super::super::all_types::*;
29use ::wl_client::builder::prelude::*;
30
31static INTERFACE: wl_interface = wl_interface {
32    name: c"xdg_popup".as_ptr(),
33    version: 6,
34    method_count: 3,
35    methods: {
36        static MESSAGES: [wl_message; 3] = [
37            wl_message {
38                name: c"destroy".as_ptr(),
39                signature: c"".as_ptr(),
40                types: {
41                    static TYPES: [Option<&'static wl_interface>; 0] = [];
42                    TYPES.as_ptr().cast()
43                },
44            },
45            wl_message {
46                name: c"grab".as_ptr(),
47                signature: c"ou".as_ptr(),
48                types: {
49                    static TYPES: [Option<&'static wl_interface>; 2] =
50                        [Some(WlSeat::WL_INTERFACE), None];
51                    TYPES.as_ptr().cast()
52                },
53            },
54            wl_message {
55                name: c"reposition".as_ptr(),
56                signature: c"ou".as_ptr(),
57                types: {
58                    static TYPES: [Option<&'static wl_interface>; 2] =
59                        [Some(XdgPositioner::WL_INTERFACE), None];
60                    TYPES.as_ptr().cast()
61                },
62            },
63        ];
64        MESSAGES.as_ptr()
65    },
66    event_count: 3,
67    events: {
68        static MESSAGES: [wl_message; 3] = [
69            wl_message {
70                name: c"configure".as_ptr(),
71                signature: c"iiii".as_ptr(),
72                types: {
73                    static TYPES: [Option<&'static wl_interface>; 4] = [None, None, None, None];
74                    TYPES.as_ptr().cast()
75                },
76            },
77            wl_message {
78                name: c"popup_done".as_ptr(),
79                signature: c"".as_ptr(),
80                types: {
81                    static TYPES: [Option<&'static wl_interface>; 0] = [];
82                    TYPES.as_ptr().cast()
83                },
84            },
85            wl_message {
86                name: c"repositioned".as_ptr(),
87                signature: c"u".as_ptr(),
88                types: {
89                    static TYPES: [Option<&'static wl_interface>; 1] = [None];
90                    TYPES.as_ptr().cast()
91                },
92            },
93        ];
94        MESSAGES.as_ptr()
95    },
96};
97
98/// An owned xdg_popup proxy.
99///
100/// See the documentation of [the module][self] for the interface description.
101#[derive(Clone, Eq, PartialEq)]
102#[repr(transparent)]
103pub struct XdgPopup {
104    /// This proxy has the interface INTERFACE.
105    proxy: UntypedOwnedProxy,
106}
107
108/// A borrowed xdg_popup proxy.
109///
110/// See the documentation of [the module][self] for the interface description.
111#[derive(Eq, PartialEq)]
112#[repr(transparent)]
113pub struct XdgPopupRef {
114    /// This proxy has the interface INTERFACE.
115    proxy: UntypedBorrowedProxy,
116}
117
118// SAFETY: XdgPopup is a transparent wrapper around UntypedOwnedProxy
119unsafe impl UntypedOwnedProxyWrapper for XdgPopup {}
120
121// SAFETY: - INTERFACE is a valid wl_interface
122//         - The only invariant is that self.proxy has a compatible interface
123unsafe impl OwnedProxy for XdgPopup {
124    const INTERFACE: &'static str = "xdg_popup";
125    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
126    const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
127        private::EventHandler(private::NoOpEventHandler);
128    const MAX_VERSION: u32 = 6;
129
130    type Borrowed = XdgPopupRef;
131    type Api = private::ProxyApi;
132    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
133}
134
135// SAFETY: XdgPopupRef is a transparent wrapper around UntypedBorrowedProxy
136unsafe impl UntypedBorrowedProxyWrapper for XdgPopupRef {}
137
138// SAFETY: - The only invariant is that self.proxy has a compatible interface
139unsafe impl BorrowedProxy for XdgPopupRef {
140    type Owned = XdgPopup;
141}
142
143impl Deref for XdgPopup {
144    type Target = XdgPopupRef;
145
146    fn deref(&self) -> &Self::Target {
147        proxy::low_level::deref(self)
148    }
149}
150
151mod private {
152    pub struct ProxyApi;
153
154    #[allow(dead_code)]
155    pub struct EventHandler<H>(pub(super) H);
156
157    #[allow(dead_code)]
158    pub struct NoOpEventHandler;
159}
160
161impl Debug for XdgPopup {
162    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
163        write!(f, "xdg_popup#{}", self.proxy.id())
164    }
165}
166
167impl Debug for XdgPopupRef {
168    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
169        write!(f, "xdg_popup#{}", self.proxy.id())
170    }
171}
172
173impl PartialEq<XdgPopupRef> for XdgPopup {
174    fn eq(&self, other: &XdgPopupRef) -> bool {
175        self.proxy == other.proxy
176    }
177}
178
179impl PartialEq<XdgPopup> for XdgPopupRef {
180    fn eq(&self, other: &XdgPopup) -> bool {
181        self.proxy == other.proxy
182    }
183}
184
185#[allow(dead_code)]
186impl XdgPopup {
187    /// Since when the destroy request is available.
188    #[allow(dead_code)]
189    pub const REQ__DESTROY__SINCE: u32 = 1;
190
191    /// Since when the grab request is available.
192    #[allow(dead_code)]
193    pub const REQ__GRAB__SINCE: u32 = 1;
194
195    /// Since when the reposition request is available.
196    #[allow(dead_code)]
197    pub const REQ__REPOSITION__SINCE: u32 = 3;
198}
199
200#[allow(dead_code)]
201impl XdgPopup {
202    /// remove xdg_popup interface
203    ///
204    /// This destroys the popup. Explicitly destroying the xdg_popup
205    /// object will also dismiss the popup, and unmap the surface.
206    ///
207    /// If this xdg_popup is not the "topmost" popup, the
208    /// xdg_wm_base.not_the_topmost_popup protocol error will be sent.
209    #[inline]
210    pub fn destroy(&self) {
211        let mut args = [];
212        // SAFETY: - self.proxy has the interface INTERFACE
213        //         - 0 < INTERFACE.method_count = 3
214        //         - the request signature is ``
215        unsafe {
216            self.proxy.send_destructor(0, &mut args);
217        }
218    }
219}
220
221#[allow(dead_code)]
222impl XdgPopupRef {
223    /// make the popup take an explicit grab
224    ///
225    /// This request makes the created popup take an explicit grab. An explicit
226    /// grab will be dismissed when the user dismisses the popup, or when the
227    /// client destroys the xdg_popup. This can be done by the user clicking
228    /// outside the surface, using the keyboard, or even locking the screen
229    /// through closing the lid or a timeout.
230    ///
231    /// If the compositor denies the grab, the popup will be immediately
232    /// dismissed.
233    ///
234    /// This request must be used in response to some sort of user action like a
235    /// button press, key press, or touch down event. The serial number of the
236    /// event should be passed as 'serial'.
237    ///
238    /// The parent of a grabbing popup must either be an xdg_toplevel surface or
239    /// another xdg_popup with an explicit grab. If the parent is another
240    /// xdg_popup it means that the popups are nested, with this popup now being
241    /// the topmost popup.
242    ///
243    /// Nested popups must be destroyed in the reverse order they were created
244    /// in, e.g. the only popup you are allowed to destroy at all times is the
245    /// topmost one.
246    ///
247    /// When compositors choose to dismiss a popup, they may dismiss every
248    /// nested grabbing popup as well. When a compositor dismisses popups, it
249    /// will follow the same dismissing order as required from the client.
250    ///
251    /// If the topmost grabbing popup is destroyed, the grab will be returned to
252    /// the parent of the popup, if that parent previously had an explicit grab.
253    ///
254    /// If the parent is a grabbing popup which has already been dismissed, this
255    /// popup will be immediately dismissed. If the parent is a popup that did
256    /// not take an explicit grab, an error will be raised.
257    ///
258    /// During a popup grab, the client owning the grab will receive pointer
259    /// and touch events for all their surfaces as normal (similar to an
260    /// "owner-events" grab in X11 parlance), while the top most grabbing popup
261    /// will always have keyboard focus.
262    ///
263    /// # Arguments
264    ///
265    /// - `seat`: the wl_seat of the user event
266    /// - `serial`: the serial of the user event
267    #[inline]
268    pub fn grab(&self, seat: &WlSeatRef, serial: u32) {
269        let (arg0, arg1) = (seat, serial);
270        let obj0_lock = proxy::lock(arg0);
271        let obj0 = check_argument_proxy("seat", obj0_lock.wl_proxy());
272        let mut args = [wl_argument { o: obj0 }, wl_argument { u: arg1 }];
273        // SAFETY: - self.proxy has the interface INTERFACE
274        //         - 1 < INTERFACE.method_count = 3
275        //         - the request signature is `ou`
276        unsafe {
277            self.proxy.send_request(1, &mut args);
278        }
279    }
280
281    /// recalculate the popup's location
282    ///
283    /// Reposition an already-mapped popup. The popup will be placed given the
284    /// details in the passed xdg_positioner object, and a
285    /// xdg_popup.repositioned followed by xdg_popup.configure and
286    /// xdg_surface.configure will be emitted in response. Any parameters set
287    /// by the previous positioner will be discarded.
288    ///
289    /// The passed token will be sent in the corresponding
290    /// xdg_popup.repositioned event. The new popup position will not take
291    /// effect until the corresponding configure event is acknowledged by the
292    /// client. See xdg_popup.repositioned for details. The token itself is
293    /// opaque, and has no other special meaning.
294    ///
295    /// If multiple reposition requests are sent, the compositor may skip all
296    /// but the last one.
297    ///
298    /// If the popup is repositioned in response to a configure event for its
299    /// parent, the client should send an xdg_positioner.set_parent_configure
300    /// and possibly an xdg_positioner.set_parent_size request to allow the
301    /// compositor to properly constrain the popup.
302    ///
303    /// If the popup is repositioned together with a parent that is being
304    /// resized, but not in response to a configure event, the client should
305    /// send an xdg_positioner.set_parent_size request.
306    ///
307    /// # Arguments
308    ///
309    /// - `positioner`:
310    /// - `token`: reposition request token
311    #[inline]
312    pub fn reposition(&self, positioner: &XdgPositionerRef, token: u32) {
313        let (arg0, arg1) = (positioner, token);
314        let obj0_lock = proxy::lock(arg0);
315        let obj0 = check_argument_proxy("positioner", obj0_lock.wl_proxy());
316        let mut args = [wl_argument { o: obj0 }, wl_argument { u: arg1 }];
317        // SAFETY: - self.proxy has the interface INTERFACE
318        //         - 2 < INTERFACE.method_count = 3
319        //         - the request signature is `ou`
320        unsafe {
321            self.proxy.send_request(2, &mut args);
322        }
323    }
324}
325
326impl XdgPopup {
327    /// Since when the configure event is available.
328    #[allow(dead_code)]
329    pub const EVT__CONFIGURE__SINCE: u32 = 1;
330
331    /// Since when the popup_done event is available.
332    #[allow(dead_code)]
333    pub const EVT__POPUP_DONE__SINCE: u32 = 1;
334
335    /// Since when the repositioned event is available.
336    #[allow(dead_code)]
337    pub const EVT__REPOSITIONED__SINCE: u32 = 3;
338}
339
340/// An event handler for [XdgPopup] proxies.
341#[allow(dead_code)]
342pub trait XdgPopupEventHandler {
343    /// configure the popup surface
344    ///
345    /// This event asks the popup surface to configure itself given the
346    /// configuration. The configured state should not be applied immediately.
347    /// See xdg_surface.configure for details.
348    ///
349    /// The x and y arguments represent the position the popup was placed at
350    /// given the xdg_positioner rule, relative to the upper left corner of the
351    /// window geometry of the parent surface.
352    ///
353    /// For version 2 or older, the configure event for an xdg_popup is only
354    /// ever sent once for the initial configuration. Starting with version 3,
355    /// it may be sent again if the popup is setup with an xdg_positioner with
356    /// set_reactive requested, or in response to xdg_popup.reposition requests.
357    ///
358    /// # Arguments
359    ///
360    /// - `x`: x position relative to parent surface window geometry
361    /// - `y`: y position relative to parent surface window geometry
362    /// - `width`: window geometry width
363    /// - `height`: window geometry height
364    #[inline]
365    fn configure(&self, _slf: &XdgPopupRef, x: i32, y: i32, width: i32, height: i32) {
366        let _ = x;
367        let _ = y;
368        let _ = width;
369        let _ = height;
370    }
371
372    /// popup interaction is done
373    ///
374    /// The popup_done event is sent out when a popup is dismissed by the
375    /// compositor. The client should destroy the xdg_popup object at this
376    /// point.
377    #[inline]
378    fn popup_done(&self, _slf: &XdgPopupRef) {}
379
380    /// signal the completion of a repositioned request
381    ///
382    /// The repositioned event is sent as part of a popup configuration
383    /// sequence, together with xdg_popup.configure and lastly
384    /// xdg_surface.configure to notify the completion of a reposition request.
385    ///
386    /// The repositioned event is to notify about the completion of a
387    /// xdg_popup.reposition request. The token argument is the token passed
388    /// in the xdg_popup.reposition request.
389    ///
390    /// Immediately after this event is emitted, xdg_popup.configure and
391    /// xdg_surface.configure will be sent with the updated size and position,
392    /// as well as a new configure serial.
393    ///
394    /// The client should optionally update the content of the popup, but must
395    /// acknowledge the new popup configuration for the new position to take
396    /// effect. See xdg_surface.ack_configure for details.
397    ///
398    /// # Arguments
399    ///
400    /// - `token`: reposition request token
401    #[inline]
402    fn repositioned(&self, _slf: &XdgPopupRef, token: u32) {
403        let _ = token;
404    }
405}
406
407impl XdgPopupEventHandler for private::NoOpEventHandler {}
408
409// SAFETY: - INTERFACE is a valid wl_interface
410unsafe impl<H> EventHandler for private::EventHandler<H>
411where
412    H: XdgPopupEventHandler,
413{
414    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
415
416    #[allow(unused_variables)]
417    unsafe fn handle_event(
418        &self,
419        queue: &Queue,
420        data: *mut u8,
421        slf: &UntypedBorrowedProxy,
422        opcode: u32,
423        args: *mut wl_argument,
424    ) {
425        // SAFETY: This function requires that slf has the interface INTERFACE
426        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<XdgPopupRef>(slf) };
427        match opcode {
428            0 => {
429                // SAFETY: INTERFACE requires that there are 4 arguments
430                let args = unsafe { &*args.cast::<[wl_argument; 4]>() };
431                // SAFETY: - INTERFACE requires that args[0] contains an int
432                let arg0 = unsafe { args[0].i };
433                // SAFETY: - INTERFACE requires that args[1] contains an int
434                let arg1 = unsafe { args[1].i };
435                // SAFETY: - INTERFACE requires that args[2] contains an int
436                let arg2 = unsafe { args[2].i };
437                // SAFETY: - INTERFACE requires that args[3] contains an int
438                let arg3 = unsafe { args[3].i };
439                self.0.configure(slf, arg0, arg1, arg2, arg3);
440            }
441            1 => {
442                self.0.popup_done(slf);
443            }
444            2 => {
445                // SAFETY: INTERFACE requires that there are 1 arguments
446                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
447                // SAFETY: - INTERFACE requires that args[0] contains a uint
448                let arg0 = unsafe { args[0].u };
449                self.0.repositioned(slf, arg0);
450            }
451            _ => {
452                invalid_opcode("xdg_popup", opcode);
453            }
454        }
455    }
456}
457
458impl<H> CreateEventHandler<H> for private::ProxyApi
459where
460    H: XdgPopupEventHandler,
461{
462    type EventHandler = private::EventHandler<H>;
463
464    #[inline]
465    fn create_event_handler(handler: H) -> Self::EventHandler {
466        private::EventHandler(handler)
467    }
468}
469
470impl XdgPopup {
471    /// Since when the error.invalid_grab enum variant is available.
472    #[allow(dead_code)]
473    pub const ENM__ERROR_INVALID_GRAB__SINCE: u32 = 1;
474}
475
476#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
477#[allow(dead_code)]
478pub struct XdgPopupError(pub u32);
479
480impl XdgPopupError {
481    /// tried to grab after being mapped
482    #[allow(dead_code)]
483    pub const INVALID_GRAB: Self = Self(0);
484}
485
486impl Debug for XdgPopupError {
487    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
488        let name = match *self {
489            Self::INVALID_GRAB => "INVALID_GRAB",
490            _ => return Debug::fmt(&self.0, f),
491        };
492        f.write_str(name)
493    }
494}
495
496/// Functional event handlers.
497pub mod event_handlers {
498    use super::*;
499
500    /// Event handler for configure events.
501    pub struct Configure<F>(F);
502    impl<F> XdgPopupEventHandler for Configure<F>
503    where
504        F: Fn(&XdgPopupRef, i32, i32, i32, i32),
505    {
506        #[inline]
507        fn configure(&self, _slf: &XdgPopupRef, x: i32, y: i32, width: i32, height: i32) {
508            self.0(_slf, x, y, width, height)
509        }
510    }
511
512    /// Event handler for popup_done events.
513    pub struct PopupDone<F>(F);
514    impl<F> XdgPopupEventHandler for PopupDone<F>
515    where
516        F: Fn(&XdgPopupRef),
517    {
518        #[inline]
519        fn popup_done(&self, _slf: &XdgPopupRef) {
520            self.0(_slf)
521        }
522    }
523
524    /// Event handler for repositioned events.
525    pub struct Repositioned<F>(F);
526    impl<F> XdgPopupEventHandler for Repositioned<F>
527    where
528        F: Fn(&XdgPopupRef, u32),
529    {
530        #[inline]
531        fn repositioned(&self, _slf: &XdgPopupRef, token: u32) {
532            self.0(_slf, token)
533        }
534    }
535
536    impl XdgPopup {
537        /// Creates an event handler for configure events.
538        ///
539        /// The event handler ignores all other events.
540        #[allow(dead_code)]
541        pub fn on_configure<F>(f: F) -> Configure<F>
542        where
543            F: Fn(&XdgPopupRef, i32, i32, i32, i32),
544        {
545            Configure(f)
546        }
547
548        /// Creates an event handler for popup_done events.
549        ///
550        /// The event handler ignores all other events.
551        #[allow(dead_code)]
552        pub fn on_popup_done<F>(f: F) -> PopupDone<F>
553        where
554            F: Fn(&XdgPopupRef),
555        {
556            PopupDone(f)
557        }
558
559        /// Creates an event handler for repositioned events.
560        ///
561        /// The event handler ignores all other events.
562        #[allow(dead_code)]
563        pub fn on_repositioned<F>(f: F) -> Repositioned<F>
564        where
565            F: Fn(&XdgPopupRef, u32),
566        {
567            Repositioned(f)
568        }
569    }
570}