介绍
在开发低代码工具时,我选择了 github.com/g3n/engine/gui
作为 Golang 的 UI 开发库。这一选择的主要原因是该库的源码设计具有良好的扩展性。此外,我在游戏开发过程中已经熟悉了 g3n/engine/gui
,因此使用它开发工具也更加顺手。
基于 g3n/engine/gui
,我已经开发出多个工具,并在其基础上封装了通用 UI 组件库,以加速其他工具的开发。这些工具可以轻松地跨平台运行,同时 UI 组件的集成也非常便捷。
注意:
github.com/g3n/engine/gui
不支持中文,需要自行创建必要字符的 TTF 字体文件。
跨平台特性
g3n/engine/gui
基于 OpenGL,能够在 Windows、macOS 和 Linux 上运行,真正实现了跨平台 GUI 开发。其模块化设计使得开发者可以灵活扩展 UI 组件,而不受平台限制。
面板布局示例
使用 g3n/engine/gui
仅需几行代码,就能快速构建 UI 界面。例如,以下代码展示了如何创建一个应用窗口,并添加 MySQL 配置和 App 配置面板:
app := function.InitApp("代码生成工具", windowWidth, windowHeight)var (windowWidth, windowHeight float32 = 600, 400panelWidth, panelHeight float32 = 280, 380padding float32 = 10
)// 创建 MySQL 配置面板
mysqlPanel := function.AddPanel(app.Scene, "MySQL 配置", padding, padding, panelWidth, panelHeight, "")
mysqlTitle := function.AddLabel(mysqlPanel, "MySQL 配置", 10, 10)
mysqlPanel.Add(mysqlTitle)// 创建 App 配置面板
appPanel := function.AddPanel(app.Scene, "App 配置", padding+panelWidth+padding, padding, panelWidth, panelHeight, "")
appTitle := function.AddLabel(appPanel, "App 配置", 10, 10)
appPanel.Add(appTitle)
按钮点击示例
以下代码展示了如何创建一个按钮,并在点击后进行 MySQL 参数校验和代码生成:
exportButton := function.AddButton(mysqlPanel, "生成代码", 10, 50+spacing*float32(len(mysqlFields)))
mysqlPanel.Add(exportButton)exportButton.Subscribe(gui.OnClick, func(name string, ev interface{}) {if !function.ValidateInputs(mysqlInputs) {function.AddMessageWindow(app.Scene, "参数校验", "有参数未填写!")return}dbConn := function.ReadMYSQL(mysqlInputs["name"].Text(), mysqlInputs["pass"].Text(), mysqlInputs["host"].Text(), mysqlInputs["port"].Text(), mysqlInputs["db"].Text())tableList := function.FilterTables(function.ReadMYSQLAllTables(&dbConn, mysqlInputs["db"].Text()))projectConfig := function.BuildProjectConfig(mysqlInputs, projectInputs)generatingServiceApp(tableList, projectConfig)function.AddMessageWindow(app.Scene, "消息", "创建成功!")
})function.RunApp(app.Application, app.Scene, app.Camera)
通用 UI 组件封装
为了提高开发效率,我在 g3n/engine/gui
的基础上封装了一系列 UI 组件,如 AddLabel
、AddPanel
、AddButton
,以减少重复代码。例如:
func AddLabel(scene *core.Node, text string, x, y float32) *gui.Label {label := gui.NewLabel(text)label.SetPosition(x, y)label.SetColor(math32.NewColor("black"))return label
}func AddPanel(scene *core.Node, title string, x, y, width, height float32, imageFile string) *gui.Panel {panel := gui.NewPanel(width, height)panel.SetPosition(x, y)panel.SetMargins(0, 0, 0, 0)panel.SetBorders(2, 2, 2, 2)panel.SetBordersColor(math32.NewColor("black"))panel.SetPaddings(1, 1, 1, 1)scene.Add(panel)if imageFile != "" {img, err := gui.NewImage(imageFile)if err != nil {panic(err)}img.SetPosition(0, 0)img.SetMargins(0, 0, 0, 0)img.SetBorders(1, 1, 1, 1)img.SetBordersColor(math32.NewColor("red"))img.SetPaddings(1, 1, 1, 1)img.SetColor(math32.NewColor("white"))img.SetContentAspectWidth(width - 5)img.SetContentAspectHeight(height - 5)panel.Add(img)}return panel
}
一些工具截图:
结论
g3n/engine/gui
作为一个 Golang 的 UI 库,提供了跨平台支持,并且具有良好的扩展性。对于低代码工具的开发,它是一个非常合适的选择。通过构建自己的工具类,可以进一步提高开发效率,使工具的创建更加快速和易用。
未来,我计划继续在 g3n/engine/gui
上扩展更多功能,使低代码开发工具更加完善,并支持更多的业务场景。