diff --git a/imgui-examples/examples/test_window_impl.rs b/imgui-examples/examples/test_window_impl.rs index 43fff99..aa204a7 100644 --- a/imgui-examples/examples/test_window_impl.rs +++ b/imgui-examples/examples/test_window_impl.rs @@ -773,7 +773,7 @@ CTRL+click on individual component to input value.\n", if ui.button("Delete..") { ui.open_popup("Delete?"); } - ui.popup_modal("Delete?").always_auto_resize(true).build(|| { + ui.modal_popup_config("Delete?").always_auto_resize(true).build(|| { ui.text("All those beautiful files will be deleted.\nThis operation cannot be undone!\n\n"); ui.separator(); let style = ui.push_style_var(StyleVar::FramePadding([0.0, 0.0])); @@ -792,7 +792,7 @@ CTRL+click on individual component to input value.\n", if ui.button("Stacked modals..") { ui.open_popup("Stacked 1"); } - ui.popup_modal("Stacked 1").build(|| { + ui.modal_popup_config("Stacked 1").build(|| { ui.text( "Hello from Stacked The First\n\ Using style[StyleColor::ModalWindowDarkening] for darkening." @@ -806,7 +806,7 @@ CTRL+click on individual component to input value.\n", if ui.button("Add another modal..") { ui.open_popup("Stacked 2") ; } - ui.popup_modal("Stacked 2").build(|| { + ui.modal_popup_config("Stacked 2").build(|| { ui.text("Hello from Stacked The Second"); if ui.button("Close") { ui.close_current_popup(); diff --git a/imgui/src/popups.rs b/imgui/src/popups.rs index 50c4a75..2e4cb9f 100644 --- a/imgui/src/popups.rs +++ b/imgui/src/popups.rs @@ -30,7 +30,7 @@ pub struct PopupModal<'ui, 'p, Label> { } impl<'ui, 'p, Label: AsRef> PopupModal<'ui, 'p, Label> { - #[deprecated(since = "0.9.0", note = "Use `ui.popup_modal(...)` instead")] + #[deprecated(since = "0.9.0", note = "Use `ui.modal_popup_config(...)` instead")] pub fn new(ui: &'ui Ui, label: Label) -> Self { PopupModal { ui, @@ -152,12 +152,16 @@ impl<'ui, 'p, Label: AsRef> PopupModal<'ui, 'p, Label> { // Widgets: Popups impl Ui { - /// Instructs ImGui to open a popup, which must be began with either [`begin_popup`](Self::begin_popup) - /// or [`popup`](Self::popup). You also use this function to begin [PopupModal]. + /// Instructs ImGui that a popup is open. /// - /// The confusing aspect to popups is that ImGui holds "control" over the popup fundamentally, so that ImGui - /// can also force close a popup when a user clicks outside a popup. If you do not want users to be - /// able to close a popup without selected an option, use [`PopupModal`]. + /// 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) { unsafe { sys::igOpenPopupStr(self.scratch_txt(str_id), 0) }; @@ -166,7 +170,7 @@ impl Ui { /// 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* - /// when you want to actual create the popup. + /// to signal that this popup is active. #[doc(alias = "BeginPopup")] pub fn begin_popup(&self, str_id: impl AsRef) -> Option> { let render = unsafe { @@ -183,7 +187,7 @@ impl Ui { /// 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* - /// when you want to actual create the popup. + /// to signal that this popup is active. #[doc(alias = "BeginPopup")] pub fn popup(&self, str_id: impl AsRef, f: F) where @@ -194,10 +198,47 @@ impl Ui { } } - /// Creates a PopupModal directly. - pub fn popup_modal>(&self, str_id: Label) -> PopupModal<'_, '_, Label> { - #[allow(deprecated)] - PopupModal::new(self, str_id) + /// 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(&self, str_id: Label, f: Func) -> Option + where + Label: AsRef, + 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>(&self, str_id: Label) -> Option> { + PopupModal { + ui: self, + label: str_id, + opened: None, + flags: WindowFlags::empty(), + } + .begin_popup() + } + + /// Creates a [PopupModal] builder. + pub fn modal_popup_config>( + &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