Kaolin
A flexible, immediate-mode UI layout library for Rust, inspired by Clay. Kaolin provides a powerful and intuitive API for creating responsive layouts that work across different rendering backends.
Features
- Immediate-mode UI: Build layouts using a declarative, function-based API
- Flexible layout engine: Supports complex flex-box style layouts with growth, shrinking, and constraint-based sizing
- Multi-backend support: Works with Raylib, embedded graphics, and custom renderers
- Type-safe styling: Comprehensive styling system with compile-time safety
- Text rendering: Built-in text layout with wrapping and multi-line support
- No-std compatible: Can be used in embedded environments
Quick Start
Add Kaolin to your Cargo.toml:
[]
= "0.1"
# For Raylib support
= { = "0.1", = ["raylib"] }
# For embedded graphics support
= { = "0.1", = ["embedded"] }
Basic Usage
Here's a simple "Hello, World!" example using the Raylib renderer:
use ;
Core Concepts
Layout Structure
Kaolin uses a tree-based layout system where each element can contain child elements. The layout is built using a functional, immediate-mode API:
kaolin.draw
Sizing Modes
Kaolin supports several sizing modes for flexible layouts:
Fixed Sizing
// Fixed dimensions
sizing! // 200px width, 100px height
sizing! // 150px for both dimensions
Fit-to-Content Sizing
// Fits to content size
sizing! // No constraints
sizing! // Maximum width constraint
sizing! // Min and max constraints
Growth-based Sizing
// Grows to fill available space
sizing! // Default growth factor (1.0)
sizing! // Custom growth factor
sizing! // Growth with constraints
Layout Direction and Alignment
Control how child elements are arranged:
new
.direction // or TopToBottom, RightToLeft, BottomToTop
.alignment // Cross-axis: Start, End, Center, Stretch
.justification // Main-axis: Start, End, Center, SpaceBetween, SpaceAround
.gap // Spacing between children
Styling
Style containers and text with comprehensive options:
// Container styling
new
.background_color
.corner_radius
.padding
.border
// Text styling
new
.font_size
.font_id
.color
Examples
Responsive Layout
Create layouts that adapt to different screen sizes:
k.with
Multi-Column Layout
k.with
Rendering Backends
Raylib
Perfect for desktop applications and games:
use RaylibRenderer;
let mut renderer = new;
while !renderer.should_close
Embedded Graphics
Ideal for embedded systems and small displays:
use EmbeddedRenderer;
use ;
let mut display = new;
let renderer = new;
renderer.onto.draw;
Custom Renderers
Implement the KaolinRenderer trait to create your own backend:
Advanced Features
Text Wrapping
Kaolin automatically handles text wrapping within constrained widths:
k.with
Proportional Layouts
Use growth factors to create proportional layouts:
k.with // Takes 1/4 of space
.with // Takes 3/4 of space
Nested Layouts
Create complex layouts by nesting containers:
k.with
Performance
Kaolin is designed for immediate-mode rendering with minimal allocations:
- Layout calculations are performed each frame but are highly optimized
- Text measurement is cached when possible
- Memory allocations are minimized through careful API design
- No-std compatibility for resource-constrained environments
Contributing
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.
License
See the LICENSE file for details.
Inspiration
Kaolin is heavily inspired by Clay, bringing similar concepts to the Rust ecosystem with type safety and zero-cost abstractions.