From d49bd73881087436737283c2c06c39206e0e9c79 Mon Sep 17 00:00:00 2001 From: pml68 Date: Sun, 13 Apr 2025 02:45:46 +0200 Subject: feat: add `backdrop` method to dialog --- src/dialog.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'src/dialog.rs') diff --git a/src/dialog.rs b/src/dialog.rs index c128f8a..17cb822 100644 --- a/src/dialog.rs +++ b/src/dialog.rs @@ -1,7 +1,7 @@ //! Dialogs can be used to provide users with //! important information and make them act on it. use iced_core::{ - self as core, Alignment, Element, Length, Padding, Pixels, + self as core, Alignment, Color, Element, Length, Padding, Pixels, alignment::Vertical, color, }; use iced_widget::{ @@ -31,6 +31,7 @@ where spacing: f32, padding: Padding, button_alignment: Alignment, + backdrop_color: Box Color + 'a>, title_class: ::Class<'a>, container_class: ::Class<'a>, } @@ -71,6 +72,7 @@ where spacing: 8.0, padding: 24.into(), button_alignment: Alignment::Start, + backdrop_color: Box::new(|_theme| color!(0x000000, 0.3)), title_class: ::default_title(), container_class: ::default_container(), } @@ -149,6 +151,12 @@ where buttons.into_iter().fold(self, Self::push_button) } + /// Sets the coloring method for the [`Dialog`]'s backdrop. + pub fn backdrop(mut self, color: impl Fn(&Theme) -> Color + 'a) -> Self { + self.backdrop_color = Box::new(color); + self + } + /// Sets the style of the [`Dialog`]'s title. #[must_use] pub fn title_style( @@ -218,7 +226,7 @@ where .class(self.container_class) .clip(true); - modal(self.base, dialog) + modal(self.base, dialog, self.backdrop_color) } else { self.base } @@ -242,6 +250,7 @@ where fn modal<'a, Message, Theme, Renderer>( base: impl Into>, content: impl Into>, + backdrop_color: impl Fn(&Theme) -> Color + 'a, ) -> Element<'a, Message, Theme, Renderer> where Message: 'a + Clone, @@ -249,11 +258,12 @@ where Theme: 'a + container::Catalog, Theme::Class<'a>: From>, { - let area = - mouse_area(center(opaque(content)).style(|_theme| container::Style { - background: Some(color!(0x000000, 0.3).into()), + let area = mouse_area(center(opaque(content)).style(move |theme| { + container::Style { + background: Some(backdrop_color(theme).into()), ..Default::default() - })); + } + })); stack![base.into(), opaque(area)].into() } -- cgit v1.2.3