|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +rui is an experimental declarative UI library for Rust, inspired by SwiftUI. It focuses on GPU-rendered UIs that update reactively when state changes, without a retained view tree or DOM diffing. The entire UI is re-rendered when state changes, under the assumption that this is fast enough for good performance. |
| 8 | + |
| 9 | +## Common Development Commands |
| 10 | + |
| 11 | +### Building and Testing |
| 12 | +- `cargo build` - Build the project |
| 13 | +- `cargo test` - Run tests |
| 14 | +- `cargo check` - Check for compilation errors without building |
| 15 | + |
| 16 | +### Running Examples |
| 17 | +- `cargo run --example <example_name>` - Run a specific example (e.g., `counter`, `shapes`, `canvas`, `slider`, `gallery`) |
| 18 | +- `cd examples/<example_name> && cargo run` - For examples with their own Cargo.toml (calculator, synth, flip_cards) |
| 19 | + |
| 20 | +Key examples: |
| 21 | +- `cargo run --example counter` - Basic counter demo |
| 22 | +- `cargo run --example gallery` - Widget gallery showing all components |
| 23 | +- `cargo run --example shapes` - Basic shapes rendering |
| 24 | +- `cargo run --example canvas` - GPU vector graphics with vger |
| 25 | +- `cd examples/calculator && cargo run` - Calculator app |
| 26 | + |
| 27 | +### Testing Examples |
| 28 | +Examples serve as both demos and integration tests. Test by running them and verifying they work correctly. |
| 29 | + |
| 30 | +## Architecture Overview |
| 31 | + |
| 32 | +### Core Components |
| 33 | + |
| 34 | +**View System**: The fundamental building block is the `View` trait, which represents immutable UI components. Views are composed hierarchically to build complex UIs. |
| 35 | + |
| 36 | +**Context**: Stores all mutable UI state, keyed by `ViewId`s. The `Context` manages: |
| 37 | +- Layout information for all views |
| 38 | +- User state created by `state` functions |
| 39 | +- Touch/mouse interaction state |
| 40 | +- Environment values |
| 41 | +- Dirty tracking for reactive updates |
| 42 | + |
| 43 | +**ViewId System**: Each view gets a unique identifier (u64) formed by hashing the traversal path down the view tree. This enables efficient state storage and retrieval. |
| 44 | + |
| 45 | +**Reactive Updates**: The entire UI re-renders when state changes. Multiple state changes in a single event cycle are coalesced for efficiency. |
| 46 | + |
| 47 | +### Key Concepts |
| 48 | + |
| 49 | +**Views**: Immutable components that define UI structure. Examples: `text()`, `button()`, `vstack()`, `hstack()`, `canvas()` |
| 50 | + |
| 51 | +**State**: Managed through the `state` function and `StateHandle`. State changes trigger UI updates. |
| 52 | + |
| 53 | +**Bindings**: Provide read/write access to application state via the `Binding` trait. Used to connect UI controls to state. |
| 54 | + |
| 55 | +**Modifiers**: Chainable methods on views (via the `Modifiers` trait) that add functionality like `.padding()`, `.tap()`, `.background()`, `.size()` |
| 56 | + |
| 57 | +**Layout**: Automatic layout system similar to SwiftUI. Uses stacks (`vstack`, `hstack`, `zstack`) for arrangement. |
| 58 | + |
| 59 | +### File Structure |
| 60 | + |
| 61 | +- `src/lib.rs` - Main library entry point and exports |
| 62 | +- `src/view.rs` - Core `View` and `DynView` traits |
| 63 | +- `src/context.rs` - Context for managing state and layout |
| 64 | +- `src/views/` - Individual view implementations (button, text, shapes, etc.) |
| 65 | +- `src/modifiers.rs` - Common view modifiers |
| 66 | +- `src/binding.rs` - State binding system |
| 67 | +- `src/lens.rs` - Lens system for focusing into state |
| 68 | +- `examples/` - Example applications demonstrating usage |
| 69 | + |
| 70 | +### State Management |
| 71 | + |
| 72 | +State is managed through a combination of: |
| 73 | +1. **Local State**: Using `state(initial_value, |state, cx| view)` |
| 74 | +2. **Bindings**: Read/write access to state via `Binding<T>` |
| 75 | +3. **Environment**: Shared values propagated down the view tree |
| 76 | +4. **Context**: Central storage for all state, indexed by ViewId |
| 77 | + |
| 78 | +### Rendering Pipeline |
| 79 | + |
| 80 | +1. **Event Processing**: Handle user input and update state |
| 81 | +2. **Layout**: Compute view sizes and positions (cached until state changes) |
| 82 | +3. **Drawing**: Render using vger (GPU vector graphics) |
| 83 | +4. **Dirty Tracking**: Only re-render when state actually changes |
| 84 | + |
| 85 | +### Platform Support |
| 86 | + |
| 87 | +- **Desktop**: macOS, Windows, Linux (via winit) |
| 88 | +- **Mobile**: iOS support available separately |
| 89 | +- **Web**: WASM support (work in progress) |
| 90 | + |
| 91 | +## Development Guidelines |
| 92 | + |
| 93 | +### Creating Views |
| 94 | +- Implement the `View` trait (which requires `DynView + Clone`) |
| 95 | +- Use composition over direct trait implementation when possible |
| 96 | +- Follow existing naming conventions (lowercase function names) |
| 97 | + |
| 98 | +### State Management |
| 99 | +- Use `state()` for component-local state |
| 100 | +- Use `Binding<T>` for two-way data flow |
| 101 | +- Leverage the lens system for accessing nested state |
| 102 | + |
| 103 | +### Testing |
| 104 | +- Examples serve as integration tests |
| 105 | +- Unit tests exist for core functionality (bindings, lenses) |
| 106 | +- Test by running examples and ensuring they work correctly |
| 107 | + |
| 108 | +### Performance Considerations |
| 109 | +- Layout is cached and only recomputed when state changes |
| 110 | +- Rendering assumes 2D UI graphics are trivial for modern GPUs |
| 111 | +- Avoid complex state dependencies that cause excessive re-renders |
0 commit comments