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.SetAlign(tomo.AlignStart, tomo.AlignMiddle) box.Add(box.swatch) box.Add(box.label) box.SetLayout(layouts.Row { false, true }) box.OnMouseUp(box.handleMouseUp) box.label.OnMouseUp(box.handleMouseUp) return box } // SetValue sets the color of the swatch. func (this *LabelSwatch) SetValue (value color.Color) { this.swatch.SetValue(value) } // Value returns the color of the swatch. func (this *LabelSwatch) Value () color.Color { return this.swatch.Value() } // RGBA satisfies the color.Color interface func (this *LabelSwatch) RGBA () (r, g, b, a uint32) { return this.swatch.RGBA() } // 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) } // OnEnter specifies a function to be called when the user selects "OK" in the // color picker. func (this *LabelSwatch) OnEnter (callback func ()) event.Cookie { return this.swatch.OnEnter(callback) } func (this *LabelSwatch) handleMouseUp (button input.Button) { if button != input.ButtonLeft { return } if this.MousePosition().In(this.Bounds()) { this.swatch.SetFocused(true) this.swatch.Choose() } }