use crate::{Color, Frame, Image, ImageFit, Rect};
use std::collections::HashMap;
use std::sync::{Mutex, OnceLock};
const SYS_ICON_RASTER_SIZE: u32 = 96;
const SYS_SVG_COUNT: usize = 68;
const SYS_MSDF_BLOB: &[u8] = include_bytes!("../../assets/material-system/icons.msdf");
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SystemIcon {
Wifi,
WifiOff,
Bluetooth,
Flashlight,
Alarm,
Airplane,
DeviceControls,
AutoRotate,
BatterySaver,
DarkMode,
Dnd,
ScreenCast,
ScreenRecord,
NightLight,
Camera,
Mic,
Aod,
Hotspot,
Location,
Nfc,
InvertColors,
Sun,
AutoBrightness,
Power,
Restart,
Screenshot,
Emergency,
Vibrate,
Silent,
Ring,
MusicNote,
Bell,
Info,
SplitScreen,
Freeform,
Maximize,
Close,
Settings,
Widgets,
Palette,
Search,
Back,
ChevronRight,
ChevronLeft,
ChevronDown,
ChevronUp,
Edit,
Trash,
Battery,
Storage,
Sound,
Display,
Accessibility,
Security,
Privacy,
Accounts,
System,
PhoneInfo,
AppsGrid,
Usb,
History,
MoreVert,
VisibilityOff,
LockOpen,
Fingerprint,
Tune,
NotificationsActive,
Gesture,
CheckCircle,
Devices,
Language,
}
impl SystemIcon {
pub const ALL: &'static [Self] = &[
Self::Wifi, Self::WifiOff, Self::Bluetooth, Self::Flashlight, Self::Alarm, Self::Airplane,
Self::DeviceControls, Self::AutoRotate, Self::BatterySaver, Self::DarkMode, Self::Dnd,
Self::ScreenCast, Self::ScreenRecord, Self::NightLight, Self::Camera, Self::Mic, Self::Aod,
Self::Hotspot, Self::Location, Self::Nfc, Self::InvertColors, Self::Sun,
Self::AutoBrightness, Self::Power, Self::Restart, Self::Screenshot, Self::Emergency,
Self::Vibrate, Self::Silent, Self::Ring, Self::MusicNote, Self::Bell, Self::Info,
Self::SplitScreen, Self::Freeform, Self::Maximize, Self::Close, Self::Settings,
Self::Widgets, Self::Palette, Self::Search, Self::Back, Self::ChevronRight,
Self::ChevronLeft, Self::ChevronDown, Self::ChevronUp, Self::Edit, Self::Trash,
Self::Battery, Self::Storage, Self::Sound, Self::Display, Self::Accessibility,
Self::Security, Self::Privacy, Self::Accounts, Self::System, Self::PhoneInfo,
Self::AppsGrid, Self::Usb, Self::History, Self::MoreVert, Self::VisibilityOff,
Self::LockOpen, Self::Fingerprint, Self::Tune, Self::NotificationsActive,
Self::Gesture, Self::CheckCircle, Self::Devices, Self::Language,
];
pub(crate) fn mask_index(self) -> usize {
match self {
Self::Wifi => 0,
Self::WifiOff => 1,
Self::Bluetooth => 2,
Self::Flashlight => 3,
Self::Alarm => 4,
Self::Airplane => 5,
Self::DeviceControls => 6,
Self::AutoRotate => 7,
Self::BatterySaver => 8,
Self::DarkMode => 9,
Self::Dnd => 10,
Self::ScreenCast => 11,
Self::ScreenRecord => 12,
Self::NightLight => 13,
Self::Camera => 14,
Self::Mic => 15,
Self::Aod => 16,
Self::Hotspot => 17,
Self::Location => 18,
Self::Nfc => 19,
Self::InvertColors => 20,
Self::Sun | Self::Display => 21,
Self::AutoBrightness => 22,
Self::Power => 23,
Self::Restart => 24,
Self::Screenshot => 25,
Self::Emergency => 26,
Self::Vibrate => 27,
Self::Silent => 28,
Self::Ring | Self::Bell => 29,
Self::MusicNote => 30,
Self::Info | Self::System => 31,
Self::SplitScreen => 32,
Self::Freeform => 33,
Self::Maximize => 34,
Self::Close => 35,
Self::Settings => 36,
Self::Widgets => 37,
Self::Palette => 38,
Self::Search => 39,
Self::Back => 40,
Self::ChevronRight => 41,
Self::ChevronLeft => 42,
Self::ChevronDown => 43,
Self::ChevronUp => 44,
Self::Edit => 45,
Self::Trash => 46,
Self::Battery => 47,
Self::Storage => 48,
Self::Sound => 49,
Self::Accessibility => 50,
Self::Security => 51,
Self::Privacy => 52,
Self::Accounts => 53,
Self::PhoneInfo => 54,
Self::AppsGrid => 55,
Self::Usb => 56,
Self::History => 57,
Self::MoreVert => 58,
Self::VisibilityOff => 59,
Self::LockOpen => 60,
Self::Fingerprint => 61,
Self::Tune => 62,
Self::NotificationsActive => 63,
Self::Gesture => 64,
Self::CheckCircle => 65,
Self::Devices => 66,
Self::Language => 67,
}
}
}
pub fn decode_rle4(rle: &[u8], total: usize) -> Vec<u8> {
let mut mask = Vec::with_capacity(total);
for &b in rle {
let v = (b >> 4) * 17;
let run = ((b & 0x0F) as usize) + 1;
let take = run.min(total - mask.len());
mask.resize(mask.len() + take, v);
if mask.len() >= total {
break;
}
}
mask.resize(total, 0);
mask
}
pub fn decode_rle8(rle: &[u8], total: usize) -> Vec<u8> {
let mut mask = Vec::with_capacity(total);
let mut i = 0;
while i < rle.len() && mask.len() < total {
let b = rle[i];
i += 1;
if b < 0x80 {
let take = ((b as usize) + 1).min(total - mask.len());
mask.resize(mask.len() + take, 0);
} else if b < 0xC0 {
let take = (((b - 0x80) as usize) + 1).min(total - mask.len());
mask.resize(mask.len() + take, 255);
} else {
let count = ((b - 0xC0) as usize) + 1;
let avail = count.min(rle.len().saturating_sub(i));
let take = avail.min(total - mask.len());
mask.extend_from_slice(&rle[i..i + take]);
i += count;
}
}
mask.resize(total, 0);
mask
}
fn decode_msdf_entry(idx: usize) -> (u32, f32, Vec<u8>) {
debug_assert!(SYS_MSDF_BLOB.len() >= 12 && &SYS_MSDF_BLOB[0..4] == b"MSDF");
let count = u16::from_le_bytes([SYS_MSDF_BLOB[4], SYS_MSDF_BLOB[5]]) as usize;
let size = u16::from_le_bytes([SYS_MSDF_BLOB[6], SYS_MSDF_BLOB[7]]) as u32;
let px_range = f32::from_le_bytes([
SYS_MSDF_BLOB[8],
SYS_MSDF_BLOB[9],
SYS_MSDF_BLOB[10],
SYS_MSDF_BLOB[11],
]);
assert!(idx < count, "SystemIcon index {idx} out of range {count}");
let table_start = 12usize;
let payload_start = table_start + (count + 1) * 4;
let off0 = u32::from_le_bytes(
SYS_MSDF_BLOB[table_start + idx * 4..table_start + (idx + 1) * 4]
.try_into()
.unwrap(),
) as usize;
let off1 = u32::from_le_bytes(
SYS_MSDF_BLOB[table_start + (idx + 1) * 4..table_start + (idx + 2) * 4]
.try_into()
.unwrap(),
) as usize;
let plane_len = (size * size) as usize;
let planar = decode_rle8(&SYS_MSDF_BLOB[payload_start + off0..payload_start + off1], plane_len * 3);
let (r_plane, rest) = planar.split_at(plane_len);
let (g_plane, b_plane) = rest.split_at(plane_len);
let mut rgba = vec![255u8; plane_len * 4];
for i in 0..plane_len {
let o = i * 4;
rgba[o] = r_plane[i];
rgba[o + 1] = g_plane[i];
rgba[o + 2] = b_plane[i];
}
(size, px_range, rgba)
}
pub fn system_icon_msdf(icon: SystemIcon) -> Image {
let idx = icon.mask_index();
static MSDF_CACHE: OnceLock<[OnceLock<Image>; SYS_SVG_COUNT]> = OnceLock::new();
let slots = MSDF_CACHE.get_or_init(|| std::array::from_fn(|_| OnceLock::new()));
slots[idx]
.get_or_init(|| {
let (size, px_range, rgba) = decode_msdf_entry(idx);
Image::msdf(size, size, px_range, rgba).expect("valid embedded MSDF system icon")
})
.clone()
}
#[inline]
fn median3(r: f32, g: f32, b: f32) -> f32 {
r.min(g).max(r.max(g).min(b))
}
pub fn system_icon_image(icon: SystemIcon, color: Color) -> Image {
let idx = icon.mask_index();
let qa = if color.3 >= 248 {
255
} else {
((color.3 as u16 + 8) / 17 * 17).min(255) as u8
};
let key = (idx as u16, color.0, color.1, color.2, qa);
static SYS_CACHE: OnceLock<Mutex<HashMap<(u16, u8, u8, u8, u8), Image>>> = OnceLock::new();
let lock = SYS_CACHE.get_or_init(|| Mutex::new(HashMap::new()));
let mut map = lock.lock().expect("sys icon cache lock");
if let Some(existing) = map.get(&key) {
return existing.clone();
}
if map.len() > 280 {
map.clear();
}
let msdf = system_icon_msdf(icon);
let src_w = msdf.width() as usize;
let src_h = msdf.height() as usize;
let src = msdf.pixels();
let px_range = msdf.msdf_px_range();
let dst_size = SYS_ICON_RASTER_SIZE as usize;
let screen_px_range = (px_range * (dst_size as f32 / (src_w as f32 - 1.0))).max(1.0);
let mut rgba = vec![0u8; dst_size * dst_size * 4];
for dy in 0..dst_size {
let sy = ((dy as f32 + 0.5) / dst_size as f32) * (src_h as f32 - 1.0);
let y0 = (sy.floor() as usize).min(src_h - 1);
let y1 = (y0 + 1).min(src_h - 1);
let fy = sy - y0 as f32;
for dx in 0..dst_size {
let sx = ((dx as f32 + 0.5) / dst_size as f32) * (src_w as f32 - 1.0);
let x0 = (sx.floor() as usize).min(src_w - 1);
let x1 = (x0 + 1).min(src_w - 1);
let fx = sx - x0 as f32;
let i00 = (y0 * src_w + x0) * 4;
let i10 = (y0 * src_w + x1) * 4;
let i01 = (y1 * src_w + x0) * 4;
let i11 = (y1 * src_w + x1) * 4;
let w00 = (1.0 - fx) * (1.0 - fy);
let w10 = fx * (1.0 - fy);
let w01 = (1.0 - fx) * fy;
let w11 = fx * fy;
let r = (src[i00] as f32 * w00
+ src[i10] as f32 * w10
+ src[i01] as f32 * w01
+ src[i11] as f32 * w11)
* (1.0 / 255.0);
let g = (src[i00 + 1] as f32 * w00
+ src[i10 + 1] as f32 * w10
+ src[i01 + 1] as f32 * w01
+ src[i11 + 1] as f32 * w11)
* (1.0 / 255.0);
let b = (src[i00 + 2] as f32 * w00
+ src[i10 + 2] as f32 * w10
+ src[i01 + 2] as f32 * w01
+ src[i11 + 2] as f32 * w11)
* (1.0 / 255.0);
let sd = median3(r, g, b) - 0.5;
let cov = (screen_px_range * sd + 0.5).clamp(0.0, 1.0);
let m = (cov * 255.0 + 0.5) as u8;
if m > 0 {
let o = (dy * dst_size + dx) * 4;
rgba[o] = color.0;
rgba[o + 1] = color.1;
rgba[o + 2] = color.2;
rgba[o + 3] = ((m as u16 * qa as u16 + 127) / 255) as u8;
}
}
}
let created = Image::rgba(SYS_ICON_RASTER_SIZE, SYS_ICON_RASTER_SIZE, rgba)
.expect("valid 96x96 sys icon");
map.insert(key, created.clone());
created
}
impl Frame<'_> {
pub fn paint_system_icon(&mut self, bounds: Rect, icon: SystemIcon, color: Color) {
if color.3 <= 2 || bounds.width <= 0.5 || bounds.height <= 0.5 {
return;
}
#[cfg(feature = "gles")]
{
let img = system_icon_msdf(icon);
self.paint_tinted_image(bounds, &img, ImageFit::Contain, 0.0, color);
}
#[cfg(not(feature = "gles"))]
{
let img = system_icon_image(icon, color);
self.paint_image(bounds, &img, ImageFit::Contain, 0.0);
}
}
}
#[cfg(test)]
#[path = "material_system.spec.rs"]
mod tests;