Compare commits

...

8 Commits

Author SHA1 Message Date
e15d66956c doc: update docs/wails.md
https://github.com/jaywcjlove/reference/discussions/580
2025-06-16 19:00:04 +08:00
32862fe194 doc: update README.md. 2025-06-16 10:58:16 +00:00
626e70a666 doc: update CONTRIBUTING.md. 2025-06-16 10:58:15 +00:00
47cf42deff feat: add wails.md cheat sheet doc (#973)
* docs: add wails.md introduction doc

* Update wails.md

* docs: update documentation
2025-06-16 18:57:12 +08:00
55ee13ad5f doc: Update README.md 2025-06-13 08:42:53 +08:00
3a106842b3 doc: Update README.md #102 2025-06-09 19:03:08 +08:00
0feb5644c9 doc: Update README.md 2025-06-08 21:44:28 +08:00
139d107454 Add OneNAS.space 2025-06-08 21:21:18 +08:00
5 changed files with 429 additions and 18 deletions

View File

@ -399,6 +399,7 @@ jobs:
<a href="https://github.com/leauny" title="leauny"><img src="https://avatars.githubusercontent.com/u/42369176?v=4" width="42;" alt="leauny"/></a>
<a href="https://github.com/liliangrong777" title="liliangrong777"><img src="https://avatars.githubusercontent.com/u/58727146?v=4" width="42;" alt="liliangrong777"/></a>
<a href="https://github.com/lozhu20" title="lozhu"><img src="https://avatars.githubusercontent.com/u/44923922?v=4" width="42;" alt="lozhu"/></a>
<a href="https://github.com/Zeng-qh" title="都一样"><img src="https://avatars.githubusercontent.com/u/40046415?v=4" width="42;" alt="都一样"/></a>
<a href="https://github.com/yanxuplay" title="hupilan"><img src="https://avatars.githubusercontent.com/u/69749541?v=4" width="42;" alt="hupilan"/></a>
<a href="https://github.com/hua03" title="hua03"><img src="https://avatars.githubusercontent.com/u/19561959?v=4" width="42;" alt="hua03"/></a>
<a href="https://github.com/gzttcydxx" title="gzttcydxx"><img src="https://avatars.githubusercontent.com/u/50025185?v=4" width="42;" alt="gzttcydxx"/></a>
@ -414,7 +415,6 @@ jobs:
<a href="https://github.com/gaoxiaoduan" title="Coder Duan"><img src="https://avatars.githubusercontent.com/u/69953511?v=4" width="42;" alt="Coder Duan"/></a>
<a href="https://github.com/lijc210" title="cizai"><img src="https://avatars.githubusercontent.com/u/10651081?v=4" width="42;" alt="cizai"/></a>
<a href="https://github.com/chyok" title="chyok"><img src="https://avatars.githubusercontent.com/u/32629225?v=4" width="42;" alt="chyok"/></a>
<a href="https://github.com/Zeng-qh" title="都一样"><img src="https://avatars.githubusercontent.com/u/40046415?v=4" width="42;" alt="都一样"/></a>
<a href="https://github.com/binscor" title="binscor"><img src="https://avatars.githubusercontent.com/u/37325821?v=4" width="42;" alt="binscor"/></a>
<a href="https://github.com/qwxingzhe" title="行者"><img src="https://avatars.githubusercontent.com/u/7071651?v=4" width="42;" alt="行者"/></a>
<a href="https://github.com/lisheng741" title="芦荟柚子茶"><img src="https://avatars.githubusercontent.com/u/53617305?v=4" width="42;" alt="芦荟柚子茶"/></a>
@ -428,6 +428,7 @@ jobs:
<a href="https://github.com/LuckyJie12" title="夜未央"><img src="https://avatars.githubusercontent.com/u/102901105?v=4" width="42;" alt="夜未央"/></a>
<a href="https://github.com/kele527" title="吹衣轻飏"><img src="https://avatars.githubusercontent.com/u/345445?v=4" width="42;" alt="吹衣轻飏"/></a>
<a href="https://github.com/lvzhenbo" title="无聊波波"><img src="https://avatars.githubusercontent.com/u/32427677?v=4" width="42;" alt="无聊波波"/></a>
<a href="https://github.com/ChuwuYo" title="初五"><img src="https://avatars.githubusercontent.com/u/141227996?v=4" width="42;" alt="初五"/></a>
<a href="https://github.com/zxx457" title="Xianxin Zeng"><img src="https://avatars.githubusercontent.com/u/114141362?v=4" width="42;" alt="Xianxin Zeng"/></a>
<a href="https://github.com/zhouhw0306" title="zhouhw0306"><img src="https://avatars.githubusercontent.com/u/82752681?v=4" width="42;" alt="zhouhw0306"/></a>
<a href="https://github.com/hiyms" title="yms"><img src="https://avatars.githubusercontent.com/u/84654050?v=4" width="42;" alt="yms"/></a>

View File

@ -21,6 +21,7 @@ Quick Reference
非常感谢一直以来支持我开源项目的朋友们!如果您认可我的工作,欢迎通过 [赞助](https://wangchujiang.com/#/sponsor)<!--rehype:target=_blank--> 我或下载并使用我开发的 [macOS 应用](https://wangchujiang.com/#/app)<!--rehype:target=_blank--> 来支持我。以下是我个人独立开发的 macOS 应用列表:
<p style="display: inline-block">
<a target="_blank" href="https://wangchujiang.com/mousio-hint/" title="Mousio Hint for macOS"><img align="center" alt="Mousio Hint" height="52" width="52" src="./appicon/mousio-hint.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6746747327" title="Mousio for macOS"><img align="center" alt="Mousio" height="52" width="52" src="./appicon/mousio.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6745227444" title="Musicer for macOS"><img align="center" alt="Musicer" height="52" width="52" src="./appicon/musicer.png"></a>
<a target="_blank" href="https://apps.apple.com/app/6743841447" title="Audioer for macOS"><img align="center" alt="Audioer" height="52" width="52" src="./appicon/audioer.png"></a>
@ -85,14 +86,8 @@ Quick Reference
[C#](./docs/cs.md)<!--rehype:style=background: rgb(6 147 13);&class=contributing-->
[C++](./docs/cpp.md)<!--rehype:style=background: rgb(6 147 13);&class=contributing-->
[Dart](./docs/dart.md)<!--rehype:style=background: rgb(64 196 255);-->
[Docker](./docs/docker.md)<!--rehype:style=background: rgb(72 143 223);-->
[Docker Compose](./docs/docker-compose.md)<!--rehype:style=background: rgb(72 143 223);&class=contributing tag&data-lang=Docker-->
[Dockerfile](./docs/dockerfile.md)<!--rehype:style=background: rgb(0 72 153);&class=tag&data-lang=Docker-->
[Django](./docs/django.md)<!--rehype:style=background: rgb(12 75 51);&class=contributing tag&data-lang=Python-->
[Elixir](./docs/elixir.md)<!--rehype:style=background: rgb(124 26 156);&class=contributing tag&data-lang=Elixir-->
[Erlang](./docs/erlang.md)<!--rehype:style=background: rgb(206 55 97);&class=contributing-->
[Flask](./docs/flask.md)<!--rehype:style=background: rgb(210 168 255);&class=contributing tag&data-lang=Python-->
[FastAPI](./docs/fastapi.md)<!--rehype:style=background: rgb(210 168 255);&class=contributing tag&data-lang=Python-->
[Flutter](./docs/flutter.md)<!--rehype:style=background: rgb(150 220 254);&class=contributing tag&data-lang=Dart-->
[Golang](./docs/golang.md)<!--rehype:style=background: rgb(39 160 193);-->
[GraphQL](./docs/graphql.md)<!--rehype:style=background: rgb(214 66 146);-->
@ -106,7 +101,6 @@ Quick Reference
[Markdown](./docs/markdown.md)<!--rehype:style=background: rgb(103 61 156);-->
[MATLAB](./docs/matlab.md)<!--rehype:style=background: rgb(0 118 168);&class=contributing-->
[PHP](./docs/php.md)<!--rehype:style=background: rgb(79 91 147);-->
[Python](./docs/python.md)<!--rehype:style=background: rgb(43 91 132);-->
[R 语言](./docs/r.md)<!--rehype:style=background: rgb(39 108 192);&class=contributing&data-info-->
[Ruby](./docs/ruby.md)<!--rehype:style=background: rgb(204 52 45);-->
[Rust](./docs/rust.md)<!--rehype:style=background: rgb(71 71 71);-->
@ -115,11 +109,16 @@ Quick Reference
[SwiftUI](./docs/swiftui.md)<!--rehype:style=background: rgb(10 127 247);&class=tag&data-lang=swift-->
[Spring Boot](./docs/springboot.md)<!--rehype:style=background: rgb(0 149 0);&class=tag&data-lang=Java-->
[Lua](./docs/lua.md)<!--rehype:style=background: rgb(3 3 128);-->
[Pytorch](./docs/pytorch.md)<!--rehype:style=background: rgb(238 76 44);&class=contributing tag&data-lang=Python&data-info=👆看看还缺点儿什么?-->
[Jupyter](./docs/jupyter.md)<!--rehype:style=background: rgb(0 72 153);&class=tag&data-lang=Python-->
[Minio](./docs/minio.md)<!--rehype:style=background: rgb(3 3 128);-->
<!--rehype:class=home-card-->
## Docker
[Docker](./docs/docker.md)<!--rehype:style=background: rgb(72 143 223);-->
[Docker Compose](./docs/docker-compose.md)<!--rehype:style=background: rgb(72 143 223);&class=contributing tag&data-lang=Docker-->
[Dockerfile](./docs/dockerfile.md)<!--rehype:style=background: rgb(0 72 153);&class=tag&data-lang=Docker-->
<!--rehype:class=home-card-->
## 配置
[INI](./docs/ini.md)<!--rehype:style=background: rgb(57 59 60);-->
@ -179,6 +178,19 @@ Quick Reference
[Bun](./docs/bun.md)<!--rehype:style=background: rgb(157 49 81);-->
<!--rehype:class=home-card-->
## Python
[Python](./docs/python.md)<!--rehype:style=background: rgb(43 91 132);-->
[Django](./docs/django.md)<!--rehype:style=background: rgb(12 75 51);&class=contributing tag&data-lang=Python-->
[Flask](./docs/flask.md)<!--rehype:style=background: rgb(210 168 255);&class=contributing tag&data-lang=Python-->
[FastAPI](./docs/fastapi.md)<!--rehype:style=background: rgb(210 168 255);&class=contributing tag&data-lang=Python-->
[Pytorch](./docs/pytorch.md)<!--rehype:style=background: rgb(238 76 44);&class=contributing tag&data-lang=Python&data-info=👆看看还缺点儿什么?-->
[Jupyter](./docs/jupyter.md)<!--rehype:style=background: rgb(0 72 153);&class=tag&data-lang=Python-->
[Conda](./docs/conda.md)<!--rehype:style=background: rgb(68 168 51);&class=tag&data-lang=Python-->
[pip](./docs/pip.md)<!--rehype:style=background: rgb(24 147 209);&class=tag contributing&data-lang=Python-->
[uv](./docs/uv.md)<!--rehype:style=background: rgb(26 35 126);&class=tag contributing&data-lang=Python-->
<!--rehype:class=home-card-->
## 命令
[ADB, Android Debug Bridge](./docs/adb.md)<!--rehype:style=background: rgb(238 0 0);&class=contributing tag&data-lang=Android&data-info=👆看看还缺点儿什么?-->
@ -218,7 +230,6 @@ Quick Reference
[tar](./docs/tar.md)<!--rehype:style=background: rgb(215 89 62);&class=tag&data-lang=解压缩-->
[Yazi](./docs/yazi.md)<!--rehype:style=background: rgb(255 165 0);&class=tag&data-lang=文件管理器;&class=contributing-->
[Zip](./docs/zip.md)<!--rehype:style=background: rgb(99 99 99);&class=tag&data-lang=解压缩-->
[uv](./docs/uv.md)<!--rehype:style=background: rgb(222 95 233);&class=tag&data-lang=Python-->
<!--rehype:class=home-card-->
## 工具
@ -238,15 +249,12 @@ Quick Reference
[APT](./docs/apt.md)<!--rehype:style=background: rgb(30 144 255);-->
[Conan](./docs/conan.md)<!--rehype:style=background: rgb(0 193 9);&class=tag&data-lang=C/C++-->
[Conda](./docs/conda.md)<!--rehype:style=background: rgb(68 168 51);&class=tag&data-lang=Python-->
[CocoaPods](./docs/cocoapods.md)<!--rehype:style=background: rgb(251 0 6);&class=tag&data-lang=C/C++-->
[Cargo](./docs/cargo.md)<!--rehype:style=background: rgb(71 71 71);&class=tag&data-lang=Rust-->
[Homebrew](./docs/homebrew.md)<!--rehype:style=background: rgb(252 185 87);&class=tag&data-lang=macOS-->
[Pacman](./docs/pacman.md)<!--rehype:style=background: rgb(24 147 209);&class=tag&data-lang=archlinux&class=contributing-->
[pip](./docs/pip.md)<!--rehype:style=background: rgb(24 147 209);&class=tag&data-lang=Python&class=contributing-->
[Pacman](./docs/pacman.md)<!--rehype:style=background: rgb(24 147 209);&class=tag contributing&data-lang=Linux-->
[YUM](./docs/yum.md)<!--rehype:style=background: rgb(86 86 123);-->
[SDKMAN](./docs/sdkman.md)<!--rehype:style=background: rgb(0 118 198);-->
[uv](./docs/uv.md)<!--rehype:style=background: rgb(26 35 126);&class=tag&data-lang=Python&class=contributing-->
<!--rehype:class=home-card-->
## Git 版本控制
@ -402,6 +410,7 @@ Quick Reference
<a href="https://github.com/leauny" title="leauny"><img src="https://avatars.githubusercontent.com/u/42369176?v=4" width="42;" alt="leauny"/></a>
<a href="https://github.com/liliangrong777" title="liliangrong777"><img src="https://avatars.githubusercontent.com/u/58727146?v=4" width="42;" alt="liliangrong777"/></a>
<a href="https://github.com/lozhu20" title="lozhu"><img src="https://avatars.githubusercontent.com/u/44923922?v=4" width="42;" alt="lozhu"/></a>
<a href="https://github.com/Zeng-qh" title="都一样"><img src="https://avatars.githubusercontent.com/u/40046415?v=4" width="42;" alt="都一样"/></a>
<a href="https://github.com/yanxuplay" title="hupilan"><img src="https://avatars.githubusercontent.com/u/69749541?v=4" width="42;" alt="hupilan"/></a>
<a href="https://github.com/hua03" title="hua03"><img src="https://avatars.githubusercontent.com/u/19561959?v=4" width="42;" alt="hua03"/></a>
<a href="https://github.com/gzttcydxx" title="gzttcydxx"><img src="https://avatars.githubusercontent.com/u/50025185?v=4" width="42;" alt="gzttcydxx"/></a>
@ -417,7 +426,6 @@ Quick Reference
<a href="https://github.com/gaoxiaoduan" title="Coder Duan"><img src="https://avatars.githubusercontent.com/u/69953511?v=4" width="42;" alt="Coder Duan"/></a>
<a href="https://github.com/lijc210" title="cizai"><img src="https://avatars.githubusercontent.com/u/10651081?v=4" width="42;" alt="cizai"/></a>
<a href="https://github.com/chyok" title="chyok"><img src="https://avatars.githubusercontent.com/u/32629225?v=4" width="42;" alt="chyok"/></a>
<a href="https://github.com/Zeng-qh" title="都一样"><img src="https://avatars.githubusercontent.com/u/40046415?v=4" width="42;" alt="都一样"/></a>
<a href="https://github.com/binscor" title="binscor"><img src="https://avatars.githubusercontent.com/u/37325821?v=4" width="42;" alt="binscor"/></a>
<a href="https://github.com/qwxingzhe" title="行者"><img src="https://avatars.githubusercontent.com/u/7071651?v=4" width="42;" alt="行者"/></a>
<a href="https://github.com/lisheng741" title="芦荟柚子茶"><img src="https://avatars.githubusercontent.com/u/53617305?v=4" width="42;" alt="芦荟柚子茶"/></a>
@ -431,6 +439,7 @@ Quick Reference
<a href="https://github.com/LuckyJie12" title="夜未央"><img src="https://avatars.githubusercontent.com/u/102901105?v=4" width="42;" alt="夜未央"/></a>
<a href="https://github.com/kele527" title="吹衣轻飏"><img src="https://avatars.githubusercontent.com/u/345445?v=4" width="42;" alt="吹衣轻飏"/></a>
<a href="https://github.com/lvzhenbo" title="无聊波波"><img src="https://avatars.githubusercontent.com/u/32427677?v=4" width="42;" alt="无聊波波"/></a>
<a href="https://github.com/ChuwuYo" title="初五"><img src="https://avatars.githubusercontent.com/u/141227996?v=4" width="42;" alt="初五"/></a>
<a href="https://github.com/zxx457" title="Xianxin Zeng"><img src="https://avatars.githubusercontent.com/u/114141362?v=4" width="42;" alt="Xianxin Zeng"/></a>
<a href="https://github.com/zhouhw0306" title="zhouhw0306"><img src="https://avatars.githubusercontent.com/u/82752681?v=4" width="42;" alt="zhouhw0306"/></a>
<a href="https://github.com/hiyms" title="yms"><img src="https://avatars.githubusercontent.com/u/84654050?v=4" width="42;" alt="yms"/></a>
@ -585,7 +594,7 @@ Quick Reference
[gistudy.net](https://reference.gistudy.net/)<!--rehype:target=_blank-->
[deyout.com](https://ref.deyout.com)<!--rehype:target=_blank-->
[const.team](https://reference.const.team)<!--rehype:target=_blank-->
[liamng.top](https://refer.liamng.top/)<!--rehype:target=_blank-->
[liamng.de](https://refer.liamng.de)<!--rehype:target=_blank-->
[zcsk18.cn](https://reference.zcsk18.cn/)<!--rehype:target=_blank-->
[hhxyrk.icu](http://hhxy.hhxyrk.icu/)<!--rehype:target=_blank-->
[vhcffh.com](https://docs.vhcffh.com)<!--rehype:target=_blank-->
@ -604,6 +613,9 @@ Quick Reference
[ecdata.cn](http://ref.ecdata.cn)<!--rehype:target=_blank-->
[cnxiaobai.com](https://quickref.cnxiaobai.com)<!--rehype:target=_blank-->
[web.app](https://reference-f1cf1.web.app)<!--rehype:target=_blank-->
[OneNAS.space](https://ref.onenas.space)<!--rehype:target=_blank-->
[leonus.cn](https://ref.leonus.cn/)<!--rehype:target=_blank-->
[taotaome.com](http://www.taotaome.com/)<!--rehype:target=_blank-->
<!--rehype:class=home-card home-links-->
下面网站暂时飞走了

BIN
appicon/mousio-hint.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

View File

@ -209,7 +209,7 @@ attrib命令可以列出或修改磁盘上文件的属性
`cliconfg` | SQL SERVER 客户端网络实用工具
`cmstp` | 连接管理器配置文件安装程序
`compmgmt.msc` | 计算机管理
`omexp.msc` | 打开系统组件服务
`comexp.msc` | 打开系统组件服务
`control` | 控制面版
### 启动程序(二)

398
docs/wails.md Normal file
View File

@ -0,0 +1,398 @@
Wails V2 备忘清单
===
[![GitHub Repo stars](https://img.shields.io/github/stars/wailsapp/wails?style=flat)](https://github.com/wailsapp/wails) [![GitHub release (latest by date)](https://img.shields.io/github/v/release/wailsapp/wails?style=flat)](https://github.com/wailsapp/wails/releases/latest) [![Go Reference](https://pkg.go.dev/badge/github.com/wailsapp/wails/v2.svg)](https://pkg.go.dev/github.com/wailsapp/wails/v2)
Wails 是一个利用 Golang 和 现代 Web 技术(如 Vue、React构建跨平台桌面应用的开源框架作为 Go 的快并且轻量的 Electron 替代品
下面是 Wails V2 的快速参考列表包含常用命令和示例Wails V3 仍处于开发测试阶段)
<!--rehype:style=padding-top: 12px;-->
## 命令行工具 (CLI)
### 安装与更新
```bash
# 安装 Wails CLI
$ go install github.com/wailsapp/wails/v2/cmd/wails@latest
# 更新到最新稳定版
$ wails update
# 更新到最新预发布版
$ wails update -pre
```
### 项目命令
```bash
# 初始化新项目 (以 Vue 模板为例)
$ wails init -n my-project -t vue
# 进入项目目录并启动实时开发
$ cd my-project
$ wails dev
# 构建生产版本 (以 Windows 平台为例)
$ wails build -platform windows/amd64 -clean -upx
# 检查环境依赖
$ wails doctor
```
### 常用命令参数
| 命令 | 参数 | 描述 |
| ------- | ----------------------- | ---------------------------------------------------------- |
| `init` | `-n` | **项目名称 (必填)** |
| | `-t` | 模板名称 (`vue`, `react`) 或模板 URL |
| | `-ide` | 为 `vscode``goland` 生成 IDE 配置 |
| `dev` | `-browser` | 在浏览器中打开前端界面进行调试 |
| | `-assetdir` | 指定前端资产目录的路径 |
| | `-frontenddevserverurl` | 使用外部前端开发服务器的 URL |
| | `-wailsjsdir` | 指定生成的 Wails JS 模块目录 |
| `build` | `-platform` | 交叉编译目标平台, 如 `darwin/arm64` |
| | `-clean` | 构建前清理 `build/bin` 目录 |
| | `-upx` | 使用 UPX 压缩最终的二进制文件 |
| | `-nsis` | (Windows) 生成 NSIS 安装程序 |
| | `-webview2` | (Windows) WebView2 依赖处理策略 (`download`, `embed`, `browser`) |
| | `-debug` | 保留调试信息 |
| | `-devtools` | 在生产版本中启用开发者工具 |
<!--rehype:className=wrap-text -->
## 项目配置
### 项目结构
```
my-project/
├── build/ # 构建输出目录
├── frontend/ # 前端源文件
│ └── wailsjs/ # Wails 自动生成的模块
├── app.go # 应用核心逻辑
├── main.go # 应用入口
├── go.mod
└── wails.json # 项目配置文件
```
### `wails.json` 详解
| 配置项 | 描述 | 示例 |
| ---------------------- | ---------------------------- | --------------------------- |
| `name` | 项目名称 | `"my-app"` |
| `outputfilename` | 输出的二进制文件名 | `"my-app.exe"` |
| `frontend:install` | 前端依赖安装命令 | `"npm install"` |
| `frontend:build` | 前端构建命令 | `"npm run build"` |
| `frontend:dev:watcher` | 开发模式下运行的前端监视命令 | `"npm run dev"` |
| `wailsjsdir` | 生成 JS 模块的目录 | `"./frontend/wailsjs"` |
| `author.name` | 作者名称,用于打包元数据 | `"Your Name"` |
| `info` | (macOS) 用于 `Info.plist` 的元数据 | `{"CFBundleName": "MyApp"}` |
<!--rehype:className=wrap-text -->
## 核心交互与生命周期
### 方法绑定 (Go <-> JS)
在 Go 中定义公共方法,即可在前端直接调用。
```go
// app.go
type App struct {
ctx context.Context
}
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s!", name)
}
// main.go
app := NewApp()
err := wails.Run(&options.App{
Title: "My App",
Bind: []interface{}{
app, // 暴露 app 实例的所有公共方法
},
})
```
```js
// frontend/main.js
import { Greet } from '../wailsjs/go/main/App';
Greet("World").then(result => console.log(result));
```
### 事件系统 (Events)
用于在 Go 和前端之间异步发送和监听消息。
#### Go 端 (`runtime.Events*`)
```go
import "github.com/wailsapp/wails/v2/pkg/runtime"
// 发送事件到前端
runtime.EventsEmit(app.ctx, "go-event", "data from Go")
// 监听来自前端的事件
runtime.EventsOn(app.ctx, "js-event", func(optionalData ...interface{}) {
// ... 处理数据
})
```
#### JavaScript 端 (`runtime.Events*`)
```js
import { EventsOn, EventsEmit } from '../wailsjs/runtime';
// 发送事件到 Go
EventsEmit("js-event", { "payload": 123 });
// 监听来自 Go 的事件
EventsOn("go-event", (data) => {
console.log("Received data from Go:", data);
});
```
### 应用生命周期钩子
`wails.Run()` 中定义,用于在应用关键节点执行 Go 代码。
```go
// main.go
err := wails.Run(&options.App{
// ...
OnStartup: app.startup,
OnDomReady: app.domReady,
OnShutdown: app.shutdown,
OnBeforeClose: app.beforeClose, // 返回 bool 值决定是否关闭
})
```
| 钩子 | 描述 |
| --------------- | ------------------------- |
| `OnStartup` | 应用启动时,在窗口创建前调用 |
| `OnDomReady` | 前端 DOM 加载完成后调用 |
| `OnShutdown` | 应用关闭前,在窗口销毁后调用 |
| `OnBeforeClose` | 用户关闭窗口时调用,返回 `true` 可阻止关闭 |
<!--rehype:className=wrap-text -->
## 运行时 API (Runtime)
### 窗口 (Window)
| 功能 | Go 示例 (`runtime.*`) | JS 示例 (`runtime.*`) |
| ------------- | ------------------------------------- | --------------------------------------------- |
| **设置标题** | `WindowSetTitle(ctx, "New")` | `WindowSetTitle("New")` |
| **设置尺寸** | `WindowSetSize(ctx, 800, 600)` | `WindowSetSize(800, 600)` |
| **设置最小/最大尺寸** | `WindowSetMinSize(ctx, 400, 300)` | `WindowSetMinSize(400, 300)` |
| **居中** | `WindowCenter(ctx)` | `WindowCenter()` |
| **全屏/取消全屏** | `WindowFullscreen(ctx)` | `WindowFullscreen()` |
| **显示/隐藏** | `WindowShow(ctx)` / `WindowHide(ctx)` | `WindowShow()` / `WindowHide()` |
| **设为置顶** | `WindowSetAlwaysOnTop(ctx, true)` | `WindowSetAlwaysOnTop(true)` |
| **拖动窗口** | (仅 JS) | 在 HTML 元素上设置 `style="--wails-draggable:drag"` |
<!--rehype:className=wrap-text -->
### 对话框 (Dialog)
#### Go 端
```go
dialogOpts := &runtime.OpenDialogOptions{ Title: "Select File" }
filePath, err := runtime.OpenFileDialog(app.ctx, *dialogOpts)
```
| 对话框类型 | Go 方法 (`runtime.*`) |
| -------- | ---------------------------------------------------------- |
| **信息框** | `MessageDialog(ctx, runtime.MessageDialogOptions{...})` |
| **打开文件** | `OpenFileDialog(ctx, runtime.OpenDialogOptions{...})` |
| **保存文件** | `SaveFileDialog(ctx, runtime.SaveDialogOptions{...})` |
| **打开目录** | `OpenDirectoryDialog(ctx, runtime.OpenDialogOptions{...})` |
#### JavaScript 端
```js
import { OpenFileDialog } from '../wailsjs/runtime';
async function selectFile() {
const filePath = await OpenFileDialog({ title: "Select File" });
}
```
### 菜单 (Menu)
```go
// main.go
appMenu := menu.NewMenu()
fileMenu := appMenu.AddSubmenu("File")
fileMenu.AddText("Quit", keys.CmdOrCtrl("q"), func(_ *menu.CallbackData) {
runtime.Quit(app.ctx)
})
err := wails.Run(&options.App{
// ...
Menu: appMenu,
})
```
| 菜单项类型 | 示例 |
| ------- | --------------------------------------------------------- |
| **文本项** | `menu.AddText("Item", accelerator, callback)` |
| **复选框** | `menu.AddCheckbox("Toggle", true, accelerator, callback)` |
| **分隔符** | `menu.AddSeparator()` |
| **子菜单** | `menu.AddSubmenu("Submenu")` |
<!--rehype:className=wrap-text -->
### 其他 Runtime API
| 功能 | Go 示例 (`runtime.*`) | JS 示例 (`runtime.*`) |
| ------- | ------------------------------- | -------------------------- |
| **日志** | `LogInfo(ctx, "Message")` | `LogInfo("Message")` |
| **剪贴板** | `ClipboardSetText(ctx, "text")` | `ClipboardSetText("text")` |
<!--rehype:className=wrap-text -->
## 应用打包与分发
### Windows 打包
- **默认生成**: `.exe` 可执行文件。
```bash
wails build -platform windows/amd64
```
- **生成 NSIS 安装程序**:
- **前提条件**: 需要预先安装 [NSIS (Nullsoft Scriptable Install System)](https://nsis.sourceforge.io/Download)。
- **下载 NSIS**: 从 [NSIS 官方网站](https://nsis.sourceforge.io/Download) 下载最新版本的 NSIS 安装程序。
- **安装 NSIS**: 运行安装程序并按照提示完成安装。**确保**在安装过程中选择将 NSIS 添加到系统的 `PATH` 环境变量中,以便 `makensis` 命令可以在命令行中全局访问。
- **验证安装**:
```bash
makensis -VERSION
```
如果安装正确,您将看到 NSIS 的版本号输出。
- **生成安装程序**:
```bash
wails build -platform windows/amd64 -nsis
```
- **处理 WebView2 依赖**:
- `download`: 提示用户下载 WebView2。
```bash
wails build -platform windows/amd64 -webview2 download
```
- `embed`: 将 WebView2 嵌入到应用中(推荐)。
```bash
wails build -platform windows/amd64 -webview2 embed
```
- `browser`: 在浏览器中打开下载页面。
```bash
wails build -platform windows/amd64 -webview2 browser
```
### macOS 打包
- **默认生成**: `.app` 应用程序包。
```bash
wails build -platform darwin/amd64
```
- **代码签名与公证**:
- 需要通过 Apple 开发者账户进行 **代码签名** 和 **公证** 才能分发。
- **跳过打包成 `.app` 步骤**:
```bash
wails build -platform darwin/amd64 -skippackage
```
### Linux 打包
- **生成可执行文件**:
```bash
wails build -platform linux/amd64
```
- **打包成 `.deb`**:
```bash
wails build -platform linux/amd64 -deb
```
- **打包成 `.rpm`**:
```bash
wails build -platform linux/amd64 -rpm
```
- **AppImage 支持**:
- Wails 不直接内置对 AppImage 的支持,但可以使用外部工具手动创建。
- **使用 `appimagetool`**:
```bash
appimagetool ./your-app-dir
```
- **安装 `appimagetool`**:
- 您可以从 [AppImage 官方网站](https://appimage.org/) 获取 `appimagetool`。
- 下载后,将其添加到您的 `PATH` 中以便全局访问。
---
## 进阶主题与杂项
### 平台特定构建选项
在 `main.go` 的 `wails.Run()` 中为不同平台提供细粒度配置。
```go
err := wails.Run(&options.App{
// ...
Windows: &windows.Options{
WebviewIsTransparent: true, // WebView2 背景透明
WindowIsTranslucent: true, // 窗口背景透明
},
Mac: &mac.Options{
TitleBar: &mac.TitleBar{
TitlebarAppearsTransparent: true, // 透明标题栏
},
About: &mac.AboutInfo{
Title: "My Awesome App",
Message: "© 2025 Me",
},
},
})
```
### 使用 TypeScript
Wails 会自动为 Go 绑定的方法生成 TypeScript 定义。
```bash
# wails.json
"frontend:build": "npm run build"
# package.json
"scripts": {
"build": "tsc && vite build"
}
```
### 调试
- **Go 部分**: 使用 `wails dev -debug` 启动并附加您的 Go 调试器。
- **前端部分**: 在 `wails dev` 模式下,右键点击应用,选择“检查”打开浏览器开发者工具。
## 参考资料
- [Wails 官方文档](https://wails.io/)
- [Wails GitHub 仓库](https://github.com/wailsapp/wails)
- [Wails Discord 社区](https://discord.gg/4K6VHPkG5c)
---