From 5fdc2df9cdf6cb001b28e7f655c4e9e30678f7b1 Mon Sep 17 00:00:00 2001 From: wiiznokes <78230769+wiiznokes@users.noreply.github.com> Date: Thu, 7 Nov 2024 18:10:41 +0100 Subject: [PATCH] feat: optional title on dialog --- src/widget/dialog.rs | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/widget/dialog.rs b/src/widget/dialog.rs index 014b738b..0ee5dcb1 100644 --- a/src/widget/dialog.rs +++ b/src/widget/dialog.rs @@ -1,12 +1,12 @@ use crate::{iced::Length, style, theme, widget, Element}; use std::borrow::Cow; -pub fn dialog<'a, Message>(title: impl Into>) -> Dialog<'a, Message> { - Dialog::new(title) +pub fn dialog<'a, Message>() -> Dialog<'a, Message> { + Dialog::new() } pub struct Dialog<'a, Message> { - title: Cow<'a, str>, + title: Option>, icon: Option>, body: Option>, controls: Vec>, @@ -16,9 +16,9 @@ pub struct Dialog<'a, Message> { } impl<'a, Message> Dialog<'a, Message> { - pub fn new(title: impl Into>) -> Self { + pub fn new() -> Self { Self { - title: title.into(), + title: None, icon: None, body: None, controls: Vec::new(), @@ -28,6 +28,11 @@ impl<'a, Message> Dialog<'a, Message> { } } + pub fn title(mut self, title: impl Into>) -> Self { + self.title = Some(title.into()); + self + } + pub fn icon(mut self, icon: impl Into>) -> Self { self.icon = Some(icon.into()); self @@ -70,16 +75,28 @@ impl<'a, Message: Clone + 'static> From> for Element<'a, Mes } = theme::THEME.lock().unwrap().cosmic().spacing; let mut content_col = widget::column::with_capacity(3 + dialog.controls.len() * 2); - content_col = content_col.push(widget::text::title3(dialog.title)); + + let mut should_space = false; + + if let Some(title) = dialog.title { + content_col = content_col.push(widget::text::title3(title)); + should_space = true; + } if let Some(body) = dialog.body { - content_col = - content_col.push(widget::vertical_space().height(Length::Fixed(space_xxs.into()))); + if should_space { + content_col = content_col + .push(widget::vertical_space().height(Length::Fixed(space_xxs.into()))); + } content_col = content_col.push(widget::text::body(body)); + should_space = true; } for control in dialog.controls { - content_col = - content_col.push(widget::vertical_space().height(Length::Fixed(space_s.into()))); + if should_space { + content_col = content_col + .push(widget::vertical_space().height(Length::Fixed(space_s.into()))); + } content_col = content_col.push(control); + should_space = true; } let mut content_row = widget::row::with_capacity(2).spacing(space_s);