This commit is contained in:
2023-08-09 11:35:24 -04:00
parent de704929c8
commit 8d7d9882ba
3 changed files with 110 additions and 10 deletions

View File

@@ -14,11 +14,14 @@ type Button struct {
}
// NewButton creates a new button with the specified text.
func NewButton (text string) Button {
box := Button { TextBox: tomo.NewTextBox() }
func NewButton (text string) *Button {
box := &Button { TextBox: tomo.NewTextBox() }
theme.Apply(box, theme.R("objects", "Button"))
box.SetText(text)
box.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
box.OnMouseUp(box.handleMouseUp)
box.OnKeyUp(box.handleKeyUp)
box.SetFocusable(true)
return box
}
@@ -27,6 +30,11 @@ 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()) {