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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
use std::ptr;

use crate::sys;
use crate::window::WindowFlags;
use crate::Ui;

/// Create a modal pop-up.
///
/// # Example
/// ```rust,no_run
/// # use imgui::*;
/// # let mut imgui = Context::create();
/// # let ui = imgui.frame();
/// if ui.button("Show modal") {
///     ui.open_popup("modal");
/// }
/// if let Some(_token) = ui.begin_modal_popup("modal") {
///     ui.text("Content of my modal");
///     if ui.button("OK") {
///         ui.close_current_popup();
///     }
/// };
/// ```
#[must_use]
pub struct PopupModal<'ui, 'p, Label> {
    ui: &'ui Ui,
    label: Label,
    opened: Option<&'p mut bool>,
    flags: WindowFlags,
}

impl<'ui, 'p, Label: AsRef<str>> PopupModal<'ui, 'p, Label> {
    #[deprecated(since = "0.9.0", note = "Use `ui.modal_popup_config(...)` instead")]
    pub fn new(ui: &'ui Ui, label: Label) -> Self {
        PopupModal {
            ui,
            label,
            opened: None,
            flags: WindowFlags::empty(),
        }
    }
    /// Pass a mutable boolean which will be updated to refer to the current
    /// "open" state of the modal.
    pub fn opened(mut self, opened: &'p mut bool) -> Self {
        self.opened = Some(opened);
        self
    }
    pub fn flags(mut self, flags: WindowFlags) -> Self {
        self.flags = flags;
        self
    }
    pub fn title_bar(mut self, value: bool) -> Self {
        self.flags.set(WindowFlags::NO_TITLE_BAR, !value);
        self
    }
    pub fn resizable(mut self, value: bool) -> Self {
        self.flags.set(WindowFlags::NO_RESIZE, !value);
        self
    }
    pub fn movable(mut self, value: bool) -> Self {
        self.flags.set(WindowFlags::NO_MOVE, !value);
        self
    }
    pub fn scroll_bar(mut self, value: bool) -> Self {
        self.flags.set(WindowFlags::NO_SCROLLBAR, !value);
        self
    }
    pub fn scrollable(mut self, value: bool) -> Self {
        self.flags.set(WindowFlags::NO_SCROLL_WITH_MOUSE, !value);
        self
    }
    pub fn collapsible(mut self, value: bool) -> Self {
        self.flags.set(WindowFlags::NO_COLLAPSE, !value);
        self
    }
    pub fn always_auto_resize(mut self, value: bool) -> Self {
        self.flags.set(WindowFlags::ALWAYS_AUTO_RESIZE, value);
        self
    }
    pub fn save_settings(mut self, value: bool) -> Self {
        self.flags.set(WindowFlags::NO_SAVED_SETTINGS, !value);
        self
    }
    pub fn inputs(mut self, value: bool) -> Self {
        self.flags.set(WindowFlags::NO_INPUTS, !value);
        self
    }
    pub fn menu_bar(mut self, value: bool) -> Self {
        self.flags.set(WindowFlags::MENU_BAR, value);
        self
    }
    pub fn horizontal_scrollbar(mut self, value: bool) -> Self {
        self.flags.set(WindowFlags::HORIZONTAL_SCROLLBAR, value);
        self
    }
    pub fn no_focus_on_appearing(mut self, value: bool) -> Self {
        self.flags.set(WindowFlags::NO_FOCUS_ON_APPEARING, value);
        self
    }
    pub fn no_bring_to_front_on_focus(mut self, value: bool) -> Self {
        self.flags
            .set(WindowFlags::NO_BRING_TO_FRONT_ON_FOCUS, value);
        self
    }
    pub fn always_vertical_scrollbar(mut self, value: bool) -> Self {
        self.flags
            .set(WindowFlags::ALWAYS_VERTICAL_SCROLLBAR, value);
        self
    }
    pub fn always_horizontal_scrollbar(mut self, value: bool) -> Self {
        self.flags
            .set(WindowFlags::ALWAYS_HORIZONTAL_SCROLLBAR, value);
        self
    }
    pub fn always_use_window_padding(mut self, value: bool) -> Self {
        self.flags
            .set(WindowFlags::ALWAYS_USE_WINDOW_PADDING, value);
        self
    }

