forked from andlabs/ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.go
80 lines (67 loc) · 1.51 KB
/
button.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
// 12 february 2014
package ui
import (
"sync"
)
// A Button represents a clickable button with some text.
type Button struct {
// Clicked gets a message when the button is clicked.
// You cannot change it once the Window containing the Button has been created.
// If you do not respond to this signal, nothing will happen.
Clicked chan struct{}
lock sync.Mutex
created bool
sysData *sysData
initText string
}
// NewButton creates a new button with the specified text.
func NewButton(text string) (b *Button) {
return &Button{
sysData: mksysdata(c_button),
initText: text,
Clicked: newEvent(),
}
}
// SetText sets the button's text.
func (b *Button) SetText(text string) {
b.lock.Lock()
defer b.lock.Unlock()
if b.created {
b.sysData.setText(text)
return
}
b.initText = text
}
// Text returns the button's text.
func (b *Button) Text() string {
b.lock.Lock()
defer b.lock.Unlock()
if b.created {
return b.sysData.text()
}
return b.initText
}
func (b *Button) make(window *sysData) error {
b.lock.Lock()
defer b.lock.Unlock()
b.sysData.event = b.Clicked
err := b.sysData.make(window)
if err != nil {
return err
}
b.sysData.setText(b.initText)
b.created = true
return nil
}
func (b *Button) setRect(x int, y int, width int, height int, rr *[]resizerequest) {
*rr = append(*rr, resizerequest{
sysData: b.sysData,
x: x,
y: y,
width: width,
height: height,
})
}
func (b *Button) preferredSize() (width int, height int) {
return b.sysData.preferredSize()
}