| 属性 | 内容 |
|---|---|
| 项目名称 | gogpu/ui |
| GitHub | https://github.com/gogpu/ui |
| 开发语言 | Go 1.25+ |
| 项目定位 | Enterprise-Grade GUI Toolkit for Go |
| 核心理念 | Modern widgets, reactive state, GPU-accelerated rendering — zero CGO |
| 开源协议 | MIT |
| Star 数 | 59+ |
| 代码规模 | 55,000+ 行, 25 个包, 1,500+ 测试 |
| 测试覆盖率 | ~97% 平均覆盖率 |
gogpu/ui 旨在构建企业级 GUI 工具包,目标应用类型:
| 特性 | gogpu/ui | Fyne | Gio |
|---|---|---|---|
| CGO-free | ✅ Yes | ❌ No | ✅ Yes |
| WebGPU 渲染 | ✅ Yes | OpenGL | Direct GPU |
| Reactive State | Signals | Binding | Events |
| Layout Engine | Flexbox + Grid | Custom | Flex |
| Accessibility | Day 1 (ARIA) | Limited | Limited |
| Plugin System | ✅ Yes | ❌ No | ❌ No |
"250K+ lines of Pure Go — no CGO, no Rust, no C."
┌─────────────────────────────────────────────────────────────────────┐
│ USER APPLICATION 用户应用层 │
│ │
├─────────────────────────────────────────────────────────────────────┤
│ THEME LAYER 主题层 │
│ ┌─────────────────┬─────────────────┬─────────────────┐ │
│ │ theme/material3 │ theme/fluent │ theme/cupertino │ │
│ │ (Complete ✅) │ (Planned) │ (Planned) │ │
│ └─────────────────┴─────────────────┴─────────────────┘ │
├─────────────────────────────────────────────────────────────────────┤
│ WIDGET LAYER 组件层 │
│ ┌─────────────┬─────────────┬─────────────┬─────────────┐ │
│ │ core/button │ core/checkbox│ core/radio │ core/textfield│ │
│ │ (Complete ✅)│ (Complete ✅)│ (Complete ✅)│ (Complete ✅) │ │
│ ├─────────────┼─────────────┼─────────────┼─────────────┤ │
│ │core/dropdown│ focus/ │ overlay/ │ │ │
│ │(Complete ✅) │ (95%+) │ (95%+) │ │ │
│ └─────────────┴─────────────┴─────────────┴─────────────┘ │
├─────────────────────────────────────────────────────────────────────┤
│ CDK (Component Dev Kit) 组件开发工具包 │
│ • Content[C] polymorphic pattern │
│ • Headless behaviors │
├─────────────────────────────────────────────────────────────────────┤
│ PRIMITIVES 基础组件层 │
│ • Box, Text, Image (Complete ✅) │
├─────────────────────────────────────────────────────────────────────┤
│ APP 应用层 │
│ • Window integration │
│ • Event loop │
├─────────────────────────────────────────────────────────────────────┤
│ INFRASTRUCTURE 基础设施层 │
│ ┌──────────┬──────────┬──────────┬──────────┬──────────┐ │
│ │ registry │ plugin │ theme │ layout │ state │ │
│ │(Complete)│(Complete)│(Complete)│(Complete)│(Complete)│ │
│ ├──────────┼──────────┼──────────┼──────────┼──────────┤ │
│ │ widget │ event │ geometry │ a11y │ │ │
│ │(Complete)│(Complete)│(Complete)│(Complete)│ │ │
│ └──────────┴──────────┴──────────┴──────────┴──────────┘ │
├─────────────────────────────────────────────────────────────────────┤
│ RENDERING 渲染层 │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ gogpu/gg │ │ gpucontext │ │coregx/signals│ │
│ │ 2D Graphics│ │ Shared Ifaces│ │State Mgmt │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
├─────────────────────────────────────────────────────────────────────┤
│ GPU FRAMEWORK (via dependency inversion) │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ gogpu │ │ wgpu │ │ naga │ │ goffi │ │
│ │ Window │ │ WebGPU │ │ Shader │ │ FFI │ │
│ └─────────┘ └─────────┘ └─────────┘ └─────────┘ │
└─────────────────────────────────────────────────────────────────────┘
关键设计: ui 包不直接依赖 gogpu,而是通过接口解耦
ui → gpucontext (interfaces) ← dependency inversion
ui → gg (2D rendering)
ui → coregx/signals (reactive)
gogpu → gpucontext (implements) ← concrete implementation
优势:
| 包 | 描述 | 覆盖率 |
|---|---|---|
| geometry | Point, Size, Rect, Constraints, Insets | 98.8% |
| event | MouseEvent, KeyEvent, WheelEvent, FocusEvent | 100% |
| widget | Widget interface, WidgetBase, Context, Canvas | 100% |
| internal/render | Canvas implementation using gogpu/gg | 96.5% |
| internal/layout | Flex, Stack, Grid layout engines | 89.9% |
| 包 | 描述 | 覆盖率 |
|---|---|---|
| a11y | 35+ ARIA roles, Accessible interface, Tree | 99.1% |
| state | Reactive signals, Binding, Scheduler | 100% |
| primitives | Box, Text, Image widgets | 94.4% |
| app | Window integration via gpucontext | 98.6% |
| 包 | 描述 | 覆盖率 |
|---|---|---|
| registry | Widget factory registration | 100% |
| plugin | Plugin bundling with dependency resolution | 99.4% |
| theme | Theme system with Extensions | 100% |
| layout | Public layout API | 89.5% |
| 包 | 描述 | 覆盖率 |
|---|---|---|
| cdk | Component Development Kit | 100% |
| core/button | 4 variants, 3 sizes | 96%+ |
| core/checkbox | checked/unchecked/indeterminate | 96%+ |
| core/radio | Radio group with keyboard nav | 96%+ |
| core/textfield | Cursor, selection, clipboard | 96%+ |
| core/dropdown | Overlay menu, keyboard nav | 96%+ |
| overlay | Popup stack, container | 95%+ |
| theme/material3 | HCT color science, 5 painters | 97%+ |
| focus | Keyboard focus management | 95.2% |
// 创建 Signal
count := state.NewSignal(0)
// 计算属性 (自动更新)
greeting := state.NewComputed(func() string {
return "Hello, " + name.Get() + "!"
})
// 绑定到 Widget
binding := state.Bind(name, ctx)
defer binding.Unbind()
// 批量更新 (单次重绘)
scheduler.Batch(func() {
firstName.Set("Alice")
lastName.Set("Smith")
age.Set(30)
})
// Box - 通用容器
primitives.Box(children...).
Padding(16).
PaddingXY(24, 8).
Background(theme.Surface).
Rounded(8).
BorderStyle(1, theme.Outline).
ShadowLevel(2).
Gap(8)
// Text - 静态文本
primitives.Text("Hello World").
FontSize(24).
Bold().
Color(theme.OnSurface).
Align(primitives.TextAlignCenter).
MaxLines(2).
Ellipsis()
// TextFn - 响应式文本
primitives.TextFn(func() string {
return fmt.Sprintf("Count: %d", count.Get())
}).FontSize(18)
// Image
primitives.Image(mySource).
Size(48, 48).
Cover().
Rounded(24).
Alt("User avatar")
// Flexbox 布局
primitives.Box(
primitives.Text("Item 1"),
primitives.Text("Item 2"),
primitives.Text("Item 3"),
).Gap(12).Padding(24) // 垂直堆叠 (默认)
// 水平布局
primitives.Box(...).Direction(layout.Horizontal).Gap(12)
// Grid 布局
primitives.Grid(
primitives.Text("A"),
primitives.Text("B"),
primitives.Text("C"),
).Columns(3).Gap(8)
// 每个 Widget 实现 a11y.Accessible
func (b *MyButton) AccessibilityRole() a11y.Role {
return a11y.RoleButton
}
func (b *MyButton) AccessibilityLabel() string {
return b.text
}
func (b *MyButton) AccessibilityActions() []a11y.Action {
return []a11y.Action{a11y.ActionClick}
}
// 无障碍树
root := a11y.NewNode(a11y.RoleWindow, "My Application")
tree := a11y.NewMemoryTree(root)
button := a11y.NewNode(a11y.RoleButton, "Save")
tree.Insert(root, button)
// UI 通过接口连接窗口系统 (非具体类型)
uiApp := app.New(
app.WithWindowProvider(gogpuApp), // gpucontext.WindowProvider
app.WithPlatformProvider(gogpuApp), // gpucontext.PlatformProvider
app.WithTheme(myTheme),
)
uiApp.SetRoot(rootWidget)
// Headless 模式 (测试用,无需窗口)
testApp := app.New()
testApp.SetRoot(rootWidget)
testApp.Window().Frame() // 处理布局 + 绘制
package main
import (
"fmt"
"github.com/gogpu/gogpu"
"github.com/gogpu/ui/app"
"github.com/gogpu/ui/primitives"
"github.com/gogpu/ui/state"
"github.com/gogpu/ui/widget"
)
func main() {
// 创建 GPU 应用
gogpuApp := gogpu.NewApp(gogpu.DefaultConfig().
WithTitle("My App").
WithSize(800, 600).
WithContinuousRender(false)) // 事件驱动: 空闲时 0% CPU
// 创建 UI 应用
uiApp := app.New(
app.WithWindowProvider(gogpuApp),
app.WithPlatformProvider(gogpuApp),
)
// 响应式状态
count := state.NewSignal(0)
// 设置根组件
uiApp.SetRoot(
primitives.Box(
primitives.Text("Hello gogpu/ui!").
FontSize(24).
Bold().
Color(widget.RGBA8(33, 33, 33, 255)),
primitives.TextFn(func() string {
return fmt.Sprintf("Count: %d", count.Get())
}).FontSize(18),
primitives.Box().
Width(200).Height(40).
Background(widget.RGBA8(98, 0, 238, 255)).
Rounded(8),
).Padding(24).Gap(12).
Background(widget.RGBA8(255, 255, 255, 255)),
)
// 运行
gogpuApp.Run()
}
| 项目 | GitHub | 描述 | 状态 |
|---|---|---|---|
| gogpu/gogpu | github.com/gogpu/gogpu | GPU 框架 | ✅ |
| gogpu/ui | github.com/gogpu/ui | GUI 工具包 | Beta ✅ |
| gogpu/gg | github.com/gogpu/gg | 2D 图形库 | ✅ |
| gogpu/wgpu | github.com/gogpu/wgpu | Pure Go WebGPU | ✅ |
| gogpu/naga | github.com/gogpu/naga | 着色器编译器 | ✅ |
总计: 250K+ 行 Pure Go 代码
已实现的 Material 3 组件:
✅ 零 CGO - 纯 Go,构建简单
✅ GPU 加速 - WebGPU 硬件加速渲染
✅ 响应式 - Signals 自动 UI 更新
✅ 无障碍 - Day 1 ARIA 支持
✅ 布局灵活 - Flexbox + Grid
✅ 企业级 - 高测试覆盖率 (~97%)
✅ 主题系统 - Material 3 + 可扩展
✅ 插件支持 - 第三方组件扩展
⚠️ 早期阶段 - API 可能不稳定
⚠️ 组件较少 - 相比 Fyne/Gio 组件数量少
⚠️ 文档待完善 - 需要更多教程和示例
⚠️ 社区小 - 用户和贡献者较少
⚠️ 移动端 - 尚未验证
⚠️ 无发布版本 - 尚未发布 v1.0
| 场景 | 推荐度 | 说明 |
|---|---|---|
| 专业 IDE | ⭐⭐⭐⭐⭐ | 目标场景 |
| 设计工具 | ⭐⭐⭐⭐⭐ | 目标场景 |
| CAD 应用 | ⭐⭐⭐⭐⭐ | 目标场景 |
| 数据可视化 | ⭐⭐⭐⭐ | GPU 加速优势 |
| 普通桌面应用 | ⭐⭐⭐ | 可用但生态待完善 |
| 移动端 | ⭐ | 待验证 |
| 维度 | gogpu/ui | Fyne | Gio | Wails | Electron |
|---|---|---|---|---|---|
| 语言 | Go | Go | Go | Go | JS/HTML/CSS |
| 渲染 | WebGPU | OpenGL | Direct GPU | WebView | Chromium |
| CGO | ❌ 无 | ✅ 有 | ❌ 无 | ✅ 有 | ❌ 无 |
| 内存占用 | 低 | 中 | 低 | 中 | 高 |
| 启动速度 | 快 | 快 | 快 | 中 | 慢 |
| 无障碍 | ⭐⭐⭐⭐⭐ | ⭐⭐ | ⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
| 自定义渲染 | ⭐⭐⭐⭐⭐ | ⭐⭐ | ⭐⭐⭐ | ⭐ | ⭐⭐ |
| 成熟度 | ⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
CDK 使用多态内容模式,允许组件接受任意内容:
// Button 可以接受文本、图标或任何 Widget
button := core.NewButton().
WithContent(primitives.Text("Click me"))
// 或
button := core.NewButton().
WithContent(primitives.Box(
primitives.Image(icon),
primitives.Text("Save"),
))
组件逻辑与渲染分离:
// Button 逻辑 (可测试,无 UI)
type ButtonBehavior struct {
Pressed bool
OnClick func()
}
// Button 渲染 (可替换主题)
type ButtonPainter interface {
Paint(ctx *widget.Context, state ButtonState)
}
Idle (0% CPU) → Animation → Continuous
↑__________________________|
空闲时 0% CPU 占用,动画时 VSync,游戏模式连续渲染。
研究时间: 2026-03-07
研究者: 小凯
标签: #gogpu #ui #GoGUI #WebGPU #零CGO #MaterialDesign #响应式