    /// Consume and draw the PopupModal.
    /// Returns the result of the closure, if it is called.
    #[doc(alias = "BeginPopupModal")]
    pub fn build<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
        self.begin_popup().map(|_popup| f())
    }

    /// Consume and draw the PopupModal.
    /// Construct a popup that can have any kind of content.
    ///
    /// This should be called *per frame*, whereas [`Ui::open_popup`]
    /// should be called *once* when you want to actual create the popup.
    #[doc(alias = "BeginPopupModal")]
    pub fn begin_popup(self) -> Option<PopupToken<'ui>> {
        let render = unsafe {
            sys::igBeginPopupModal(
                self.ui.scratch_txt(self.label),
                self.opened
                    .map(|x| x as *mut bool)
                    .unwrap_or(ptr::null_mut()),
                self.flags.bits() as i32,
            )
        };

        if render {
            Some(PopupToken::new(self.ui))
        } else {
            None
        }
    }
}

// Widgets: Popups
impl Ui {
    /// Instructs ImGui that a popup is open.
    ///
    /// You should **call this function once** while calling any of the following per-frame:
    ///
    /// - [`begin_popup`](Self::begin_popup)
    /// - [`popup`](Self::popup)
    /// - [`modal_popup`](Self::modal_popup)
    /// - [`modal_popup_config`](Self::modal_popup_config)
    ///
    /// The confusing aspect to popups is that ImGui holds control over the popup itself.
    #[doc(alias = "OpenPopup")]
    pub fn open_popup(&self, str_id: impl AsRef<str>) {
        unsafe { sys::igOpenPopup_Str(self.scratch_txt(str_id), 0) };
    }

    /// Construct a popup that can have any kind of content.
    ///
    /// This should be called *per frame*, whereas [`open_popup`](Self::open_popup) should be called *once*
    /// to signal that this popup is active.
    #[doc(alias = "BeginPopup")]
    pub fn begin_popup(&self, str_id: impl AsRef<str>) -> Option<PopupToken<'_>> {
        let render = unsafe {
            sys::igBeginPopup(self.scratch_txt(str_id), WindowFlags::empty().bits() as i32)
        };

