diff --git a/examples/cosmic-sctk/src/window.rs b/examples/cosmic-sctk/src/window.rs index 6ccf5a1..38d7f65 100644 --- a/examples/cosmic-sctk/src/window.rs +++ b/examples/cosmic-sctk/src/window.rs @@ -130,7 +130,7 @@ impl Window { self.nav_bar_pages .insert() .text(page.title()) - .icon(IconSource::Name(page.icon_name().into())) + .icon(IconSource::from(page.icon_name())) .data(page) } diff --git a/examples/cosmic/src/window.rs b/examples/cosmic/src/window.rs index b6ef6d4..daac699 100644 --- a/examples/cosmic/src/window.rs +++ b/examples/cosmic/src/window.rs @@ -198,7 +198,7 @@ impl Window { self.nav_bar .insert() .text(page.title()) - .icon(IconSource::Name(page.icon_name().into())) + .icon(IconSource::from(page.icon_name())) .secondary(&mut self.nav_id_to_page, page) } diff --git a/src/widget/icon.rs b/src/widget/icon.rs index af85a67..40c0f70 100644 --- a/src/widget/icon.rs +++ b/src/widget/icon.rs @@ -80,6 +80,35 @@ impl<'a> IconSource<'a> { Handle::Image(image::Handle::from_memory(Vec::new())) } } + + /// Get a handle to a raster image from a path. + pub fn raster_from_path(path: impl Into) -> Self { + IconSource::Embedded(image::Handle::from_path(path)) + } + + /// Get a handle to a raster image from memory. + pub fn raster_from_memory(bytes: impl Into>) -> Self { + IconSource::Embedded(image::Handle::from_memory(bytes)) + } + + /// Get a handle to a raster image from RGBA data, where you must define the width and height. + pub fn raster_from_pixels( + width: u32, + height: u32, + pixels: impl Into>, + ) -> Self { + IconSource::Embedded(image::Handle::from_pixels(width, height, pixels)) + } + + /// Get a handle to a SVG from a path. + pub fn svg_from_path(path: impl Into) -> Self { + IconSource::EmbeddedSvg(svg::Handle::from_path(path)) + } + + /// Get a handle to a SVG from memory. + pub fn svg_from_memory(bytes: impl Into>) -> Self { + IconSource::EmbeddedSvg(svg::Handle::from_memory(bytes)) + } } impl<'a> From> for IconSource<'a> { diff --git a/src/widget/nav_bar_toggle.rs b/src/widget/nav_bar_toggle.rs index 9a9c322..2f6012e 100644 --- a/src/widget/nav_bar_toggle.rs +++ b/src/widget/nav_bar_toggle.rs @@ -29,11 +29,9 @@ impl From> for Element<'static, fn from(nav_bar_toggle: NavBarToggle) -> Self { let mut widget = super::icon( if nav_bar_toggle.nav_bar_active { - IconSource::EmbeddedSvg(iced::widget::svg::Handle::from_memory( - &include_bytes!("../../res/sidebar-active.svg")[..], - )) + IconSource::svg_from_memory(&include_bytes!("../../res/sidebar-active.svg")[..]) } else { - IconSource::Name("open-menu-symbolic".into()) + IconSource::from("open-menu-symbolic") }, 16, ) diff --git a/src/widget/segmented_button/model/entity.rs b/src/widget/segmented_button/model/entity.rs index da575ac..02cad02 100644 --- a/src/widget/segmented_button/model/entity.rs +++ b/src/widget/segmented_button/model/entity.rs @@ -79,7 +79,7 @@ where /// Define an icon for the item. /// /// ```ignore - /// model.insert().text("Item A").icon(IconSource::Name("icon-a".into())); + /// model.insert().text("Item A").icon(IconSource::from("icon-a")); /// ``` #[allow(clippy::must_use_candidate, clippy::return_self_not_must_use)] pub fn icon(self, icon: impl Into>) -> Self { diff --git a/src/widget/segmented_button/model/mod.rs b/src/widget/segmented_button/model/mod.rs index 0fbc836..4f0cb67 100644 --- a/src/widget/segmented_button/model/mod.rs +++ b/src/widget/segmented_button/model/mod.rs @@ -11,6 +11,7 @@ mod selection; pub use self::selection::{MultiSelect, Selectable, SingleSelect}; use crate::widget::IconSource; +use crate::Element; use slotmap::{SecondaryMap, SlotMap}; use std::any::{Any, TypeId}; use std::borrow::Cow; @@ -49,7 +50,7 @@ pub type MultiSelectEntityMut<'a> = EntityMut<'a, MultiSelect>; pub(super) struct Storage(HashMap>>); /// The model held by the application, containing the unique IDs and data of each inserted item. -#[derive(Default, Debug)] +#[derive(Default)] pub struct Model { /// The content used for drawing segmented items. pub(super) items: SlotMap, @@ -201,7 +202,7 @@ where /// Sets a new icon for an item. /// /// ```ignore - /// if let Some(old_icon) = model.icon_set(IconSource::Name("new-icon".into())) { + /// if let Some(old_icon) = model.icon_set(IconSource::from("new-icon")) { /// println!("previously had icon: {:?}", old_icon); /// } /// ```