herogpui-components 0.12.0

HeroUI-style component library for GPUI
Documentation
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! A named, embedded Lucide icon set (HeroGPUI extension, not a HeroUI v3
//! API).
//!
//! [`IconName`] names a curated subset of [Lucide](https://lucide.dev)
//! icons, and [`Icon`] draws one at a size and colour. The SVGs are copied
//! verbatim from the official `lucide-static` [`LUCIDE_VERSION`] release
//! into this crate's `assets/lucide/` and compiled in with `include_bytes!`,
//! so [`HeroGpuiAssets`](crate::HeroGpuiAssets) serves every variant with no
//! file or feature to set up:
//!
//! ```
//! use herogpui_components::{Icon, IconName};
//! use gpui::{px, rgb};
//!
//! let _search = Icon::new(IconName::Search);
//! let _big_red = Icon::new(IconName::Heart).size(px(24.)).color(rgb(0xe11d48));
//!
//! // Any builder that takes an icon path takes a name too.
//! let path: gpui::SharedString = IconName::Folder.into();
//! assert_eq!(path.as_ref(), "herogpui/icons/lucide/folder.svg");
//! ```
//!
//! The chrome paths in [`icons`](crate::icons) are separate and unchanged:
//! those are HeroUI's own glyphs, drawn by the components, and keep their
//! `herogpui/icons/<name>.svg` paths. Lucide icons live one level down, under
//! [`LUCIDE_ICON_PREFIX`], so the two sets never collide.
//!
//! Lucide is ISC-licensed, and a few of its icons derive from Feather (MIT);
//! the full text ships beside the SVGs as `assets/lucide/LICENSE` and the
//! attribution is in this crate's `NOTICE`.
//!
//! To add an icon, copy `icons/<name>.svg` byte for byte out of the same
//! `lucide-static` tarball into `assets/lucide/` and add one
//! `Variant => "name"` line to the list below; `tests/icon.rs` fails for a
//! variant without a file and for a file without a variant.

use gpui::{prelude::*, px, svg, App, Hsla, IntoElement, Pixels, RenderOnce, SharedString, Window};
use herogpui_theme::ActiveTheme;

/// The `lucide-static` release the embedded SVGs were copied from.
pub const LUCIDE_VERSION: &str = "1.31.0";

/// The asset-path directory every [`IconName::path`] lives in.
pub const LUCIDE_ICON_PREFIX: &str = "herogpui/icons/lucide";

/// The size [`Icon`] draws at unless told otherwise — the 16px the
/// components' own chrome icons use.
pub const DEFAULT_ICON_SIZE: Pixels = px(16.);

macro_rules! lucide_icons {
    ($($variant:ident => $name:literal),* $(,)?) => {
        /// A Lucide icon embedded in this crate (HeroGPUI extension).
        ///
        /// Each variant is the Lucide icon of the same name in PascalCase
        /// (`circle-check` is [`IconName::CircleCheck`]). The set is
        /// curated, not all of Lucide, and grows in minor releases, which is
        /// why the enum is `#[non_exhaustive]`.
        #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
        #[non_exhaustive]
        pub enum IconName {
            $(
                #[doc = concat!("Lucide `", $name, "`.")]
                $variant,
            )*
        }

        impl IconName {
            /// Every icon, in name order.
            pub const ALL: &'static [IconName] = &[$(IconName::$variant),*];

            /// Lucide's own kebab-case name, e.g. `"circle-check"`.
            pub const fn name(self) -> &'static str {
                match self {
                    $(IconName::$variant => $name,)*
                }
            }

            /// The asset path [`HeroGpuiAssets`](crate::HeroGpuiAssets)
            /// serves this icon under, e.g.
            /// `"herogpui/icons/lucide/circle-check.svg"`.
            pub const fn path(self) -> &'static str {
                match self {
                    $(IconName::$variant => concat!("herogpui/icons/lucide/", $name, ".svg"),)*
                }
            }

            /// The embedded SVG document, byte-identical to `lucide-static`.
            pub const fn svg(self) -> &'static [u8] {
                match self {
                    $(IconName::$variant => include_bytes!(concat!("../assets/lucide/", $name, ".svg")),)*
                }
            }
        }
    };
}

