From 5ad54bd3b1b9e4767da4438607c7aa51aae788f8 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Fri, 30 Sep 2022 09:35:55 -0600 Subject: [PATCH] Move implementations from example to libcosmic --- Cargo.toml | 3 + examples/cosmic/Cargo.toml | 7 +- examples/cosmic/src/main.rs | 158 +++++------------- .../cosmic/res => res}/Fira/Sans/Light.otf | Bin .../cosmic/res => res}/Fira/Sans/Regular.otf | Bin .../Fira/Sans/SIL Open Font License.txt | 0 .../cosmic/res => res}/Fira/Sans/SemiBold.otf | Bin src/font.rs | 16 ++ src/lib.rs | 14 ++ src/widget/icon.rs | 18 ++ src/widget/list_view.rs | 23 +++ src/widget/mod.rs | 22 +++ src/widget/nav_bar.rs | 23 +++ 13 files changed, 162 insertions(+), 122 deletions(-) rename {examples/cosmic/res => res}/Fira/Sans/Light.otf (100%) rename {examples/cosmic/res => res}/Fira/Sans/Regular.otf (100%) rename {examples/cosmic/res => res}/Fira/Sans/SIL Open Font License.txt (100%) rename {examples/cosmic/res => res}/Fira/Sans/SemiBold.otf (100%) create mode 100644 src/font.rs create mode 100644 src/widget/icon.rs create mode 100644 src/widget/list_view.rs create mode 100644 src/widget/mod.rs create mode 100644 src/widget/nav_bar.rs diff --git a/Cargo.toml b/Cargo.toml index b0fd93ae..8f8f41ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,9 @@ name = "libcosmic" version = "0.1.0" edition = "2021" +[lib] +name = "cosmic" + [dependencies] freedesktop-icons = "0.2.1" diff --git a/examples/cosmic/Cargo.toml b/examples/cosmic/Cargo.toml index ccaf5645..38e2cff2 100644 --- a/examples/cosmic/Cargo.toml +++ b/examples/cosmic/Cargo.toml @@ -6,9 +6,4 @@ edition = "2021" publish = false [dependencies] -freedesktop-icons = "0.2.1" - -[dependencies.iced] -git = "https://github.com/pop-os/iced.git" -branch = "cosmic-design-system" -features = ["cosmic_theme", "svg"] +libcosmic = { path = "../.." } diff --git a/examples/cosmic/src/main.rs b/examples/cosmic/src/main.rs index 79abe684..5bee2fe4 100644 --- a/examples/cosmic/src/main.rs +++ b/examples/cosmic/src/main.rs @@ -1,81 +1,24 @@ -use iced::widget::{ - button, checkbox, column, container, horizontal_rule, horizontal_space, progress_bar, radio, - row, slider, svg, text, toggler, - vertical_space, -}; -use iced::{theme, Alignment, Background, Color, Element, Font, Length, Sandbox, Settings, Theme}; - -const FONT: Font = Font::External { - name: "Fira Sans Regular", - bytes: include_bytes!("../res/Fira/Sans/Regular.otf"), +use cosmic::{ + font::FONT_SEMIBOLD, + widget::{ + button, + icon, + list_view_style, + nav_bar_style, + }, + 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, + vertical_space, + }, }; -const FONT_LIGHT: Font = Font::External { - name: "Fira Sans Light", - bytes: include_bytes!("../res/Fira/Sans/Light.otf"), -}; - -const FONT_SEMIBOLD: Font = Font::External { - name: "Fira Sans SemiBold", - bytes: include_bytes!("../res/Fira/Sans/SemiBold.otf"), -}; - -pub fn main() -> iced::Result { - let mut settings = Settings::default(); - settings.default_font = match FONT { - Font::Default => None, - Font::External { bytes, .. } => Some(bytes), - }; - settings.default_text_size = 18; - Window::run(settings) +pub fn main() -> cosmic::iced::Result { + Window::run(settings()) } -fn icon(name: &str, size: u16) -> svg::Svg { - let handle = match freedesktop_icons::lookup(name) - .with_size(size) - .with_theme("Pop") - .with_cache() - .force_svg() - .find() - { - Some(path) => svg::Handle::from_path(path), - None => { - eprintln!("icon '{}' size {} not found", name, size); - svg::Handle::from_memory(Vec::new()) - }, - }; - svg::Svg::new(handle) -} - -fn sidebar_style(theme: &Theme) -> container::Appearance { - container::Appearance { - text_color: None, - background: Some(Background::Color( - match theme { - Theme::Dark => Color::from_rgb8(0x29, 0x29, 0x29), - Theme::Light => Color::from_rgb8(0xfd, 0xfd, 0xfd), - } - )), - border_radius: 8.0, - border_width: 0.0, - border_color: Color::TRANSPARENT, - } -} - -fn listview_style(theme: &Theme) -> container::Appearance { - container::Appearance { - text_color: None, - background: Some(Background::Color( - match theme { - Theme::Dark => Color::from_rgb8(0x27, 0x27, 0x27), - Theme::Light => Color::from_rgb8(0xf7, 0xf7, 0xf7), - } - )), - border_radius: 8.0, - border_width: 0.0, - border_color: Color::TRANSPARENT, - } -} #[derive(Default)] struct Window { @@ -127,38 +70,26 @@ impl Sandbox for Window { let sidebar: Element<_> = container( column![ //TODO: Support symbolic icons - button( - row![ - icon("network-wireless", 16).width(Length::Units(16)), - text("Wi-Fi"), - horizontal_space(Length::Fill), - ] - .padding([4, 12]) - .spacing(8) + 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( - row![ - icon("preferences-desktop", 16).width(Length::Units(16)), - text("Desktop"), - horizontal_space(Length::Fill), - ] - .padding([4, 12]) - .spacing(8) + 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( - row![ - icon("system-software-update", 16).width(Length::Units(16)), - text("OS Upgrade & Recovery"), - horizontal_space(Length::Fill), - ] - .padding([4, 12]) - .spacing(8) + 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 }) @@ -169,7 +100,7 @@ impl Sandbox for Window { .padding(12) .max_width(300) ) - .style(theme::Container::Custom(sidebar_style)) + .style(theme::Container::Custom(nav_bar_style)) .into(); let choose_theme = [Theme::Light, Theme::Dark].iter().fold( @@ -201,51 +132,46 @@ impl Sandbox for Window { container( column![ row![ - button("Primary") + button!("Primary") .style(theme::Button::Primary) - .padding([8, 16]) .on_press(Message::ButtonPressed) , - button("Secondary") + button!("Secondary") .style(theme::Button::Secondary) - .padding([8, 16]) .on_press(Message::ButtonPressed) , - button("Positive") + button!("Positive") .style(theme::Button::Positive) - .padding([8, 16]) .on_press(Message::ButtonPressed) , - button("Destructive") + button!("Destructive") .style(theme::Button::Destructive) - .padding([8, 16]) .on_press(Message::ButtonPressed) , - button("Text") + button!("Text") .style(theme::Button::Text) - .padding([8, 16]) .on_press(Message::ButtonPressed) , ].spacing(12), horizontal_rule(12), row![ - button("Primary") + button!("Primary") .style(theme::Button::Primary) .padding([8, 16]) , - button("Secondary") + button!("Secondary") .style(theme::Button::Secondary) .padding([8, 16]) , - button("Positive") + button!("Positive") .style(theme::Button::Positive) .padding([8, 16]) , - button("Destructive") + button!("Destructive") .style(theme::Button::Destructive) .padding([8, 16]) , - button("Text") + button!("Text") .style(theme::Button::Text) .padding([8, 16]) , @@ -254,7 +180,7 @@ impl Sandbox for Window { .padding([12, 16]) .spacing(12) ) - .style(theme::Container::Custom(listview_style)) + .style(theme::Container::Custom(list_view_style)) , vertical_space(Length::Units(16)), text("Controls").font(FONT_SEMIBOLD), @@ -293,7 +219,7 @@ impl Sandbox for Window { .padding([12, 16]) .spacing(12) ) - .style(theme::Container::Custom(listview_style)) + .style(theme::Container::Custom(list_view_style)) ] .spacing(8) .padding(24) diff --git a/examples/cosmic/res/Fira/Sans/Light.otf b/res/Fira/Sans/Light.otf similarity index 100% rename from examples/cosmic/res/Fira/Sans/Light.otf rename to res/Fira/Sans/Light.otf diff --git a/examples/cosmic/res/Fira/Sans/Regular.otf b/res/Fira/Sans/Regular.otf similarity index 100% rename from examples/cosmic/res/Fira/Sans/Regular.otf rename to res/Fira/Sans/Regular.otf diff --git a/examples/cosmic/res/Fira/Sans/SIL Open Font License.txt b/res/Fira/Sans/SIL Open Font License.txt similarity index 100% rename from examples/cosmic/res/Fira/Sans/SIL Open Font License.txt rename to res/Fira/Sans/SIL Open Font License.txt diff --git a/examples/cosmic/res/Fira/Sans/SemiBold.otf b/res/Fira/Sans/SemiBold.otf similarity index 100% rename from examples/cosmic/res/Fira/Sans/SemiBold.otf rename to res/Fira/Sans/SemiBold.otf diff --git a/src/font.rs b/src/font.rs new file mode 100644 index 00000000..e8184222 --- /dev/null +++ b/src/font.rs @@ -0,0 +1,16 @@ +pub use iced::Font; + +pub const FONT: Font = Font::External { + name: "Fira Sans Regular", + bytes: include_bytes!("../res/Fira/Sans/Regular.otf"), +}; + +pub const FONT_LIGHT: Font = Font::External { + name: "Fira Sans Light", + bytes: include_bytes!("../res/Fira/Sans/Light.otf"), +}; + +pub const FONT_SEMIBOLD: Font = Font::External { + name: "Fira Sans SemiBold", + bytes: include_bytes!("../res/Fira/Sans/SemiBold.otf"), +}; diff --git a/src/lib.rs b/src/lib.rs index e69de29b..bb2611a5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -0,0 +1,14 @@ +pub use iced; + +pub mod font; +pub mod widget; + +pub fn settings() -> iced::Settings { + let mut settings = iced::Settings::default(); + settings.default_font = match font::FONT { + iced::Font::Default => None, + iced::Font::External { bytes, .. } => Some(bytes), + }; + settings.default_text_size = 18; + settings +} diff --git a/src/widget/icon.rs b/src/widget/icon.rs new file mode 100644 index 00000000..9ec01ff1 --- /dev/null +++ b/src/widget/icon.rs @@ -0,0 +1,18 @@ +use iced::widget::svg; + +pub fn icon(name: &str, size: u16) -> svg::Svg { + let handle = match freedesktop_icons::lookup(name) + .with_size(size) + .with_theme("Pop") + .with_cache() + .force_svg() + .find() + { + Some(path) => svg::Handle::from_path(path), + None => { + eprintln!("icon '{}' size {} not found", name, size); + svg::Handle::from_memory(Vec::new()) + }, + }; + svg::Svg::new(handle) +} diff --git a/src/widget/list_view.rs b/src/widget/list_view.rs new file mode 100644 index 00000000..55c249d0 --- /dev/null +++ b/src/widget/list_view.rs @@ -0,0 +1,23 @@ +use iced::{ + Background, + Color, + Theme, + widget::{ + container, + }, +}; + +pub fn list_view_style(theme: &Theme) -> container::Appearance { + container::Appearance { + text_color: None, + background: Some(Background::Color( + match theme { + Theme::Dark => Color::from_rgb8(0x27, 0x27, 0x27), + Theme::Light => Color::from_rgb8(0xf7, 0xf7, 0xf7), + } + )), + border_radius: 8.0, + border_width: 0.0, + border_color: Color::TRANSPARENT, + } +} diff --git a/src/widget/mod.rs b/src/widget/mod.rs new file mode 100644 index 00000000..3df8ec59 --- /dev/null +++ b/src/widget/mod.rs @@ -0,0 +1,22 @@ +#[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 icon; +pub use self::icon::icon; + +mod list_view; +pub use list_view::list_view_style; + +mod nav_bar; +pub use nav_bar::nav_bar_style; diff --git a/src/widget/nav_bar.rs b/src/widget/nav_bar.rs new file mode 100644 index 00000000..cd75be15 --- /dev/null +++ b/src/widget/nav_bar.rs @@ -0,0 +1,23 @@ +use iced::{ + Background, + Color, + Theme, + widget::{ + container, + }, +}; + +pub fn nav_bar_style(theme: &Theme) -> container::Appearance { + container::Appearance { + text_color: None, + background: Some(Background::Color( + match theme { + Theme::Dark => Color::from_rgb8(0x29, 0x29, 0x29), + Theme::Light => Color::from_rgb8(0xfd, 0xfd, 0xfd), + } + )), + border_radius: 8.0, + border_width: 0.0, + border_color: Color::TRANSPARENT, + } +}