Make ui.popup* methods consistent [#454]

Closes #592
This commit is contained in:
dbr 2022-01-17 12:51:55 +11:00
parent 35342a0c82
commit dd061d4ad4
2 changed files with 18 additions and 14 deletions

View File

@ -773,7 +773,7 @@ CTRL+click on individual component to input value.\n",
if ui.button("Delete..") { if ui.button("Delete..") {
ui.open_popup("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.text("All those beautiful files will be deleted.\nThis operation cannot be undone!\n\n");
ui.separator(); ui.separator();
let style = ui.push_style_var(StyleVar::FramePadding([0.0, 0.0])); 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..") { if ui.button("Stacked modals..") {
ui.open_popup("Stacked 1"); ui.open_popup("Stacked 1");
} }
PopupModal::new("Stacked 1").build(ui, || { ui.popup_modal("Stacked 1").build(|| {
ui.text( ui.text(
"Hello from Stacked The First\n\ "Hello from Stacked The First\n\
Using style[StyleColor::ModalWindowDarkening] for darkening." 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..") { if ui.button("Add another modal..") {
ui.open_popup("Stacked 2") ; ui.open_popup("Stacked 2") ;
} }
PopupModal::new("Stacked 2").build(ui, || { ui.popup_modal("Stacked 2").build(|| {
ui.text("Hello from Stacked The Second"); ui.text("Hello from Stacked The Second");
if ui.button("Close") { if ui.button("Close") {
ui.close_current_popup(); ui.close_current_popup();

View File

@ -14,7 +14,7 @@ use crate::Ui;
/// if ui.button(im_str!("Show modal")) { /// if ui.button(im_str!("Show modal")) {
/// ui.open_popup(im_str!("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"); /// ui.text("Content of my modal");
/// if ui.button(im_str!("OK")) { /// if ui.button(im_str!("OK")) {
/// ui.close_current_popup(); /// ui.close_current_popup();
@ -22,15 +22,18 @@ use crate::Ui;
/// }; /// };
/// ``` /// ```
#[must_use] #[must_use]
pub struct PopupModal<'p, Label> { pub struct PopupModal<'ui, 'p, Label> {
ui: &'ui Ui,
label: Label, label: Label,
opened: Option<&'p mut bool>, opened: Option<&'p mut bool>,
flags: WindowFlags, flags: WindowFlags,
} }
impl<'p, Label: AsRef<str>> PopupModal<'p, Label> { impl<'ui, 'p, Label: AsRef<str>> PopupModal<'ui, 'p, Label> {
pub fn new(label: Label) -> Self { #[deprecated(since = "0.9.0", note = "Use `ui.popup_modal(...)` instead")]
pub fn new(ui: &'ui Ui, label: Label) -> Self {
PopupModal { PopupModal {
ui,
label, label,
opened: None, opened: None,
flags: WindowFlags::empty(), flags: WindowFlags::empty(),
@ -118,8 +121,8 @@ impl<'p, Label: AsRef<str>> PopupModal<'p, Label> {
/// Consume and draw the PopupModal. /// Consume and draw the PopupModal.
/// Returns the result of the closure, if it is called. /// Returns the result of the closure, if it is called.
#[doc(alias = "BeginPopupModal")] #[doc(alias = "BeginPopupModal")]
pub fn build<T, F: FnOnce() -> T>(self, ui: &Ui, f: F) -> Option<T> { pub fn build<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
self.begin_popup(ui).map(|_popup| f()) self.begin_popup().map(|_popup| f())
} }
/// Consume and draw the PopupModal. /// Consume and draw the PopupModal.
@ -128,10 +131,10 @@ impl<'p, Label: AsRef<str>> PopupModal<'p, Label> {
/// This should be called *per frame*, whereas [`Ui::open_popup`] /// This should be called *per frame*, whereas [`Ui::open_popup`]
/// should be called *once* when you want to actual create the popup. /// should be called *once* when you want to actual create the popup.
#[doc(alias = "BeginPopupModal")] #[doc(alias = "BeginPopupModal")]
pub fn begin_popup(self, ui: &Ui) -> Option<PopupToken<'_>> { pub fn begin_popup(self) -> Option<PopupToken<'ui>> {
let render = unsafe { let render = unsafe {
sys::igBeginPopupModal( sys::igBeginPopupModal(
ui.scratch_txt(self.label), self.ui.scratch_txt(self.label),
self.opened self.opened
.map(|x| x as *mut bool) .map(|x| x as *mut bool)
.unwrap_or(ptr::null_mut()), .unwrap_or(ptr::null_mut()),
@ -140,7 +143,7 @@ impl<'p, Label: AsRef<str>> PopupModal<'p, Label> {
}; };
if render { if render {
Some(PopupToken::new(ui)) Some(PopupToken::new(self.ui))
} else { } else {
None None
} }
@ -192,8 +195,9 @@ impl Ui {
} }
/// Creates a PopupModal directly. /// Creates a PopupModal directly.
pub fn popup_modal<'p, Label: AsRef<str>>(&self, str_id: Label) -> PopupModal<'p, Label> { pub fn popup_modal<'ui, 'p, Label: AsRef<str>>(&self, str_id: Label) -> PopupModal<'_, '_, Label> {
PopupModal::new(str_id) #[allow(deprecated)]
PopupModal::new(self, str_id)
} }
/// Close a popup. Should be called within the closure given as argument to /// Close a popup. Should be called within the closure given as argument to