objects/button.go

75 lines
2.0 KiB
Go

package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/theme"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
var buttonLayout = layouts.NewGrid([]bool { true }, []bool { true })
var iconButtonLayout = layouts.NewGrid([]bool { false, true }, []bool { true })
// Button is a clickable button.
type Button struct {
tomo.ContainerBox
label *Label
icon *Icon
on struct {
click event.FuncBroadcaster
}
}
// NewButton creates a new button with the specified text.
func NewButton (text string) *Button {
box := &Button {
ContainerBox: tomo.NewContainerBox(),
label: NewLabel(text),
}
theme.Apply(box, theme.R("objects", "Button", ""))
box.label.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
box.Add(box.label)
box.SetLayout(buttonLayout)
box.SetPropagateEvents(false)
box.OnMouseUp(box.handleMouseUp)
box.OnKeyUp(box.handleKeyUp)
box.SetFocusable(true)
return box
}
// SetText sets the text of the button's label.
func (this *Button) SetText (text string) {
this.label.SetText(text)
}
// SetIcon sets an icon for this button.
// TODO: use tomo.Icon instead, use small size icons
func (this *Button) SetIcon (icon *Icon) {
if this.icon != nil { this.Delete(this.icon) }
this.icon = icon
if this.icon == nil {
this.SetLayout(buttonLayout)
} else {
this.SetLayout(iconButtonLayout)
this.Insert(this.icon, this.label)
}
}
// OnClick specifies a function to be called when the button is clicked.
func (this *Button) OnClick (callback func ()) event.Cookie {
return this.on.click.Connect(callback)
}
func (this *Button) handleKeyUp (key input.Key, numberPad bool) {
if key != input.KeyEnter && key != input.Key(' ') { return }
this.on.click.Broadcast()
}
func (this *Button) handleMouseUp (button input.Button) {
if button != input.ButtonLeft { return }
if this.MousePosition().In(this.Bounds()) {
this.on.click.Broadcast()
}
}