Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 32 additions & 131 deletions native/src/avatars.rs
Original file line number Diff line number Diff line change
@@ -1,159 +1,60 @@
//! The icons a bot can have, by the name kept as its avatar (`icons.ts`): drawn on the bot's wash of
//! colour by `view::avatar` (`.avatar svg.lucide`), and as the choices of the icon picker
//! (`.icon-picker`). The drawings are the page's own set.
//! The Solar Linear icons (stroke 1.8) a bot can have, by the name kept as its avatar
//! (`icons.ts` / `solar_icons_data.rs`): drawn on the bot's wash of colour by `view::avatar`
//! (`.avatar svg.solar`), and as the choices of the icon picker (`.icon-picker`).

use std::sync::LazyLock;
use weft::{Color, El, Icon, icon};

/// `.avatar svg.lucide`: the line of an icon on a bot's face.
const STROKE_AVATAR: f32 = 1.5;
/// `svg.lucide`: the line of every other icon.
const STROKE: f32 = 1.75;

/// The name kept as the avatar, and the drawing's body (a 24 by 24 box).
const ICONS: &[(&str, &str)] = &[
(
"bot",
r##"<path d="M12 8V4H8"/><rect width="16" height="12" x="4" y="8" rx="2"/><path d="M2 14h2"/><path d="M20 14h2"/><path d="M15 13v2"/><path d="M9 13v2"/>"##,
),
(
"search",
r##"<path d="m21 21-4.34-4.34"/><circle cx="11" cy="11" r="8"/>"##,
),
(
"pen",
r##"<path d="M13 21h8"/><path d="M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z"/>"##,
),
(
"code",
r##"<path d="m18 16 4-4-4-4"/><path d="m6 8-4 4 4 4"/><path d="m14.5 4-5 16"/>"##,
),
(
"terminal",
r##"<path d="m7 11 2-2-2-2"/><path d="M11 13h4"/><rect width="18" height="18" x="3" y="3" rx="2" ry="2"/>"##,
),
(
"folder",
r##"<path d="M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z"/>"##,
),
(
"compass",
r##"<circle cx="12" cy="12" r="10"/><path d="m16.24 7.76-1.804 5.411a2 2 0 0 1-1.265 1.265L7.76 16.24l1.804-5.411a2 2 0 0 1 1.265-1.265z"/>"##,
),
(
"brain",
r##"<path d="M12 18V5"/><path d="M15 13a4.17 4.17 0 01-3-4 4.17 4.17 0 01-3 4"/><path d="M17.707 6.294A3 3 0 1012 5a3 3 0 10-5.707 1.294"/><path d="M17.996 5.125a4 4 0 012.526 5.77"/><path d="M18 18a4 4 0 002-7.465"/><path d="M19.967 17.482a4 4 0 11-7.967.517 4 4 0 11-7.967-.517"/><path d="M6 18a4 4 0 01-2-7.465"/><path d="M6.004 5.125a4 4 0 00-2.526 5.77"/>"##,
),
(
"book",
r##"<path d="M12 5v16"/><path d="M20.001 19A2 2 0 0022 17V5a2 2 0 00-1.999-2L16 3.002A5 5 0 0012 5a5 5 0 00-4-2H4a2 2 0 00-2 2v12a2 2 0 001.999 2H8a5 5 0 014 2 5 5 0 014-2z"/>"##,
),
(
"bug",
r##"<path d="M12 20v-9"/><path d="M14 7a4 4 0 0 1 4 4v3a6 6 0 0 1-12 0v-3a4 4 0 0 1 4-4z"/><path d="M14.12 3.88 16 2"/><path d="M21 21a4 4 0 0 0-3.81-4"/><path d="M21 5a4 4 0 0 1-3.55 3.97"/><path d="M22 13h-4"/><path d="M3 21a4 4 0 0 1 3.81-4"/><path d="M3 5a4 4 0 0 0 3.55 3.97"/><path d="M6 13H2"/><path d="m8 2 1.88 1.88"/><path d="M9 7.13V6a3 3 0 1 1 6 0v1.13"/>"##,
),
(
"chart",
r##"<path d="M3 3v16a2 2 0 0 0 2 2h16"/><path d="m19 9-5 5-4-4-3 3"/>"##,
),
(
"data",
r##"<ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M3 5V19A9 3 0 0 0 21 19V5"/><path d="M3 12A9 3 0 0 0 21 12"/>"##,
),
(
"globe",
r##"<circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/>"##,
),
(
"shield",
r##"<path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/>"##,
),
(
"wrench",
r##"<path d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.106-3.105c.32-.322.863-.22.983.218a6 6 0 0 1-8.259 7.057l-7.91 7.91a1 1 0 0 1-2.999-3l7.91-7.91a6 6 0 0 1 7.057-8.259c.438.12.54.662.219.984z"/>"##,
),
(
"rocket",
r##"<path d="M12 15v5s3.03-.55 4-2c1.08-1.62 0-5 0-5"/><path d="M4.5 16.5c-1.5 1.26-2 5-2 5s3.74-.5 5-2c.71-.84.7-2.13-.09-2.91a2.18 2.18 0 0 0-2.91-.09"/><path d="M9 12a22 22 0 0 1 2-3.95A12.88 12.88 0 0 1 22 2c0 2.72-.78 7.5-6 11a22.4 22.4 0 0 1-4 2z"/><path d="M9 12H4s.55-3.03 2-4c1.62-1.08 5 .05 5 .05"/>"##,
),
(
"flask",
r##"<path d="M14 2v6a2 2 0 0 0 .245.96l5.51 10.08A2 2 0 0 1 18 22H6a2 2 0 0 1-1.755-2.96l5.51-10.08A2 2 0 0 0 10 8V2"/><path d="M6.453 15h11.094"/><path d="M8.5 2h7"/>"##,
),
(
"mail",
r##"<path d="m22 7-8.991 5.727a2 2 0 0 1-2.009 0L2 7"/><rect x="2" y="4" width="20" height="16" rx="2"/>"##,
),
(
"calendar",
r##"<path d="M8 2v3"/><path d="M16 2v3"/><rect x="3" y="3" width="18" height="18" rx="2"/><path d="M3 9h18"/>"##,
),
(
"camera",
r##"<path d="M13.997 4a2 2 0 0 1 1.76 1.05l.486.9A2 2 0 0 0 18.003 7H20a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h1.997a2 2 0 0 0 1.759-1.048l.489-.904A2 2 0 0 1 10.004 4z"/><circle cx="12" cy="13" r="3"/>"##,
),
(
"music",
r##"<path d="M21 16V3a1 1 0 0 0-1.28-.96l-10 2.917a1 1 0 0 0-.72.96V19"/><circle cx="18" cy="16" r="3"/><circle cx="6" cy="19" r="3"/>"##,
),
(
"sprout",
r##"<path d="M14 9.536V7a4 4 0 0 1 4-4h1.5a.5.5 0 0 1 .5.5V5a4 4 0 0 1-4 4 4 4 0 0 0-4 4c0 2 1 3 1 5a5 5 0 0 1-1 3"/><path d="M4 9a5 5 0 0 1 8 4 5 5 0 0 1-8-4"/><path d="M5 21h14"/>"##,
),
(
"leaf",
r##"<path d="M11 20a10 10 0 0010-10 25.9 25.9 0 00-1.04-7.281 1 1 0 00-1.755-.325C15.833 5.5 13 5.5 9.8 6.1A7 7 0 0011 20"/><path d="M2 21a5 5 0 012.911-4.544C7.613 15.212 8.351 15.24 11 13"/>"##,
),
(
"zap",
r##"<path d="M15.914 4a1.5 1.5 0 00-2.474-1.561l-9 9A1.5 1.5 0 005.5 14h4.002a.5.5 0 01.471.666L8.086 20a1.5 1.5 0 002.475 1.56l9-9A1.5 1.5 0 0018.5 10h-3.997a.5.5 0 01-.472-.667z"/>"##,
),
(
"key",
r##"<path d="M2.586 17.414A2 2 0 0 0 2 18.828V21a1 1 0 0 0 1 1h3a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1h1a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1h.172a2 2 0 0 0 1.414-.586l.814-.814a6.5 6.5 0 1 0-4-4z"/><circle cx="16.5" cy="7.5" r=".5" fill="#000"/>"##,
),
];
use crate::solar_icons_data::{SOLAR_AVATARS, SOLAR_STROKE};

