/
main.go
122 lines (104 loc) · 2.51 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// This package provides an example application built using the goey package
// that demonstrates using the Image widget. Clicking the button will cycle
// through images that are a uniform colour, as well as an image of the Go
// mascot.
//
// The management of scrollbars can be tested by using the environment variable
// GOEY_SCROLL. Allowed values are 0 through 3, which enable no scrollbars,
// the vertical scrollbar, the horizontal scrollbar, or both scrollbars.
package main
import (
"fmt"
"image"
"image/color"
"image/draw"
_ "image/png"
"os"
"bitbucket.org/rj/goey"
"bitbucket.org/rj/goey/base"
"bitbucket.org/rj/goey/loop"
)
var (
mainWindow *goey.Window
clickCount int
colors = [3]color.RGBA{
{0xff, 0, 0, 0xff},
{0, 0xff, 0, 0xff},
{0, 0, 0xff, 0xff},
}
colorNames = [3]string{
"Red", "Green", "Blue",
}
gopher image.Image
)
func loadImage(filename string) (image.Image, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
return img, err
}
func selectImage(index int) (image.Image, string) {
if clickCount%4 == 3 {
return gopher, "Image of the Go gopher."
}
img := image.NewRGBA(image.Rect(0, 0, 100, 100))
draw.Draw(img, img.Bounds(), image.NewUniform(colors[index%4]), image.Point{}, draw.Src)
return img, colorNames[index%4]
}
func main() {
err := error(nil)
gopher, err = loadImage("gopher.png")
if err != nil {
fmt.Println("Error: ", err)
return
}
err = loop.Run(createWindow)
if err != nil {
fmt.Println("Error: ", err)
}
}
func createWindow() error {
// Add the controls
mw, err := goey.NewWindow("Colour", render())
if err != nil {
return err
}
mainWindow = mw
// Set the icon
img, _ := selectImage(clickCount)
mw.SetIcon(img)
return nil
}
func update() {
err := mainWindow.SetChild(render())
if err != nil {
fmt.Println("Error: ", err.Error())
}
img, _ := selectImage(clickCount)
mainWindow.SetIcon(img)
}
func render() base.Widget {
img, description := selectImage(clickCount)
return &goey.Padding{
Insets: goey.DefaultInsets(),
Child: &goey.VBox{
AlignMain: goey.MainCenter,
AlignCross: goey.CrossCenter,
Children: []base.Widget{
&goey.Button{Text: "Change the colour", OnClick: func() {
clickCount++
update()
}},
&goey.Img{
Image: img,
Width: (1 * goey.DIP).Scale(img.Bounds().Dx(), 1),
Height: (1 * goey.DIP).Scale(img.Bounds().Dy(), 1),
},
&goey.P{Text: description, Align: goey.JustifyCenter},
},
},
}
}