Getting Started
This guide takes you from an empty folder to a desktop window with a label and a button. Install Rust and your platform’s system libraries first; see Installation.
#Create a project
-
Create a new Cargo project:
cargo new gpui-hello cd gpui-hello -
Add GPUI Kit to the generated
Cargo.toml:[dependencies] gpui-kit = "0.12.0"
This one dependency brings in GPUI, GPUI Base, the styled GPUI Component library and its default icons. Your code reaches GPUI through use gpui_kit::*; and components through gpui_kit::component. To change the feature selection later, see Icons & Assets.
#Add a view and run it
-
Replace
src/main.rswith:use gpui_kit::component::{ button::{Button, ButtonVariants}, label::Label, }; use gpui_kit::*; struct HelloWorld; impl Render for HelloWorld { fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { div() .flex() .flex_col() .size_full() .items_center() .justify_center() .gap_2() .child(Label::new("Hello, World!")) .child( Button::new("hello") .primary() .label("Click me") .on_click(|_, _, _| println!("Clicked!")), ) } } fn main() { application() .with_assets(assets::Assets) .run(|cx| { init(cx); open_window(WindowOptions::default(), cx, |_, cx| { cx.new(|_| HelloWorld) }) .expect("Failed to open window"); }); } -
Run it from the project directory:
cargo runThe first build takes a while because Cargo compiles every dependency.
A window opens with “Hello, World!” above a Click me button. Clicking the button prints Clicked! in the terminal.
#What the code does
application()creates the desktop application;.with_assets(...)registers the default icon source.init(cx)initializes the enabled Kit layers, including component themes. Call it once, before opening windows or constructing components.open_window(...)creates anEntity<HelloWorld>from the closure and wraps it in aRoot.Roothosts the window’s dialogs, sheets and notifications. Return your content view from the closure, not anotherRoot.
HelloWorld implements GPUI’s Render trait. Each time GPUI renders the view, render returns an element tree: a div holding a Label and a Button.
The greeting is a Label rather than a bare string so that screen readers can read it. .child("Hello") looks the same but is not exposed to assistive technology.
#A small mental model
- An Entity
holds state across frames. It can own a model that draws nothing. - When
TimplementsRenderand is mounted, the entity is a persistent View. It builds a fresh element tree each time it renders. - Elements such as the
Buttonabove are values built for one render. When a control needs lasting state, such as an input’s text, the owning view keeps an Entity for that state instead of recreating it inrender. - A RenderOnce component takes its inputs as a value and describes a reusable piece of the tree. Use one where the caller supplies the state and handlers; it can still keep small keyed element state. Complex state, subscriptions and tasks need a lasting owner.
app shell → feature (model, commands, view)
├─ Entity<Model> retained state
└─ Entity<View> retained view; View implements Render
└─ element tree rebuilt for each render
└─ RenderOnce values for reusable pieces
As an app grows, a feature with its own workflow can keep its model and views together in a feature crate. Use a private Global only for truly application-wide state. Features cooperate through small public interfaces, events or Entity handles, which keeps reusable pieces cheap to adopt and gives teammates or AI agents clear boundaries for parallel changes. The Coding Guides explain when to split and how to keep ownership and dependencies clear.
#Optional configuration
The example above needs none of this. Come back when you want a different rendering backend or depend on GPUI without Kit.
#Use GPUI Fast
By default, GPUI Kit uses a pinned snapshot of upstream GPUI. Enable gpui-fast to switch to GPUI Fast’s retained rendering and layout engine:
[dependencies]
gpui-kit = { version = "0.12.0", features = ["gpui-fast"] }
- The feature switches GPUI, the native platforms, the WebAssembly backend and every enabled Kit layer together.
- Your code does not change: keep using
gpui_kit::*,gpui_kit::componentand#[gpui_kit::test]. No[patch], extra engine dependency or crate alias is needed. - It works with
default-features = falseand withassets,test-support,inspectororprofiler. - GPUI Fast dependencies use
0.1.0requirements, socargo updatecan select compatible0.1.xreleases. - Cargo features are additive: enabling Fast selects its public types for the whole dependency graph, while upstream packages remain build dependencies. Do not mix Kit’s Fast types with a direct dependency on upstream GPUI.
- If you use Base, Component, assets, FPS, WebView or Shell on their own, enable their
gpui-fastfeature too when selecting a backend explicitly.
In a checkout of this repository, compare the gallery with cargo run and cargo run --features gpui-fast. WebAssembly builds use the nightly toolchain described in WebAssembly.
#macOS text rendering: font-kit
If you depend on gpui-kit, skip this section: it already enables font-kit. It applies only to applications that depend on GPUI directly.
GPUI is published on crates.io as version-pinned snapshot crates: gpui-pre is GPUI itself and gpui-pre-platform holds the native platform backends. gpui-kit depends on matching versions for you. On macOS, text rendering needs the font-kit feature of gpui-pre-platform, which is off by default. Without it, the backend falls back to NoopTextSystem: a window opens but shows no text, and all_font_names() returns an empty list.
To enable it in a direct GPUI application:
-
Add the feature to the
gpui_platformdependency. For a macOS-only application:[dependencies] gpui = { package = "gpui-pre", version = "=0.3.15" } gpui_platform = { package = "gpui-pre-platform", version = "=0.3.15", features = ["font-kit"] }Keep any other platform features your application uses, and keep both snapshot versions aligned.
-
Rebuild with
cargo run. -
To confirm, run
cargo tree -e features -i gpui-pre-macoson macOS and check thatfont-kitis listed.
The feature belongs to gpui-pre-platform, which enables gpui-pre-macos/font-kit. Do not add features = ["font-kit"] to gpui or gpui-kit, do not add a separate font-kit dependency, and do not add these entries to a project that already uses gpui-kit.
#Where to go next
Read these in order as your app grows:
- Entity, Context and Render: retain a value, change it from a button callback, and confirm the window shows the new value. Window shows how
Roothosts that view and its overlays. - Element and RenderOnce: tell the rebuilt element tree apart from persistent state. Run the Brush exercise in Paint and confirm a pointer press changes the drawing.
- Focus, Action and Event: use Tab to reach an interactive target, then trigger a command and observe its state change. Run the streaming example in Task; press Replay twice and confirm old chunks do not return.
- Accessibility and Testing: complete the Save flow with a keyboard and check its focus and visible result, then run the documented UI test and confirm both the rendered status and the saved model value. Check assistive technology separately on each target platform.
- Component catalog: choose controls for your application, then read Icons & Assets and Fonts as your interface needs them.
For a browser target, continue with WebAssembly. For a tested example of retained input state and subscriptions, read the application recipes; the Coding Guides explain the conventions behind them.
#Complete tested view
This settings view shows retained input state and subscriptions. It is a compiled recipe, kept in sync with its Rust source.
To check it, run the settings interaction test from the repository root. It mounts Settings in a GPUI test window:
cargo test -p gpui-kit-recipes --test settings
The test passes when typing updates the retained preview to a, then to ab after an unrelated redraw, with change counts of 1 and 2. It verifies the input subscription and state lifetime, not the native visuals. The repository’s default gpui-kit-recipes executable opens a small bootstrap view and does not display this settings view.
use gpui_kit::component::{
ActiveTheme, IconName, WindowExt,
button::Button,
checkbox::Checkbox,
form::{Field, Form},
input::{Input, InputEvent, InputState},
radio::RadioGroup,
switch::Switch,
};
use gpui_kit::{
AppContext as _, Context, Entity, IntoElement, ParentElement as _, Render, SharedString,
Styled as _, Subscription, Window, div,
};
pub struct Settings {
name: Entity<InputState>,
preview: SharedString,
changes: usize,
enabled: bool,
remember: bool,
delivery: Option<usize>,
_subscriptions: Vec<Subscription>,
}
impl Settings {
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let name = cx.new(|cx| InputState::new(window, cx).placeholder("Name"));
let subscription = cx.subscribe_in(&name, window, |this, state, event, _, cx| {
if matches!(event, InputEvent::Change) {
this.preview = state.read(cx).value().to_string().into();
this.changes += 1;
cx.notify();
}
});
Self {
name,
preview: "".into(),
changes: 0,
enabled: false,
remember: false,
delivery: Some(0),
_subscriptions: vec![subscription],
}
}
pub fn input(&self) -> Entity<InputState> {
self.name.clone()
}
pub fn preview(&self) -> &SharedString {
&self.preview
}
pub fn changes(&self) -> usize {
self.changes
}
}
impl Render for Settings {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
div()
.flex()
.flex_col()
.size_full()
.p_4()
.gap_3()
.bg(cx.theme().background)
.text_color(cx.theme().foreground)
.child("Profile")
.child(
Form::new()
.child(Field::new().label("Name").child(Input::new(&self.name)))
.child(Field::new().label("Preview").child(self.preview.clone()))
.child(
Field::new().label_indent(false).child(
Checkbox::new("remember")
.label("Remember name")
.checked(self.remember)
.on_change(cx.listener(|this, value, _, cx| {
this.remember = *value;
cx.notify();
})),
),
)
.child(
Field::new().label_indent(false).child(
Switch::new("enabled")
.label("Enable notifications")
.checked(self.enabled)
.on_change(cx.listener(|this, value, _, cx| {
this.enabled = *value;
cx.notify();
})),
),
)
.child(
Field::new().label("Delivery").child(
RadioGroup::new("delivery")
.children(["Immediately", "Daily summary"])
.selected_index(self.delivery)
.on_change(cx.listener(|this, value, _, cx| {
this.delivery = Some(*value);
cx.notify();
})),
),
)
.footer(
Button::new("about")
.label("About…")
.icon(IconName::Info)
.on_click(|_, window, cx| {
window.open_dialog(cx, |dialog, _, _| {
dialog.title("About").child("A complete GPUI Kit window")
});
}),
),
)
}
}