andlabs-ui/button.go

89 lines
1.7 KiB
Go
Raw Normal View History

// 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.
2014-06-10 13:59:39 -05:00
Clicked chan struct{}
2014-06-10 13:59:39 -05:00
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{
2014-06-10 13:59:39 -05:00
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()
2014-02-14 19:41:36 -06:00
if b.created {
b.sysData.setText(text)
return
2014-02-14 19:41:36 -06:00
}
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)
2014-02-14 19:41:36 -06:00
if err != nil {
return err
}
b.sysData.setText(b.initText)
2014-02-14 19:41:36 -06:00
b.created = true
return nil
}
func (b *Button) allocate(x int, y int, width int, height int, d *sysSizeData) []*allocation {
return []*allocation{&allocation{
2014-06-10 13:59:39 -05:00
x: x,
y: y,
width: width,
height: height,
this: b,
}}
}
func (b *Button) preferredSize(d *sysSizeData) (width int, height int) {
return b.sysData.preferredSize(d)
}
func (b *Button) commitResize(a *allocation, d *sysSizeData) {
b.sysData.preferredSize(a, d)
}
func (b *Button) getAuxResizeInfo(d *sysSizeData) {
b.sysData.getAuxResizeInfo(d)
}