AnyBot/src/icons.rs

520 lines
24 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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,
Bookmark,
Brain,
Target,
// 工作流 (the canvas and its tools).
Workflow,
Split,
Hand,
Pointer,
StickyNote,
Square,
Type,
Map,
Scan,
Send,
// 設定's rows (setting_icon).
Palette,
Moon,
Globe,
Sliders,
Volume,
Drive,
}
impl Icon {
pub fn from_name(n: &str) -> Option<Icon> {
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::Bookmark => include_str!("../icons/lucide/bookmark.svg"),
Icon::Brain => include_str!("../icons/lucide/brain.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::Target => include_str!("../icons/lucide/target.svg"),
Icon::Workflow => include_str!("../icons/lucide/workflow.svg"),
Icon::Split => include_str!("../icons/lucide/split.svg"),
Icon::Hand => include_str!("../icons/lucide/hand.svg"),
Icon::Pointer => include_str!("../icons/lucide/mouse-pointer-2.svg"),
Icon::StickyNote => include_str!("../icons/lucide/sticky-note.svg"),
Icon::Square => include_str!("../icons/lucide/square.svg"),
Icon::Type => include_str!("../icons/lucide/type.svg"),
Icon::Map => include_str!("../icons/lucide/map.svg"),
Icon::Scan => include_str!("../icons/lucide/scan.svg"),
Icon::Send => include_str!("../icons/lucide/send.svg"),
Icon::Palette => include_str!("../icons/lucide/palette.svg"),
Icon::Moon => include_str!("../icons/lucide/moon.svg"),
Icon::Globe => include_str!("../icons/lucide/globe.svg"),
Icon::Sliders => include_str!("../icons/lucide/sliders-horizontal.svg"),
Icon::Volume => include_str!("../icons/lucide/volume-2.svg"),
Icon::Drive => include_str!("../icons/lucide/hard-drive.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<HashMap<(Icon, u32), egui::TextureHandle>>;
fn cache() -> &'static Cache {
static C: OnceLock<Cache> = OnceLock::new();
C.get_or_init(|| Mutex::new(HashMap::new()))
}
/// Render the SVG white at `px` × `px`.
fn rasterize(icon: Icon, px: u32) -> Option<egui::ColorImage> {
// (A little heavier than Lucide draws them: 2.25 sits better next to the text.)
let mut svg = icon.svg().replace("currentColor", "#ffffff").replace("stroke-width=\"2\"", "stroke-width=\"2.25\"");
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<Color32> = 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 pill exactly `width` wide (a whole row), icon and words in the middle. The words are the
/// first of `texts` that fits, or else the last cut short with "…" — never wider than the row.
pub fn wide_pill(ui: &mut egui::Ui, icon: Icon, texts: &[String], fill: Color32, fg: Color32, height: f32, width: f32) -> egui::Response {
let font = egui::FontId::proportional(13.0);
let icon_s = 14.0;
let room = (width - 12.0 * 2.0 - icon_s - 6.0).max(0.0);
let measure = |t: &str| ui.painter().layout_no_wrap(t.to_string(), font.clone(), fg).size().x;
let text = texts.iter().find(|t| measure(t) <= room).cloned().unwrap_or_else(|| {
let mut chars: Vec<char> = texts.last().map(|t| t.chars().collect()).unwrap_or_default();
while !chars.is_empty() && measure(&format!("{}…", chars.iter().collect::<String>())) > room {
chars.pop();
}
format!("{}…", chars.into_iter().collect::<String>())
});
let galley = ui.painter().layout_no_wrap(text, font, fg);
let (rect, r) = ui.allocate_exact_size(vec2(width, 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);
let left = rect.center().x - (icon_s + 6.0 + galley.size().x) / 2.0;
paint(ui.painter(), Rect::from_center_size(egui::pos2(left + icon_s / 2.0, rect.center().y), vec2(icon_s, icon_s)), icon, fg);
ui.painter().galley(egui::pos2(left + 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)
}
/// The picture before a setting's name (設定: every row has one — the eye finds 通知 by its bell
/// before it reads the word). By the words in its name, first match wins; a name that matches
/// none gets the plain sliders.
pub fn setting_icon(label: &str) -> Icon {
const RULES: &[(&[&str], Icon)] = &[
(&["上網"], Icon::Globe),
(&["頁面"], Icon::FileText),
(&["合作", "同事"], Icon::Users),
(&["電話", "簡訊"], Icon::Message),
(&["主題"], Icon::Moon),
(&["風格", "強調色", "顏色", "底色", "對比", "外觀", "外殼"], Icon::Palette),
(&["密度"], Icon::Apps),
(&["慶祝"], Icon::Star),
(&["頭像"], Icon::Smile),
(&["語言"], Icon::Globe),
(&["時區", "時間"], Icon::Clock),
(&["通知", "響一聲", "提醒"], Icon::Bell),
(&["記憶", "意思找"], Icon::Brain),
(&["資料夾", "文件", "檔案"], Icon::Folder),
(&["金鑰", "Token", "token", "密鑰"], Icon::Key),
(&["密碼", "保險庫", "兩步"], Icon::Lock),
(&["瀏覽器", "網址", "網頁"], Icon::Globe),
(&["發布前", "檢查"], Icon::CircleCheck),
(&["更新", "版本", "重新啟動"], Icon::Update),
(&["備份", "快照", "還原"], Icon::History),
(&["磁碟", "用量", "容量"], Icon::Drive),
(&["團隊", "成員", "分享"], Icon::Users),
(&["帳號", "帳戶", "登入", "登出", "個人"], Icon::Person),
(&["手機", "裝置"], Icon::Monitor),
(&["語音", "聽", "說", "朗讀", "聲音"], Icon::Volume),
(&["麥克風", "錄音"], Icon::Mic),
(&["審核", "規則", "權限", "直接操作", "一律", "允許", "詢問", "隔離", "智慧"], Icon::Shield),
(&["連接器", "MCP", "整合"], Icon::Apps),
(&["管道", "Telegram", "Slack", "Discord", "訊息"], Icon::Message),
(&["技能"], Icon::Sparkle),
(&["模型", "AI"], Icon::Sparkle),
(&["電腦", "螢幕", "SSH", "Docker", "主機", "容器", "終端機", "執行"], Icon::Monitor),
(&["例行", "排程", "日程"], Icon::Alarm),
(&["工作流"], Icon::Workflow),
(&["Bot"], Icon::Bot),
(&["園丁", "改進", "診斷"], Icon::Target),
(&["資料", "放在哪"], Icon::Drive),
];
RULES.iter().find(|(words, _)| words.iter().any(|w| label.contains(w))).map(|(_, i)| *i).unwrap_or(Icon::Sliders)
}
/// A model service's mark: a small rounded tile in the service's own colour with a simple shape
/// drawn here (no one's artwork copied — just enough to tell them apart at a glance, the way an
/// app icon is found before its name is read). The same tile in light and dark.
pub fn provider_mark(p: &Painter, rect: Rect, kind: crate::providers::ProviderKind, radius: u8) {
use crate::providers::ProviderKind as K;
use egui::{pos2, Shape, Stroke};
let rgb = |r, g, b| Color32::from_rgb(r, g, b);
let (tile, ink) = match kind {
K::Xai => (rgb(0x14, 0x14, 0x14), Color32::WHITE),
K::OpenAi => (rgb(0x14, 0x14, 0x14), Color32::WHITE),
K::ChatGpt => (rgb(0x10, 0xa3, 0x7f), Color32::WHITE),
K::Anthropic => (rgb(0xd9, 0x77, 0x57), Color32::WHITE),
K::Gemini => (rgb(0xee, 0xf2, 0xfd), rgb(0x3f, 0x7b, 0xf0)),
K::OpenRouter => (rgb(0x64, 0x67, 0xf2), Color32::WHITE),
K::OpenCodeGo => (rgb(0x23, 0x20, 0x1e), Color32::WHITE),
K::Ollama => (rgb(0xf4, 0xf4, 0xf2), rgb(0x1a, 0x1a, 0x1a)),
K::Custom => (rgb(0x6b, 0x72, 0x80), Color32::WHITE),
};
p.rect_filled(rect, radius, tile);
let c = rect.center();
let r = rect.width().min(rect.height()) * 0.28;
let line = |w: f32| Stroke::new((rect.width() * w).max(1.2), ink);
match kind {
// A plain X, one stroke longer.
K::Xai => {
p.line_segment([pos2(c.x - r, c.y - r), pos2(c.x + r, c.y + r)], line(0.075));
p.line_segment([pos2(c.x + r * 1.1, c.y - r * 1.25), pos2(c.x - r, c.y + r)], line(0.075));
}
// Six rings around a centre: a flower.
K::OpenAi | K::ChatGpt => {
for i in 0..6 {
let a = i as f32 * std::f32::consts::TAU / 6.0;
p.circle_stroke(c + vec2(a.cos(), a.sin()) * r * 0.45, r * 0.55, line(0.045));
}
}
// A burst of rays.
K::Anthropic => {
for i in 0..10 {
let a = i as f32 * std::f32::consts::TAU / 10.0;
let len = if i % 2 == 0 { r * 1.15 } else { r * 0.8 };
p.line_segment([c + vec2(a.cos(), a.sin()) * r * 0.18, c + vec2(a.cos(), a.sin()) * len], line(0.07));
}
}
// A four-pointed sparkle (four thin triangles from a small centre).
K::Gemini => {
let k = r * 0.2;
for (tip, a, b) in [
(pos2(c.x, c.y - r * 1.3), pos2(c.x - k, c.y), pos2(c.x + k, c.y)),
(pos2(c.x, c.y + r * 1.3), pos2(c.x - k, c.y), pos2(c.x + k, c.y)),
(pos2(c.x - r * 1.3, c.y), pos2(c.x, c.y - k), pos2(c.x, c.y + k)),
(pos2(c.x + r * 1.3, c.y), pos2(c.x, c.y - k), pos2(c.x, c.y + k)),
] {
p.add(Shape::convex_polygon(vec![tip, a, b], ink, Stroke::NONE));
}
}
// Two arrows passing each other: a router.
K::OpenRouter => {
let s = line(0.06);
let (y1, y2) = (c.y - r * 0.45, c.y + r * 0.45);
p.line_segment([pos2(c.x - r, y1), pos2(c.x + r, y1)], s);
p.line_segment([pos2(c.x + r, y1), pos2(c.x + r * 0.55, y1 - r * 0.4)], s);
p.line_segment([pos2(c.x + r, y2), pos2(c.x - r, y2)], s);
p.line_segment([pos2(c.x - r, y2), pos2(c.x - r * 0.55, y2 + r * 0.4)], s);
}
// Angle brackets: code.
K::OpenCodeGo => {
let s = line(0.06);
p.line_segment([pos2(c.x - r * 0.3, c.y - r * 0.8), pos2(c.x - r, c.y)], s);
p.line_segment([pos2(c.x - r, c.y), pos2(c.x - r * 0.3, c.y + r * 0.8)], s);
p.line_segment([pos2(c.x + r * 0.3, c.y - r * 0.8), pos2(c.x + r, c.y)], s);
p.line_segment([pos2(c.x + r, c.y), pos2(c.x + r * 0.3, c.y + r * 0.8)], s);
}
// A round face with two ears.
K::Ollama => {
let s = line(0.055);
p.circle_stroke(pos2(c.x, c.y + r * 0.25), r * 0.75, s);
p.line_segment([pos2(c.x - r * 0.4, c.y - r * 0.35), pos2(c.x - r * 0.5, c.y - r * 1.1)], s);
p.line_segment([pos2(c.x + r * 0.4, c.y - r * 0.35), pos2(c.x + r * 0.5, c.y - r * 1.1)], s);
p.circle_filled(pos2(c.x - r * 0.28, c.y + r * 0.15), r * 0.09, ink);
p.circle_filled(pos2(c.x + r * 0.28, c.y + r * 0.15), r * 0.09, ink);
}
K::Custom => paint(p, Rect::from_center_size(c, vec2(r * 2.2, r * 2.2)), Icon::Gear, ink),
}
}
#[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);
}
/// A wide pill is exactly as wide as asked, however long its words (the sidebar's
/// 重新啟動以更新 ran past the sidebar's edge).
#[test]
fn a_wide_pill_never_outgrows_its_row() {
let ctx = egui::Context::default();
for width in [60.0, 160.0, 400.0] {
let mut got = 0.0;
let mut out = ctx.run_ui(egui::RawInput::default(), |root| {
let texts = ["重新啟動以更新(2026-11-11 17:23)".to_string(), "重新啟動以更新".to_string()];
got = wide_pill(root, Icon::Update, &texts, Color32::BLUE, Color32::WHITE, 30.0, width).rect.width();
});
out.textures_delta.clear(); // (no renderer here to hand the font atlas to)
assert_eq!(got, width);
}
}
}