From dd061d4ad45016d6db06e3e3920e0d40fa35bd12 Mon Sep 17 00:00:00 2001 From: dbr Date: Mon, 17 Jan 2022 12:51:55 +1100 Subject: [PATCH 1/4] Make ui.popup* methods consistent [#454] Closes #592 --- imgui-examples/examples/test_window_impl.rs | 6 ++--- imgui/src/popups.rs | 26 ++++++++++++--------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/imgui-examples/examples/test_window_impl.rs b/imgui-examples/examples/test_window_impl.rs index d9fac4d..b10bacd 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?"); } - PopupModal::new("Delete?").always_auto_resize(true).build(ui, || { + ui.popup_modal("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"); } - PopupModal::new("Stacked 1").build(ui, || { + ui.popup_modal("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") ; } - PopupModal::new("Stacked 2").build(ui, || { + ui.popup_modal("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 d4185c5..c44d42c 100644 --- a/imgui/src/popups.rs +++ b/imgui/src/popups.rs @@ -14,7 +14,7 @@ use crate::Ui; /// if ui.button(im_str!("Show modal")) { /// ui.open_popup(im_str!("modal")); /// } -/// if let Some(_token) = PopupModal::new(im_str!("modal")).begin_popup(&ui) { +/// if let Some(_token) = ui.popup_modal("modal").begin_popup() { /// ui.text("Content of my modal"); /// if ui.button(im_str!("OK")) { /// ui.close_current_popup(); @@ -22,15 +22,18 @@ use crate::Ui; /// }; /// ``` #[must_use] -pub struct PopupModal<'p, Label> { +pub struct PopupModal<'ui, 'p, Label> { + ui: &'ui Ui, label: Label, opened: Option<&'p mut bool>, flags: WindowFlags, } -impl<'p, Label: AsRef> PopupModal<'p, Label> { - pub fn new(label: Label) -> Self { +impl<'ui, 'p, Label: AsRef> PopupModal<'ui, 'p, Label> { + #[deprecated(since = "0.9.0", note = "Use `ui.popup_modal(...)` instead")] + pub fn new(ui: &'ui Ui, label: Label) -> Self { PopupModal { + ui, label, opened: None, flags: WindowFlags::empty(), @@ -118,8 +121,8 @@ impl<'p, Label: AsRef> PopupModal<'p, Label> { /// Consume and draw the PopupModal. /// Returns the result of the closure, if it is called. #[doc(alias = "BeginPopupModal")] - pub fn build T>(self, ui: &Ui, f: F) -> Option { - self.begin_popup(ui).map(|_popup| f()) + pub fn build T>(self, f: F) -> Option { + self.begin_popup().map(|_popup| f()) } /// Consume and draw the PopupModal. @@ -128,10 +131,10 @@ impl<'p, Label: AsRef> PopupModal<'p, Label> { /// 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, ui: &Ui) -> Option> { + pub fn begin_popup(self) -> Option> { let render = unsafe { sys::igBeginPopupModal( - ui.scratch_txt(self.label), + self.ui.scratch_txt(self.label), self.opened .map(|x| x as *mut bool) .unwrap_or(ptr::null_mut()), @@ -140,7 +143,7 @@ impl<'p, Label: AsRef> PopupModal<'p, Label> { }; if render { - Some(PopupToken::new(ui)) + Some(PopupToken::new(self.ui)) } else { None } @@ -192,8 +195,9 @@ impl Ui { } /// Creates a PopupModal directly. - pub fn popup_modal<'p, Label: AsRef>(&self, str_id: Label) -> PopupModal<'p, Label> { - PopupModal::new(str_id) + pub fn popup_modal<'ui, 'p, Label: AsRef>(&self, str_id: Label) -> PopupModal<'_, '_, Label> { + #[allow(deprecated)] + PopupModal::new(self, str_id) } /// Close a popup. Should be called within the closure given as argument to From 10eba1c25e461542864838c315b084a7a9457255 Mon Sep 17 00:00:00 2001 From: dbr Date: Mon, 17 Jan 2022 13:09:18 +1100 Subject: [PATCH 2/4] fmt --- imgui/src/popups.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/imgui/src/popups.rs b/imgui/src/popups.rs index c44d42c..fde191b 100644 --- a/imgui/src/popups.rs +++ b/imgui/src/popups.rs @@ -195,7 +195,10 @@ impl Ui { } /// Creates a PopupModal directly. - pub fn popup_modal<'ui, 'p, Label: AsRef>(&self, str_id: Label) -> PopupModal<'_, '_, Label> { + pub fn popup_modal<'ui, 'p, Label: AsRef>( + &self, + str_id: Label, + ) -> PopupModal<'_, '_, Label> { #[allow(deprecated)] PopupModal::new(self, str_id) } From 4bbbc459f16c1c3aacf8bcc5c78397446222e341 Mon Sep 17 00:00:00 2001 From: dbr Date: Mon, 17 Jan 2022 14:35:07 +1100 Subject: [PATCH 3/4] clippy yelling --- imgui/src/popups.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imgui/src/popups.rs b/imgui/src/popups.rs index fde191b..75d8e49 100644 --- a/imgui/src/popups.rs +++ b/imgui/src/popups.rs @@ -195,7 +195,7 @@ impl Ui { } /// Creates a PopupModal directly. - pub fn popup_modal<'ui, 'p, Label: AsRef>( + pub fn popup_modal>( &self, str_id: Label, ) -> PopupModal<'_, '_, Label> { From 45c7e2dd922d613c886a3118094590623bbd665c Mon Sep 17 00:00:00 2001 From: dbr Date: Mon, 17 Jan 2022 14:47:17 +1100 Subject: [PATCH 4/4] ...and once more --- imgui/src/popups.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/imgui/src/popups.rs b/imgui/src/popups.rs index 75d8e49..50c4a75 100644 --- a/imgui/src/popups.rs +++ b/imgui/src/popups.rs @@ -195,10 +195,7 @@ impl Ui { } /// Creates a PopupModal directly. - pub fn popup_modal>( - &self, - str_id: Label, - ) -> PopupModal<'_, '_, Label> { + pub fn popup_modal>(&self, str_id: Label) -> PopupModal<'_, '_, Label> { #[allow(deprecated)] PopupModal::new(self, str_id) }