Debian Codegraph @

shiny/example/layout/main.go

raw symbols SCIP
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build example
6 // +build example
7
8 //
9 // This build tag means that "go install golang.org/x/exp/shiny/..." doesn't
10 // install this example program. Use "go run main.go" to run it or "go install
11 // -tags=example" to install it.
12
13 // layout is an example of a laying out a widget node tree. Real GUI programs
14 // won't need to do this explicitly, as the shiny/widget package will
15 // coordinate with the shiny/screen package to call Measure, Layout and Paint
16 // as necessary, and will re-layout widgets when windows are re-sized. This
17 // program merely demonstrates how a widget node tree can be rendered onto a
18 // statically sized RGBA image, for visual verification of widget code without
19 // having to bring up and manually inspect an interactive GUI window.
20 package main
21
22 import (
23 "fmt"
24 "image"
25 "image/color"
26 "image/draw"
27 "image/png"
28 "log"
29 "os"
30
31 "golang.org/x/exp/shiny/unit"
32 "golang.org/x/exp/shiny/widget"
33 "golang.org/x/exp/shiny/widget/node"
34 "golang.org/x/exp/shiny/widget/theme"
35 )
36
37 var px = unit.Pixels
38
39 func colorPatch(c color.Color, w, h unit.Value) *widget.Sizer {
40 return widget.NewSizer(w, h, widget.NewUniform(theme.StaticColor(c), nil))
41 }
42
43 func main() {
44 t := theme.Default
45
46 // Make the widget node tree.
47 hf := widget.NewFlow(widget.AxisHorizontal,
48 widget.NewLabel("Cyan:"),
49 widget.WithLayoutData(
50 colorPatch(color.RGBA{0x00, 0x7f, 0x7f, 0xff}, px(0), px(20)),
51 widget.FlowLayoutData{AlongWeight: 1, ExpandAlong: true},
52 ),
53 widget.NewLabel("Magenta:"),
54 widget.WithLayoutData(
55 colorPatch(color.RGBA{0x7f, 0x00, 0x7f, 0xff}, px(0), px(30)),
56 widget.FlowLayoutData{AlongWeight: 2, ExpandAlong: true},
57 ),
58 widget.NewLabel("Yellow:"),
59 widget.WithLayoutData(
60 colorPatch(color.RGBA{0x7f, 0x7f, 0x00, 0xff}, px(0), px(40)),
61 widget.FlowLayoutData{AlongWeight: 3, ExpandAlong: true},
62 ),
63 )
64
65 vf := widget.NewFlow(widget.AxisVertical,
66 colorPatch(color.RGBA{0xff, 0x00, 0x00, 0xff}, px(80), px(40)),
67 colorPatch(color.RGBA{0x00, 0xff, 0x00, 0xff}, px(50), px(50)),
68 colorPatch(color.RGBA{0x00, 0x00, 0xff, 0xff}, px(20), px(60)),
69 widget.WithLayoutData(
70 hf,
71 widget.FlowLayoutData{ExpandAcross: true},
72 ),
73 widget.NewLabel(fmt.Sprintf(
74 "The black rectangle is 1.5 inches x 1 inch when viewed at %v DPI.", t.GetDPI())),
75 widget.NewPadder(widget.AxisBoth, unit.Pixels(8),
76 colorPatch(color.Black, unit.Inches(1.5), unit.Inches(1)),
77 ),
78 )
79
80 // Make the RGBA image.
81 const width, height = 640, 480
82 rgba := image.NewRGBA(image.Rect(0, 0, width, height))
83 draw.Draw(rgba, rgba.Bounds(), t.GetPalette().Neutral(), image.Point{}, draw.Src)
84
85 // Measure, layout and paint.
86 vf.Measure(t, width, height)
87 vf.Rect = rgba.Bounds()
88 vf.Layout(t)
89 vf.PaintBase(&node.PaintBaseContext{
90 Theme: t,
91 Dst: rgba,
92 }, image.Point{})
93
94 // Encode to PNG.
95 out, err := os.Create("out.png")
96 if err != nil {
97 log.Fatalf("os.Create: %v", err)
98 }
99 defer out.Close()
100 if err := png.Encode(out, rgba); err != nil {
101 log.Fatalf("png.Encode: %v", err)
102 }
103 fmt.Println("Wrote out.png OK.")
104 }