SuperAPI AI 工具集

gpui-components

编程与开发

查看项目

更新于 10-16 05:59

技能介绍

用途

gpui-components 技能用于生成 GPUI 桌面 UI 组件的 Rust 代码,遵循 Zed 编辑器与 gpui-component 库的实现模式。当开发者使用 gpui crate 构建桌面应用、创建主题化 UI、构建自动补全或命令面板等场景时,可参考该技能内容。

适用场景

  • 使用 GPUI 框架构建桌面 UI 应用
  • 使用 RenderOnce 或 Render trait 创建自定义组件
  • 使用 ActiveTheme 实现主题化组件
  • 构建自动补全/补全菜单
  • 创建命令面板和弹出菜单
  • 使用 gpui-component crate

核心概念与使用方式

应用初始化

使用 Application::new().run 启动,并通过 gpui_component::init(cx) 初始化组件库,随后调用 cx.open_window 打开窗口,窗口根组件使用 Root::new 包裹应用内容。

组件模式

技能区分两种组件写法:

  • RenderOnce(无状态):通过 #[derive(IntoElement)] 定义结构体,实现 RenderOnce trait,在 render 中构建 div 并应用内边距、背景色、文字颜色、圆角等样式。
  • Render(有状态,配合 Entity):为持有状态的结构体实现 Render trait,在 render 中通过 cx.listener 绑定点击事件,修改状态后调用 cx.notify() 触发重渲染。

可用组件(gpui-component)

  • 表单组件:Button(支持 primary、danger、ghost、link 等变体)、Input(支持 LSP 补全)、NumberInput(带步进控件)、Checkbox、Switch、Radio、Select、Slider。
  • 布局组件:Dialog(模态对话框)、Popover、Menu / PopupMenu、Tab、Accordion、Sidebar、Dock。
  • 显示组件:Badge、Avatar、Icon(IconName 枚举)、Label、Tooltip、Notification(Toast 通知)。
  • 数据组件:Table、List / VirtualList、Tree、Chart。

主题系统

通过 ActiveTheme 的 cx.theme() 访问主题。主题颜色包括:

  • 背景色:background、secondary、muted、card
  • 前景色:foreground、muted_foreground
  • 语义色:primary、destructive、success、warning
  • 边框色:border、ring

组件示例

  • Button:通过 Button::new 创建,可链式调用 .label()、.icon()、.primary()、.on_click()。
  • Input:先创建 InputState,再通过 Input::new(&self.input_state) 使用,支持 .placeholder()、.cleanable()。
  • PopupMenu / 命令面板:通过 PopupMenu::build 构建,使用 menu_item、separator 等方法,菜单项可绑定图标和 action。
  • CompletionProvider:实现 completions 方法返回 CompletionResponse,实现 is_completion_trigger 判断触发字符。

项目设置

Cargo.toml 依赖示例:

[dependencies]
gpui = "0.2"
gpui-component = { version = "0.4", features = ["webview"] }

基本应用结构:使用 gpui::* 与 gpui_component::*,为应用结构体实现 Render,在 render 中通过 Root::new 包裹根 div,组织工具栏与内容区域。

参考文件

技能内提供参考文档,包括:reference/components.md(完整组件 API)、reference/theming.md(主题系统细节)、reference/input-lsp.md(带补全的输入框)、reference/menus.md(菜单与命令面板)。

限制

关于该技能的收费、价格、平台支持范围、效果等方面的信息,资料中暂未提供。