gpui-context by longbridge/gpui-component
npx skills add https://github.com/longbridge/gpui-component --skill gpui-contextGPUI 针对不同场景使用不同的上下文类型:
上下文类型:
App : 全局应用状态,实体创建Window : 窗口特定操作,绘制,布局Context<T>: 组件 T 的实体特定上下文AsyncApp : 前台任务的异步上下文AsyncWindowContext : 具有窗口访问权限的异步上下文impl MyComponent {
fn update_state(&mut self, cx: &mut Context<Self>) {
self.value = 42;
cx.notify(); // 触发重新渲染
// 生成异步任务
cx.spawn(async move |cx| {
// 异步工作
}).detach();
// 获取当前实体
let entity = cx.entity();
}
}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
fn main() {
let app = Application::new();
app.run(|cx: &mut App| {
// 创建实体
let entity = cx.new(|cx| MyState::default());
// 打开窗口
cx.open_window(WindowOptions::default(), |window, cx| {
cx.new(|cx| Root::new(view, window, cx))
});
});
}
impl Render for MyView {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
// 窗口操作
let is_focused = window.is_window_focused();
let bounds = window.bounds();
div().child("Content")
}
}
cx.spawn(async move |cx: &mut AsyncApp| {
let data = fetch_data().await;
entity.update(cx, |state, inner_cx| {
state.data = data;
inner_cx.notify();
}).ok();
}).detach();
// 创建实体
let entity = cx.new(|cx| MyState::default());
// 更新实体
entity.update(cx, |state, cx| {
state.value = 42;
cx.notify();
});
// 读取实体
let value = entity.read(cx).value;
// 触发重新渲染
cx.notify();
// 发出事件
cx.emit(MyEvent::Updated);
// 观察实体
cx.observe(&entity, |this, observed, cx| {
// 响应变化
}).detach();
// 订阅事件
cx.subscribe(&entity, |this, source, event, cx| {
// 处理事件
}).detach();
// 窗口状态
let focused = window.is_window_focused();
let bounds = window.bounds();
let scale = window.scale_factor();
// 关闭窗口
window.remove_window();
// 生成前台任务
cx.spawn(async move |cx| {
// 具有实体访问权限的异步工作
}).detach();
// 生成后台任务
cx.background_spawn(async move {
// 繁重计算
}).detach();
App (全局)
└─ Window (每个窗口)
└─ Context<T> (每个组件)
└─ AsyncApp (在异步任务中)
└─ AsyncWindowContext (异步 + 窗口)
每周安装量
151
仓库
GitHub 星标
10.7K
首次出现
2026年1月21日
安全审计
安装于
opencode137
codex129
gemini-cli127
github-copilot118
cursor113
amp108
GPUI uses different context types for different scenarios:
Context Types:
App : Global app state, entity creationWindow : Window-specific operations, painting, layoutContext<T>: Entity-specific context for component TAsyncApp : Async context for foreground tasksAsyncWindowContext : Async context with window accessimpl MyComponent {
fn update_state(&mut self, cx: &mut Context<Self>) {
self.value = 42;
cx.notify(); // Trigger re-render
// Spawn async task
cx.spawn(async move |cx| {
// Async work
}).detach();
// Get current entity
let entity = cx.entity();
}
}
fn main() {
let app = Application::new();
app.run(|cx: &mut App| {
// Create entities
let entity = cx.new(|cx| MyState::default());
// Open windows
cx.open_window(WindowOptions::default(), |window, cx| {
cx.new(|cx| Root::new(view, window, cx))
});
});
}
impl Render for MyView {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
// Window operations
let is_focused = window.is_window_focused();
let bounds = window.bounds();
div().child("Content")
}
}
cx.spawn(async move |cx: &mut AsyncApp| {
let data = fetch_data().await;
entity.update(cx, |state, inner_cx| {
state.data = data;
inner_cx.notify();
}).ok();
}).detach();
// Create entity
let entity = cx.new(|cx| MyState::default());
// Update entity
entity.update(cx, |state, cx| {
state.value = 42;
cx.notify();
});
// Read entity
let value = entity.read(cx).value;
// Trigger re-render
cx.notify();
// Emit event
cx.emit(MyEvent::Updated);
// Observe entity
cx.observe(&entity, |this, observed, cx| {
// React to changes
}).detach();
// Subscribe to events
cx.subscribe(&entity, |this, source, event, cx| {
// Handle event
}).detach();
// Window state
let focused = window.is_window_focused();
let bounds = window.bounds();
let scale = window.scale_factor();
// Close window
window.remove_window();
// Spawn foreground task
cx.spawn(async move |cx| {
// Async work with entity access
}).detach();
// Spawn background task
cx.background_spawn(async move {
// Heavy computation
}).detach();
App (Global)
└─ Window (Per-window)
└─ Context<T> (Per-component)
└─ AsyncApp (In async tasks)
└─ AsyncWindowContext (Async + Window)
Weekly Installs
151
Repository
GitHub Stars
10.7K
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode137
codex129
gemini-cli127
github-copilot118
cursor113
amp108
Rust性能优化指南:m10-performance技能详解,包含算法、数据结构与内存优化策略
718 周安装