2022-12-13 19:11:37 +08:00
|
|
|
|
# GioUI 概览
|
|
|
|
|
|
|
|
|
|
GioUI 是一款支持多平台的 Go GUI 库,支持 Linux、macOS、Windows、Android、iOS、FreeBSD、OpenBSD 和 WebAssembly。
|
|
|
|
|
|
|
|
|
|
## 1. 安装
|
|
|
|
|
|
|
|
|
|
GioUI 的安装与其他 Go 包一样,只要在源码中引用,然后通过 go 命令即可自动下载:
|
|
|
|
|
|
|
|
|
|
源码中引用“gioui”后:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
go mod tidy
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
即可自动安装。
|
|
|
|
|
|
|
|
|
|
另外也可以到[官方网站](https://gioui.org/)下载[源码](https://git.sr.ht/~eliasnaur/gio)或获取帮助文档:
|
|
|
|
|
|
|
|
|
|
编译 Android 程序时需要用到 gogio 工具,需要通过如下命令安装:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
go install gioui.org/cmd/gogio@latest
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 1.1. Android 开发环境配置
|
|
|
|
|
|
|
|
|
|
如果需要开发基于 GioUI 的 Android 程序,需要先搭建好 Android 开发环境:
|
|
|
|
|
|
|
|
|
|
1. 安装 Android Studio 后通过 IDE 安装 Android SDK(>31) ,配置 ANDROID_SDK_ROOT。ANDROID_SDK_ROOT 可在 Android Studio->More Actions->SDK Manager->Android SDK Location 中获取到;
|
2023-02-10 21:37:26 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
export ANDROID_SDK_ROOT=/home/rick/Android/Sdk
|
|
|
|
|
```
|
|
|
|
|
|
2022-12-13 19:11:37 +08:00
|
|
|
|
2. 进入 Android Studio->More Actions->SDK Manager->SDK Tools 勾选 NDK(我使用的是 25.1.893xxxx 版本);
|
2023-02-10 21:37:26 +08:00
|
|
|
|
3. 当前使用的 GioUI commit id 为 dee53b36,该版本仅支持 OpenJDK 1.8,与之对应的版本是 OpenJDK 8,因此需要下载 [openjdk-8u42-b03-windows-i586-14_jul_2022.zip](https://download.java.net/openjdk/jdk8u42/ri/openjdk-8u42-b03-windows-i586-14_jul_2022.zip) 并进行安装([官网地址](https://jdk.java.net/java-se-ri/8-MR4))。配置 JAVA_HOME 并将 %JAVA_HOME%\bin 添加到系统 PATH 中。
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
sudo pacman -S jdk8-openjdk jre8-openjdk
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
4. 配置好后通过命令行输入:
|
2022-12-13 19:11:37 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
java -version
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
进行版本验证。
|
|
|
|
|
|
|
|
|
|
## 2. PC 示例
|
|
|
|
|
|
|
|
|
|
创建模块:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
go mod init gio.test
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
main 包代码:
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"image/color"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
"gioui.org/app"
|
|
|
|
|
"gioui.org/font/gofont"
|
|
|
|
|
"gioui.org/io/system"
|
|
|
|
|
"gioui.org/layout"
|
|
|
|
|
"gioui.org/op"
|
|
|
|
|
"gioui.org/text"
|
|
|
|
|
"gioui.org/widget/material"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
go func() {
|
|
|
|
|
w := app.NewWindow()
|
|
|
|
|
err := run(w)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
}()
|
|
|
|
|
app.Main()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func run(w *app.Window) error {
|
|
|
|
|
th := material.NewTheme(gofont.Collection())
|
|
|
|
|
var ops op.Ops
|
|
|
|
|
for {
|
|
|
|
|
e := <-w.Events()
|
|
|
|
|
switch e := e.(type) {
|
|
|
|
|
case system.DestroyEvent:
|
|
|
|
|
return e.Err
|
|
|
|
|
case system.FrameEvent:
|
|
|
|
|
gtx := layout.NewContext(&ops, e)
|
|
|
|
|
|
|
|
|
|
title := material.H1(th, "Hello, Gio")
|
|
|
|
|
maroon := color.NRGBA{R: 127, G: 0, B: 0, A: 255}
|
|
|
|
|
title.Color = maroon
|
|
|
|
|
title.Alignment = text.Middle
|
|
|
|
|
title.Layout(gtx)
|
|
|
|
|
|
|
|
|
|
e.Frame(gtx.Ops)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
编译命令:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
go mod tidy
|
|
|
|
|
go run .
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 2.1. PC 示例说明
|
|
|
|
|
|
|
|
|
|
调用 app.NewWindow() 将创建新的窗体。调用 app.Main() 将阻塞至窗体退出。
|
|
|
|
|
|
|
|
|
|
通过 w.Events() 可以接收窗体事件消息。system.DestroyEvent 为窗体析构消息;system.FrameEvent 为窗体绘制请求消息。
|
|
|
|
|
|
|
|
|
|
需要绘制的控件需要像压栈那样装入 Ops 的 Context,最后调用 e.Frame(gtx.Ops) 绘制全部控件。
|
|
|
|
|
|
|
|
|
|
## 3. Android 示例
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
git clone https://git.sr.ht/~eliasnaur/gio-example
|
|
|
|
|
cd gio-example
|
|
|
|
|
gogio -target android <module name>[/sub-directory] [-token <github token>]
|
|
|
|
|
# 如
|
|
|
|
|
gogio -target android gioui.org/example/kitchen
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
将生成 APK 文件,可通过手机安装或使用如下命令:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
adb install kitchen.apk
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 4. 外部参考资料
|
|
|
|
|
|
|
|
|
|
1. [GUI With Gio](https://jonegil.github.io/gui-with-gio/teleprompter/02_user_input.html)
|
2023-01-15 15:01:47 +08:00
|
|
|
|
2. [gio中文乱码解决方式](https://blog.csdn.net/weixin_45886778/article/details/125363997)
|