gpui-fundamentals
БесплатноБез исполняемых скриптовНе проверенCore GPUI concepts including contexts, windows, entities, elements, and rendering. Use when learning GPUI basics, understanding the framework architecture, or…
Об этом скилле
GPUI Fundamentals
This skill covers the core concepts of GPUI framework for building desktop UI applications in Rust.
Overview
GPUI is a UI framework that provides:
- Entities: Handles to state with lifecycle management
- Contexts: Access to global state, windows, and system services
- Elements: Composable UI building blocks
- Rendering:
Rendertrait for creating element trees - Concurrency: Async primitives for background work
Context Types
Context types allow interaction with global state, windows, entities, and system services. They are passed as the argument named cx.
App
App is the root context type, providing access to global state and read/update of entities.
fn do_something(cx: &mut App) {
// Access global state
// Read/update entities
}
Context
Provided when updating an Entity<T>. This context dereferences into App, so functions which take &App can also take &Context<T>.
struct MyView {
count: usize,
}
impl MyView {
fn increment(&mut self, cx: &mut Context<Self>) {
self.count += 1;
cx.notify(); // Tell GPUI to re-render
}
}
AsyncApp and AsyncWindowContext
Provided by cx.spawn() for async operations. These can be held across await points.
fn start_async_work(&mut self, cx: &mut Context<Self>) {
cx.spawn(async move |this, cx| {
// this: WeakEntity<Self>
// cx: &mut AsyncApp
// Do async work
Ok(())
}).detach();
}
Window
Window provides access to the state of an application window. It is passed as an argument named window and comes before cx when present.
impl Render for MyView {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
div().child("Hello")
}
}
Used for:
- Managing focus
- Dispatching actions
- Directly drawing
- Getting user input state
Entities
An Entity<T> is a handle to state of type T. Entities enable:
- Shared ownership of UI state
- Automatic lifecycle management
- Safe concurrent access
Creating Entities
app.run(move |cx| {
cx.spawn(async move |cx| {
cx.open_window(WindowOptions::default(), |window, cx| {
// Create an entity
cx.new(|_cx| MyView { count: 0 })
})?;
Ok::<_, anyhow::Error>(())
}).detach();
});
Entity Operations
// Given: thing: Entity<MyView>
// Get entity ID
let id = thing.entity_id();
// Downgrade to weak reference
let weak = thing.downgrade();
// Read (immutable access)
let value = thing.read(cx);
println!("Count: {}", value.count);
// Read with closure
let count = thing.read_with(cx, |view, cx| view.count);
// Update (mutable access)
thing.update(cx, |view, cx| {
view.count += 1;
cx.notify();
});
// Update with window access
thing.update_in(cx, |view, window, cx| {
view.count += 1;
window.dispatch_action(SomeAction.boxed_clone(), cx);
cx.notify();
});
Important Rules
- Use inner cx: Within closures, use the inner
cxprovided to the closure, not the outercx
// ❌ WRONG
entity.update(cx, |view, inner_cx| {
view.count += 1;
cx.notify(); // Using outer cx - WRONG!
});
// ✅ CORRECT
entity.update(cx, |view, inner_cx| {
view.count += 1;
inner_cx.notify(); // Using inner cx
});
- Avoid update-while-updating: Never update an entity while it's already being updated (causes panic)
// ❌ WRONG - will panic
entity.update(cx, |view, cx| {
entity.update(cx, |view2, cx2| { // Nested update - PANIC!
// ...
});
});
Elements
The Render trait is used to render state into an element tree with flexbox layout.
Render Trait
use gpui::*;
struct MyView {
text: SharedString,
}
impl Render for MyView {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div()
.flex()
.flex_col()
.gap_2()
.child(self.text.clone())
.child("More text")
}
}
RenderOnce Trait
For components constructed just to be turned into elements:
use gpui::*;
#[derive(IntoElement)]
struct Card {
title: SharedString,
content: SharedString,
}
impl RenderOnce for Card {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
div()
.p_4()
.bg(rgb(0x1a1a1a))
.rounded(px(8.0))
.child(
div().font_bold().child(self.title)
)
.child(
div().text_sm().child(self.content)
)
}
}
// Usage
impl Render for MyView {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div().child(Card {
title: "Hello".into(),
content: "World".into(),
})
}
}
SharedString
Us
Установить gpui-fundamentals в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл gpui-fundamentals?
Core GPUI concepts including contexts, windows, entities, elements, and rendering. Use when learning GPUI basics, understanding the framework architecture, or implementing core UI patterns.
Как установить скилл gpui-fundamentals?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл gpui-fundamentals запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
frontend-design
Guidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making…
от Anthropicwebapp-testing
Toolkit for interacting with and testing local web applications using Playwright. Supports verifying frontend functionality, debugging UI behavior, capturing…
от Anthropicbrand-guidelines
Applies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand…
от Anthropicweb-artifacts-builder
Suite of tools for creating elaborate, multi-component claude.ai HTML artifacts using modern frontend web technologies (React, Tailwind CSS, shadcn/ui). Use…
от Anthropic