From f4e8b952e923be9cd91ba2da87d8b6785a11012b Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Fri, 2 May 2025 10:28:26 +0800 Subject: [PATCH] modal: Add alert method to create modal with a Ok button. (#829) - Fix to handle close modal when `on_ok`, `on_cancel` not present. --- crates/story/src/modal_story.rs | 18 ++++++++++++++++++ crates/ui/src/modal.rs | 31 ++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/crates/story/src/modal_story.rs b/crates/story/src/modal_story.rs index 56c85131..2c4bad93 100644 --- a/crates/story/src/modal_story.rs +++ b/crates/story/src/modal_story.rs @@ -327,6 +327,24 @@ impl Render for ModalStory { }); })), ), + ) + .child( + section("Alert Modal").child( + Button::new("alert-modal") + .label("Alert") + .on_click(cx.listener(|_, _, window, cx| { + window.open_modal(cx, |modal, _, _| { + modal + .confirm() + .child("You are successfully logged in.") + .alert() + .on_close(|_, window, cx| { + window + .push_notification("You have pressed Ok.", cx); + }) + }); + })), + ), ), ) } diff --git a/crates/ui/src/modal.rs b/crates/ui/src/modal.rs index d7a6e651..c8e20c7d 100644 --- a/crates/ui/src/modal.rs +++ b/crates/ui/src/modal.rs @@ -173,13 +173,24 @@ impl Modal { self } - /// Set to use confirm modal, with OK and CANCEL buttons. + /// Set to use confirm modal, with OK and Cancel buttons. + /// + /// See also [`Self::alert`] pub fn confirm(self) -> Self { self.footer(|ok, cancel, window, cx| vec![cancel(window, cx), ok(window, cx)]) .overlay_closable(false) .show_close(false) } + /// Set to as a alter modal, with OK button. + /// + /// See also [`Self::confirm`] + pub fn alert(self) -> Self { + self.footer(|ok, _, window, cx| vec![ok(window, cx)]) + .overlay_closable(false) + .show_close(false) + } + /// Set the button props of the modal. pub fn button_props(mut self, button_props: ModalButtonProps) -> Self { self.button_props = button_props; @@ -187,6 +198,8 @@ impl Modal { } /// Sets the callback for when the modal is closed. + /// + /// Called after [`Self::on_ok`] or [`Self::on_cancel`] callback. pub fn on_close( mut self, on_close: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static, @@ -303,11 +316,13 @@ impl RenderOnce for Modal { move |_, window, cx| { if let Some(on_ok) = &on_ok { - if on_ok(&ClickEvent::default(), window, cx) { - on_close(&ClickEvent::default(), window, cx); - window.close_modal(cx); + if !on_ok(&ClickEvent::default(), window, cx) { + return; } } + + on_close(&ClickEvent::default(), window, cx); + window.close_modal(cx); } }) .into_any_element() @@ -329,10 +344,12 @@ impl RenderOnce for Modal { let on_cancel = on_cancel.clone(); let on_close = on_close.clone(); move |_, window, cx| { - if on_cancel(&ClickEvent::default(), window, cx) { - on_close(&ClickEvent::default(), window, cx); - window.close_modal(cx); + if !on_cancel(&ClickEvent::default(), window, cx) { + return; } + + on_close(&ClickEvent::default(), window, cx); + window.close_modal(cx); } }) .into_any_element()