1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// DO NOT EDIT

use crate::Gravity;
use crate::PopupLayout;
use crate::Surface;
use glib::object::IsA;
use glib::translate::*;
use std::fmt;

glib::wrapper! {
    /// A [`Popup`][crate::Popup] is a surface that is attached to another surface.
    ///
    /// The [`Popup`][crate::Popup] is positioned relative to its parent surface.
    ///
    /// [`Popup`][crate::Popup]s are typically used to implement menus and similar popups.
    /// They can be modal, which is indicated by the `property::GdkPopup::autohide`
    /// property.
    ///
    /// # Implements
    ///
    /// [`PopupExt`][trait@crate::prelude::PopupExt], [`SurfaceExt`][trait@crate::prelude::SurfaceExt], [`SurfaceExtManual`][trait@crate::prelude::SurfaceExtManual]
    #[doc(alias = "GdkPopup")]
    pub struct Popup(Interface<ffi::GdkPopup, ffi::GdkPopupInterface>) @requires Surface;

    match fn {
        type_ => || ffi::gdk_popup_get_type(),
    }
}

impl Popup {
    pub const NONE: Option<&'static Popup> = None;
}

/// Trait containing all [`struct@Popup`] methods.
///
/// # Implementors
///
/// [`Popup`][struct@crate::Popup]
pub trait PopupExt: 'static {
    /// Returns whether this popup is set to hide on outside clicks.
    ///
    /// # Returns
    ///
    /// [`true`] if @self will autohide
    #[doc(alias = "gdk_popup_get_autohide")]
    #[doc(alias = "get_autohide")]
    fn is_autohide(&self) -> bool;

    /// Returns the parent surface of a popup.
    ///
    /// # Returns
    ///
    /// the parent surface
    #[doc(alias = "gdk_popup_get_parent")]
    #[doc(alias = "get_parent")]
    fn parent(&self) -> Option<Surface>;

    /// Obtains the position of the popup relative to its parent.
    ///
    /// # Returns
    ///
    /// the X coordinate of @self position
    #[doc(alias = "gdk_popup_get_position_x")]
    #[doc(alias = "get_position_x")]
    fn position_x(&self) -> i32;

    /// Obtains the position of the popup relative to its parent.
    ///
    /// # Returns
    ///
    /// the Y coordinate of @self position
    #[doc(alias = "gdk_popup_get_position_y")]
    #[doc(alias = "get_position_y")]
    fn position_y(&self) -> i32;

    /// Gets the current popup rectangle anchor.
    ///
    /// The value returned may change after calling [`present()`][Self::present()],
    /// or after the `signal::Surface::layout` signal is emitted.
    ///
    /// # Returns
    ///
    /// the current rectangle anchor value of @self
    #[doc(alias = "gdk_popup_get_rect_anchor")]
    #[doc(alias = "get_rect_anchor")]
    fn rect_anchor(&self) -> Gravity;

    /// Gets the current popup surface anchor.
    ///
    /// The value returned may change after calling [`present()`][Self::present()],
    /// or after the `signal::Surface::layout` signal is emitted.
    ///
    /// # Returns
    ///
    /// the current surface anchor value of @self
    #[doc(alias = "gdk_popup_get_surface_anchor")]
    #[doc(alias = "get_surface_anchor")]
    fn surface_anchor(&self) -> Gravity;

    /// Present @self after having processed the [`PopupLayout`][crate::PopupLayout] rules.
    ///
    /// If the popup was previously now showing, it will be showed,
    /// otherwise it will change position according to @layout.
    ///
    /// After calling this function, the result should be handled in response
    /// to the `signal::GdkSurface::layout` signal being emitted. The resulting
    /// popup position can be queried using [`position_x()`][Self::position_x()],
    /// [`position_y()`][Self::position_y()], and the resulting size will be sent as
    /// parameters in the layout signal. Use [`rect_anchor()`][Self::rect_anchor()]
    /// and [`surface_anchor()`][Self::surface_anchor()] to get the resulting anchors.
    ///
    /// Presenting may fail, for example if the @self is set to autohide
    /// and is immediately hidden upon being presented. If presenting failed,
    /// the `signal::Surface::layout` signal will not me emitted.
    /// ## `width`
    /// the unconstrained popup width to layout
    /// ## `height`
    /// the unconstrained popup height to layout
    /// ## `layout`
    /// the [`PopupLayout`][crate::PopupLayout] object used to layout
    ///
    /// # Returns
    ///
    /// [`false`] if it failed to be presented, otherwise [`true`].
    #[doc(alias = "gdk_popup_present")]
    fn present(&self, width: i32, height: i32, layout: &PopupLayout) -> bool;
}

impl<O: IsA<Popup>> PopupExt for O {
    fn is_autohide(&self) -> bool {
        unsafe { from_glib(ffi::gdk_popup_get_autohide(self.as_ref().to_glib_none().0)) }
    }

    fn parent(&self) -> Option<Surface> {
        unsafe { from_glib_none(ffi::gdk_popup_get_parent(self.as_ref().to_glib_none().0)) }
    }

    fn position_x(&self) -> i32 {
        unsafe { ffi::gdk_popup_get_position_x(self.as_ref().to_glib_none().0) }
    }

    fn position_y(&self) -> i32 {
        unsafe { ffi::gdk_popup_get_position_y(self.as_ref().to_glib_none().0) }
    }

    fn rect_anchor(&self) -> Gravity {
        unsafe {
            from_glib(ffi::gdk_popup_get_rect_anchor(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn surface_anchor(&self) -> Gravity {
        unsafe {
            from_glib(ffi::gdk_popup_get_surface_anchor(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn present(&self, width: i32, height: i32, layout: &PopupLayout) -> bool {
        unsafe {
            from_glib(ffi::gdk_popup_present(
                self.as_ref().to_glib_none().0,
                width,
                height,
                layout.to_glib_none().0,
            ))
        }
    }
}

impl fmt::Display for Popup {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("Popup")
    }
}