objects/labelswatch.go

75 lines
2.0 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"
// LabelSwatch is a swatch with a label.
type LabelSwatch struct {
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 {
box := &LabelSwatch {
ContainerBox: tomo.NewContainerBox(),
swatch: NewSwatch(value),
label: NewLabel(text),
}
box.SetRole(tomo.R("objects", "LabelSwatch"))
box.swatch.label = text
box.label.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
box.Add(box.swatch)
box.Add(box.label)
box.SetLayout(layouts.Row { false, true })
box.OnButtonDown(box.handleButtonDown)
box.OnButtonUp(box.handleButtonUp)
return box
}
// 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 (catch func (), button input.Button) {
catch()
}
func (this *LabelSwatch) handleButtonUp (catch func (), button input.Button) {
catch()
if button != input.ButtonLeft { return }
if this.Window().MousePosition().In(this.Bounds()) {
this.swatch.SetFocused(true)
this.swatch.Choose()
}
}