gpui-symbols
macOS SF Symbols rendering for Rust / GPUI applications.
Installation
[]
= "0.6"
# With GPUI integration
= { = "0.7", = ["gpui"] }
# With Icon component (recommended for GPUI apps)
= { = "0.7", = ["component"] }
Usage
Basic - Raw RGBA Pixels
use SfSymbol;
let = new
.size
.color
.render_rgba
.unwrap;
With GPUI Integration
use SfSymbol;
use ;
let image = new
.size
.color
.render
.unwrap;
// Use in GPUI element
img.size
Icon Component (Recommended)
The Icon component provides a high-level API similar to GPUI Components:
use Icon;
use px;
Using Preset Symbols (Recommended)
With the presets feature, use type-safe SF Symbols enums directly:
use ;
// All 9000+ SF Symbols as compile-time checked enums
let icon = from_name;
let heart = from_name.text_color;
Using Unified Symbol Enum (New in 0.6)
Use SfSymbol for a single type covering all SF Symbols versions:
use ;
// Unified enum with version metadata
let icon = from_name;
// Check minimum version required
let = Gearshape.min_version; // (2, 0)
This is useful for cross-platform abstractions:
use SfSymbol;
Define Custom Icon Enums
Alternatively, use the define_icons! macro:
use ;
define_icons!
let icon = from_name.text_color;
Aspect Ratio Preservation (New in 0.6.1)
SF Symbols are not always square - for example, gearshape.2 is wider than tall. The Icon component automatically preserves the original aspect ratio:
// Non-square symbols render correctly
new.size // Square gear
new.size // Wide: two gears side by side
new.size // Wide: three people
The size() method sets the maximum dimension while maintaining the symbol's natural proportions.
Advanced Symbol Options
Customize weight, scale, and rendering mode:
use ;
// Bold weight icon
new
.weight
.size;
// Large scale with multicolor rendering
new
.symbol_scale
.rendering_mode;
// Thin weight, monochrome style
new
.weight
.rendering_mode
.text_color;
Integration with gpui-component
Use SF Symbols with gpui-component Button via child():
use ;
use Button;
// Icon-only button
new
.child
// Icon + label button
new
.child
Note: gpui-component's
Button::icon()expects SVG paths, while gpui-symbols renders to pixel images. UseButton::child()for SF Symbols integration.
API
SfSymbol
Low-level builder for rendering SF Symbols.
| Method | Description | Default |
|---|---|---|
new(name) |
Create symbol with name | - |
size(f32) |
Point size | 32.0 |
scale(f32) |
Scale factor (Retina) | 2.0 |
color(u32) |
RGB hex color | 0x000000 |
weight(SymbolWeight) |
Symbol weight | Regular |
symbol_scale(SymbolScale) |
Symbol scale | Medium |
rendering_mode(RenderingMode) |
Rendering mode | Hierarchical |
render_rgba() |
Render to (width, height, Vec<u8>) |
- |
render() |
Render to Arc<RenderImage> (requires gpui feature) |
- |
Icon
High-level GPUI component (requires component feature).
| Method | Description | Default |
|---|---|---|
new(name) |
Create icon with SF Symbol name | - |
from_name(T) |
Create from IconName type |
- |
size(Pixels) |
Set maximum dimension (preserves aspect ratio) | 16px |
color(impl Into<Hsla>) |
Set color (Hsla, Rgba, or rgb(hex)) |
black |
text_color(u32) |
Set RGB hex color (convenience) | black |
weight(SymbolWeight) |
Symbol weight | Regular |
symbol_scale(SymbolScale) |
Symbol scale | Medium |
rendering_mode(RenderingMode) |
Rendering mode | Hierarchical |
Enums
SymbolWeight: UltraLight, Thin, Light, Regular, Medium, Semibold, Bold, Heavy, Black
SymbolScale: Small, Medium, Large
RenderingMode: Monochrome, Hierarchical, Palette, Multicolor
Cache Management
With the cache feature (enabled by default), rendered symbols are cached globally for performance:
use ;
// Get number of cached symbols
let count = cache_size;
// Clear all cached symbols (e.g., on memory pressure or appearance change)
clear_cache;
Features
| Feature | Description |
|---|---|
default |
component + presets + cache |
cache |
Global cache for rendered symbols (implies gpui) |
gpui |
GPUI integration, returns Arc<RenderImage> |
component |
High-level Icon component (implies gpui) |
presets |
Type-safe SF Symbols enums via sfsymbols crate |
Requirements
- macOS 11.0+ (SF Symbols support)
- Rust 1.75+
Examples
Run the example:
Symbol Names
See SF Symbols for available symbol names.
Common symbols:
star.fill,heart.fill,house.fillgearshape,magnifyingglass,trashperson.fill,folder.fill,doc.fill
Related Crates
| Crate | Description |
|---|---|
| sfsymbols | Type-safe SF Symbols enums (9000+ symbols) |
| sfsymbols-codegen | Code generator for sfsymbols |
License
MIT OR Apache-2.0