LabelSwatch no longer embeds tomo.ContainerBox

This commit is contained in:
Sasha Koshka 2024-08-24 20:02:14 -04:00
parent 6ee2c5669e
commit f0c334c278

View File

@ -6,33 +6,47 @@ 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(LabelSwatch)
// LabelSwatch is a swatch with a label. // LabelSwatch is a swatch with a label.
type LabelSwatch struct { type LabelSwatch struct {
tomo.ContainerBox box tomo.ContainerBox
swatch *Swatch swatch *Swatch
label *Label label *Label
} }
// NewLabelSwatch creates a new labeled swatch with the specified color and // NewLabelSwatch creates a new labeled swatch with the specified color and
// label text. // label text.
func NewLabelSwatch (value color.Color, text string) *LabelSwatch { func NewLabelSwatch (value color.Color, text string) *LabelSwatch {
box := &LabelSwatch { labelSwatch := &LabelSwatch {
ContainerBox: tomo.NewContainerBox(), box: tomo.NewContainerBox(),
swatch: NewSwatch(value), swatch: NewSwatch(value),
label: NewLabel(text), label: NewLabel(text),
} }
box.SetRole(tomo.R("objects", "LabelSwatch")) labelSwatch.box.SetRole(tomo.R("objects", "LabelSwatch"))
box.swatch.label = text labelSwatch.swatch.label = text
box.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle) labelSwatch.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
box.label.GetBox().(tomo.TextBox).SetSelectable(false) labelSwatch.label.GetBox().(tomo.TextBox).SetSelectable(false)
box.label.GetBox().(tomo.TextBox).SetFocusable(false) labelSwatch.label.GetBox().(tomo.TextBox).SetFocusable(false)
box.Add(box.swatch) labelSwatch.box.Add(labelSwatch.swatch)
box.Add(box.label) labelSwatch.box.Add(labelSwatch.label)
box.SetAttr(tomo.ALayout(layouts.Row { false, true })) labelSwatch.box.SetAttr(tomo.ALayout(layouts.Row { false, true }))
box.OnButtonDown(box.handleButtonDown) labelSwatch.box.OnButtonDown(labelSwatch.handleButtonDown)
box.OnButtonUp(box.handleButtonUp) labelSwatch.box.OnButtonUp(labelSwatch.handleButtonUp)
return box return labelSwatch
}
// GetBox returns the underlying box.
func (this *LabelSwatch) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this swatch has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *LabelSwatch) SetFocused (focused bool) {
this.swatch.SetFocused(focused)
} }
// Value returns the color of the swatch. // Value returns the color of the swatch.
@ -69,7 +83,7 @@ func (this *LabelSwatch) handleButtonDown (button input.Button) bool {
func (this *LabelSwatch) handleButtonUp (button input.Button) bool { func (this *LabelSwatch) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return true } if !isClickingButton(button) { return true }
if this.Window().MousePosition().In(this.Bounds()) { if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.swatch.SetFocused(true) this.swatch.SetFocused(true)
this.swatch.Choose() this.swatch.Choose()
} }