lucide_icons! {
    Activity => "activity",
    AlarmClock => "alarm-clock",
    Archive => "archive",
    ArrowDown => "arrow-down",
    ArrowDownLeft => "arrow-down-left",
    ArrowDownRight => "arrow-down-right",
    ArrowDownToLine => "arrow-down-to-line",
    ArrowLeft => "arrow-left",
    ArrowLeftRight => "arrow-left-right",
    ArrowRight => "arrow-right",
    ArrowUp => "arrow-up",
    ArrowUpDown => "arrow-up-down",
    ArrowUpLeft => "arrow-up-left",
    ArrowUpRight => "arrow-up-right",
    ArrowUpToLine => "arrow-up-to-line",
    AtSign => "at-sign",
    Ban => "ban",
    Battery => "battery",
    Bell => "bell",
    BellOff => "bell-off",
    Bold => "bold",
    Book => "book",
    BookOpen => "book-open",
    Bookmark => "bookmark",
    Bot => "bot",
    Box => "box",
    Briefcase => "briefcase",
    Bug => "bug",
    Building => "building",
    Calendar => "calendar",
    CalendarDays => "calendar-days",
    Camera => "camera",
    ChartBar => "chart-bar",
    ChartColumn => "chart-column",
    ChartLine => "chart-line",
    ChartPie => "chart-pie",
    Check => "check",
    CheckCheck => "check-check",
    ChevronDown => "chevron-down",
    ChevronLeft => "chevron-left",
    ChevronRight => "chevron-right",
    ChevronUp => "chevron-up",
    ChevronsDown => "chevrons-down",
    ChevronsLeft => "chevrons-left",
    ChevronsRight => "chevrons-right",
    ChevronsUp => "chevrons-up",
    ChevronsUpDown => "chevrons-up-down",
    Circle => "circle",
    CircleAlert => "circle-alert",
    CircleCheck => "circle-check",
    CircleDot => "circle-dot",
    CircleHelp => "circle-help",
    CircleMinus => "circle-minus",
    CirclePlus => "circle-plus",
    CircleUser => "circle-user",
    CircleX => "circle-x",
    Clipboard => "clipboard",
    ClipboardCheck => "clipboard-check",
    ClipboardCopy => "clipboard-copy",
    Clock => "clock",
    Cloud => "cloud",
    CloudDownload => "cloud-download",
    CloudUpload => "cloud-upload",
    Code => "code",
    CodeXml => "code-xml",
    Columns2 => "columns-2",
    Command => "command",
    Compass => "compass",
    Contrast => "contrast",
    Copy => "copy",
    CornerDownLeft => "corner-down-left",
    Cpu => "cpu",
    CreditCard => "credit-card",
    Crop => "crop",
    Database => "database",
    DollarSign => "dollar-sign",
    Download => "download",
    Droplet => "droplet",
    Ellipsis => "ellipsis",
    EllipsisVertical => "ellipsis-vertical",
    Expand => "expand",
    ExternalLink => "external-link",
    Eye => "eye",
    EyeOff => "eye-off",
    File => "file",
    FileCode => "file-code",
    FilePlus => "file-plus",
    FileText => "file-text",
    Files => "files",
    Film => "film",
    Flag => "flag",
    Folder => "folder",
    FolderOpen => "folder-open",
    FolderPlus => "folder-plus",
    Funnel => "funnel",
    Gift => "gift",
    GitBranch => "git-branch",
    GitCommitHorizontal => "git-commit-horizontal",
    GitMerge => "git-merge",
    GitPullRequest => "git-pull-request",
    Globe => "globe",
    GraduationCap => "graduation-cap",
    Grid2x2 => "grid-2x2",
    GripHorizontal => "grip-horizontal",
    GripVertical => "grip-vertical",
    HardDrive => "hard-drive",
    Hash => "hash",
    Headphones => "headphones",
    Heart => "heart",
    History => "history",
    House => "house",
    Image => "image",
    Inbox => "inbox",
    Info => "info",
    Italic => "italic",
    Key => "key",
    Keyboard => "keyboard",
    Languages => "languages",
    Laptop => "laptop",
    Layers => "layers",
    LayoutDashboard => "layout-dashboard",
    LayoutGrid => "layout-grid",
    LayoutList => "layout-list",
    Lightbulb => "lightbulb",
    Link => "link",
    Link2 => "link-2",
    List => "list",
    ListChecks => "list-checks",
    ListFilter => "list-filter",
    ListOrdered => "list-ordered",
    Loader => "loader",
    LoaderCircle => "loader-circle",
    Lock => "lock",
    LockOpen => "lock-open",
    LogIn => "log-in",
    LogOut => "log-out",
    Mail => "mail",
    Map => "map",
    MapPin => "map-pin",
    Maximize => "maximize",
    Maximize2 => "maximize-2",
    Menu => "menu",
    MessageCircle => "message-circle",
    MessageSquare => "message-square",
    Mic => "mic",
    MicOff => "mic-off",
    Minimize => "minimize",
    Minimize2 => "minimize-2",
    Minus => "minus",
    Monitor => "monitor",
    Moon => "moon",
    MousePointer => "mouse-pointer",
    Move => "move",
    OctagonAlert => "octagon-alert",
    Package => "package",
    Palette => "palette",
    PanelLeft => "panel-left",
    PanelRight => "panel-right",
    Paperclip => "paperclip",
    Pause => "pause",
    Pencil => "pencil",
    Phone => "phone",
    Pin => "pin",
    Pipette => "pipette",
    Play => "play",
    Plus => "plus",
    Power => "power",
    Printer => "printer",
    Puzzle => "puzzle",
    Quote => "quote",
    Redo2 => "redo-2",
    RefreshCw => "refresh-cw",
    Repeat => "repeat",
    Rocket => "rocket",
    RotateCcw => "rotate-ccw",
    RotateCw => "rotate-cw",
    Rows2 => "rows-2",
    Rss => "rss",
    Save => "save",
    Scissors => "scissors",
    Search => "search",
    Send => "send",
    Server => "server",
    Settings => "settings",
    Settings2 => "settings-2",
    Share => "share",
    Share2 => "share-2",
    Shield => "shield",
    ShieldCheck => "shield-check",
    ShoppingBag => "shopping-bag",
    ShoppingCart => "shopping-cart",
    Shrink => "shrink",
    Shuffle => "shuffle",
    SkipBack => "skip-back",
    SkipForward => "skip-forward",
    SlidersHorizontal => "sliders-horizontal",
    Smartphone => "smartphone",
    Smile => "smile",
    Sparkles => "sparkles",
    Square => "square",
    SquareCheck => "square-check",
    SquarePen => "square-pen",
    Star => "star",
    Strikethrough => "strikethrough",
    Sun => "sun",
    SunMoon => "sun-moon",
    Table => "table",
    Tablet => "tablet",
    Tag => "tag",
    Tags => "tags",
    Terminal => "terminal",
    TextAlignCenter => "text-align-center",
    TextAlignEnd => "text-align-end",
    TextAlignStart => "text-align-start",
    ThumbsDown => "thumbs-down",
    ThumbsUp => "thumbs-up",
    Timer => "timer",
    Trash => "trash",
    Trash2 => "trash-2",
    TrendingDown => "trending-down",
    TrendingUp => "trending-up",
    TriangleAlert => "triangle-alert",
    Truck => "truck",
    Type => "type",
    Underline => "underline",
    Undo2 => "undo-2",
    Unlink => "unlink",
    Upload => "upload",
    User => "user",
    UserCheck => "user-check",
    UserMinus => "user-minus",
    UserPlus => "user-plus",
    Users => "users",
    Video => "video",
    Volume2 => "volume-2",
    VolumeX => "volume-x",
    Wallet => "wallet",
    WandSparkles => "wand-sparkles",
    Wifi => "wifi",
    WifiOff => "wifi-off",
    Wrench => "wrench",
    X => "x",
    Zap => "zap",
    ZoomIn => "zoom-in",
    ZoomOut => "zoom-out",
}

