Rust GPUI Development
Patterns and API reference for building Rust applications with the GPUI framework.
Reference Files
Load based on current task:
| Task |
Reference |
Content |
| Working with GPUI |
gpui-patterns.md |
App architecture, context types, entities, actions, rendering, modals, drag & drop, menus, tooltips, testing |
| Designing state flow |
state-management.md |
Global state, view-model separation, state machines, derived state, unidirectional flow |
Quick Reference
GPUI Context Types
| Context |
Scope |
When Used |
&mut App |
Global |
App init, global operations |
&mut Window |
Window |
Rendering, events |
&mut Context<T> |
Entity |
Entity methods, render() |
View Structure
pub struct MyView {
focus_handle: FocusHandle,
}
impl Render for MyView {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
div()
.key_context("MyView")
.on_action(cx.listener(Self::handle_action))
.child(/* ... */)
}
}
impl MyView {
fn handle_action(&mut self, _: &MyAction, window: &mut Window, cx: &mut Context<Self>) {
cx.notify();
}
}
Actions
// Simple actions
actions!(my_namespace, [DoSomething, Cancel]);
// Parameterized actions
#[derive(Clone, PartialEq, Deserialize, JsonSchema, Action)]
#[action(namespace = my_namespace)]
pub struct SelectItem { pub index: usize }
// Register in render() via cx.listener()
div().on_action(cx.listener(Self::handle_action))
Global State
impl Global for AppSettings {}
cx.set_global(AppSettings::new());
let settings = cx.global::<AppSettings>();
cx.observe_global::<AppSettings>(|this, cx| { cx.notify(); }).detach();
Critical Rules
- Never store
cx - always pass through method parameters
- Call
.detach() on subscriptions and observations
- Call
cx.notify() after state changes that affect rendering
- Use
cx.listener() for action/click handlers in render() - wraps entity access
window: &mut Window is a separate parameter in all entity callbacks
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: rust-gpui-development3description: Development skill for Rust GUI applications using GPUI framework (Zed's UI framework). Use when working on GPUI-based desktop applications - creating views, managing state, handling actions, or reviewing/refactoring existing code. Triggers on Rust GUI development, GPUI views, Entity/Context usage, terminal emulators, code editors, or any desktop app built with GPUI. Use when this capability is needed.4---56# Rust GPUI Development78Patterns and API reference for building Rust applications with the GPUI framework.910## Reference Files1112Load based on current task:1314| Task | Reference | Content |15|------|-----------|---------|16| Working with GPUI | [gpui-patterns.md](references/gpui-patterns.md) | App architecture, context types, entities, actions, rendering, modals, drag & drop, menus, tooltips, testing |17| Designing state flow | [state-management.md](references/state-management.md) | Global state, view-model separation, state machines, derived state, unidirectional flow |1819## Quick Reference2021### GPUI Context Types2223| Context | Scope | When Used |24|---------|-------|-----------|25| `&mut App` | Global | App init, global operations |26| `&mut Window` | Window | Rendering, events |27| `&mut Context<T>` | Entity | Entity methods, `render()` |2829### View Structure3031```rust32pub struct MyView {33 focus_handle: FocusHandle,34}3536impl Render for MyView {37 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {38 div()39 .key_context("MyView")40 .on_action(cx.listener(Self::handle_action))41 .child(/* ... */)42 }43}4445impl MyView {46 fn handle_action(&mut self, _: &MyAction, window: &mut Window, cx: &mut Context<Self>) {47 cx.notify();48 }49}50```5152### Actions5354```rust55// Simple actions56actions!(my_namespace, [DoSomething, Cancel]);5758// Parameterized actions59#[derive(Clone, PartialEq, Deserialize, JsonSchema, Action)]60#[action(namespace = my_namespace)]61pub struct SelectItem { pub index: usize }6263// Register in render() via cx.listener()64div().on_action(cx.listener(Self::handle_action))65```6667### Global State6869```rust70impl Global for AppSettings {}71cx.set_global(AppSettings::new());72let settings = cx.global::<AppSettings>();73cx.observe_global::<AppSettings>(|this, cx| { cx.notify(); }).detach();74```7576### Critical Rules77781. **Never store `cx`** - always pass through method parameters792. **Call `.detach()`** on subscriptions and observations803. **Call `cx.notify()`** after state changes that affect rendering814. **Use `cx.listener()`** for action/click handlers in `render()` - wraps entity access825. **`window: &mut Window`** is a separate parameter in all entity callbacks8384---85> Converted and distributed by [TomeVault](https://tomevault.io/claim/contember) — claim your Tome and manage your conversions.86<!-- tomevault:4.0:skill_md:2026-04-11 -->