feat: optional title on dialog

This commit is contained in:
wiiznokes 2024-11-07 18:10:41 +01:00 committed by Michael Murphy
parent 707f2115eb
commit 5fdc2df9cd

View file

@ -1,12 +1,12 @@
use crate::{iced::Length, style, theme, widget, Element}; use crate::{iced::Length, style, theme, widget, Element};
use std::borrow::Cow; use std::borrow::Cow;
pub fn dialog<'a, Message>(title: impl Into<Cow<'a, str>>) -> Dialog<'a, Message> { pub fn dialog<'a, Message>() -> Dialog<'a, Message> {
Dialog::new(title) Dialog::new()
} }
pub struct Dialog<'a, Message> { pub struct Dialog<'a, Message> {
title: Cow<'a, str>, title: Option<Cow<'a, str>>,
icon: Option<Element<'a, Message>>, icon: Option<Element<'a, Message>>,
body: Option<Cow<'a, str>>, body: Option<Cow<'a, str>>,
controls: Vec<Element<'a, Message>>, controls: Vec<Element<'a, Message>>,
@ -16,9 +16,9 @@ pub struct Dialog<'a, Message> {
} }
impl<'a, Message> Dialog<'a, Message> { impl<'a, Message> Dialog<'a, Message> {
pub fn new(title: impl Into<Cow<'a, str>>) -> Self { pub fn new() -> Self {
Self { Self {
title: title.into(), title: None,
icon: None, icon: None,
body: None, body: None,
controls: Vec::new(), controls: Vec::new(),
@ -28,6 +28,11 @@ impl<'a, Message> Dialog<'a, Message> {
} }
} }
pub fn title(mut self, title: impl Into<Cow<'a, str>>) -> Self {
self.title = Some(title.into());
self
}
pub fn icon(mut self, icon: impl Into<Element<'a, Message>>) -> Self { pub fn icon(mut self, icon: impl Into<Element<'a, Message>>) -> Self {
self.icon = Some(icon.into()); self.icon = Some(icon.into());
self self
@ -70,16 +75,28 @@ impl<'a, Message: Clone + 'static> From<Dialog<'a, Message>> for Element<'a, Mes
} = theme::THEME.lock().unwrap().cosmic().spacing; } = theme::THEME.lock().unwrap().cosmic().spacing;
let mut content_col = widget::column::with_capacity(3 + dialog.controls.len() * 2); 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 { if let Some(body) = dialog.body {
content_col = if should_space {
content_col.push(widget::vertical_space().height(Length::Fixed(space_xxs.into()))); content_col = content_col
.push(widget::vertical_space().height(Length::Fixed(space_xxs.into())));
}
content_col = content_col.push(widget::text::body(body)); content_col = content_col.push(widget::text::body(body));
should_space = true;
} }
for control in dialog.controls { for control in dialog.controls {
content_col = if should_space {
content_col.push(widget::vertical_space().height(Length::Fixed(space_s.into()))); content_col = content_col
.push(widget::vertical_space().height(Length::Fixed(space_s.into())));
}
content_col = content_col.push(control); content_col = content_col.push(control);
should_space = true;
} }
let mut content_row = widget::row::with_capacity(2).spacing(space_s); let mut content_row = widget::row::with_capacity(2).spacing(space_s);