aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpml68 <contact@pml68.dev>2025-07-05 09:14:58 +0200
committerpml68 <contact@pml68.dev>2025-07-05 09:14:58 +0200
commit718d2ddb7b3e8179e36ef4881e94d7facbd17b28 (patch)
tree40987731a9d81f5a00ae885a9031a6e6e914db21
parentfeat: "smarter" sizing for the dialog (diff)
downloadiced_dialog-718d2ddb7b3e8179e36ef4881e94d7facbd17b28.tar.gz
feat: add `on_press` and `on_press_with` methods for backdrop clicks
-rw-r--r--README.md1
-rw-r--r--examples/save.rs1
-rw-r--r--src/dialog.rs49
3 files changed, 42 insertions, 9 deletions
diff --git a/README.md b/README.md
index cdccd3c..87c9302 100644
--- a/README.md
+++ b/README.md
@@ -65,6 +65,7 @@ impl State {
.push_button(iced_dialog::button("Cancel", Message::Cancelled))
.width(350)
.height(234)
+ .on_press(Message::Cancelled)
.into()
}
}
diff --git a/examples/save.rs b/examples/save.rs
index a7c73a2..6eb31b6 100644
--- a/examples/save.rs
+++ b/examples/save.rs
@@ -57,6 +57,7 @@ impl State {
.push_button(iced_dialog::button("Cancel", Message::Cancelled))
.width(350)
.height(234)
+ .on_press(Message::Cancelled)
.into()
}
}
diff --git a/src/dialog.rs b/src/dialog.rs
index fd46bc2..0d3bbd5 100644
--- a/src/dialog.rs
+++ b/src/dialog.rs
@@ -42,6 +42,7 @@ pub struct Dialog<
title: Option<Fragment<'a>>,
content: Element<'a, Message, Theme, Renderer>,
buttons: Vec<Element<'a, Message, Theme, Renderer>>,
+ on_press: Option<Box<dyn Fn() -> Message + 'a>>,
font: Option<Renderer::Font>,
width: Length,
height: Length,
@@ -96,6 +97,7 @@ where
title: None,
content,
buttons,
+ on_press: None,
font: None,
width: size.width.fluid(),
height: size.height.fluid(),
@@ -116,6 +118,27 @@ where
self
}
+ /// Sets the message that will be produced when the [`Dialog`]'s backdrop is pressed.
+ pub fn on_press(mut self, on_press: Message) -> Self
+ where
+ Message: Clone,
+ {
+ self.on_press = Some(Box::new(move || on_press.clone()));
+ self
+ }
+
+ /// Sets the message that will be produced when the [`Dialog`]'s backdrop is pressed.
+ ///
+ /// This is analogous to [`Dialog::on_press`], but using a closure to produce
+ /// the message.
+ pub fn on_press_with(
+ mut self,
+ on_press: impl Fn() -> Message + 'a,
+ ) -> Self {
+ self.on_press = Some(Box::new(on_press));
+ self
+ }
+
/// Sets the [`Dialog`]'s width.
pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width.into();
@@ -349,7 +372,7 @@ where
.clip(true)
});
- modal(self.base, dialog, self.class)
+ modal(self.base, dialog, self.on_press, self.class)
}
}
@@ -370,6 +393,7 @@ where
fn modal<'a, Message, Theme, Renderer>(
base: impl Into<Element<'a, Message, Theme, Renderer>>,
content: Option<impl Into<Element<'a, Message, Theme, Renderer>>>,
+ on_press: Option<impl Fn() -> Message + 'a>,
class: <Theme as Catalog>::Class<'a>,
) -> Element<'a, Message, Theme, Renderer>
where
@@ -380,14 +404,21 @@ where
From<container::StyleFn<'a, Theme>>,
{
let area = content.map(|content| {
- opaque(mouse_area(center(opaque(content)).style(move |theme| {
- container::Style {
- background: Some(
- Catalog::style(theme, &class).backdrop_color.into(),
- ),
- ..Default::default()
- }
- })))
+ let backdrop =
+ mouse_area(center(opaque(content)).style(move |theme| {
+ container::Style {
+ background: Some(
+ Catalog::style(theme, &class).backdrop_color.into(),
+ ),
+ ..Default::default()
+ }
+ }));
+
+ if let Some(on_press) = on_press {
+ opaque(backdrop.on_press(on_press()))
+ } else {
+ opaque(backdrop)
+ }
});
stack![base.into()].push_maybe(area).into()