User experience

Using a single API does not guarantee consistency across multiple operating systems. The toolkits used may have different layout defaults, with different padding or alignment, for example. The andlabs UI (and the underlying andlabs libui) API is designed to provide an application that's as close to the OS defaults as possible. If you have specific requirements that should be met regarding the user interface appearance (other than the style), such as layout or alignment, you may need to write special code. Using Go's approach to build tags and load different code for different operating systems, you can adapt your code to behave slightly differently on different platforms.

If we look at the previous hello world example, we can update the code to adjust the quit button's layout on different platforms. Here, we will load a right-aligned button for macOS, but leave it as full width for other systems.

The code to create the Quit button is removed from hello.go and replaced with a line that calls into a new layoutQuit() function:

button := layoutQuit()

In a new file, called custom_other.go, we move the previous button definition into a new layoutQuit() function. Additionally, a conditional build comment is added at the top to ensure that this file is not included for macOS (darwin). Note that the text has also changed to Exit, to illustrate how platforms can be adapted:

// +build !darwin

package main

import "github.com/andlabs/ui"

func layoutQuit() ui.Control {
button := ui.NewButton("Exit")
button.OnClicked(func(*ui.Button)
{
ui.Quit()
})

return button
}

That's all pretty straightforward; then we add another file, named custom_darwin.go, where we define the alternative behavior. In this file, we don't need the build definition, as the filename provides that for us. In this implementation, we create a horizontal ui.Box with the Quit button padded to the right using an empty, stretchy ui.Label, as follows:

package main

import "github.com/andlabs/ui"

func layoutQuit() ui.Control {
button := ui.NewButton("Quit")
button.OnClicked(func(*ui.Button) {
ui.Quit()
})

box := ui.NewHorizontalBox()
box.Append(ui.NewLabel(""), true)
box.Append(button, false)

return box
}

Using this approach, it's possible to adapt your user interface to appear slightly differently on specific platforms. This is a useful approach if you need to have different widget layouts on different systems:

  
The updated hello app (left) and layout for macOS (right)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.181.47