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, the questioners were sent either to go-qml or to andilabs / ui (bindings to C-implementations of the native UI for each platform), but in general there was no worthy project on the native Go UI yet. The other day, a couple of developers from Google 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 crude, but it looks good and promising.

    Let's take a closer look.



    Project address: github.com/google/gxui , and here's a wonderful README in its own way :
    This is 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 an “idiomatic” Go. In the coming months, he will be strongly refactored.

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


    I agree, the readme is not inspiring, but let's see what is at the moment. Having fluently studied and tested the examples in the samples / directory, I 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)
    }
    


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

    So far, only OpenGL is used to interact with the graphics subsystem, but the design of the library allows you to add any other output driver in the future, at least AsciiArt, at least DirectX.

    The result looks like this:


    I have not yet found how to correctly align, resize widgets or relayout when resizing a window (although the functions for this seem to be there). With fonts, too, everything is at the very minimum level.

    Here are some more examples from source:

    Animated progress bar .

    Polygons

    Panels that you can resize and switch tabs:

    Lists


    In general, of course, the demand for desktop UIs is less and less every year and web solutions have taken a strong position here. And although there is still a need for desktop UIs, 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 of the task of creating a full-featured UI library in general, also cross-platform, is huge, and needs enormous resources to do this efficiently and correctly. Considering all this, I personally would not have invested much in this project, but it will be interesting to play, especially at the moment when README says 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 are looking forward to such a library.

    So, to whom it is interesting, try, contribute, follow the project.

    Also popular now: