Button no longer embeds tomo.ContainerBox

This commit is contained in:
Sasha Koshka 2024-08-24 15:11:57 -04:00
parent 04f44cea86
commit 8de08a9bdc

View File

@ -5,13 +5,15 @@ import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event" import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts" import "git.tebibyte.media/tomo/objects/layouts"
var _ tomo.Object = new(Button)
var buttonLayout = layouts.Row { true } var buttonLayout = layouts.Row { true }
var iconButtonLayout = layouts.Row { true } var iconButtonLayout = layouts.Row { true }
var bothButtonLayout = layouts.Row { false, true } var bothButtonLayout = layouts.Row { false, true }
// Button is a clickable button. // Button is a clickable button.
type Button struct { type Button struct {
tomo.ContainerBox box tomo.ContainerBox
label *Label label *Label
icon *Icon icon *Icon
@ -24,33 +26,45 @@ type Button struct {
// NewButton creates a new button with the specified text. // NewButton creates a new button with the specified text.
func NewButton (text string) *Button { func NewButton (text string) *Button {
box := &Button { button := &Button {
ContainerBox: tomo.NewContainerBox(), box: tomo.NewContainerBox(),
label: NewLabel(text), label: NewLabel(text),
} }
box.SetRole(tomo.R("objects", "Button")) button.box.SetRole(tomo.R("objects", "Button"))
box.label.SetAttr(tomo.AAlign(tomo.AlignMiddle, tomo.AlignMiddle)) button.label.SetAttr(tomo.AAlign(tomo.AlignMiddle, tomo.AlignMiddle))
box.SetAttr(tomo.ALayout(buttonLayout)) button.box.SetAttr(tomo.ALayout(buttonLayout))
box.SetText(text) button.SetText(text)
box.SetInputMask(true) button.box.SetInputMask(true)
box.OnButtonDown(box.handleButtonDown) button.box.OnButtonDown(button.handleButtonDown)
box.OnButtonUp(box.handleButtonUp) button.box.OnButtonUp(button.handleButtonUp)
box.OnKeyDown(box.handleKeyDown) button.box.OnKeyDown(button.handleKeyDown)
box.OnKeyUp(box.handleKeyUp) button.box.OnKeyUp(button.handleKeyUp)
box.SetFocusable(true) button.box.SetFocusable(true)
return box return button
}
// GetBox returns the underlying box.
func (this *Button) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this button has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *Button) SetFocused (focused bool) {
this.box.SetFocused(focused)
} }
// SetText sets the text of the button's label. // SetText sets the text of the button's label.
func (this *Button) SetText (text string) { func (this *Button) SetText (text string) {
this.label.SetText(text) this.label.SetText(text)
if this.labelActive && text == "" { if this.labelActive && text == "" {
this.Remove(this.label) this.box.Remove(this.label)
this.labelActive = false this.labelActive = false
} }
if !this.labelActive && text != "" { if !this.labelActive && text != "" {
this.Add(this.label) this.box.Add(this.label)
this.labelActive = true this.labelActive = true
} }
this.applyLayout() this.applyLayout()
@ -59,7 +73,7 @@ func (this *Button) SetText (text string) {
// SetIcon sets an icon for this button. Setting the icon to IconUnknown will // SetIcon sets an icon for this button. Setting the icon to IconUnknown will
// remove it. // remove it.
func (this *Button) SetIcon (id tomo.Icon) { func (this *Button) SetIcon (id tomo.Icon) {
if this.icon != nil { this.Remove(this.icon) } if this.icon != nil { this.box.Remove(this.icon) }
var icon *Icon; if id != tomo.IconUnknown { var icon *Icon; if id != tomo.IconUnknown {
icon = NewIcon(id, tomo.IconSizeSmall) icon = NewIcon(id, tomo.IconSizeSmall)
@ -67,9 +81,9 @@ func (this *Button) SetIcon (id tomo.Icon) {
this.icon = icon this.icon = icon
if this.icon != nil { if this.icon != nil {
this.Insert(this.icon, this.label) this.box.Insert(this.icon, this.label)
} }
this.SetTag("icon", this.icon != nil) this.box.SetTag("icon", this.icon != nil)
this.applyLayout() this.applyLayout()
} }
@ -80,11 +94,11 @@ func (this *Button) OnClick (callback func ()) event.Cookie {
func (this *Button) applyLayout () { func (this *Button) applyLayout () {
if this.labelActive && this.icon == nil { if this.labelActive && this.icon == nil {
this.SetAttr(tomo.ALayout(buttonLayout)) this.box.SetAttr(tomo.ALayout(buttonLayout))
} else if !this.labelActive && this.icon != nil { } else if !this.labelActive && this.icon != nil {
this.SetAttr(tomo.ALayout(iconButtonLayout)) this.box.SetAttr(tomo.ALayout(iconButtonLayout))
} else { } else {
this.SetAttr(tomo.ALayout(bothButtonLayout)) this.box.SetAttr(tomo.ALayout(bothButtonLayout))
} }
} }
@ -106,7 +120,7 @@ func (this *Button) handleButtonDown (button input.Button) bool {
func (this *Button) handleButtonUp (button input.Button) bool { func (this *Button) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return false } if !isClickingButton(button) { return false }
if this.Window().MousePosition().In(this.Bounds()) { if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.on.click.Broadcast() this.on.click.Broadcast()
} }
return true return true