impl IconName {
    /// The icon Lucide calls `name` (kebab-case, as in [`IconName::name`]).
    pub fn from_name(name: &str) -> Option<Self> {
        Self::ALL
            .binary_search_by(|icon| icon.name().cmp(name))
            .ok()
            .map(|index| Self::ALL[index])
    }

    /// The icon whose [`IconName::path`] is `path`.
    pub fn from_path(path: &str) -> Option<Self> {
        path.strip_prefix(LUCIDE_ICON_PREFIX)?
            .strip_prefix('/')?
            .strip_suffix(".svg")
            .and_then(Self::from_name)
    }
}

impl From<IconName> for SharedString {
    fn from(icon: IconName) -> Self {
        SharedString::new_static(icon.path())
    }
}

impl From<IconName> for Icon {
    fn from(icon: IconName) -> Self {
        Icon::new(icon)
    }
}

/// One icon, drawn as a GPUI `svg()` (HeroGPUI extension).
///
/// Sized [`DEFAULT_ICON_SIZE`] and painted in the theme's `foreground`
/// unless told otherwise — gpui's `svg()` does not inherit the surrounding
/// text colour, so an icon on an accent or danger fill needs `.color(..)`.
/// The icon is decorative and reports no accessibility node, as
/// `lucide-react` marks its `<svg>` `aria-hidden` when it is given no label:
/// name the control that holds it instead.
#[derive(IntoElement)]
pub struct Icon {
    path: SharedString,
    size: Pixels,
    color: Option<Hsla>,
}

impl Icon {
    /// An embedded Lucide icon.
    pub fn new(icon: IconName) -> Self {
        Self::from_path(icon)
    }

    /// Any SVG the registered asset source serves: one of the
    /// [`icons`](crate::icons) chrome paths, or an application's own.
    pub fn from_path(path: impl Into<SharedString>) -> Self {
        Self {
            path: path.into(),
            size: DEFAULT_ICON_SIZE,
            color: None,
        }
    }

    /// The width and height of the square the icon draws in.
    pub fn size(mut self, size: impl Into<Pixels>) -> Self {
        self.size = size.into();
        self
    }

    /// The paint colour (default: the theme's `foreground`).
    pub fn color(mut self, color: impl Into<Hsla>) -> Self {
        self.color = Some(color.into());
        self
    }
}

impl RenderOnce for Icon {
    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
        let color = self.color.unwrap_or(cx.colors().foreground);
        svg()
            .path(self.path)
            .size(self.size)
            .flex_none()
            .text_color(color)
    }
}