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

View File

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

View File

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

View File

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

View File

@ -33,19 +33,14 @@ func NewLabelSwatch (value color.Color, text string) *LabelSwatch {
return box 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. // Value returns the color of the swatch.
func (this *LabelSwatch) Value () color.Color { func (this *LabelSwatch) Value () color.Color {
return this.swatch.Value() return this.swatch.Value()
} }
// RGBA satisfies the color.Color interface // SetValue sets the color of the swatch.
func (this *LabelSwatch) RGBA () (r, g, b, a uint32) { func (this *LabelSwatch) SetValue (value color.Color) {
return this.swatch.RGBA() this.swatch.SetValue(value)
} }
// OnValueChange specifies a function to be called when the swatch's color // 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) 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 // OnEnter specifies a function to be called when the user selects "OK" in the
// color picker. // color picker.
func (this *LabelSwatch) OnEnter (callback func ()) event.Cookie { func (this *LabelSwatch) OnEnter (callback func ()) event.Cookie {

View File

@ -15,8 +15,8 @@ type NumberInput struct {
increment *Button increment *Button
decrement *Button decrement *Button
on struct { on struct {
enter event.FuncBroadcaster enter event.FuncBroadcaster
edit event.FuncBroadcaster valueChange event.FuncBroadcaster
} }
} }
@ -44,37 +44,38 @@ func NewNumberInput (value float64) *NumberInput {
box.OnScroll(box.handleScroll) box.OnScroll(box.handleScroll)
box.OnKeyDown(box.handleKeyDown) box.OnKeyDown(box.handleKeyDown)
box.input.OnEnter(box.handleEnter) 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.increment.OnClick(func () { box.shift(1) })
box.decrement.OnClick(func () { box.shift(-1) }) box.decrement.OnClick(func () { box.shift(-1) })
return box 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. // Value returns the value of the input.
func (this *NumberInput) Value () float64 { func (this *NumberInput) Value () float64 {
value, _ := strconv.ParseFloat(this.input.Text(), 64) value, _ := strconv.ParseFloat(this.input.Text(), 64)
return value 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 // OnEnter specifies a function to be called when the user presses enter within
// the text input. // the text input.
func (this *NumberInput) OnEnter (callback func ()) event.Cookie { func (this *NumberInput) OnEnter (callback func ()) event.Cookie {
return this.on.enter.Connect(callback) 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) { func (this *NumberInput) shift (by int) {
this.SetValue(this.Value() + float64(by)) this.SetValue(this.Value() + float64(by))
this.on.edit.Broadcast() this.on.valueChange.Broadcast()
} }
func (this *NumberInput) handleKeyDown (key input.Key, numpad bool) { 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 // 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 // all the way to the left/top, and 1 is scrolled all the way to the
// right/bottom. // 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 // OnValueChange specifies a function to be called when the user changes the
// all the way to the left/top, and 1 is scrolled all the way to the // position of the scrollbar.
// 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.
func (this *Scrollbar) OnValueChange (callback func ()) event.Cookie { func (this *Scrollbar) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback) return this.on.valueChange.Connect(callback)
} }

View File

@ -42,6 +42,10 @@ type ScrollContainer struct {
horizontalCookie event.Cookie horizontalCookie event.Cookie
verticalCookie event.Cookie verticalCookie event.Cookie
on struct {
valueChange event.FuncBroadcaster
}
} }
// NewScrollContainer creates a new scroll container. // NewScrollContainer creates a new scroll container.
@ -52,10 +56,12 @@ func NewScrollContainer (sides ScrollSide) *ScrollContainer {
} }
if sides.Vertical() { if sides.Vertical() {
this.layout.vertical = NewVerticalScrollbar() this.layout.vertical = NewVerticalScrollbar()
this.layout.vertical.OnValueChange(this.handleValueChange)
this.Add(this.layout.vertical) this.Add(this.layout.vertical)
} }
if sides.Horizontal() { if sides.Horizontal() {
this.layout.horizontal = NewHorizontalScrollbar() this.layout.horizontal = NewHorizontalScrollbar()
this.layout.horizontal.OnValueChange(this.handleValueChange)
this.Add(this.layout.horizontal) this.Add(this.layout.horizontal)
} }
this.CaptureScroll(true) 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 // 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. // way to the left/top, and 1 is all the way to the right/bottom.
func (this *ScrollContainer) SetValue (x, y float64) { 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 // OnValueChange specifies a function to be called when the user changes the
// way to the left/top, and 1 is all the way to the right/bottom. // position of the horizontal or vertical scrollbars.
func (this *ScrollContainer) Value () (x, y float64) { func (this *ScrollContainer) OnValueChange (callback func ()) event.Cookie {
if this.layout.horizontal != nil { return this.on.valueChange.Connect(callback)
x = this.layout.horizontal.Value() }
}
if this.layout.vertical != nil { func (this *ScrollContainer) handleValueChange () {
y = this.layout.vertical.Value() this.on.valueChange.Broadcast()
}
return x, y
} }
func (this *ScrollContainer) handleScroll (x, y float64) { func (this *ScrollContainer) handleScroll (x, y float64) {

View File

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

View File

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

View File

@ -11,8 +11,8 @@ type TextInput struct {
tomo.TextBox tomo.TextBox
text []rune text []rune
on struct { on struct {
enter event.FuncBroadcaster 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) return this.on.enter.Connect(callback)
} }
// OnEdit specifies a function to be called when the user edits the input text. // OnValueChange specifies a function to be called when the user edits the input
func (this *TextInput) OnEdit (callback func ()) event.Cookie { // text.
return this.on.edit.Connect(callback) func (this *TextInput) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
} }
func (this *TextInput) handleKeyDown (key input.Key, numpad bool) { 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) this.Select(dot)
if changed { if changed {
this.SetText(string(this.text)) this.SetText(string(this.text))
this.on.edit.Broadcast() this.on.valueChange.Broadcast()
} }
} }