Debian Codegraph @

shiny/example/widgetgallery/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 // Widgetgallery exhibits the shiny/widget package's widget set.
14 package main
15
16 import (
17 "image"
18 "image/color"
19 "image/draw"
20 "log"
21
22 "golang.org/x/exp/shiny/driver"
23 "golang.org/x/exp/shiny/gesture"
24 "golang.org/x/exp/shiny/screen"
25 "golang.org/x/exp/shiny/widget"
26 "golang.org/x/exp/shiny/widget/node"
27 )
28
29 var uniforms = [...]*image.Uniform{
30 image.NewUniform(color.RGBA{0xbf, 0x00, 0x00, 0xff}),
31 image.NewUniform(color.RGBA{0x9f, 0x9f, 0x00, 0xff}),
32 image.NewUniform(color.RGBA{0x00, 0xbf, 0x00, 0xff}),
33 image.NewUniform(color.RGBA{0x00, 0x9f, 0x9f, 0xff}),
34 image.NewUniform(color.RGBA{0x00, 0x00, 0xbf, 0xff}),
35 image.NewUniform(color.RGBA{0x9f, 0x00, 0x9f, 0xff}),
36 }
37
38 // custom is a custom widget.
39 type custom struct {
40 node.LeafEmbed
41 index int
42 }
43
44 func newCustom() *custom {
45 w := &custom{}
46 w.Wrapper = w
47 return w
48 }
49
50 func (w *custom) OnInputEvent(e interface{}, origin image.Point) node.EventHandled {
51 switch e := e.(type) {
52 case gesture.Event:
53 if e.Type != gesture.TypeTap {
54 break
55 }
56 w.index++
57 if w.index == len(uniforms) {
58 w.index = 0
59 }
60 w.Mark(node.MarkNeedsPaintBase)
61 }
62 return node.Handled
63 }
64
65 func (w *custom) PaintBase(ctx *node.PaintBaseContext, origin image.Point) error {
66 w.Marks.UnmarkNeedsPaintBase()
67 draw.Draw(ctx.Dst, w.Rect.Add(origin), uniforms[w.index], image.Point{}, draw.Src)
68 return nil
69 }
70
71 func main() {
72 log.SetFlags(0)
73 driver.Main(func(s screen.Screen) {
74 // TODO: create a bunch of standard widgets: buttons, labels, etc.
75 w := widget.NewSheet(newCustom())
76 if err := widget.RunWindow(s, w, &widget.RunWindowOptions{
77 NewWindowOptions: screen.NewWindowOptions{
78 Title: "WidgetGallery Shiny Example",
79 },
80 }); err != nil {
81 log.Fatal(err)
82 }
83 })
84 }