Skip to main content

dear_imgui_rs/widget/popup/
flags.rs

1use crate::sys;
2
3fn assert_supported_bits(caller: &str, ty: &str, bits: i32, supported: i32) {
4    let unsupported = bits & !supported;
5    assert!(
6        unsupported == 0,
7        "{caller} received unsupported {ty} bits: 0x{unsupported:X}"
8    );
9}
10
11pub(super) fn validate_popup_open_flags(caller: &str, flags: PopupOpenFlags) {
12    assert_supported_bits(
13        caller,
14        "PopupOpenFlags",
15        flags.bits(),
16        PopupOpenFlags::all().bits(),
17    );
18}
19
20pub(super) fn validate_popup_context_flags(caller: &str, flags: PopupContextFlags) {
21    assert_supported_bits(
22        caller,
23        "PopupContextFlags",
24        flags.bits(),
25        PopupContextFlags::all().bits(),
26    );
27}
28
29pub(super) fn validate_popup_query_flags(caller: &str, flags: PopupQueryFlags) {
30    assert_supported_bits(
31        caller,
32        "PopupQueryFlags",
33        flags.bits(),
34        PopupQueryFlags::all().bits(),
35    );
36    assert!(
37        !flags.contains(PopupQueryFlags::ANY_POPUP_LEVEL)
38            || flags.contains(PopupQueryFlags::ANY_POPUP_ID),
39        "{caller} requires ANY_POPUP_ID when using ANY_POPUP_LEVEL with a string id"
40    );
41}
42
43macro_rules! impl_popup_flag_raw {
44    ($ty:ident) => {
45        impl $ty {
46            #[inline]
47            pub(crate) fn raw(self) -> i32 {
48                self.bits()
49            }
50        }
51    };
52}
53
54bitflags::bitflags! {
55    /// Independent flags accepted by `OpenPopup*()` call sites.
56    #[repr(transparent)]
57    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
58    pub struct PopupOpenFlags: i32 {
59        /// No flags
60        const NONE = sys::ImGuiPopupFlags_None as i32;
61        /// Do not reopen the same popup if already open.
62        const NO_REOPEN = sys::ImGuiPopupFlags_NoReopen as i32;
63        /// Don't open if there's already a popup at the same level of the popup stack.
64        const NO_OPEN_OVER_EXISTING_POPUP = sys::ImGuiPopupFlags_NoOpenOverExistingPopup as i32;
65    }
66}
67
68bitflags::bitflags! {
69    /// Independent flags accepted by `OpenPopupOnItemClick()` and
70    /// `BeginPopupContext*()` call sites.
71    ///
72    /// Mouse button selection is a single-choice setting represented by
73    /// [`PopupContextMouseButton`](super::PopupContextMouseButton).
74    #[repr(transparent)]
75    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
76    pub struct PopupContextFlags: i32 {
77        /// No flags
78        const NONE = sys::ImGuiPopupFlags_None as i32;
79        /// Do not reopen the same popup if already open.
80        const NO_REOPEN = sys::ImGuiPopupFlags_NoReopen as i32;
81        /// Don't open if there's already a popup at the same level of the popup stack.
82        const NO_OPEN_OVER_EXISTING_POPUP = sys::ImGuiPopupFlags_NoOpenOverExistingPopup as i32;
83        /// For window context helpers: don't return true when hovering items, only when hovering empty space.
84        const NO_OPEN_OVER_ITEMS = sys::ImGuiPopupFlags_NoOpenOverItems as i32;
85    }
86}
87
88bitflags::bitflags! {
89    /// Independent flags accepted by `IsPopupOpen()` string-id queries.
90    #[repr(transparent)]
91    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
92    pub struct PopupQueryFlags: i32 {
93        /// No flags
94        const NONE = sys::ImGuiPopupFlags_None as i32;
95        /// Ignore the string id and test for any popup at the current popup stack level.
96        const ANY_POPUP_ID = sys::ImGuiPopupFlags_AnyPopupId as i32;
97        /// Search/test at any level of the popup stack.
98        ///
99        /// With a string-id query this must be combined with [`ANY_POPUP_ID`](Self::ANY_POPUP_ID),
100        /// matching Dear ImGui's string-id assertion.
101        const ANY_POPUP_LEVEL = sys::ImGuiPopupFlags_AnyPopupLevel as i32;
102        /// Test for any popup at any popup stack level.
103        const ANY_POPUP = Self::ANY_POPUP_ID.bits() | Self::ANY_POPUP_LEVEL.bits();
104    }
105}
106
107impl_popup_flag_raw!(PopupOpenFlags);
108impl_popup_flag_raw!(PopupContextFlags);
109impl_popup_flag_raw!(PopupQueryFlags);