From 8ef4e2eb17c17a35e75957319e9e6e70b0140f03 Mon Sep 17 00:00:00 2001 From: Floyd Wang Date: Mon, 14 Apr 2025 16:56:16 +0800 Subject: [PATCH] modal: Respond confirm events only when `on_ok` or `footer` present (#786) --- crates/ui/src/modal.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/crates/ui/src/modal.rs b/crates/ui/src/modal.rs index a2c41738..d7a6e651 100644 --- a/crates/ui/src/modal.rs +++ b/crates/ui/src/modal.rs @@ -83,7 +83,7 @@ pub struct Modal { margin_top: Option, on_close: Rc, - on_ok: Rc bool + 'static>, + on_ok: Option bool + 'static>>, on_cancel: Rc bool + 'static>, button_props: ModalButtonProps, show_close: bool, @@ -137,7 +137,7 @@ impl Modal { layer_ix: 0, overlay_visible: true, on_close: Rc::new(|_, _, _| {}), - on_ok: Rc::new(|_, _, _| true), + on_ok: None, on_cancel: Rc::new(|_, _, _| true), button_props: ModalButtonProps::default(), show_close: true, @@ -202,7 +202,7 @@ impl Modal { mut self, on_ok: impl Fn(&ClickEvent, &mut Window, &mut App) -> bool + 'static, ) -> Self { - self.on_ok = Rc::new(on_ok); + self.on_ok = Some(Rc::new(on_ok)); self } @@ -302,9 +302,11 @@ impl RenderOnce for Modal { let on_close = on_close.clone(); move |_, window, cx| { - if on_ok(&ClickEvent::default(), window, cx) { - on_close(&ClickEvent::default(), window, cx); - window.close_modal(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); + } } } }) @@ -399,9 +401,14 @@ impl RenderOnce for Modal { .on_action({ let on_ok = on_ok.clone(); let on_close = on_close.clone(); + let has_footer = self.footer.is_some(); move |_: &Confirm, window, cx| { - if on_ok(&ClickEvent::default(), window, cx) { - on_close(&ClickEvent::default(), 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); + } + } else if has_footer { window.close_modal(cx); } }