📜 ⬆️ ⬇️

Native UI library for Go

One of the common questions about Go is whether there is a good cross-platform UI library on Go. As a rule, questioners were sent either to go-qml , or to andilabs / ui (bindings to C-implementations of the native UI for each platform), but on the whole there was no worthy project for the native Go UI. The other day, a couple of Google developers opened the gxui project for the open-source world, which aims to fill the niche of native UI libraries for Go.

The project is still raw, but it looks good and promising.

Let's take a closer look.
')


Project address: github.com/google/gxui , and here's a great README :
This is an experimental code, and will undergo significant changes. Play with it freely, try it, but don't be discouraged if the API is substantially redesigned.

So far, the code is undocumented, and it is definitely not the “idiomatic” Go. In the coming months, it will be very refactored.

This is not an official Google product, it's just a code that happened to belong to Google.


I agree, readme is not inspiring, but let's see what is at the moment. He quickly studied and tried the examples in the samples / directory and tried to write a banal modal window.

package main import ( "fmt" "github.com/google/gxui" "github.com/google/gxui/drivers/gl" "github.com/google/gxui/themes/dark" ) func appMain(driver gxui.Driver) { theme := dark.CreateTheme(driver) label := theme.CreateLabel() label.SetText("Are you sure?") yesButton := theme.CreateButton() yesButton.SetText("Yes") yesButton.OnClick(func(ev gxui.MouseEvent) { fmt.Println("Yes") }) noButton := theme.CreateButton() noButton.SetText("No") noButton.OnClick(func(ev gxui.MouseEvent) { fmt.Println("No") }) layout := theme.CreateLinearLayout() layout.AddChild(label) btnLayout := theme.CreateLinearLayout() btnLayout.AddChild(yesButton) btnLayout.AddChild(noButton) btnLayout.SetOrientation(gxui.Horizontal) layout.AddChild(btnLayout) layout.SetHorizontalAlignment(gxui.AlignCenter) layout.SetVerticalAlignment(gxui.AlignMiddle) window := theme.CreateWindow(120, 60, "Message Box") window.AddChild(layout) window.OnClose(driver.Terminate) gxui.EventLoop(driver) } func main() { gl.StartDriver("", appMain) } 


It looks like GTK, and Qt, right? In my opinion, this is good - anyone who has experience with GTK / Qt - it will be easier to understand the logic of working with the library, and when it comes to autogenerators of the code for the UI, it will also be possible to walk along the trodden path (it may even be easy adapt Glade or Qt Designer for this library?).

To interact with the graphics subsystem, only OpenGL is used so far, but the library's design allows you to add any other output driver in the future, even AsciiArt, even DirectX.

The result looks like this:


I have not found yet how to make the correct alignment, resize widgets or relayout when resizing a window (although the functions for this are, it seems, as it is). With fonts, too, at the very minimum level.

Here are some more examples from the source:

Animated progress bar.

Polygons

Panels that can be resized and switch tabs:

Lists


In general, of course, the demand for desktop UI is less and less every year and the web solutions have taken a strong position here. And although the need for desktop UI is still there, but this trend can kill the enthusiasm and motivation of the authors of the library. At the same time, it is important to understand that the complexity in general of the very task of creating a full-featured UI library, also cross-platform, is enormous and needs enormous resources in order to do this qualitatively and correctly. Given all this, I personally would not have invested high expectations in this project, but it will be interesting to play around, especially at the moment when it is written in the README that the API is stable.
However, on the other hand, the project has already collected 1345 stars on github, and judging by the reaction in twitter / reddit, many people are waiting for such a library.

So, who are interested, try, contribute, follow the project.

Source: https://habr.com/ru/post/253519/


All Articles