List hierarchy changes

This commit is contained in:
Jeremy Soller 2022-09-30 10:38:31 -06:00
parent 5d25af14a9
commit ae3f5b9b2e
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
2 changed files with 60 additions and 43 deletions

View file

@ -1,9 +1,9 @@
use cosmic::{ use cosmic::{
font::FONT_SEMIBOLD,
widget::{ widget::{
button, button,
icon, icon,
list_item, list_item,
list_section,
list_view, list_view,
nav_bar, nav_bar,
toggler, toggler,
@ -11,7 +11,7 @@ use cosmic::{
settings, settings,
iced::{theme, Alignment, Color, Element, Length, Sandbox, Theme}, iced::{theme, Alignment, Color, Element, Length, Sandbox, Theme},
iced::widget::{ iced::widget::{
checkbox, column, container, horizontal_space, progress_bar, radio, checkbox, container, horizontal_space, progress_bar, radio,
row, slider, text, row, slider, text,
vertical_space, vertical_space,
}, },
@ -100,7 +100,7 @@ impl Sandbox for Window {
.into(); .into();
let choose_theme = [Theme::Light, Theme::Dark].iter().fold( 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, theme| {
row.push(radio( row.push(radio(
format!("{:?}", theme), format!("{:?}", theme),
@ -111,18 +111,18 @@ impl Sandbox for Window {
}, },
); );
let content: Element<_> = column![ let content: Element<_> = list_view!(
choose_theme, list_section!(
vertical_space(Length::Units(16)), "Debug",
toggler( choose_theme,
String::from("Debug layout"), toggler(
self.debug, String::from("Debug layout"),
Message::Debug, self.debug,
) Message::Debug,
, )
vertical_space(Length::Units(16)), ),
text("Buttons").font(FONT_SEMIBOLD), list_section!(
list_view!( "Buttons",
list_item!( list_item!(
button!("Primary") button!("Primary")
.style(theme::Button::Primary) .style(theme::Button::Primary)
@ -167,11 +167,9 @@ impl Sandbox for Window {
.padding([8, 16]) .padding([8, 16])
, ,
), ),
) ),
, list_section!(
vertical_space(Length::Units(16)), "Controls",
text("Controls").font(FONT_SEMIBOLD),
list_view!(
list_item!( list_item!(
text("Toggler"), text("Toggler"),
horizontal_space(Length::Fill), horizontal_space(Length::Fill),
@ -191,10 +189,7 @@ impl Sandbox for Window {
), ),
checkbox("Checkbox", self.checkbox_value, Message::CheckboxToggled), checkbox("Checkbox", self.checkbox_value, Message::CheckboxToggled),
) )
] )
.spacing(8)
.padding(24)
.max_width(600)
.into(); .into();
container(row![ container(row![

View file

@ -9,6 +9,7 @@ use iced::{
macro_rules! list_item { macro_rules! list_item {
($($x:expr),+ $(,)?) => ( ($($x:expr),+ $(,)?) => (
$crate::iced::widget::Row::with_children(vec![$($crate::iced::Element::from($x)),+]) $crate::iced::widget::Row::with_children(vec![$($crate::iced::Element::from($x)),+])
.align_items(Alignment::Center)
.padding([0, 8]) .padding([0, 8])
.spacing(12) .spacing(12)
); );
@ -16,30 +17,38 @@ macro_rules! list_item {
pub use list_item; pub use list_item;
#[macro_export] #[macro_export]
macro_rules! list_view { macro_rules! list_section {
($($x:expr),+ $(,)?) => ( ($heading:expr, $($x:expr),+ $(,)?) => (
$crate::iced::widget::Container::new({ $crate::iced::widget::Column::with_children(vec![
let mut children = vec![$($crate::iced::Element::from($x)),+]; $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 //TODO: more efficient method for adding separators
let mut i = 1; let mut i = 1;
while i < children.len() { while i < children.len() {
children.insert(i, $crate::iced::widget::horizontal_rule(12).into()); children.insert(i, $crate::iced::widget::horizontal_rule(12).into());
i += 2; i += 2;
} }
$crate::iced::widget::Column::with_children(children) $crate::iced::widget::Column::with_children(children)
.spacing(12) .spacing(12)
}) })
.padding([12, 16]) .padding([12, 16])
.style(theme::Container::Custom( .style(theme::Container::Custom(
$crate::widget::list_view_style $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 { widget::container::Appearance {
text_color: None, text_color: None,
background: Some(Background::Color( background: Some(Background::Color(
@ -53,3 +62,16 @@ pub fn list_view_style(theme: &Theme) -> widget::container::Appearance {
border_color: Color::TRANSPARENT, 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;