Simple GPUI
Overview
Simple GPUI is a Rust library that provides a simplified component-based framework for building GPUI applications. It offers a declarative approach to creating UI components using procedural macros, making GPUI development more intuitive and ergonomic.
Features
- Component Macro: Simplify component creation with the
#[component]attribute macro - Stateless Component Macro: Build
RenderOncecomponents with#[component_stateless] - Reactive Properties: Define component properties with automatic getter/setter generation
- Event Subscriptions: Easy event handling with the
subscribe!macro - Global State Observe: Observe global state changes with the
observe!macro - Context Management: Simplified context access with
init_with_context! - Type-Safe: Full Rust type safety with compile-time guarantees
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
= "0.2.2"
= "0.3.1"
Quick Start
Here's a simple "Hello World" example:
use *;
use component;
Core Concepts
Component Properties
Define component properties using the component_property! macro:
component_property! has reserved internal prefixes. Currently, names starting with _ob_ are reserved for observe!-generated properties. Defining them manually will produce a compile-time error.
// ❌ compile error: `_ob_` prefix is reserved
component_property!;
Stateless Components
Use #[component_stateless] to transform a function into a RenderOnce component and auto-derive IntoElement:
use *;
use component_stateless;
#[component_stateless] is intended for reusable, stateless element components. It supports component_property!, but does not support subscribe!, observe!, or init_with_context!.
Event Subscriptions
Subscribe to events from entities using the subscribe! macro:
Global State Observe
Observe global state changes using the observe! macro:
observe! automatically expands to an internal Subscription component property.
Generated names are incremental in declaration order within one component: _ob_1, _ob_2, ...
So _ob_ names are internal and should not be declared by component_property!.
Context Access
Use init_with_context!() when you need to access the window or context during property initialization:
Examples
The repository includes several examples:
- hello_world.rs - Basic component with properties
- gpui_component_input.rs - Input handling with event subscriptions
- temperature_calculator.rs - Temperature converter with tabs and input validation
- global_observe.rs - Global state observe and reactive input update
Run examples with:
Project Structure
simple-gpui/
├── src/ # Main library exports
├── simple_gpui_core/ # Core procedural macros
│ ├── src/
│ │ ├── lib.rs # Component macro implementation
│ │ ├── extractors.rs # Macro parsing logic
│ │ └── methods.rs # Code generation for methods
├── examples/ # Example applications
└── Cargo.toml
How It Works
The #[component] macro transforms your function into a struct with:
- Generated struct with fields for each
component_property! - new() method for initialization
- Setter methods for each property
- Render trait implementation using your function body
- Subscription management for event handlers
Requirements
- Rust 2024 edition or later
- GPUI 0.2.2+
- gpui-component 0.3.1+
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.