AgentSkillsCN

rust-gpui-development

面向 Rust GUI 应用程序的开发技能,基于 GPUI 框架(Zed 的 UI 框架)进行开发。适用于基于 GPUI 的桌面应用程序开发——无论是创建视图、管理状态、处理操作,还是对现有代码进行审查或重构。本技能将触发于 Rust GUI 开发、GPUI 视图、Entity/Context 的使用、终端模拟器、代码编辑器,或任何采用 GPUI 构建的桌面应用。

SKILL.md
--- frontmatter
name: rust-gpui-development
description: 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.

Rust GPUI Development

Patterns and API reference for building Rust applications with the GPUI framework.

Reference Files

Load based on current task:

TaskReferenceContent
Working with GPUIgpui-patterns.mdApp architecture, context types, entities, actions, rendering, modals, drag & drop, menus, tooltips, testing
Designing state flowstate-management.mdGlobal state, view-model separation, state machines, derived state, unidirectional flow

Quick Reference

GPUI Context Types

ContextScopeWhen Used
&mut AppGlobalApp init, global operations
&mut WindowWindowRendering, events
&mut Context<T>EntityEntity methods, render()

View Structure

rust
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

rust
// 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

rust
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

  1. Never store cx - always pass through method parameters
  2. Call .detach() on subscriptions and observations
  3. Call cx.notify() after state changes that affect rendering
  4. Use cx.listener() for action/click handlers in render() - wraps entity access
  5. window: &mut Window is a separate parameter in all entity callbacks