Skip to main content

get_registry/common/protocols_data/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    type Data: 'static;
344
345    /// configure the popup surface
346    ///
347    /// This event asks the popup surface to configure itself given the
348    /// configuration. The configured state should not be applied immediately.
349    /// See xdg_surface.configure for details.
350    ///
351    /// The x and y arguments represent the position the popup was placed at
352    /// given the xdg_positioner rule, relative to the upper left corner of the
353    /// window geometry of the parent surface.
354    ///
355    /// For version 2 or older, the configure event for an xdg_popup is only
356    /// ever sent once for the initial configuration. Starting with version 3,
357    /// it may be sent again if the popup is setup with an xdg_positioner with
358    /// set_reactive requested, or in response to xdg_popup.reposition requests.
359    ///
360    /// # Arguments
361    ///
362    /// - `x`: x position relative to parent surface window geometry
363    /// - `y`: y position relative to parent surface window geometry
364    /// - `width`: window geometry width
365    /// - `height`: window geometry height
366    #[inline]
367    fn configure(
368        &self,
369        _data: &mut Self::Data,
370        _slf: &XdgPopupRef,
371        x: i32,
372        y: i32,
373        width: i32,
374        height: i32,
375    ) {
376        let _ = x;
377        let _ = y;
378        let _ = width;
379        let _ = height;
380    }
381
382    /// popup interaction is done
383    ///
384    /// The popup_done event is sent out when a popup is dismissed by the
385    /// compositor. The client should destroy the xdg_popup object at this
386    /// point.
387    #[inline]
388    fn popup_done(&self, _data: &mut Self::Data, _slf: &XdgPopupRef) {}
389
390    /// signal the completion of a repositioned request
391    ///
392    /// The repositioned event is sent as part of a popup configuration
393    /// sequence, together with xdg_popup.configure and lastly
394    /// xdg_surface.configure to notify the completion of a reposition request.
395    ///
396    /// The repositioned event is to notify about the completion of a
397    /// xdg_popup.reposition request. The token argument is the token passed
398    /// in the xdg_popup.reposition request.
399    ///
400    /// Immediately after this event is emitted, xdg_popup.configure and
401    /// xdg_surface.configure will be sent with the updated size and position,
402    /// as well as a new configure serial.
403    ///
404    /// The client should optionally update the content of the popup, but must
405    /// acknowledge the new popup configuration for the new position to take
406    /// effect. See xdg_surface.ack_configure for details.
407    ///
408    /// # Arguments
409    ///
410    /// - `token`: reposition request token
411    #[inline]
412    fn repositioned(&self, _data: &mut Self::Data, _slf: &XdgPopupRef, token: u32) {
413        let _ = token;
414    }
415}
416
417impl XdgPopupEventHandler for private::NoOpEventHandler {
418    type Data = ();
419}
420
421// SAFETY: - INTERFACE is a valid wl_interface
422//         - mutable_type always returns the same value
423unsafe impl<H> EventHandler for private::EventHandler<H>
424where
425    H: XdgPopupEventHandler,
426{
427    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
428
429    #[inline]
430    fn mutable_type() -> Option<(TypeId, &'static str)> {
431        let id = TypeId::of::<H::Data>();
432        let name = std::any::type_name::<H::Data>();
433        Some((id, name))
434    }
435
436    #[allow(unused_variables)]
437    unsafe fn handle_event(
438        &self,
439        queue: &Queue,
440        data: *mut u8,
441        slf: &UntypedBorrowedProxy,
442        opcode: u32,
443        args: *mut wl_argument,
444    ) {
445        // SAFETY: This function requires that slf has the interface INTERFACE
446        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<XdgPopupRef>(slf) };
447        // SAFETY: This function requires that data is `&mut T` where `T`
448        //         has the type id returned by `Self::mutable_type`, i.e.,
449        //         `T = H::Data`.
450        let data: &mut H::Data = unsafe { &mut *data.cast() };
451        match opcode {
452            0 => {
453                // SAFETY: INTERFACE requires that there are 4 arguments
454                let args = unsafe { &*args.cast::<[wl_argument; 4]>() };
455                // SAFETY: - INTERFACE requires that args[0] contains an int
456                let arg0 = unsafe { args[0].i };
457                // SAFETY: - INTERFACE requires that args[1] contains an int
458                let arg1 = unsafe { args[1].i };
459                // SAFETY: - INTERFACE requires that args[2] contains an int
460                let arg2 = unsafe { args[2].i };
461                // SAFETY: - INTERFACE requires that args[3] contains an int
462                let arg3 = unsafe { args[3].i };
463                self.0.configure(data, slf, arg0, arg1, arg2, arg3);
464            }
465            1 => {
466                self.0.popup_done(data, slf);
467            }
468            2 => {
469                // SAFETY: INTERFACE requires that there are 1 arguments
470                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
471                // SAFETY: - INTERFACE requires that args[0] contains a uint
472                let arg0 = unsafe { args[0].u };
473                self.0.repositioned(data, slf, arg0);
474            }
475            _ => {
476                invalid_opcode("xdg_popup", opcode);
477            }
478        }
479    }
480}
481
482impl<H> CreateEventHandler<H> for private::ProxyApi
483where
484    H: XdgPopupEventHandler,
485{
486    type EventHandler = private::EventHandler<H>;
487
488    #[inline]
489    fn create_event_handler(handler: H) -> Self::EventHandler {
490        private::EventHandler(handler)
491    }
492}
493
494impl XdgPopup {
495    /// Since when the error.invalid_grab enum variant is available.
496    #[allow(dead_code)]
497    pub const ENM__ERROR_INVALID_GRAB__SINCE: u32 = 1;
498}
499
500#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
501#[allow(dead_code)]
502pub struct XdgPopupError(pub u32);
503
504impl XdgPopupError {
505    /// tried to grab after being mapped
506    #[allow(dead_code)]
507    pub const INVALID_GRAB: Self = Self(0);
508}
509
510impl Debug for XdgPopupError {
511    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
512        let name = match *self {
513            Self::INVALID_GRAB => "INVALID_GRAB",
514            _ => return Debug::fmt(&self.0, f),
515        };
516        f.write_str(name)
517    }
518}
519
520/// Functional event handlers.
521pub mod event_handlers {
522    use super::*;
523
524    /// Event handler for configure events.
525    pub struct Configure<T, F>(F, PhantomData<fn(&mut T)>);
526    impl<T, F> XdgPopupEventHandler for Configure<T, F>
527    where
528        T: 'static,
529        F: Fn(&mut T, &XdgPopupRef, i32, i32, i32, i32),
530    {
531        type Data = T;
532
533        #[inline]
534        fn configure(
535            &self,
536            _data: &mut T,
537            _slf: &XdgPopupRef,
538            x: i32,
539            y: i32,
540            width: i32,
541            height: i32,
542        ) {
543            self.0(_data, _slf, x, y, width, height)
544        }
545    }
546
547    /// Event handler for popup_done events.
548    pub struct PopupDone<T, F>(F, PhantomData<fn(&mut T)>);
549    impl<T, F> XdgPopupEventHandler for PopupDone<T, F>
550    where
551        T: 'static,
552        F: Fn(&mut T, &XdgPopupRef),
553    {
554        type Data = T;
555
556        #[inline]
557        fn popup_done(&self, _data: &mut T, _slf: &XdgPopupRef) {
558            self.0(_data, _slf)
559        }
560    }
561
562    /// Event handler for repositioned events.
563    pub struct Repositioned<T, F>(F, PhantomData<fn(&mut T)>);
564    impl<T, F> XdgPopupEventHandler for Repositioned<T, F>
565    where
566        T: 'static,
567        F: Fn(&mut T, &XdgPopupRef, u32),
568    {
569        type Data = T;
570
571        #[inline]
572        fn repositioned(&self, _data: &mut T, _slf: &XdgPopupRef, token: u32) {
573            self.0(_data, _slf, token)
574        }
575    }
576
577    impl XdgPopup {
578        /// Creates an event handler for configure events.
579        ///
580        /// The event handler ignores all other events.
581        #[allow(dead_code)]
582        pub fn on_configure<T, F>(f: F) -> Configure<T, F>
583        where
584            T: 'static,
585            F: Fn(&mut T, &XdgPopupRef, i32, i32, i32, i32),
586        {
587            Configure(f, PhantomData)
588        }
589
590        /// Creates an event handler for popup_done events.
591        ///
592        /// The event handler ignores all other events.
593        #[allow(dead_code)]
594        pub fn on_popup_done<T, F>(f: F) -> PopupDone<T, F>
595        where
596            T: 'static,
597            F: Fn(&mut T, &XdgPopupRef),
598        {
599            PopupDone(f, PhantomData)
600        }
601
602        /// Creates an event handler for repositioned events.
603        ///
604        /// The event handler ignores all other events.
605        #[allow(dead_code)]
606        pub fn on_repositioned<T, F>(f: F) -> Repositioned<T, F>
607        where
608            T: 'static,
609            F: Fn(&mut T, &XdgPopupRef, u32),
610        {
611            Repositioned(f, PhantomData)
612        }
613    }
614}