From 02fed8ce48614e6c6b84460ae6fe537fd4cfb3ce Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 24 Aug 2024 20:19:01 -0400 Subject: [PATCH] NumberInput no longer embeds tomo.ContainerBox --- numberinput.go | 69 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/numberinput.go b/numberinput.go index 26a09ca..a4ca433 100644 --- a/numberinput.go +++ b/numberinput.go @@ -3,14 +3,17 @@ package objects import "math" import "strconv" import "git.tebibyte.media/tomo/tomo" +import "git.tebibyte.media/tomo/tomo/text" import "git.tebibyte.media/tomo/tomo/input" import "git.tebibyte.media/tomo/tomo/event" import "git.tebibyte.media/tomo/objects/layouts" +var _ tomo.Object = new(NumberInput) + // NumberInput is an editable text box which accepts only numbers, and has // controls to increment and decrement its value. type NumberInput struct { - tomo.ContainerBox + box tomo.ContainerBox input *TextInput increment *Button decrement *Button @@ -22,30 +25,52 @@ type NumberInput struct { // NewNumberInput creates a new number input with the specified value. func NewNumberInput (value float64) *NumberInput { - box := &NumberInput { - ContainerBox: tomo.NewContainerBox(), - input: NewTextInput(""), - increment: NewButton(""), - decrement: NewButton(""), + numberInput := &NumberInput { + box: tomo.NewContainerBox(), + input: NewTextInput(""), + increment: NewButton(""), + decrement: NewButton(""), } - box.SetRole(tomo.R("objects", "NumberInput")) - box.Add(box.input) - box.Add(box.decrement) - box.Add(box.increment) - box.SetAttr(tomo.ALayout(layouts.Row { true, false, false })) - box.increment.SetIcon(tomo.IconValueIncrement) - box.decrement.SetIcon(tomo.IconValueDecrement) + numberInput.box.SetRole(tomo.R("objects", "NumberInput")) + numberInput.box.Add(numberInput.input) + numberInput.box.Add(numberInput.decrement) + numberInput.box.Add(numberInput.increment) + numberInput.box.SetAttr(tomo.ALayout(layouts.Row { true, false, false })) + numberInput.increment.SetIcon(tomo.IconValueIncrement) + numberInput.decrement.SetIcon(tomo.IconValueDecrement) - box.SetValue(value) + numberInput.SetValue(value) - box.OnScroll(box.handleScroll) - box.OnKeyDown(box.handleKeyDown) - box.OnKeyUp(box.handleKeyUp) - box.input.OnConfirm(box.handleConfirm) - box.input.OnValueChange(box.on.valueChange.Broadcast) - box.increment.OnClick(func () { box.shift(1) }) - box.decrement.OnClick(func () { box.shift(-1) }) - return box + numberInput.box.OnScroll(numberInput.handleScroll) + numberInput.box.OnKeyDown(numberInput.handleKeyDown) + numberInput.box.OnKeyUp(numberInput.handleKeyUp) + numberInput.input.OnConfirm(numberInput.handleConfirm) + numberInput.input.OnValueChange(numberInput.on.valueChange.Broadcast) + numberInput.increment.OnClick(func () { numberInput.shift( 1) }) + numberInput.decrement.OnClick(func () { numberInput.shift(-1) }) + return numberInput +} + +// GetBox returns the underlying box. +func (this *NumberInput) GetBox () tomo.Box { + return this.box +} + +// SetFocused sets whether or not this number input has keyboard focus. If set +// to true, this method will steal focus away from whichever object currently +// has focus. +func (this *NumberInput) SetFocused (focused bool) { + this.input.SetFocused(focused) +} + +// Select sets the text cursor or selection. +func (this *NumberInput) Select (dot text.Dot) { + this.input.Select(dot) +} + +// Dot returns the text cursor or selection. +func (this *NumberInput) Dot () text.Dot { + return this.input.Dot() } // Value returns the value of the input.