Agent SkillsfeigeCode/navop › gpui-element

gpui-element

GitHub

用于在GPUI中通过低层Element API实现自定义UI组件。适用于需要精细控制布局、预绘制和绘制阶段的高性能复杂场景,弥补高层API的不足。

.codex/skills/gpui-element/SKILL.md feigeCode/navop

Trigger Scenarios

需要自定义复杂布局算法 构建高性能关键UI组件 高层Render/RenderOnce API无法满足需求

Install

npx skills add feigeCode/navop --skill gpui-element -g -y
More Options

Non-standard path

npx skills add https://github.com/feigeCode/navop/tree/dev/.codex/skills/gpui-element -g -y

Use without installing

npx skills use feigeCode/navop@gpui-element

指定 Agent (Claude Code)

npx skills add feigeCode/navop --skill gpui-element -a claude-code -g -y

安装 repo 全部 skill

npx skills add feigeCode/navop --all -g -y

预览 repo 内 skill

npx skills add feigeCode/navop --list

SKILL.md

Frontmatter
{
    "name": "gpui-element",
    "description": "Implementing custom elements using GPUI's low-level Element API (vs. high-level Render\/RenderOnce APIs). Use when you need maximum control over layout, prepaint, and paint phases for complex, performance-critical custom UI components that cannot be achieved with Render\/RenderOnce traits."
}

When to Use

Use the low-level Element trait when:

  • Need fine-grained control over layout calculation
  • Building complex, performance-critical components
  • Implementing custom layout algorithms (masonry, circular, etc.)
  • High-level Render/RenderOnce APIs are insufficient

Prefer Render/RenderOnce for: Simple components, standard layouts, declarative UI

Quick Start

The Element trait provides direct control over three rendering phases:

impl Element for MyElement {
    type RequestLayoutState = MyLayoutState;  // Data passed to later phases
    type PrepaintState = MyPaintState;        // Data for painting

    fn id(&self) -> Option<ElementId> {
        Some(self.id.clone())
    }

    fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
        None
    }

    // Phase 1: Calculate sizes and positions
    fn request_layout(&mut self, .., window: &mut Window, cx: &mut App)
        -> (LayoutId, Self::RequestLayoutState)
    {
        let layout_id = window.request_layout(
            Style { size: size(px(200.), px(100.)), ..default() },
            vec![],
            cx
        );
        (layout_id, MyLayoutState { /* ... */ })
    }

    // Phase 2: Create hitboxes, prepare for painting
    fn prepaint(&mut self, .., bounds: Bounds<Pixels>, layout: &mut Self::RequestLayoutState,
                window: &mut Window, cx: &mut App) -> Self::PrepaintState
    {
        let hitbox = window.insert_hitbox(bounds, HitboxBehavior::Normal);
        MyPaintState { hitbox }
    }

    // Phase 3: Render and handle interactions
    fn paint(&mut self, .., bounds: Bounds<Pixels>, layout: &mut Self::RequestLayoutState,
             paint_state: &mut Self::PrepaintState, window: &mut Window, cx: &mut App)
    {
        window.paint_quad(paint_quad(bounds, Corners::all(px(4.)), cx.theme().background));

        window.on_mouse_event({
            let hitbox = paint_state.hitbox.clone();
            move |event: &MouseDownEvent, phase, window, cx| {
                if hitbox.is_hovered(window) && phase.bubble() {
                    // Handle interaction
                    cx.stop_propagation();
                }
            }
        });
    }
}

// Enable element to be used as child
impl IntoElement for MyElement {
    type Element = Self;
    fn into_element(self) -> Self::Element { self }
}

Core Concepts

Three-Phase Rendering

  1. request_layout: Calculate sizes and positions, return layout ID and state
  2. prepaint: Create hitboxes, compute final bounds, prepare for painting
  3. paint: Render element, set up interactions (mouse events, cursor styles)

State Flow

RequestLayoutState → PrepaintState → paint

State flows in one direction through associated types, passed as mutable references between phases.

Key Operations

  • Layout: window.request_layout(style, children, cx) - Create layout node
  • Hitboxes: window.insert_hitbox(bounds, behavior) - Create interaction area
  • Painting: window.paint_quad(...) - Render visual content
  • Events: window.on_mouse_event(handler) - Handle user input

Reference Documentation

Complete API Documentation

  • Element Trait API: See api-reference.md
    • Associated types, methods, parameters, return values
    • Hitbox system, event handling, cursor styles

Implementation Guides

  • Examples: See examples.md

    • Simple text element with highlighting
    • Interactive element with selection
    • Complex element with child management
  • Best Practices: See best-practices.md

    • State management, performance optimization
    • Interaction handling, layout strategies
    • Error handling, testing, common pitfalls
  • Common Patterns: See patterns.md

    • Text rendering, container, interactive, composite, scrollable patterns
    • Pattern selection guide
  • Advanced Patterns: See advanced-patterns.md

    • Custom layout algorithms (masonry, circular)
    • Element composition with traits
    • Async updates, memoization, virtual lists

Version History

  • e768f09 Current 2026-07-19 08:48

Same Skill Collection

.codex/skills/gpui-action/SKILL.md
.codex/skills/gpui-async/SKILL.md
.codex/skills/gpui-context/SKILL.md
.codex/skills/gpui-entity/SKILL.md
.codex/skills/gpui-event/SKILL.md
.codex/skills/gpui-focus-handle/SKILL.md
.codex/skills/gpui-global/SKILL.md
.codex/skills/gpui-layout-and-style/SKILL.md
.codex/skills/gpui-style-guide/SKILL.md
.codex/skills/gpui-test/SKILL.md
.codex/skills/navop-release-notes/SKILL.md
.codex/skills/new-component/SKILL.md

Metadata

Files
0
Version
ba0720c
Hash
073ac502
Indexed
2026-07-19 08:48

Accueil - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-07-20 15:26
浙ICP备14020137号-1 $Carte des visiteurs$