Button now removes its label if it is empty

This commit is contained in:
Sasha Koshka 2024-05-07 20:15:49 -04:00
parent b4ab60df77
commit f99f60d642

View File

@ -7,13 +7,17 @@ import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts" import "git.tebibyte.media/tomo/objects/layouts"
var buttonLayout = layouts.NewGrid([]bool { true }, []bool { true }) var buttonLayout = layouts.NewGrid([]bool { true }, []bool { true })
var iconButtonLayout = layouts.NewGrid([]bool { false, true }, []bool { true }) var iconButtonLayout = layouts.NewGrid([]bool { false }, []bool { true })
var bothButtonLayout = layouts.NewGrid([]bool { false, true }, []bool { true })
// Button is a clickable button. // Button is a clickable button.
type Button struct { type Button struct {
tomo.ContainerBox tomo.ContainerBox
label *Label label *Label
icon *Icon icon *Icon
labelActive bool
on struct { on struct {
click event.FuncBroadcaster click event.FuncBroadcaster
} }
@ -27,8 +31,8 @@ func NewButton (text string) *Button {
} }
theme.Apply(box, theme.R("objects", "Button", "")) theme.Apply(box, theme.R("objects", "Button", ""))
box.label.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle) box.label.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
box.Add(box.label)
box.SetLayout(buttonLayout) box.SetLayout(buttonLayout)
box.SetText(text)
box.CaptureDND(true) box.CaptureDND(true)
box.CaptureMouse(true) box.CaptureMouse(true)
@ -44,6 +48,15 @@ func NewButton (text string) *Button {
// 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 == "" {
this.Delete(this.label)
this.labelActive = false
}
if !this.labelActive && text != "" {
this.Add(this.label)
this.labelActive = true
}
this.applyLayout()
} }
// 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
@ -56,12 +69,10 @@ func (this *Button) SetIcon (id theme.Icon) {
} }
this.icon = icon this.icon = icon
if this.icon == nil { if this.icon != nil {
this.SetLayout(buttonLayout)
} else {
this.SetLayout(iconButtonLayout)
this.Insert(this.icon, this.label) this.Insert(this.icon, this.label)
} }
this.applyLayout()
} }
// OnClick specifies a function to be called when the button is clicked. // OnClick specifies a function to be called when the button is clicked.
@ -69,6 +80,16 @@ func (this *Button) OnClick (callback func ()) event.Cookie {
return this.on.click.Connect(callback) return this.on.click.Connect(callback)
} }
func (this *Button) applyLayout () {
if this.labelActive && this.icon == nil {
this.SetLayout(buttonLayout)
} else if !this.labelActive && this.icon != nil {
this.SetLayout(iconButtonLayout)
} else {
this.SetLayout(bothButtonLayout)
}
}
func (this *Button) handleKeyUp (key input.Key, numberPad bool) { func (this *Button) handleKeyUp (key input.Key, numberPad bool) {
if key != input.KeyEnter && key != input.Key(' ') { return } if key != input.KeyEnter && key != input.Key(' ') { return }
this.on.click.Broadcast() this.on.click.Broadcast()