Merge pull request #603 from dbr/uipopup

Make ui.popup* methods consistent
This commit is contained in:
dbr/Ben 2022-01-24 18:06:14 +11:00 committed by GitHub
commit 18ded1a58a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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..") {
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();

View File

@ -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<str>> PopupModal<'p, Label> {
pub fn new(label: Label) -> Self {
impl<'ui, 'p, Label: AsRef<str>> 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<str>> 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, F: FnOnce() -> T>(self, ui: &Ui, f: F) -> Option<T> {
self.begin_popup(ui).map(|_popup| f())
pub fn build<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
self.begin_popup().map(|_popup| f())
}
/// 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`]
/// should be called *once* when you want to actual create the popup.
#[doc(alias = "BeginPopupModal")]
pub fn begin_popup(self, ui: &Ui) -> Option<PopupToken<'_>> {
pub fn begin_popup(self) -> Option<PopupToken<'ui>> {
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<str>> 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<str>>(&self, str_id: Label) -> PopupModal<'p, Label> {
PopupModal::new(str_id)
pub fn popup_modal<Label: AsRef<str>>(&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