Make value getters/setters more consistent

See #6
This commit is contained in:
Sasha Koshka 2024-06-27 14:01:14 -04:00
parent d0ee6c432c
commit 638fc61d83
11 changed files with 128 additions and 105 deletions

View File

@ -16,7 +16,7 @@ type Calendar struct {
monthLabel *Label
on struct {
edit event.FuncBroadcaster
valueChange event.FuncBroadcaster
}
}
@ -33,13 +33,13 @@ func NewCalendar (tm time.Time) *Calendar {
prevButton.SetIcon(tomo.IconGoPrevious)
prevButton.OnClick(func () {
calendar.prevMonth()
calendar.on.edit.Broadcast()
calendar.on.valueChange.Broadcast()
})
nextButton := NewButton("")
nextButton.SetIcon(tomo.IconGoNext)
nextButton.OnClick(func () {
calendar.nextMonth()
calendar.on.edit.Broadcast()
calendar.on.valueChange.Broadcast()
})
calendar.monthLabel = NewLabel("")
calendar.monthLabel.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
@ -60,17 +60,22 @@ func NewCalendar (tm time.Time) *Calendar {
return calendar
}
// SetTime sets the date the calendar will display.
func (this *Calendar) SetTime (tm time.Time) {
// Value returns the time this calendar is displaying.
func (this *Calendar) Value () time.Time {
return this.time
}
// SetValue sets the date the calendar will display.
func (this *Calendar) SetValue (tm time.Time) {
if this.time == tm { return }
this.time = tm
this.refresh()
}
// OnEdit sets a function to be called when the user changes the date on the
// calendar.
func (this *Calendar) OnEdit (callback func ()) {
this.on.edit.Connect(callback)
// OnValueChange sets a function to be called when the user changes the date on
// the calendar.
func (this *Calendar) OnValueChange (callback func ()) {
this.on.valueChange.Connect(callback)
}
func (this *Calendar) prevMonth () {

View File

@ -27,6 +27,11 @@ func NewCheckbox (value bool) *Checkbox {
return box
}
// Value returns the value of the checkbox.
func (this *Checkbox) Value () bool {
return this.value
}
// SetValue sets the value of the checkbox.
func (this *Checkbox) SetValue (value bool) {
this.value = value
@ -42,13 +47,8 @@ func (this *Checkbox) Toggle () {
this.SetValue(!this.Value())
}
// Value returns the value of the checkbox.
func (this *Checkbox) Value () bool {
return this.value
}
// OnValueChange specifies a function to be called when the checkbox's value
// changes.
// OnValueChange specifies a function to be called when the user checks or
// unchecks the checkbox.
func (this *Checkbox) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}

View File

@ -35,7 +35,7 @@ func NewColorPicker (value color.Color) *ColorPicker {
picker.hueSlider = NewVerticalSlider(0.0)
picker.Add(picker.hueSlider)
picker.hueSlider.OnSlide(func () {
picker.hueSlider.OnValueChange(func () {
picker.value.H = picker.hueSlider.Value()
picker.on.valueChange.Broadcast()
picker.pickerMap.Invalidate()
@ -43,7 +43,7 @@ func NewColorPicker (value color.Color) *ColorPicker {
picker.alphaSlider = NewVerticalSlider(0.0)
picker.Add(picker.alphaSlider)
picker.alphaSlider.OnSlide(func () {
picker.alphaSlider.OnValueChange(func () {
picker.value.A = uint8(picker.alphaSlider.Value() * 255)
picker.on.valueChange.Broadcast()
picker.pickerMap.Invalidate()
@ -54,6 +54,11 @@ func NewColorPicker (value color.Color) *ColorPicker {
return picker
}
// Value returns the color of the picker.
func (this *ColorPicker) Value () color.Color {
return this.value
}
// SetValue sets the color of the picker.
func (this *ColorPicker) SetValue (value color.Color) {
if value == nil { value = color.Transparent }
@ -62,9 +67,10 @@ func (this *ColorPicker) SetValue (value color.Color) {
this.alphaSlider.SetValue(float64(this.value.A) / 255)
}
// Value returns the color of the picker.
func (this *ColorPicker) Value () color.Color {
return this.value
// OnValueChange specifies a function to be called when the user changes the
// swatch's color.
func (this *ColorPicker) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
// RGBA satisfies the color.Color interface
@ -72,12 +78,6 @@ func (this *ColorPicker) RGBA () (r, g, b, a uint32) {
return this.value.RGBA()
}
// OnValueChange specifies a function to be called when the swatch's color
// changes.
func (this *ColorPicker) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
type colorPickerMap struct {
tomo.CanvasBox
dragging bool

View File

@ -31,6 +31,11 @@ func NewLabelCheckbox (value bool, text string) *LabelCheckbox {
return box
}
// Value returns the value of the checkbox.
func (this *LabelCheckbox) Value () bool {
return this.checkbox.Value()
}
// SetValue sets the value of the checkbox.
func (this *LabelCheckbox) SetValue (value bool) {
this.checkbox.SetValue(value)
@ -41,13 +46,8 @@ func (this *LabelCheckbox) Toggle () {
this.checkbox.Toggle()
}
// Value returns the value of the checkbox.
func (this *LabelCheckbox) Value () bool {
return this.checkbox.Value()
}
// OnValueChange specifies a function to be called when the checkbox's value
// changes.
// OnValueChange specifies a function to be called when the user checks or
// unchecks the checkbox.
func (this *LabelCheckbox) OnValueChange (callback func ()) event.Cookie {
return this.checkbox.OnValueChange(callback)
}

View File

@ -33,19 +33,14 @@ func NewLabelSwatch (value color.Color, text string) *LabelSwatch {
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()
// 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
@ -54,6 +49,11 @@ 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()
}
// OnEnter specifies a function to be called when the user selects "OK" in the
// color picker.
func (this *LabelSwatch) OnEnter (callback func ()) event.Cookie {

View File

@ -16,7 +16,7 @@ type NumberInput struct {
decrement *Button
on struct {
enter event.FuncBroadcaster
edit event.FuncBroadcaster
valueChange event.FuncBroadcaster
}
}
@ -44,37 +44,38 @@ func NewNumberInput (value float64) *NumberInput {
box.OnScroll(box.handleScroll)
box.OnKeyDown(box.handleKeyDown)
box.input.OnEnter(box.handleEnter)
box.input.OnEdit(box.on.edit.Broadcast)
box.input.OnValueChange(box.on.valueChange.Broadcast)
box.increment.OnClick(func () { box.shift(1) })
box.decrement.OnClick(func () { box.shift(-1) })
return box
}
// SetValue sets the value of the input.
func (this *NumberInput) SetValue (value float64) {
this.input.SetText(strconv.FormatFloat(value, 'g', -1, 64))
}
// Value returns the value of the input.
func (this *NumberInput) Value () float64 {
value, _ := strconv.ParseFloat(this.input.Text(), 64)
return value
}
// SetValue sets the value of the input.
func (this *NumberInput) SetValue (value float64) {
this.input.SetText(strconv.FormatFloat(value, 'g', -1, 64))
}
// OnValueChange specifies a function to be called when the user edits the input
// value.
func (this *NumberInput) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
// OnEnter specifies a function to be called when the user presses enter within
// the text input.
func (this *NumberInput) OnEnter (callback func ()) event.Cookie {
return this.on.enter.Connect(callback)
}
// OnEdit specifies a function to be called when the user edits the input value.
func (this *NumberInput) OnEdit (callback func ()) event.Cookie {
return this.on.edit.Connect(callback)
}
func (this *NumberInput) shift (by int) {
this.SetValue(this.Value() + float64(by))
this.on.edit.Broadcast()
this.on.valueChange.Broadcast()
}
func (this *NumberInput) handleKeyDown (key input.Key, numpad bool) {

View File

@ -86,6 +86,14 @@ func (this *Scrollbar) handleLinkedContentBoundsChange () {
}
}
// Value returns the value of the scrollbar between 0 and 1 where 0 is scrolled
// all the way to the left/top, and 1 is scrolled all the way to the
// right/bottom.
func (this *Scrollbar) Value () float64 {
if this.layout.linked == nil { return 0 }
return this.layout.value
}
// SetValue sets the value of the scrollbar between 0 and 1, where 0 is scrolled
// all the way to the left/top, and 1 is scrolled all the way to the
// right/bottom.
@ -106,16 +114,8 @@ func (this *Scrollbar) SetValue (value float64) {
}
// Value returns the value of the scrollbar between 0 and 1 where 0 is scrolled
// all the way to the left/top, and 1 is scrolled all the way to the
// right/bottom.
func (this *Scrollbar) Value () float64 {
if this.layout.linked == nil { return 0 }
return this.layout.value
}
// OnValueChange specifies a function to be called when the position of the
// scrollbar changes.
// OnValueChange specifies a function to be called when the user changes the
// position of the scrollbar.
func (this *Scrollbar) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}

View File

@ -42,6 +42,10 @@ type ScrollContainer struct {
horizontalCookie event.Cookie
verticalCookie event.Cookie
on struct {
valueChange event.FuncBroadcaster
}
}
// NewScrollContainer creates a new scroll container.
@ -52,10 +56,12 @@ func NewScrollContainer (sides ScrollSide) *ScrollContainer {
}
if sides.Vertical() {
this.layout.vertical = NewVerticalScrollbar()
this.layout.vertical.OnValueChange(this.handleValueChange)
this.Add(this.layout.vertical)
}
if sides.Horizontal() {
this.layout.horizontal = NewHorizontalScrollbar()
this.layout.horizontal.OnValueChange(this.handleValueChange)
this.Add(this.layout.horizontal)
}
this.CaptureScroll(true)
@ -103,6 +109,18 @@ func (this *ScrollContainer) SetRoot (root tomo.ContentObject) {
}
}
// Value returns the horizontal and vertical scrollbar values where 0 is all the
// way to the left/top, and 1 is all the way to the right/bottom.
func (this *ScrollContainer) Value () (x, y float64) {
if this.layout.horizontal != nil {
x = this.layout.horizontal.Value()
}
if this.layout.vertical != nil {
y = this.layout.vertical.Value()
}
return x, y
}
// SetValue sets the horizontal and vertical scrollbar values where 0 is all the
// way to the left/top, and 1 is all the way to the right/bottom.
func (this *ScrollContainer) SetValue (x, y float64) {
@ -114,16 +132,14 @@ func (this *ScrollContainer) SetValue (x, y float64) {
}
}
// Value returns the horizontal and vertical scrollbar values where 0 is all the
// way to the left/top, and 1 is all the way to the right/bottom.
func (this *ScrollContainer) Value () (x, y float64) {
if this.layout.horizontal != nil {
x = this.layout.horizontal.Value()
// OnValueChange specifies a function to be called when the user changes the
// position of the horizontal or vertical scrollbars.
func (this *ScrollContainer) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
if this.layout.vertical != nil {
y = this.layout.vertical.Value()
}
return x, y
func (this *ScrollContainer) handleValueChange () {
this.on.valueChange.Broadcast()
}
func (this *ScrollContainer) handleScroll (x, y float64) {

View File

@ -16,7 +16,7 @@ type Slider struct {
step float64
on struct {
slide event.FuncBroadcaster
valueChange event.FuncBroadcaster
enter event.FuncBroadcaster
}
}
@ -86,8 +86,8 @@ func (this *Slider) Value () float64 {
// OnValueChange specifies a function to be called when the user moves the
// slider.
func (this *Slider) OnSlide (callback func ()) event.Cookie {
return this.on.slide.Connect(callback)
func (this *Slider) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
// OnEnter specifies a function to be called when the user stops moving the
@ -110,20 +110,20 @@ func (this *Slider) handleKeyDown (key input.Key, numpad bool) {
} else {
this.SetValue(this.Value() - increment)
}
this.on.slide.Broadcast()
this.on.valueChange.Broadcast()
case input.KeyDown, input.KeyRight:
if this.Modifiers().Alt {
this.SetValue(1)
} else {
this.SetValue(this.Value() + increment)
}
this.on.slide.Broadcast()
this.on.valueChange.Broadcast()
case input.KeyHome:
this.SetValue(0)
this.on.slide.Broadcast()
this.on.valueChange.Broadcast()
case input.KeyEnd:
this.SetValue(1)
this.on.slide.Broadcast()
this.on.valueChange.Broadcast()
}
}
@ -154,21 +154,21 @@ func (this *Slider) handleMouseDown (button input.Button) {
case input.ButtonMiddle:
if above {
this.SetValue(0)
this.on.slide.Broadcast()
this.on.valueChange.Broadcast()
this.on.enter.Broadcast()
} else {
this.SetValue(1)
this.on.slide.Broadcast()
this.on.valueChange.Broadcast()
this.on.enter.Broadcast()
}
case input.ButtonRight:
if above {
this.SetValue(this.Value() - this.step)
this.on.slide.Broadcast()
this.on.valueChange.Broadcast()
this.on.enter.Broadcast()
} else {
this.SetValue(this.Value() + this.step)
this.on.slide.Broadcast()
this.on.valueChange.Broadcast()
this.on.enter.Broadcast()
}
}
@ -188,7 +188,7 @@ func (this *Slider) handleMouseMove () {
func (this *Slider) handleScroll (x, y float64) {
delta := (x + y) * 0.005
this.SetValue(this.Value() + delta)
this.on.slide.Broadcast()
this.on.valueChange.Broadcast()
this.on.enter.Broadcast()
}
@ -207,7 +207,7 @@ func (this *Slider) drag () {
float64(pointer.X) /
float64(gutter.Dx() - handle.Dx()))
}
this.on.slide.Broadcast()
this.on.valueChange.Broadcast()
}
func (this *Slider) fallbackDragOffset () image.Point {

View File

@ -36,6 +36,11 @@ func NewSwatch (value color.Color) *Swatch {
return swatch
}
// Value returns the color of the swatch.
func (this *Swatch) Value () color.Color {
return this.value
}
// SetValue sets the color of the swatch.
func (this *Swatch) SetValue (value color.Color) {
this.value = value
@ -43,9 +48,10 @@ func (this *Swatch) SetValue (value color.Color) {
this.Invalidate()
}
// Value returns the color of the swatch.
func (this *Swatch) Value () color.Color {
return this.value
// OnValueChange specifies a function to be called when the swatch's color
// is changed by the user.
func (this *Swatch) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
// RGBA satisfies the color.Color interface
@ -54,12 +60,6 @@ func (this *Swatch) RGBA () (r, g, b, a uint32) {
return this.value.RGBA()
}
// OnValueChange specifies a function to be called when the swatch's color
// is changed by the user.
func (this *Swatch) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
// OnEnter specifies a function to be called when the user selects "OK" in the
// color picker.
func (this *Swatch) OnEnter (callback func ()) event.Cookie {

View File

@ -12,7 +12,7 @@ type TextInput struct {
text []rune
on struct {
enter event.FuncBroadcaster
edit event.FuncBroadcaster
valueChange event.FuncBroadcaster
}
}
@ -47,9 +47,10 @@ func (this *TextInput) OnEnter (callback func ()) event.Cookie {
return this.on.enter.Connect(callback)
}
// OnEdit specifies a function to be called when the user edits the input text.
func (this *TextInput) OnEdit (callback func ()) event.Cookie {
return this.on.edit.Connect(callback)
// OnValueChange specifies a function to be called when the user edits the input
// text.
func (this *TextInput) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
func (this *TextInput) handleKeyDown (key input.Key, numpad bool) {
@ -100,7 +101,7 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) {
this.Select(dot)
if changed {
this.SetText(string(this.text))
this.on.edit.Broadcast()
this.on.valueChange.Broadcast()
}
}