/// The names of the icons, in the order of the picker.
pub fn names() -> impl Iterator<Item = &'static str> {
ICONS.iter().map(|(name, _)| *name)
SOLAR_AVATARS.iter().map(|(name, _)| *name)
}

/// Whether `name` is the name of one of the icons.
pub fn has(name: &str) -> bool {
ICONS.iter().any(|(n, _)| *n == name)
SOLAR_AVATARS.iter().any(|(n, _)| *n == name)
}

fn svg(body: &str, stroke: f32) -> String {
fn svg(body: &str) -> String {
format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\" fill=\"none\" \
stroke=\"#000\" stroke-width=\"{stroke}\" stroke-linecap=\"round\" \
stroke=\"#000\" stroke-width=\"{SOLAR_STROKE}\" stroke-linecap=\"round\" \
stroke-linejoin=\"round\">{body}</svg>"
)
}

/// Each icon drawn with the line of a face and with the usual one.
fn drawn() -> &'static [(Icon, Icon)] {
static SET: LazyLock<Vec<(Icon, Icon)>> = LazyLock::new(|| {
ICONS
/// Each Solar Linear bot icon parsed once and shared.
fn drawn() -> &'static [Icon] {
static SET: LazyLock<Vec<Icon>> = LazyLock::new(|| {
SOLAR_AVATARS
.iter()
.map(|(name, body)| {
let make = |stroke| {
Icon::from_svg(&format!("bot-icon-{name}"), &svg(body, stroke))
.expect("a drawing is valid SVG")
};
(make(STROKE_AVATAR), make(STROKE))
Icon::from_svg(&format!("bot-icon-{name}"), &svg(body))
.expect("a Solar avatar drawing is valid SVG")
})
.collect()
});
&SET
}

/// The icon `name` as an element of `size` px in `color`, with the line a bot's face has
/// (`on_face`) or the usual one; none when `name` is not one of the icons.
pub fn sign<S>(name: &str, size: f32, on_face: bool, color: Color) -> Option<El<S>> {
let at = ICONS.iter().position(|(n, _)| *n == name)?;
let (face, usual) = &drawn()[at];
let chosen = if on_face { face } else { usual };
Some(icon(chosen.clone()).size(size).color(color).el())
/// The icon `name` as an element of `size` px in `color`; none when `name` is not one of the
/// icons. (`_on_face` is retained for signature compatibility — Solar Linear uses stroke 1.8
/// consistently on both faces and the icon picker.)
pub fn sign<S>(name: &str, size: f32, _on_face: bool, color: Color) -> Option<El<S>> {
let at = SOLAR_AVATARS.iter().position(|(n, _)| *n == name)?;
Some(icon(drawn()[at].clone()).size(size).color(color).el())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn every_solar_linear_avatar_parses() {
assert_eq!(drawn().len(), 25);
assert!(has("bot"));
assert!(!has("constructor"));
}
}
Loading
Loading