blob: b5cac9d618ff5538db10a7517a4aa9afed89bcea [file] [log] [blame]
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build example
// This build tag means that "go install" doesn't
// install this example program. Use "go run main.go" to run it or "go install
// -tags=example" to install it.
// layout is an example of a laying out a widget node tree. Real GUI programs
// won't need to do this explicitly, as the shiny/widget package will
// coordinate with the shiny/screen package to call Measure, Layout and Paint
// as necessary, and will re-layout widgets when windows are re-sized. This
// program merely demonstrates how a widget node tree can be rendered onto a
// statically sized RGBA image, for visual verification of widget code without
// having to bring up and manually inspect an interactive GUI window.
package main
import (
var px = unit.Pixels
func colorPatch(c color.Color, w, h unit.Value) *widget.Sizer {
return widget.NewSizer(w, h, widget.NewUniform(theme.StaticColor(c), nil))
func main() {
t := theme.Default
// Make the widget node tree.
hf := widget.NewFlow(widget.AxisHorizontal,
colorPatch(color.RGBA{0x00, 0x7f, 0x7f, 0xff}, px(0), px(20)),
widget.FlowLayoutData{AlongWeight: 1, ExpandAlong: true},
colorPatch(color.RGBA{0x7f, 0x00, 0x7f, 0xff}, px(0), px(30)),
widget.FlowLayoutData{AlongWeight: 2, ExpandAlong: true},
colorPatch(color.RGBA{0x7f, 0x7f, 0x00, 0xff}, px(0), px(40)),
widget.FlowLayoutData{AlongWeight: 3, ExpandAlong: true},
vf := widget.NewFlow(widget.AxisVertical,
colorPatch(color.RGBA{0xff, 0x00, 0x00, 0xff}, px(80), px(40)),
colorPatch(color.RGBA{0x00, 0xff, 0x00, 0xff}, px(50), px(50)),
colorPatch(color.RGBA{0x00, 0x00, 0xff, 0xff}, px(20), px(60)),
widget.FlowLayoutData{ExpandAcross: true},
"The black rectangle is 1.5 inches x 1 inch when viewed at %v DPI.", t.GetDPI())),
widget.NewPadder(widget.AxisBoth, unit.Pixels(8),
colorPatch(color.Black, unit.Inches(1.5), unit.Inches(1)),
// Make the RGBA image.
const width, height = 640, 480
rgba := image.NewRGBA(image.Rect(0, 0, width, height))
draw.Draw(rgba, rgba.Bounds(), t.GetPalette().Neutral(), image.Point{}, draw.Src)
// Measure, layout and paint.
vf.Measure(t, width, height)
vf.Rect = rgba.Bounds()
Theme: t,
Dst: rgba,
}, image.Point{})
// Encode to PNG.
out, err := os.Create("out.png")
if err != nil {
log.Fatalf("os.Create: %v", err)
defer out.Close()
if err := png.Encode(out, rgba); err != nil {
log.Fatalf("png.Encode: %v", err)
fmt.Println("Wrote out.png OK.")