feat(iconsource): additional helper methods for creating handles

This commit is contained in:
Michael Aaron Murphy 2023-01-19 13:01:02 +01:00 committed by Michael Murphy
parent b3d550cc5e
commit 2b0227d34f
6 changed files with 37 additions and 9 deletions

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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<PathBuf>) -> 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<Cow<'static, [u8]>>) -> 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<Cow<'static, [u8]>>,
) -> 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<PathBuf>) -> Self {
IconSource::EmbeddedSvg(svg::Handle::from_path(path))
}
/// Get a handle to a SVG from memory.
pub fn svg_from_memory(bytes: impl Into<Cow<'static, [u8]>>) -> Self {
IconSource::EmbeddedSvg(svg::Handle::from_memory(bytes))
}
}
impl<'a> From<Cow<'a, Path>> for IconSource<'a> {

View file

@ -29,11 +29,9 @@ impl<Message: 'static + Clone> From<NavBarToggle<Message>> for Element<'static,
fn from(nav_bar_toggle: NavBarToggle<Message>) -> 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,
)

View file

@ -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<IconSource<'static>>) -> Self {

View file

@ -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<TypeId, SecondaryMap<Entity, Box<dyn Any>>>);
/// The model held by the application, containing the unique IDs and data of each inserted item.
#[derive(Default, Debug)]
#[derive(Default)]
pub struct Model<SelectionMode: Default> {
/// The content used for drawing segmented items.
pub(super) items: SlotMap<Entity, Settings>,
@ -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);
/// }
/// ```