-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
144 lines (128 loc) · 2.91 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// This package provides an example application built using the goey package
// that demonstrates three buttons with different behaviours. The top button
// maintains a running count of how many times it has been clicked. The middle
// button changes the vertical alignment of the buttons. The bottom button
// changes the horizontal alignment.
//
// The different values for MainAxisAlignemnt and CrossAxisAlignment can be
// explored with this example.
//
// 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.
//
// Setting the environment variable GOEY_DIR=1 will change the layout of the
// buttons from vertical to horizontal.
package main
import (
"fmt"
"os"
"strconv"
"bitbucket.org/rj/goey"
"bitbucket.org/rj/goey/base"
"bitbucket.org/rj/goey/loop"
)
var (
mainWindow *goey.Window
clickCount int
alignMain = goey.SpaceAround
alignCross = goey.Stretch
direction bool
)
func main() {
if env := os.Getenv("GOEY_DIR"); env != "" {
value, err := strconv.ParseUint(env, 10, 64)
if err != nil {
fmt.Println("Error: ", err)
} else {
direction = (value % 2) != 0
}
}
err := loop.Run(createWindow)
if err != nil {
fmt.Println("Error: ", err)
}
}
func createWindow() error {
mw, err := goey.NewWindow("Three Buttons", render())
if err != nil {
return err
}
mainWindow = mw
return nil
}
func updateWindow() {
err := mainWindow.SetChild(render())
if err != nil {
fmt.Println("Error: ", err.Error())
}
}
func cycleMainAxisAlign() {
alignMain++
if alignMain > goey.Homogeneous {
alignMain = goey.MainStart
}
updateWindow()
}
func cycleCrossAxisAlign() {
alignCross++
if alignCross > goey.CrossEnd {
alignCross = goey.Stretch
}
updateWindow()
}
func onfocus(ndx int) func() {
return func() {
fmt.Println("focus", ndx)
}
}
func onblur(ndx int) func() {
return func() {
fmt.Println("blur", ndx)
}
}
func render() base.Widget {
text := "Click me!"
if clickCount > 0 {
text = text + " (" + strconv.Itoa(clickCount) + ")"
}
children := []base.Widget{
&goey.Button{Text: text,
Default: true,
OnClick: func() {
clickCount++
updateWindow()
},
OnFocus: onfocus(1),
OnBlur: onblur(1),
},
&goey.Button{Text: "Cycle main axis align",
OnClick: cycleMainAxisAlign,
OnFocus: onfocus(2),
OnBlur: onblur(2),
},
&goey.Button{Text: "Cycle cross axis align",
OnClick: cycleCrossAxisAlign,
OnFocus: onfocus(3),
OnBlur: onblur(3),
},
}
if direction {
return &goey.Padding{
Insets: goey.DefaultInsets(),
Child: &goey.HBox{
AlignMain: alignMain,
AlignCross: alignCross,
Children: children,
},
}
}
return &goey.Padding{
Insets: goey.DefaultInsets(),
Child: &goey.VBox{
AlignMain: alignMain,
AlignCross: alignCross,
Children: children,
},
}
}