From bfcaf829eb990b698996070c3617568f930848b8 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Fri, 30 Sep 2022 09:51:00 -0600 Subject: [PATCH] Move more code to libcosmic --- examples/cosmic/src/main.rs | 62 +++++++++++++++++-------------------- src/widget/icon.rs | 7 ++++- src/widget/mod.rs | 2 +- src/widget/nav_bar.rs | 26 +++++++++++++--- 4 files changed, 56 insertions(+), 41 deletions(-) diff --git a/examples/cosmic/src/main.rs b/examples/cosmic/src/main.rs index 5bee2fe4..391ddb84 100644 --- a/examples/cosmic/src/main.rs +++ b/examples/cosmic/src/main.rs @@ -3,8 +3,8 @@ use cosmic::{ widget::{ button, icon, + nav_bar, list_view_style, - nav_bar_style, }, settings, iced::{theme, Alignment, Color, Element, Length, Sandbox, Theme}, @@ -67,40 +67,34 @@ impl Sandbox for Window { } fn view(&self) -> Element { - let sidebar: Element<_> = container( - column![ - //TODO: Support symbolic icons - button!( - icon("network-wireless", 16).width(Length::Units(16)), - text("Wi-Fi"), - horizontal_space(Length::Fill), - ) - .on_press(Message::Page(0)) - .style(if self.page == 0 { theme::Button::Primary } else { theme::Button::Text }) - , - button!( - icon("preferences-desktop", 16).width(Length::Units(16)), - text("Desktop"), - horizontal_space(Length::Fill), - ) - .on_press(Message::Page(1)) - .style(if self.page == 1 { theme::Button::Primary } else { theme::Button::Text }) - , - button!( - icon("system-software-update", 16).width(Length::Units(16)), - text("OS Upgrade & Recovery"), - horizontal_space(Length::Fill), - ) - .on_press(Message::Page(2)) - .style(if self.page == 2 { theme::Button::Primary } else { theme::Button::Text }) - , - vertical_space(Length::Fill), - ] - .spacing(12) - .padding(12) - .max_width(300) + let sidebar: Element<_> = nav_bar!( + //TODO: Support symbolic icons + button!( + icon("network-wireless", 16), + text("Wi-Fi"), + horizontal_space(Length::Fill), + ) + .on_press(Message::Page(0)) + .style(if self.page == 0 { theme::Button::Primary } else { theme::Button::Text }) + , + button!( + icon("preferences-desktop", 16), + text("Desktop"), + horizontal_space(Length::Fill), + ) + .on_press(Message::Page(1)) + .style(if self.page == 1 { theme::Button::Primary } else { theme::Button::Text }) + , + button!( + icon("system-software-update", 16), + text("OS Upgrade & Recovery"), + horizontal_space(Length::Fill), + ) + .on_press(Message::Page(2)) + .style(if self.page == 2 { theme::Button::Primary } else { theme::Button::Text }) + , + vertical_space(Length::Fill), ) - .style(theme::Container::Custom(nav_bar_style)) .into(); let choose_theme = [Theme::Light, Theme::Dark].iter().fold( diff --git a/src/widget/icon.rs b/src/widget/icon.rs index 9ec01ff1..0539c123 100644 --- a/src/widget/icon.rs +++ b/src/widget/icon.rs @@ -1,4 +1,7 @@ -use iced::widget::svg; +use iced::{ + Length, + widget::svg, +}; pub fn icon(name: &str, size: u16) -> svg::Svg { let handle = match freedesktop_icons::lookup(name) @@ -15,4 +18,6 @@ pub fn icon(name: &str, size: u16) -> svg::Svg { }, }; svg::Svg::new(handle) + .width(Length::Units(size)) + .height(Length::Units(size)) } diff --git a/src/widget/mod.rs b/src/widget/mod.rs index 3df8ec59..971d2dc1 100644 --- a/src/widget/mod.rs +++ b/src/widget/mod.rs @@ -19,4 +19,4 @@ mod list_view; pub use list_view::list_view_style; mod nav_bar; -pub use nav_bar::nav_bar_style; +pub use nav_bar::{nav_bar, nav_bar_style}; diff --git a/src/widget/nav_bar.rs b/src/widget/nav_bar.rs index cd75be15..ec63e99d 100644 --- a/src/widget/nav_bar.rs +++ b/src/widget/nav_bar.rs @@ -2,13 +2,29 @@ use iced::{ Background, Color, Theme, - widget::{ - container, - }, + widget }; -pub fn nav_bar_style(theme: &Theme) -> container::Appearance { - container::Appearance { +#[macro_export] +macro_rules! nav_bar { + ($($x:expr),+ $(,)?) => ( + $crate::iced::widget::Container::new( + $crate::iced::widget::Column::with_children( + vec![$($crate::iced::Element::from($x)),+] + ) + .spacing(12) + ) + .max_width(300) + .padding(12) + .style(theme::Container::Custom( + $crate::widget::nav_bar_style + )) + ); +} +pub use nav_bar; + +pub fn nav_bar_style(theme: &Theme) -> widget::container::Appearance { + widget::container::Appearance { text_color: None, background: Some(Background::Color( match theme {