objects/labelswatch.go

78 lines
2.2 KiB
Go
Raw Normal View History

2024-06-22 13:44:37 -06:00
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),
}
2024-07-21 09:48:28 -06:00
box.SetRole(tomo.R("objects", "LabelSwatch"))
box.swatch.label = text
2024-07-21 09:48:28 -06:00
box.label.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
box.label.SetSelectable(false)
box.label.SetFocusable(false)
2024-06-22 13:44:37 -06:00
box.Add(box.swatch)
box.Add(box.label)
2024-07-25 10:58:38 -06:00
box.SetAttr(tomo.ALayout(layouts.Row { false, true }))
2024-06-22 13:44:37 -06:00
2024-07-21 09:48:28 -06:00
box.OnButtonDown(box.handleButtonDown)
box.OnButtonUp(box.handleButtonUp)
2024-06-22 13:44:37 -06:00
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)
2024-06-22 13:44:37 -06:00
}
// OnValueChange specifies a function to be called when the swatch's color
2024-06-26 09:20:53 -06:00
// is changed by the user.
2024-06-22 13:44:37 -06:00
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
2024-06-26 09:20:53 -06:00
// color picker.
func (this *LabelSwatch) OnConfirm (callback func ()) event.Cookie {
return this.swatch.OnConfirm(callback)
2024-06-26 09:20:53 -06:00
}
2024-07-25 10:58:38 -06:00
func (this *LabelSwatch) handleButtonDown (button input.Button) bool {
if !isClickingButton(button) { return true }
2024-07-25 10:58:38 -06:00
return true
2024-07-21 09:48:28 -06:00
}
2024-07-25 10:58:38 -06:00
func (this *LabelSwatch) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return true }
2024-07-21 09:48:28 -06:00
if this.Window().MousePosition().In(this.Bounds()) {
2024-06-22 13:44:37 -06:00
this.swatch.SetFocused(true)
this.swatch.Choose()
}
2024-07-25 10:58:38 -06:00
return true
2024-06-22 13:44:37 -06:00
}