        if render {
            Some(PopupToken::new(self))
        } else {
            None
        }
    }

    /// Construct a popup that can have any kind of content.
    ///
    /// This should be called *per frame*, whereas [`open_popup`](Self::open_popup) should be called *once*
    /// to signal that this popup is active.
    #[doc(alias = "BeginPopup")]
    pub fn popup<F>(&self, str_id: impl AsRef<str>, f: F)
    where
        F: FnOnce(),
    {
        if let Some(_t) = self.begin_popup(str_id) {
            f();
        }
    }

    /// Creates a [PopupModal], and runs a closure on it.
    ///
    /// To customize the behavior of this [PopupModal], use [`modal_popup_config`](Self::modal_popup_config).
    pub fn modal_popup<Label, Func, R>(&self, str_id: Label, f: Func) -> Option<R>
    where
        Label: AsRef<str>,
        Func: FnOnce() -> R,
    {
        PopupModal {
            ui: self,
            label: str_id,
            opened: None,
            flags: WindowFlags::empty(),
        }
        .build(f)
    }

    /// Creates a [PopupModal], returning a drop token.
    ///
    /// To customize the behavior of this [PopupModal], use [`modal_popup_config`](Self::modal_popup_config).
    pub fn begin_modal_popup<Label: AsRef<str>>(&self, str_id: Label) -> Option<PopupToken<'_>> {
        PopupModal {
            ui: self,
            label: str_id,
            opened: None,
            flags: WindowFlags::empty(),
        }
        .begin_popup()
    }

    /// Creates a [PopupModal] builder.
    pub fn modal_popup_config<Label: AsRef<str>>(
        &self,
        str_id: Label,
    ) -> PopupModal<'_, '_, Label> {
        PopupModal {
            ui: self,
            label: str_id,
            opened: None,
            flags: WindowFlags::empty(),
        }
    }

    /// Close a popup. Should be called within the closure given as argument to
    /// [`Ui::popup`] or [`Ui::modal_popup`].
    #[doc(alias = "CloseCurrentPopup")]
    pub fn close_current_popup(&self) {
        unsafe { sys::igCloseCurrentPopup() };
    }

    /// Open and begin popup when clicked with the right mouse button on last item.
    ///
    /// This does not take a label, which means that multiple calls **in a row** will use the same label, which
    /// is based on the last node which had a label. Text and other non-interactive elements generally don't have
    /// ids, so you'll need to use [begin_popup_context_with_label](Self::begin_popup_context_with_label) for them.
    #[doc(alias = "BeginPopupContextItem")]
    pub fn begin_popup_context_item(&self) -> Option<PopupToken<'_>> {
        let render = unsafe {
            sys::igBeginPopupContextItem(
                std::ptr::null(),
                imgui_sys::ImGuiPopupFlags_MouseButtonRight as i32,
            )
        };

        if render {
            Some(PopupToken::new(self))
        } else {
            None
        }
    }

    /// Open and begin popup when clicked with the right mouse button on the given item with a dedicated label.
    ///
    /// If you want to use the label of the previous popup (outside of `Text` and other non-interactive cases, that
    /// is the more normal case), use [begin_popup_context_item](Self::begin_popup_context_item).
    #[doc(alias = "BeginPopupContextItem")]
    pub fn begin_popup_context_with_label<Label: AsRef<str>>(
        &self,
        str_id: Label,
    ) -> Option<PopupToken<'_>> {
        let render = unsafe {
            sys::igBeginPopupContextItem(
                self.scratch_txt(str_id),
                imgui_sys::ImGuiPopupFlags_MouseButtonRight as i32,
            )
        };

        if render {
            Some(PopupToken::new(self))
        } else {
            None
        }
    }

    /// Open and begin popup when clicked on current window.
    ///
    /// This does not take a label, which means that multiple calls will use the same provided label.
    /// If you want an explicit label, such as having two different kinds of windows popups in your program,
    /// use [begin_popup_context_window_with_label](Self::begin_popup_context_window_with_label).
    #[doc(alias = "BeginPopupContextWindow")]
    pub fn begin_popup_context_window(&self) -> Option<PopupToken<'_>> {
        let render = unsafe {
            sys::igBeginPopupContextWindow(
                std::ptr::null(),
                imgui_sys::ImGuiPopupFlags_MouseButtonRight as i32,
            )
        };

        if render {
            Some(PopupToken::new(self))
        } else {
            None
        }
    }

    /// Open and begin popup when clicked on current window.
    ///
    /// This takes a label explicitly. This is useful when multiple code
    /// locations may want to manipulate/open the same popup, given an explicit id.
    #[doc(alias = "BeginPopupContextWindow")]
    pub fn begin_popup_context_window_with_label<Label: AsRef<str>>(
        &self,
        str_id: Label,
    ) -> Option<PopupToken<'_>> {
        let render = unsafe {
            sys::igBeginPopupContextWindow(
                self.scratch_txt(str_id),
                imgui_sys::ImGuiPopupFlags_MouseButtonRight as i32,
            )
        };

        if render {
            Some(PopupToken::new(self))
        } else {
            None
        }
    }

    /// Open and begin popup when right clicked in void (where there are no windows).
    ///
    /// This does not take a label, which means that multiple calls will use the same provided label.
    /// If you want an explicit label, such as having two different kinds of void popups in your program,
    /// use [begin_popup_context_void_with_label](Self::begin_popup_context_void_with_label).
    #[doc(alias = "BeginPopupContextWindow")]
    pub fn begin_popup_context_void(&self) -> Option<PopupToken<'_>> {
        let render = unsafe {
            sys::igBeginPopupContextVoid(
                std::ptr::null(),
                imgui_sys::ImGuiPopupFlags_MouseButtonRight as i32,
            )
        };

        if render {
            Some(PopupToken::new(self))
        } else {
            None
        }
    }

    /// Open and begin popup when right clicked in void (where there are no windows).
    ///
    /// This takes a label explicitly. This is useful when multiple code
    /// locations may want to manipulate/open the same popup, given an explicit id.
    #[doc(alias = "BeginPopupContextVoid")]
    pub fn begin_popup_context_void_with_label<Label: AsRef<str>>(
        &self,
        str_id: Label,
    ) -> Option<PopupToken<'_>> {
        let render = unsafe {
            sys::igBeginPopupContextVoid(
                self.scratch_txt(str_id),
                imgui_sys::ImGuiPopupFlags_MouseButtonRight as i32,
            )
        };

        if render {
            Some(PopupToken::new(self))
        } else {
            None
        }
    }
}

create_token!(
    /// Tracks a popup token that can be ended with `end` or by dropping.
    pub struct PopupToken<'ui>;

    /// Drops the popup token manually. You can also just allow this token
    /// to drop on its own.
    drop { sys::igEndPopup() }
);