1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
#![doc(html_favicon_url = "https://raw.githubusercontent.com/zng-ui/zng/main/examples/image/res/zng-logo-icon.png")]
#![doc(html_logo_url = "https://raw.githubusercontent.com/zng-ui/zng/main/examples/image/res/zng-logo.png")]
//!
//! Material icons for the [`Icon!`] widget.
//!
//! A map from name to icon codepoint is defined in a module for each font. The font files are embedded
//! by default and can are registered using the [`MaterialIconsManager`] app extension. The extension
//! also registers [`ICONS`] handlers that provide the icons.
//!
//! The icons are from the [Material Design Icons] project.
//!
//! [`Icon!`]: struct@zng_wgt_text::icon::Icon
//! [Material Design Icons]: https://github.com/google/material-design-icons
//!
//! # Crate
//!
#![doc = include_str!(concat!("../", std::env!("CARGO_PKG_README")))]
#![warn(unused_extern_crates)]
#![warn(missing_docs)]
use zng_app::widget::node::{NilUiNode, UiNode as _};
use zng_ext_font::FontName;
use zng_wgt::{wgt_fn, IconRequestArgs, ICONS};
use zng_wgt_text::icon::{GlyphIcon, Icon};
zng_wgt::enable_widget_macros!();
/// Material icon fonts manager.
///
/// This app extension registers the fonts in `"embedded"` builds and registers [`ICONS`] handlers that provide the icons.
pub struct MaterialIconsManager;
impl MaterialIconsManager {
#[cfg(feature = "embedded")]
fn register_fonts(&self) {
let sets = [
#[cfg(feature = "outlined")]
(outlined::FONT_NAME, outlined::FONT_BYTES),
#[cfg(feature = "filled")]
(filled::FONT_NAME, filled::FONT_BYTES),
#[cfg(feature = "rounded")]
(rounded::FONT_NAME, rounded::FONT_BYTES),
#[cfg(feature = "sharp")]
(sharp::FONT_NAME, sharp::FONT_BYTES),
];
for (name, bytes) in sets {
let font = zng_ext_font::CustomFont::from_bytes(name, zng_ext_font::FontDataRef::from_static(bytes), 0);
zng_ext_font::FONTS.register(font);
}
}
}
#[cfg(feature = "embedded")]
impl zng_app::AppExtension for MaterialIconsManager {
fn init(&mut self) {
self.register_fonts();
ICONS.register(wgt_fn!(|args: IconRequestArgs| {
if let Some(strong_key) = args.name().strip_prefix("material/") {
#[expect(clippy::type_complexity)]
let sets: &[(&str, fn(&str) -> Option<GlyphIcon>)] = &[
#[cfg(feature = "outlined")]
("outlined/", outlined::get),
#[cfg(feature = "filled")]
("filled/", filled::get),
#[cfg(feature = "rounded")]
("rounded/", rounded::get),
#[cfg(feature = "sharp")]
("sharp/", sharp::get),
];
for (name, get) in sets {
if let Some(key) = strong_key.strip_prefix(name) {
if let Some(ico) = get(key) {
return Icon!(ico).boxed();
}
}
}
}
NilUiNode.boxed()
}));
ICONS.register_fallback(wgt_fn!(|args: IconRequestArgs| {
let sets = [
#[cfg(feature = "outlined")]
outlined::get,
#[cfg(feature = "filled")]
filled::get,
#[cfg(feature = "rounded")]
rounded::get,
#[cfg(feature = "sharp")]
sharp::get,
];
for get in sets {
if let Some(ico) = get(args.name()) {
return Icon!(ico).boxed();
}
}
NilUiNode.boxed()
}));
}
}
macro_rules! getters {
($FONT_NAME:ident, $MAP:ident) => {
/// Gets the [`GlyphIcon`].
pub fn get(key: &str) -> Option<GlyphIcon> {
Some(GlyphIcon::new($FONT_NAME.clone(), *$MAP.get(key)?))
}
/// Require the [`GlyphIcon`], logs an error if not found.
///
/// # Panics
///
/// Panics if the `key` is not found.
pub fn req(key: &str) -> GlyphIcon {
match get(key) {
Some(g) => g,
None => {
tracing::error!("icon {key:?} not found in `outlined`");
GlyphIcon::new("", '\0')
}
}
}
/// All icons.
pub fn all() -> impl ExactSizeIterator<Item = (&'static str, GlyphIcon)> {
$MAP.entries()
.map(|(key, val)| (*key, GlyphIcon::new($FONT_NAME.clone(), *val)))
}
};
}
/// Outline icons.
///
/// This is the "Material Icons Outlined" font.
///
/// # Icons
///
/// Use the [`ICONS`] service with key `"material/outlined/{name}"` or `"{name}"` to get an widget that renders the icon.
///
/// Use [`outlined::req`] to get a [`GlyphIcon`] directly for use in the [`Icon!`] widget.
///
/// [`Icon!`]: struct@Icon
///
/// | Name | Icon |
/// |------|------|
#[doc = include_str!(concat!(env!("OUT_DIR"), "/generated.outlined.docs.txt"))]
#[cfg(feature = "outlined")]
pub mod outlined {
use super::*;
/// "Material Icons Outlined".
pub const FONT_NAME: FontName = FontName::from_static("Material Icons Outlined");
/// Embedded font bytes.
#[cfg(feature = "embedded")]
pub const FONT_BYTES: &[u8] = include_bytes!("../fonts/MaterialIconsOutlined-Regular.otf");
include!(concat!(env!("OUT_DIR"), "/generated.outlined.map.rs"));
getters!(FONT_NAME, MAP);
}
/// Filled icons.
///
/// This is the "Material Icons" font.
///
/// # Icons
///
/// Use the [`ICONS`] service with key `"material/filled/{name}"` or `"{name}"` to get an widget that renders the icon.
///
/// Use [`filled::req`] to get a [`GlyphIcon`] directly for use in the [`Icon!`] widget.
///
/// [`Icon!`]: struct@Icon
///
/// | Name | Icon |
/// |------|------|
#[doc = include_str!(concat!(env!("OUT_DIR"), "/generated.filled.docs.txt"))]
#[cfg(feature = "filled")]
pub mod filled {
use super::*;
/// "Material Icons".
pub const FONT_NAME: FontName = FontName::from_static("Material Icons");
/// Embedded font bytes.
#[cfg(feature = "embedded")]
pub const FONT_BYTES: &[u8] = include_bytes!("../fonts/MaterialIcons-Regular.ttf");
include!(concat!(env!("OUT_DIR"), "/generated.filled.map.rs"));
getters!(FONT_NAME, MAP);
}
/// Rounded icons.
///
/// This is the "Material Icons Rounded" font.
///
/// # Icons
///
/// Use the [`ICONS`] service with key `"material/rounded/{name}"` or `"{name}"` to get an widget that renders the icon.
///
/// Use [`rounded::req`] to get a [`GlyphIcon`] directly for use in the [`Icon!`] widget.
///
/// [`Icon!`]: struct@Icon
///
/// | Name | Icon |
/// |------|------|
#[doc = include_str!(concat!(env!("OUT_DIR"), "/generated.rounded.docs.txt"))]
#[cfg(feature = "rounded")]
pub mod rounded {
use super::*;
/// "Material Icons Rounded".
pub const FONT_NAME: FontName = FontName::from_static("Material Icons Rounded");
/// Embedded font bytes.
#[cfg(feature = "embedded")]
pub const FONT_BYTES: &[u8] = include_bytes!("../fonts/MaterialIconsRound-Regular.otf");
include!(concat!(env!("OUT_DIR"), "/generated.rounded.map.rs"));
getters!(FONT_NAME, MAP);
}
/// Sharp icons.
///
/// This is the "Material Icons Sharp" font.
///
/// # Icons
///
/// Use the [`ICONS`] service with key `"material/sharp/{name}"` or `"{name}"` to get an widget that renders the icon.
///
/// Use [`sharp::req`] to get a [`GlyphIcon`] directly for use in the [`Icon!`] widget.
///
/// [`Icon!`]: struct@Icon
///
/// | Name | Icon |
/// |------|------|
#[doc = include_str!(concat!(env!("OUT_DIR"), "/generated.sharp.docs.txt"))]
#[cfg(feature = "sharp")]
pub mod sharp {
use super::*;
/// "Material Icons Sharp".
pub const FONT_NAME: FontName = FontName::from_static("Material Icons Sharp");
/// Embedded font bytes.
#[cfg(feature = "embedded")]
pub const FONT_BYTES: &[u8] = include_bytes!("../fonts/MaterialIconsSharp-Regular.otf");
include!(concat!(env!("OUT_DIR"), "/generated.sharp.map.rs"));
getters!(FONT_NAME, MAP);
}