objects/labelswatch.go

97 lines
2.8 KiB
Go

package objects
import "image/color"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
var _ tomo.Object = new(LabelSwatch)
// LabelSwatch is a swatch with a label.
type LabelSwatch struct {
box tomo.ContainerBox
swatch *Swatch
label *Label
}
// NewLabelSwatch creates a new labeled swatch with the specified color and
// label text.
func NewLabelSwatch (value color.Color, text string) *LabelSwatch {
labelSwatch := &LabelSwatch {
box: tomo.NewContainerBox(),
swatch: NewSwatch(value),
label: NewLabel(text),
}
labelSwatch.box.SetRole(tomo.R("objects", "LabelSwatch"))
labelSwatch.swatch.label = text
labelSwatch.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
labelSwatch.label.GetBox().(tomo.TextBox).SetSelectable(false)
labelSwatch.label.GetBox().(tomo.TextBox).SetFocusable(false)
labelSwatch.box.Add(labelSwatch.swatch)
labelSwatch.box.Add(labelSwatch.label)
labelSwatch.box.SetAttr(tomo.ALayout(layouts.Row { false, true }))
labelSwatch.box.OnButtonDown(labelSwatch.handleButtonDown)
labelSwatch.box.OnButtonUp(labelSwatch.handleButtonUp)
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)
}
// SetText sets the text label of the swatch.
func (this *LabelSwatch) SetText (text string) {
this.label.SetText(text)
}
// Value returns the color of the swatch.
func (this *LabelSwatch) Value () color.Color {
return this.swatch.Value()
}
// SetValue sets the color of the swatch.
func (this *LabelSwatch) SetValue (value color.Color) {
this.swatch.SetValue(value)
}
// OnValueChange specifies a function to be called when the swatch's color
// is changed by the user.
func (this *LabelSwatch) OnValueChange (callback func ()) event.Cookie {
return this.swatch.OnValueChange(callback)
}
// RGBA satisfies the color.Color interface
func (this *LabelSwatch) RGBA () (r, g, b, a uint32) {
return this.swatch.RGBA()
}
// OnConfirm specifies a function to be called when the user selects "OK" in the
// color picker.
func (this *LabelSwatch) OnConfirm (callback func ()) event.Cookie {
return this.swatch.OnConfirm(callback)
}
func (this *LabelSwatch) handleButtonDown (button input.Button) bool {
if !isClickingButton(button) { return true }
return true
}
func (this *LabelSwatch) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return true }
if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.swatch.SetFocused(true)
this.swatch.Choose()
}
return true
}