Code

Let's get started with writing a simple hello world window, as in the previous chapters. This code is a little more complicated than in previous examples due to the low-level nature of the toolkit at this time. As well as defining the window, label, and button, we will need to set up a background layer and measure the minimum size for the containing window:

package main

import (
"golang.org/x/exp/shiny/driver"
"golang.org/x/exp/shiny/screen"
"golang.org/x/exp/shiny/widget"
"golang.org/x/exp/shiny/widget/theme"

"log"
)

func main() {
driver.Main(func(s screen.Screen) {
label := widget.NewLabel("Hello World!")
button := newButton("Quit",
func() {
log.Println("To quit close this window")
})

w := widget.NewFlow(widget.AxisVertical, label, button)
sheet := widget.NewSheet(widget.NewUniform(theme.Neutral, w))

w.Measure(theme.Default, 0, 0)
if err := widget.RunWindow(s, sheet, &widget.RunWindowOptions{
NewWindowOptions: screen.NewWindowOptions{
Title: "Hello",
Width: w.MeasuredSize.X,
Height: w.MeasuredSize.Y,
},
}); err != nil {
log.Fatal(err)
}
})
}

In the preceding code, you can see the flow layout (widget.NewFlow()), a background layer (widget.NewSheet()), and the measurement initialization (w.Measure()). With Shiny, widget.Sheet is required underneath any widgets so they can paint correctly. On a simple application, a single sheet should be sufficient, but on a more complex user interface where items move independently (that is, scrolling), additional sheets will probably be required.

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

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