跳到正文

入门

#系统要求

  • macOS 14 或更新版本
  • Go 1.23 或更新版本
  • Bun 1.4 或更新版本
  • Xcode Command Line Tools(xcode-select --install)

当前支持的目标平台是 macOS。

#创建项目

bunx @quickgui/cli init my-app --language go
cd my-app
quickgui dev

init 会写入项目并安装依赖。省略 --language 可以交互选择 Go、TypeScript 或 Rust。

#第一个窗口

main.go 通过 native.Run 启动应用并打开窗口。把组件函数传给 WindowOptions.Component。组件返回该窗口的界面。

package main

import (
	"log"

	"github.com/egoist/quickgui/go/native"
	"github.com/egoist/quickgui/go/ui"
)

func Counter() *ui.Element {
	count, setCount := ui.CreateSignal(0)
	return ui.View().
		FlexCol().
		SizeFull().
		ItemsCenter().
		JustifyCenter().
		Gap(20).
		Bg("#090d16").
		TextColor("#e2e8f0").
		Child(ui.Text("Count: ", count()).FontSize(28).FontWeight(700)).
		Child(ui.Button().
			OnClick(func() { setCount(count() + 1) }).
			Padding(12).
			RoundedLg().
			Bg("#2563eb").
			Hover(func(s ui.StyleBuilder) ui.StyleBuilder { return s.BackgroundColor("#3b82f6") }).
			Child("Increment"))
}

func main() {
	if err := native.Run(func() {
		open := func() {
			native.NewWindow(native.WindowOptions{
				Title:     "My App",
				Width:     720,
				Height:    480,
				Component: Counter,
			})
		}
		native.App.OnReopen(func(event native.ReopenEvent) {
			if !event.HasVisibleWindows {
				open()
			}
		})
		open()
	}); err != nil {
		log.Fatal(err)
	}
}

当用户点击 Dock 图标且没有可见窗口时,OnReopen 会再次打开窗口。

#运行与构建

quickgui dev
quickgui fmt
quickgui check
quickgui build

dev 会在你编辑时重新编译 Go 代码并重启应用。fmt 会为较长的 UI 调用换行。build 把打包后的应用写到 dist/。

#后续步骤