打包资源
把不会编译进可执行文件的应用文件放在 resources/ 目录:图像、模板、JSON 和其他数据。CLI 会把该目录的内容复制进打包后的应用。运行中的应用从资源目录读取它们,不依赖启动时的工作目录。
自定义字体是单独的选项。见字体。
#resources 目录
my-app/
├── resources/
│ ├── icon.png
│ ├── hero.png
│ └── copy/
│ └── template.txt
├── main.go
└── quickgui.config.ts每个文件保留相对于 resources/ 的路径。resources/hero.png 安装为 hero.png。resources/copy/template.txt 安装为 copy/template.txt。目标名称不得覆盖 fonts、AppIcon.icns、quickgui.json 或主机共享库等保留名。
quickgui dev 和 quickgui build 都会暂存这些文件。配置里列出的额外路径缺失时构建失败。resources/ 目录本身是可选的。
#额外文件
用 resources 配置项把额外文件或文件夹合并进同一个打包目录。每条额外路径按其基名复制。不要在这里列出项目的 resources/ 文件夹,它会自动包含。
import { defineConfig } from "@quickgui/cli";
export default defineConfig({
name: "My App",
identifier: "com.example.my-app",
entry: ".",
resources: ["legal/NOTICE.txt", "vendor/data"],
fonts: ["assets/MyFont.ttf"],
});legal/NOTICE.txt 安装为 NOTICE.txt。vendor/data 安装为 data/。
#安装位置
| Platform | Resource directory |
|---|---|
macOS .app | Contents/Resources |
Linux AppDir, AppImage, and .deb | Next to the executable (usr/bin) |
Linux install.sh tarball | Next to the executable (~/.local/<package>.app/bin) |
| Windows installer | Next to the executable in $INSTDIR |
运行时把打包后的名称拼到 ResourceDir 上;不要从项目树或当前工作目录读取。
#读取文件
在 native.Run 就绪后向主机查询路径,再拼接打包后的名称:
import (
"path/filepath"
"github.com/egoist/quickgui/go/native"
"github.com/egoist/quickgui/go/ui"
)
func App() *ui.Element {
return ui.Image().Value(hero).Width(160).Height(120)
}
var hero string
func main() {
native.Run(func() {
native.App.GetPaths(func(paths *native.AppPaths, err error) {
if err != nil || paths == nil {
return
}
hero = filepath.Join(paths.ResourceDir, "hero.png")
native.NewWindow(native.WindowOptions{
Title: "My App",
Component: App,
})
})
})
}ui.Image 接受该文件系统路径。其他文件用同样的拼接:用 os.ReadFile 或自己的加载器打开。相对图像路径不会相对资源目录解析。
#字体
fonts 列出 OpenType 文件。CLI 把它们复制到资源目录里的 fonts/,并在第一个窗口打开前注册。
export default defineConfig({
name: "My App",
identifier: "com.example.my-app",
entry: ".",
fonts: ["assets/MyFont.ttf"],
});在样式里使用字体系列名。字体不能代替 resources/:图像或 JSON 文件仍应放在该目录或额外的 resources 列表里。
