// Icons: one free set, Lucide (ISC licence, icons/lucide/LICENSE), drawn from its SVG files so // every icon in the app has the same 2 px rounded line on a 24 px grid — no emoji anywhere. // Each SVG is rendered once per size (white), kept as a texture and tinted when painted. use eframe::egui::{self, vec2, Color32, Painter, Rect}; use std::collections::HashMap; use std::sync::{Mutex, OnceLock}; #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub enum Icon { SidebarCollapse, SidebarExpand, Plus, Search, Gear, More, ArrowUp, Stop, Monitor, Mic, Sparkle, Chat, Shield, Vault, Key, CreditCard, Person, Eye, CloudDownload, Clock, Calendar, Wave, Close, Download, Smile, Reply, Apps, Paperclip, Record, Pin, Folder, Bell, Pencil, Copy, EyeOff, Trash, ArrowDown, Refresh, Star, Share, Upload, NewChat, History, Eraser, Bot, Users, Template, Check, FileSheet, FileText, FileCode, FileImage, FileVideo, FileAudio, File, Lock, Update, Zap, Alert, Summary, Plane, MapPin, CloudSun, ShoppingBag, Message, Alarm, CircleCheck, CircleX, Minus, Loader, Play, Pause, FileJson, FileWord, ChevronRight, } impl Icon { pub fn from_name(n: &str) -> Option { Some(match n { "◧" => Icon::SidebarCollapse, "◨" => Icon::SidebarExpand, "+" => Icon::Plus, "🔍" => Icon::Search, "⚙" => Icon::Gear, "⋯" => Icon::More, "↑" => Icon::ArrowUp, "■" => Icon::Stop, "monitor" => Icon::Monitor, "mic" => Icon::Mic, _ => return None, }) } /// The Lucide file. fn svg(self) -> &'static str { match self { Icon::ChevronRight => include_str!("../icons/lucide/chevron-right.svg"), Icon::SidebarCollapse => include_str!("../icons/lucide/panel-left-close.svg"), Icon::SidebarExpand => include_str!("../icons/lucide/panel-left-open.svg"), Icon::Plus => include_str!("../icons/lucide/plus.svg"), Icon::Search => include_str!("../icons/lucide/search.svg"), Icon::Gear => include_str!("../icons/lucide/settings.svg"), Icon::More => include_str!("../icons/lucide/ellipsis.svg"), Icon::ArrowUp => include_str!("../icons/lucide/arrow-up.svg"), Icon::Stop => include_str!("../icons/lucide/square.svg"), Icon::Monitor => include_str!("../icons/lucide/monitor.svg"), Icon::Mic => include_str!("../icons/lucide/mic.svg"), Icon::Sparkle => include_str!("../icons/lucide/sparkles.svg"), Icon::Chat => include_str!("../icons/lucide/messages-square.svg"), Icon::Shield => include_str!("../icons/lucide/shield-check.svg"), Icon::Vault => include_str!("../icons/lucide/vault.svg"), Icon::Key => include_str!("../icons/lucide/key-round.svg"), Icon::CreditCard => include_str!("../icons/lucide/credit-card.svg"), Icon::Person => include_str!("../icons/lucide/user-round.svg"), Icon::Eye => include_str!("../icons/lucide/eye.svg"), Icon::CloudDownload => include_str!("../icons/lucide/cloud-download.svg"), Icon::Clock => include_str!("../icons/lucide/clock.svg"), Icon::Calendar => include_str!("../icons/lucide/calendar.svg"), Icon::Wave => include_str!("../icons/lucide/audio-lines.svg"), Icon::Close => include_str!("../icons/lucide/x.svg"), Icon::Download => include_str!("../icons/lucide/download.svg"), Icon::Smile => include_str!("../icons/lucide/smile-plus.svg"), Icon::Reply => include_str!("../icons/lucide/reply.svg"), Icon::Apps => include_str!("../icons/lucide/layout-grid.svg"), Icon::Paperclip => include_str!("../icons/lucide/paperclip.svg"), Icon::Record => include_str!("../icons/lucide/circle-dot.svg"), Icon::Pin => include_str!("../icons/lucide/pin.svg"), Icon::Folder => include_str!("../icons/lucide/folder.svg"), Icon::Bell => include_str!("../icons/lucide/bell-dot.svg"), Icon::Pencil => include_str!("../icons/lucide/pencil.svg"), Icon::Copy => include_str!("../icons/lucide/copy.svg"), Icon::EyeOff => include_str!("../icons/lucide/eye-off.svg"), Icon::Trash => include_str!("../icons/lucide/trash-2.svg"), Icon::ArrowDown => include_str!("../icons/lucide/arrow-down.svg"), Icon::Refresh => include_str!("../icons/lucide/refresh-cw.svg"), Icon::Star => include_str!("../icons/lucide/star.svg"), Icon::Share => include_str!("../icons/lucide/share.svg"), Icon::Upload => include_str!("../icons/lucide/upload.svg"), Icon::NewChat => include_str!("../icons/lucide/square-pen.svg"), Icon::History => include_str!("../icons/lucide/history.svg"), Icon::Eraser => include_str!("../icons/lucide/eraser.svg"), Icon::Bot => include_str!("../icons/lucide/bot.svg"), Icon::Users => include_str!("../icons/lucide/users.svg"), Icon::Template => include_str!("../icons/lucide/layout-template.svg"), Icon::Check => include_str!("../icons/lucide/check.svg"), Icon::FileSheet => include_str!("../icons/lucide/file-spreadsheet.svg"), Icon::FileText => include_str!("../icons/lucide/file-text.svg"), Icon::FileCode => include_str!("../icons/lucide/file-code.svg"), Icon::FileImage => include_str!("../icons/lucide/image.svg"), Icon::FileVideo => include_str!("../icons/lucide/file-video.svg"), Icon::FileAudio => include_str!("../icons/lucide/file-music.svg"), Icon::File => include_str!("../icons/lucide/file.svg"), Icon::Lock => include_str!("../icons/lucide/lock.svg"), Icon::Update => include_str!("../icons/lucide/circle-arrow-up.svg"), Icon::Zap => include_str!("../icons/lucide/zap.svg"), Icon::Alert => include_str!("../icons/lucide/circle-alert.svg"), Icon::Summary => include_str!("../icons/lucide/notebook-pen.svg"), Icon::Plane => include_str!("../icons/lucide/plane.svg"), Icon::MapPin => include_str!("../icons/lucide/map-pin.svg"), Icon::CloudSun => include_str!("../icons/lucide/cloud-sun.svg"), Icon::ShoppingBag => include_str!("../icons/lucide/shopping-bag.svg"), Icon::Message => include_str!("../icons/lucide/message-circle.svg"), Icon::Alarm => include_str!("../icons/lucide/alarm-clock.svg"), Icon::CircleCheck => include_str!("../icons/lucide/circle-check.svg"), Icon::CircleX => include_str!("../icons/lucide/circle-x.svg"), Icon::Minus => include_str!("../icons/lucide/minus.svg"), Icon::Loader => include_str!("../icons/lucide/loader.svg"), Icon::Play => include_str!("../icons/lucide/play.svg"), Icon::Pause => include_str!("../icons/lucide/pause.svg"), Icon::FileJson => include_str!("../icons/lucide/file-json.svg"), Icon::FileWord => include_str!("../icons/lucide/file-type.svg"), } } /// Filled shapes (stop is a solid square, like a media stop button). fn filled(self) -> bool { matches!(self, Icon::Stop | Icon::Play | Icon::Pause) } } /// The icon for a file name (cards, previews, the phone page). pub fn for_file(name: &str) -> Icon { match crate::preview::kind_of(name) { "sheet" => Icon::FileSheet, "image" => Icon::FileImage, "video" => Icon::FileVideo, "audio" => Icon::FileAudio, "text" | "html" => Icon::FileCode, "json" => Icon::FileJson, "docx" | "doc" => Icon::FileWord, "markdown" | "pdf" => Icon::FileText, _ => Icon::File, } } type Cache = Mutex>; fn cache() -> &'static Cache { static C: OnceLock = OnceLock::new(); C.get_or_init(|| Mutex::new(HashMap::new())) } /// Render the SVG white at `px` × `px`. fn rasterize(icon: Icon, px: u32) -> Option { let mut svg = icon.svg().replace("currentColor", "#ffffff"); if icon.filled() { svg = svg.replace("fill=\"none\"", "fill=\"#ffffff\""); } let tree = resvg::usvg::Tree::from_str(&svg, &resvg::usvg::Options::default()).ok()?; let mut pixmap = resvg::tiny_skia::Pixmap::new(px, px)?; let size = tree.size(); let scale = px as f32 / size.width().max(size.height()); resvg::render(&tree, resvg::tiny_skia::Transform::from_scale(scale, scale), &mut pixmap.as_mut()); // tiny-skia keeps premultiplied RGBA, which is what egui wants. let data: Vec = pixmap.pixels().iter().map(|p| Color32::from_rgba_premultiplied(p.red(), p.green(), p.blue(), p.alpha())).collect(); Some(egui::ColorImage { size: [px as usize, px as usize], pixels: data, source_size: vec2(px as f32, px as f32) }) } /// Draw `icon` filling `rect` (square), in `col`. pub fn paint(p: &Painter, rect: Rect, icon: Icon, col: Color32) { let ppp = p.ctx().pixels_per_point(); let side = rect.width().min(rect.height()); // Quantised size (2 px steps, capped): a resized window used to create a new texture for // every pixel width, so the cache grew without limit while the window was dragged. let px = ((side * ppp).round().max(8.0) as u32 / 2 * 2).clamp(8, 512); let tex = { let mut c = cache().lock().unwrap(); match c.get(&(icon, px)) { Some(t) => t.clone(), None => { let Some(img) = rasterize(icon, px) else { return }; // Roomy hard cap: past it, start the cache fresh (textures reload on demand). if c.len() >= 1500 { c.clear(); } let t = p.ctx().load_texture(format!("icon-{icon:?}-{px}"), img, egui::TextureOptions::LINEAR); c.insert((icon, px), t.clone()); t } } }; let r = Rect::from_center_size(rect.center(), vec2(side, side)); p.image(tex.id(), r, Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0)), col); } /// An icon followed by words, on one line. pub fn label(ui: &mut egui::Ui, icon: Icon, text: &str, size: f32, col: Color32) -> egui::Response { ui.horizontal(|ui| { ui.spacing_mut().item_spacing.x = 6.0; let (r, _) = ui.allocate_exact_size(vec2(size + 1.0, size + 2.0), egui::Sense::hover()); paint(ui.painter(), Rect::from_center_size(r.center(), vec2(size, size)), icon, col); ui.label(egui::RichText::new(text).size(size).color(col)); }) .response } /// A rounded button with an icon and words (pills like 「需要你允許」). pub fn pill(ui: &mut egui::Ui, icon: Icon, text: &str, fill: Color32, fg: Color32, height: f32) -> egui::Response { let font = egui::FontId::proportional(13.0); let galley = ui.painter().layout_no_wrap(text.to_string(), font, fg); let icon_s = 14.0; let w = 12.0 + icon_s + 6.0 + galley.size().x + 14.0; let (rect, r) = ui.allocate_exact_size(vec2(w, height), egui::Sense::click()); let f = if r.hovered() { fill.gamma_multiply(0.88) } else { fill }; ui.painter().rect_filled(rect, height / 2.0, f); paint(ui.painter(), Rect::from_center_size(egui::pos2(rect.left() + 12.0 + icon_s / 2.0, rect.center().y), vec2(icon_s, icon_s)), icon, fg); ui.painter().galley(egui::pos2(rect.left() + 12.0 + icon_s + 6.0, rect.center().y - galley.size().y / 2.0), galley, fg); r.on_hover_cursor(egui::CursorIcon::PointingHand) } /// A borderless icon button (hover shows a soft rounded square). pub fn button(ui: &mut egui::Ui, icon: Icon, size: f32, col: Color32, hover_fill: Color32) -> egui::Response { let (rect, r) = ui.allocate_exact_size(vec2(size, size), egui::Sense::click()); if r.hovered() { ui.painter().rect_filled(rect, 7.0, hover_fill); } let inner = Rect::from_center_size(rect.center(), vec2(16.0, 16.0)); paint(ui.painter(), inner, icon, col); r.on_hover_cursor(egui::CursorIcon::PointingHand) } #[cfg(test)] mod tests { use super::*; #[test] fn every_icon_renders() { for icon in [Icon::Plus, Icon::Gear, Icon::Stop, Icon::FileSheet, Icon::Trash, Icon::Wave, Icon::Summary, Icon::Lock, Icon::Update, Icon::Play, Icon::Pause, Icon::FileJson, Icon::FileWord] { let img = rasterize(icon, 32).expect("renders"); assert_eq!(img.size, [32, 32]); assert!(img.pixels.iter().any(|p| p.a() > 0), "{icon:?} is empty"); } assert_eq!(for_file("a.xlsx"), Icon::FileSheet); assert_eq!(for_file("a.png"), Icon::FileImage); } }