From ae3f5b9b2ec0a961ce9c453fe659a4c36194aaf0 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Fri, 30 Sep 2022 10:38:31 -0600 Subject: [PATCH] List hierarchy changes --- examples/cosmic/src/main.rs | 43 ++++++++++++-------------- src/widget/list.rs | 60 +++++++++++++++++++++++++------------ 2 files changed, 60 insertions(+), 43 deletions(-) diff --git a/examples/cosmic/src/main.rs b/examples/cosmic/src/main.rs index 0c9e4a6a..72379f38 100644 --- a/examples/cosmic/src/main.rs +++ b/examples/cosmic/src/main.rs @@ -1,9 +1,9 @@ use cosmic::{ - font::FONT_SEMIBOLD, widget::{ button, icon, list_item, + list_section, list_view, nav_bar, toggler, @@ -11,7 +11,7 @@ use cosmic::{ settings, iced::{theme, Alignment, Color, Element, Length, Sandbox, Theme}, iced::widget::{ - checkbox, column, container, horizontal_space, progress_bar, radio, + checkbox, container, horizontal_space, progress_bar, radio, row, slider, text, vertical_space, }, @@ -100,7 +100,7 @@ impl Sandbox for Window { .into(); let choose_theme = [Theme::Light, Theme::Dark].iter().fold( - row![text("Theme:")].spacing(10).align_items(Alignment::Center), + row![text("Debug theme:")].spacing(10).align_items(Alignment::Center), |row, theme| { row.push(radio( format!("{:?}", theme), @@ -111,18 +111,18 @@ impl Sandbox for Window { }, ); - let content: Element<_> = column![ - choose_theme, - vertical_space(Length::Units(16)), - toggler( - String::from("Debug layout"), - self.debug, - Message::Debug, - ) - , - vertical_space(Length::Units(16)), - text("Buttons").font(FONT_SEMIBOLD), - list_view!( + let content: Element<_> = list_view!( + list_section!( + "Debug", + choose_theme, + toggler( + String::from("Debug layout"), + self.debug, + Message::Debug, + ) + ), + list_section!( + "Buttons", list_item!( button!("Primary") .style(theme::Button::Primary) @@ -167,11 +167,9 @@ impl Sandbox for Window { .padding([8, 16]) , ), - ) - , - vertical_space(Length::Units(16)), - text("Controls").font(FONT_SEMIBOLD), - list_view!( + ), + list_section!( + "Controls", list_item!( text("Toggler"), horizontal_space(Length::Fill), @@ -191,10 +189,7 @@ impl Sandbox for Window { ), checkbox("Checkbox", self.checkbox_value, Message::CheckboxToggled), ) - ] - .spacing(8) - .padding(24) - .max_width(600) + ) .into(); container(row![ diff --git a/src/widget/list.rs b/src/widget/list.rs index 02f816a3..e16b404a 100644 --- a/src/widget/list.rs +++ b/src/widget/list.rs @@ -9,6 +9,7 @@ use iced::{ macro_rules! list_item { ($($x:expr),+ $(,)?) => ( $crate::iced::widget::Row::with_children(vec![$($crate::iced::Element::from($x)),+]) + .align_items(Alignment::Center) .padding([0, 8]) .spacing(12) ); @@ -16,30 +17,38 @@ macro_rules! list_item { pub use list_item; #[macro_export] -macro_rules! list_view { - ($($x:expr),+ $(,)?) => ( - $crate::iced::widget::Container::new({ - let mut children = vec![$($crate::iced::Element::from($x)),+]; +macro_rules! list_section { + ($heading:expr, $($x:expr),+ $(,)?) => ( + $crate::iced::widget::Column::with_children(vec![ + $crate::iced::widget::Text::new($heading) + .font($crate::font::FONT_SEMIBOLD) + .into() + , + $crate::iced::widget::Container::new({ + let mut children = vec![$($crate::iced::Element::from($x)),+]; - //TODO: more efficient method for adding separators - let mut i = 1; - while i < children.len() { - children.insert(i, $crate::iced::widget::horizontal_rule(12).into()); - i += 2; - } + //TODO: more efficient method for adding separators + let mut i = 1; + while i < children.len() { + children.insert(i, $crate::iced::widget::horizontal_rule(12).into()); + i += 2; + } - $crate::iced::widget::Column::with_children(children) - .spacing(12) - }) - .padding([12, 16]) - .style(theme::Container::Custom( - $crate::widget::list_view_style - )) + $crate::iced::widget::Column::with_children(children) + .spacing(12) + }) + .padding([12, 16]) + .style(theme::Container::Custom( + $crate::widget::list_section_style + )) + .into() + ]) + .spacing(8) ); } -pub use list_view; +pub use list_section; -pub fn list_view_style(theme: &Theme) -> widget::container::Appearance { +pub fn list_section_style(theme: &Theme) -> widget::container::Appearance { widget::container::Appearance { text_color: None, background: Some(Background::Color( @@ -53,3 +62,16 @@ pub fn list_view_style(theme: &Theme) -> widget::container::Appearance { border_color: Color::TRANSPARENT, } } + +#[macro_export] +macro_rules! list_view { + ($($x:expr),+ $(,)?) => ( + $crate::iced::widget::Column::with_children( + vec![$($crate::iced::Element::from($x)),+] + ) + .spacing(24) + .padding(24) + .max_width(600) + ); +} +pub use list_view;