gpui-element by longbridge/gpui-component
npx skills add https://github.com/longbridge/gpui-component --skill gpui-element在以下情况下使用底层的 Element 特性:
Render/RenderOnce API 无法满足需求时对于以下情况,优先使用 Render/RenderOnce: 简单组件、标准布局、声明式 UI
Element 特性提供了对三个渲染阶段的直接控制:
impl Element for MyElement {
type RequestLayoutState = MyLayoutState; // 传递给后续阶段的数据
type PrepaintState = MyPaintState; // 用于绘制的数据
fn id(&self) -> Option<ElementId> {
Some(self.id.clone())
}
fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
None
}
// 阶段 1: 计算尺寸和位置
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 { /* ... */ })
}
// 阶段 2: 创建命中框,为绘制做准备
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 }
}
// 阶段 3: 渲染并处理交互
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() {
// 处理交互
cx.stop_propagation();
}
}
});
}
}
// 使元素能够作为子元素使用
impl IntoElement for MyElement {
type Element = Self;
fn into_element(self) -> Self::Element { self }
}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
RequestLayoutState → PrepaintState → paint
状态通过关联类型单向流动,在阶段之间作为可变引用传递。
window.request_layout(style, children, cx) - 创建布局节点window.insert_hitbox(bounds, behavior) - 创建交互区域window.paint_quad(...) - 渲染视觉内容window.on_mouse_event(handler) - 处理用户输入每周安装量
136
代码仓库
GitHub 星标数
10.7K
首次出现
2026年1月21日
安全审计
安装于
opencode123
codex117
gemini-cli117
github-copilot110
cursor106
claude-code100
Use the low-level Element trait when:
Render/RenderOnce APIs are insufficientPreferRender/RenderOnce for: Simple components, standard layouts, declarative UI
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 }
}
RequestLayoutState → PrepaintState → paint
State flows in one direction through associated types, passed as mutable references between phases.
window.request_layout(style, children, cx) - Create layout nodewindow.insert_hitbox(bounds, behavior) - Create interaction areawindow.paint_quad(...) - Render visual contentwindow.on_mouse_event(handler) - Handle user inputExamples : See examples.md
Best Practices : See best-practices.md
Common Patterns : See patterns.md
Advanced Patterns : See advanced-patterns.md
Weekly Installs
136
Repository
GitHub Stars
10.7K
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode123
codex117
gemini-cli117
github-copilot110
cursor106
claude-code100
Rust性能优化指南:m10-performance技能详解,包含算法、数据结构与内存优化策略
715 周安装
主题工厂技能:一键应用专业字体颜色主题,提升演示文稿设计效率
142 周安装
合规审计专家:SOC2、HIPAA、GDPR、PCI DSS 合规审计、差距分析与整改指导
141 周安装
agent-browser 浏览器自动化工具 - 快速网页交互与测试命令行工具
134 周安装
游戏安全逆向工程工具大全:调试、反编译、内存分析与反作弊绕过
135 周安装
Flutter 表单验证教程:使用 Form、TextFormField 和 GlobalKey 实现高效验证
135 周安装
Apache ECharts 教程 - 20+交互式图表制作与数据可视化技能
136 周安装