Move list view row and toggler to libcosmic

This commit is contained in:
Jeremy Soller 2022-09-30 10:20:03 -06:00
parent 8e42dc46ea
commit 5d25af14a9
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
6 changed files with 82 additions and 49 deletions

View file

@ -14,6 +14,11 @@ git = "https://github.com/pop-os/iced.git"
branch = "cosmic-design-system" branch = "cosmic-design-system"
features = ["cosmic_theme", "svg"] features = ["cosmic_theme", "svg"]
[dependencies.iced_native]
git = "https://github.com/pop-os/iced.git"
branch = "cosmic-design-system"
features = ["cosmic_theme"]
[workspace] [workspace]
members = [ members = [
"examples/cosmic", "examples/cosmic",

View file

@ -3,14 +3,16 @@ use cosmic::{
widget::{ widget::{
button, button,
icon, icon,
list_item,
list_view, list_view,
nav_bar, nav_bar,
toggler,
}, },
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_rule, horizontal_space, progress_bar, radio, checkbox, column, container, horizontal_space, progress_bar, radio,
row, slider, text, toggler, row, slider, text,
vertical_space, vertical_space,
}, },
}; };
@ -117,14 +119,11 @@ impl Sandbox for Window {
self.debug, self.debug,
Message::Debug, Message::Debug,
) )
.width(Length::Shrink)
.size(24)
.spacing(12)
, ,
vertical_space(Length::Units(16)), vertical_space(Length::Units(16)),
text("Buttons").font(FONT_SEMIBOLD), text("Buttons").font(FONT_SEMIBOLD),
list_view!( list_view!(
row![ list_item!(
button!("Primary") button!("Primary")
.style(theme::Button::Primary) .style(theme::Button::Primary)
.on_press(Message::ButtonPressed) .on_press(Message::ButtonPressed)
@ -145,9 +144,8 @@ impl Sandbox for Window {
.style(theme::Button::Text) .style(theme::Button::Text)
.on_press(Message::ButtonPressed) .on_press(Message::ButtonPressed)
, ,
].spacing(12), ),
horizontal_rule(12), list_item!(
row![
button!("Primary") button!("Primary")
.style(theme::Button::Primary) .style(theme::Button::Primary)
.padding([8, 16]) .padding([8, 16])
@ -168,40 +166,29 @@ impl Sandbox for Window {
.style(theme::Button::Text) .style(theme::Button::Text)
.padding([8, 16]) .padding([8, 16])
, ,
].spacing(12), ),
) )
, ,
vertical_space(Length::Units(16)), vertical_space(Length::Units(16)),
text("Controls").font(FONT_SEMIBOLD), text("Controls").font(FONT_SEMIBOLD),
list_view!( list_view!(
row![ list_item!(
text("Toggler"), text("Toggler"),
horizontal_space(Length::Fill), horizontal_space(Length::Fill),
toggler(None, self.toggler_value, Message::TogglerToggled) toggler(None, self.toggler_value, Message::TogglerToggled)
.size(24) ),
.width(Length::Shrink), list_item!(
]
.padding([0, 8])
,
horizontal_rule(12),
row![
text("Slider"), text("Slider"),
horizontal_space(Length::Fill), horizontal_space(Length::Fill),
slider(0.0..=100.0, self.slider_value, Message::SliderChanged) slider(0.0..=100.0, self.slider_value, Message::SliderChanged)
.width(Length::Units(250)), .width(Length::Units(250)),
] ),
.padding([0, 8]) list_item!(
,
horizontal_rule(12),
row![
text("Progress"), text("Progress"),
horizontal_space(Length::Fill), horizontal_space(Length::Fill),
progress_bar(0.0..=100.0, self.slider_value).height(Length::Units(4)) progress_bar(0.0..=100.0, self.slider_value).height(Length::Units(4))
.width(Length::Units(250)), .width(Length::Units(250)),
] ),
.padding([0, 8])
,
horizontal_rule(12),
checkbox("Checkbox", self.checkbox_value, Message::CheckboxToggled), checkbox("Checkbox", self.checkbox_value, Message::CheckboxToggled),
) )
] ]

13
src/widget/button.rs Normal file
View file

@ -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;

View file

@ -5,15 +5,32 @@ use iced::{
widget, 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_export]
macro_rules! list_view { macro_rules! list_view {
($($x:expr),+ $(,)?) => ( ($($x:expr),+ $(,)?) => (
$crate::iced::widget::Container::new( $crate::iced::widget::Container::new({
$crate::iced::widget::Column::with_children( let mut children = vec![$($crate::iced::Element::from($x)),+];
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) .spacing(12)
) })
.padding([12, 16]) .padding([12, 16])
.style(theme::Container::Custom( .style(theme::Container::Custom(
$crate::widget::list_view_style $crate::widget::list_view_style

View file

@ -1,22 +1,14 @@
#[macro_export] mod button;
macro_rules! button { pub use 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 icon; mod icon;
pub use self::icon::icon; pub use self::icon::*;
mod list_view; mod list;
pub use list_view::{list_view, list_view_style}; pub use list::*;
mod nav_bar; mod nav_bar;
pub use nav_bar::{nav_bar, nav_bar_style}; pub use nav_bar::*;
mod toggler;
pub use toggler::*;

19
src/widget/toggler.rs Normal file
View file

@ -0,0 +1,19 @@
use iced::{
Length,
widget,
};
pub fn toggler<'a, Message, Renderer>(
label: impl Into<Option<String>>,
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)
}