diff options
| author | pml68 <contact@pml68.dev> | 2025-07-06 01:05:25 +0200 |
|---|---|---|
| committer | pml68 <contact@pml68.dev> | 2025-07-06 01:05:25 +0200 |
| commit | d2770a2e277c32e62fb3d79989d224dce332e237 (patch) | |
| tree | 3bd7535ccf8027267b51975832ce4c6c12f2fa61 | |
| parent | feat: `Dialog::padding` -> `padding_inner`, add `padding_outer` method (diff) | |
| download | iced_dialog-d2770a2e277c32e62fb3d79989d224dce332e237.tar.gz | |
refactor: simplify `view`, inline `modal`
| -rw-r--r-- | src/dialog.rs | 79 |
1 files changed, 26 insertions, 53 deletions
diff --git a/src/dialog.rs b/src/dialog.rs index cd9b1b5..857fa90 100644 --- a/src/dialog.rs +++ b/src/dialog.rs @@ -429,7 +429,7 @@ where }, ); - Container::new( + let content = Container::new( Column::new() .push(contents) .push_maybe(has_buttons.then_some(vertical_space())) @@ -440,18 +440,33 @@ where .max_width(max_width) .max_height(max_height) .class(self.container_class) - .clip(true) + .clip(true); + + let backdrop = mouse_area( + container(opaque(content)) + .style(move |theme| container::Style { + background: Some( + Catalog::style(theme, &self.class) + .backdrop_color + .into(), + ), + ..Default::default() + }) + .padding(self.padding_outer) + .width(Length::Fill) + .height(Length::Fill) + .align_x(self.horizontal_alignment) + .align_y(self.vertical_alignment), + ); + + if let Some(on_press) = self.on_press { + opaque(backdrop.on_press(on_press())) + } else { + opaque(backdrop) + } }); - modal( - self.base, - dialog, - self.on_press, - self.horizontal_alignment, - self.vertical_alignment, - self.padding_outer, - self.class, - ) + stack![self.base].push_maybe(dialog).into() } } @@ -469,48 +484,6 @@ 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>, - horizontal_alignment: alignment::Horizontal, - vertical_alignment: alignment::Vertical, - padding: Padding, - class: <Theme as Catalog>::Class<'a>, -) -> Element<'a, Message, Theme, Renderer> -where - Message: 'a + Clone, - Renderer: 'a + core::Renderer, - Theme: 'a + container::Catalog + Catalog, - <Theme as container::Catalog>::Class<'a>: - From<container::StyleFn<'a, Theme>>, -{ - let area = content.map(|content| { - let backdrop = mouse_area( - container(opaque(content)) - .style(move |theme| container::Style { - background: Some( - Catalog::style(theme, &class).backdrop_color.into(), - ), - ..Default::default() - }) - .padding(padding) - .width(Length::Fill) - .height(Length::Fill) - .align_x(horizontal_alignment) - .align_y(vertical_alignment), - ); - - if let Some(on_press) = on_press { - opaque(backdrop.on_press(on_press())) - } else { - opaque(backdrop) - } - }); - - stack![base.into()].push_maybe(area).into() -} - /// The style of a [`Dialog`]. #[derive(Debug, Clone, Copy, PartialEq)] pub struct Style { |
