From 5d25af14a936f9ba1b96a1eeebeee89dc49e21ce Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Fri, 30 Sep 2022 10:20:03 -0600 Subject: [PATCH] Move list view row and toggler to libcosmic --- Cargo.toml | 5 ++++ examples/cosmic/src/main.rs | 41 ++++++++++------------------ src/widget/button.rs | 13 +++++++++ src/widget/{list_view.rs => list.rs} | 27 ++++++++++++++---- src/widget/mod.rs | 26 ++++++------------ src/widget/toggler.rs | 19 +++++++++++++ 6 files changed, 82 insertions(+), 49 deletions(-) create mode 100644 src/widget/button.rs rename src/widget/{list_view.rs => list.rs} (53%) create mode 100644 src/widget/toggler.rs diff --git a/Cargo.toml b/Cargo.toml index 8f8f41ec..2e2947af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,11 @@ git = "https://github.com/pop-os/iced.git" branch = "cosmic-design-system" features = ["cosmic_theme", "svg"] +[dependencies.iced_native] +git = "https://github.com/pop-os/iced.git" +branch = "cosmic-design-system" +features = ["cosmic_theme"] + [workspace] members = [ "examples/cosmic", diff --git a/examples/cosmic/src/main.rs b/examples/cosmic/src/main.rs index 80cdfa2a..0c9e4a6a 100644 --- a/examples/cosmic/src/main.rs +++ b/examples/cosmic/src/main.rs @@ -3,14 +3,16 @@ use cosmic::{ widget::{ button, icon, + list_item, list_view, nav_bar, + toggler, }, settings, iced::{theme, Alignment, Color, Element, Length, Sandbox, Theme}, iced::widget::{ - checkbox, column, container, horizontal_rule, horizontal_space, progress_bar, radio, - row, slider, text, toggler, + checkbox, column, container, horizontal_space, progress_bar, radio, + row, slider, text, vertical_space, }, }; @@ -117,14 +119,11 @@ impl Sandbox for Window { self.debug, Message::Debug, ) - .width(Length::Shrink) - .size(24) - .spacing(12) , vertical_space(Length::Units(16)), text("Buttons").font(FONT_SEMIBOLD), list_view!( - row![ + list_item!( button!("Primary") .style(theme::Button::Primary) .on_press(Message::ButtonPressed) @@ -145,9 +144,8 @@ impl Sandbox for Window { .style(theme::Button::Text) .on_press(Message::ButtonPressed) , - ].spacing(12), - horizontal_rule(12), - row![ + ), + list_item!( button!("Primary") .style(theme::Button::Primary) .padding([8, 16]) @@ -168,40 +166,29 @@ impl Sandbox for Window { .style(theme::Button::Text) .padding([8, 16]) , - ].spacing(12), + ), ) , vertical_space(Length::Units(16)), text("Controls").font(FONT_SEMIBOLD), list_view!( - row![ + list_item!( text("Toggler"), horizontal_space(Length::Fill), toggler(None, self.toggler_value, Message::TogglerToggled) - .size(24) - .width(Length::Shrink), - ] - .padding([0, 8]) - , - horizontal_rule(12), - row![ + ), + list_item!( text("Slider"), horizontal_space(Length::Fill), slider(0.0..=100.0, self.slider_value, Message::SliderChanged) .width(Length::Units(250)), - ] - .padding([0, 8]) - , - horizontal_rule(12), - row![ + ), + list_item!( text("Progress"), horizontal_space(Length::Fill), progress_bar(0.0..=100.0, self.slider_value).height(Length::Units(4)) .width(Length::Units(250)), - ] - .padding([0, 8]) - , - horizontal_rule(12), + ), checkbox("Checkbox", self.checkbox_value, Message::CheckboxToggled), ) ] diff --git a/src/widget/button.rs b/src/widget/button.rs new file mode 100644 index 00000000..7a1de227 --- /dev/null +++ b/src/widget/button.rs @@ -0,0 +1,13 @@ +#[macro_export] +macro_rules! button { + ($($x:expr),+ $(,)?) => ( + $crate::iced::widget::Button::new( + $crate::iced::widget::Row::with_children( + vec![$($crate::iced::Element::from($x)),+] + ) + .spacing(8) + ) + .padding([8, 16]) + ); +} +pub use button; diff --git a/src/widget/list_view.rs b/src/widget/list.rs similarity index 53% rename from src/widget/list_view.rs rename to src/widget/list.rs index 61633f2f..02f816a3 100644 --- a/src/widget/list_view.rs +++ b/src/widget/list.rs @@ -5,15 +5,32 @@ use iced::{ widget, }; +#[macro_export] +macro_rules! list_item { + ($($x:expr),+ $(,)?) => ( + $crate::iced::widget::Row::with_children(vec![$($crate::iced::Element::from($x)),+]) + .padding([0, 8]) + .spacing(12) + ); +} +pub use list_item; + #[macro_export] macro_rules! list_view { ($($x:expr),+ $(,)?) => ( - $crate::iced::widget::Container::new( - $crate::iced::widget::Column::with_children( - vec![$($crate::iced::Element::from($x)),+] - ) + $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; + } + + $crate::iced::widget::Column::with_children(children) .spacing(12) - ) + }) .padding([12, 16]) .style(theme::Container::Custom( $crate::widget::list_view_style diff --git a/src/widget/mod.rs b/src/widget/mod.rs index fb3089b0..3b9f442b 100644 --- a/src/widget/mod.rs +++ b/src/widget/mod.rs @@ -1,22 +1,14 @@ -#[macro_export] -macro_rules! button { - ($($x:expr),+ $(,)?) => ( - $crate::iced::widget::Button::new( - $crate::iced::widget::Row::with_children( - vec![$($crate::iced::Element::from($x)),+] - ) - .spacing(8) - ) - .padding([8, 16]) - ); -} -pub use button; +mod button; +pub use button::*; mod icon; -pub use self::icon::icon; +pub use self::icon::*; -mod list_view; -pub use list_view::{list_view, list_view_style}; +mod list; +pub use list::*; mod nav_bar; -pub use nav_bar::{nav_bar, nav_bar_style}; +pub use nav_bar::*; + +mod toggler; +pub use toggler::*; diff --git a/src/widget/toggler.rs b/src/widget/toggler.rs new file mode 100644 index 00000000..437398d9 --- /dev/null +++ b/src/widget/toggler.rs @@ -0,0 +1,19 @@ +use iced::{ + Length, + widget, +}; + +pub fn toggler<'a, Message, Renderer>( + label: impl Into>, + is_checked: bool, + f: impl Fn(bool) -> Message + 'a, +) -> widget::Toggler<'a, Message, Renderer> +where + Renderer: iced_native::text::Renderer, + Renderer::Theme: widget::toggler::StyleSheet, +{ + widget::Toggler::new(is_checked, label, f) + .size(24) + .spacing(12) + .width(Length::Shrink) +}