2023-01-17 22:38:58 -07:00
|
|
|
package basic
|
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
2023-01-17 23:19:10 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/textmanip"
|
2023-01-17 22:38:58 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
|
|
|
|
2023-01-18 09:56:14 -07:00
|
|
|
// TextBox is a single-line text input.
|
2023-01-17 22:38:58 -07:00
|
|
|
type TextBox struct {
|
|
|
|
*core.Core
|
2023-01-27 15:55:49 -07:00
|
|
|
*core.SelectableCore
|
2023-01-17 22:38:58 -07:00
|
|
|
core core.CoreControl
|
2023-01-27 15:55:49 -07:00
|
|
|
selectableControl core.SelectableCoreControl
|
2023-01-17 22:38:58 -07:00
|
|
|
|
|
|
|
cursor int
|
2023-01-18 13:56:36 -07:00
|
|
|
scroll int
|
2023-01-17 22:38:58 -07:00
|
|
|
placeholder string
|
2023-01-17 23:19:10 -07:00
|
|
|
text []rune
|
2023-01-18 13:56:36 -07:00
|
|
|
|
2023-01-17 22:38:58 -07:00
|
|
|
placeholderDrawer artist.TextDrawer
|
|
|
|
valueDrawer artist.TextDrawer
|
2023-01-18 13:56:36 -07:00
|
|
|
|
2023-01-20 15:40:28 -07:00
|
|
|
onKeyDown func (key tomo.Key, modifiers tomo.Modifiers) (handled bool)
|
2023-01-18 13:56:36 -07:00
|
|
|
onChange func ()
|
2023-01-19 14:49:34 -07:00
|
|
|
onScrollBoundsChange func ()
|
2023-01-17 22:38:58 -07:00
|
|
|
}
|
|
|
|
|
2023-01-18 09:56:14 -07:00
|
|
|
// NewTextBox creates a new text box with the specified placeholder text, and
|
|
|
|
// a value. When the value is empty, the placeholder will be displayed in gray
|
|
|
|
// text.
|
|
|
|
func NewTextBox (placeholder, value string) (element *TextBox) {
|
2023-01-27 15:55:49 -07:00
|
|
|
element = &TextBox { }
|
2023-01-17 22:38:58 -07:00
|
|
|
element.Core, element.core = core.NewCore(element)
|
2023-01-27 15:55:49 -07:00
|
|
|
element.SelectableCore,
|
|
|
|
element.selectableControl = core.NewSelectableCore (func () {
|
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
})
|
2023-01-17 22:38:58 -07:00
|
|
|
element.placeholderDrawer.SetFace(theme.FontFaceRegular())
|
|
|
|
element.valueDrawer.SetFace(theme.FontFaceRegular())
|
|
|
|
element.placeholder = placeholder
|
2023-01-17 22:42:04 -07:00
|
|
|
element.placeholderDrawer.SetText([]rune(placeholder))
|
2023-01-17 22:38:58 -07:00
|
|
|
element.updateMinimumSize()
|
2023-01-18 09:56:14 -07:00
|
|
|
element.SetValue(value)
|
2023-01-17 22:38:58 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *TextBox) Resize (width, height int) {
|
|
|
|
element.core.AllocateCanvas(width, height)
|
2023-01-18 13:56:36 -07:00
|
|
|
element.scrollToCursor()
|
2023-01-17 22:38:58 -07:00
|
|
|
element.draw()
|
2023-01-20 13:52:46 -07:00
|
|
|
if element.onScrollBoundsChange != nil {
|
|
|
|
element.onScrollBoundsChange()
|
|
|
|
}
|
2023-01-17 22:38:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *TextBox) HandleMouseDown (x, y int, button tomo.Button) {
|
2023-01-27 15:55:49 -07:00
|
|
|
if !element.Enabled() { return }
|
|
|
|
if !element.Selected() { element.Select() }
|
2023-01-17 22:38:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *TextBox) HandleMouseUp (x, y int, button tomo.Button) { }
|
|
|
|
func (element *TextBox) HandleMouseMove (x, y int) { }
|
2023-01-19 16:03:50 -07:00
|
|
|
func (element *TextBox) HandleMouseScroll (x, y int, deltaX, deltaY float64) { }
|
2023-01-17 22:38:58 -07:00
|
|
|
|
2023-01-20 15:40:28 -07:00
|
|
|
func (element *TextBox) HandleKeyDown(key tomo.Key, modifiers tomo.Modifiers) {
|
|
|
|
if element.onKeyDown != nil && element.onKeyDown(key, modifiers) {
|
2023-01-18 09:56:14 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-20 13:52:46 -07:00
|
|
|
scrollMemory := element.scroll
|
2023-01-18 13:56:36 -07:00
|
|
|
altered := true
|
|
|
|
textChanged := false
|
2023-01-17 22:38:58 -07:00
|
|
|
switch {
|
|
|
|
case key == tomo.KeyBackspace:
|
|
|
|
if len(element.text) < 1 { break }
|
2023-01-17 23:19:10 -07:00
|
|
|
element.text, element.cursor = textmanip.Backspace (
|
|
|
|
element.text,
|
|
|
|
element.cursor,
|
|
|
|
modifiers.Control)
|
2023-01-18 13:56:36 -07:00
|
|
|
textChanged = true
|
2023-01-17 23:19:10 -07:00
|
|
|
|
|
|
|
case key == tomo.KeyDelete:
|
|
|
|
if len(element.text) < 1 { break }
|
|
|
|
element.text, element.cursor = textmanip.Delete (
|
|
|
|
element.text,
|
|
|
|
element.cursor,
|
|
|
|
modifiers.Control)
|
2023-01-18 13:56:36 -07:00
|
|
|
textChanged = true
|
2023-01-17 23:19:10 -07:00
|
|
|
|
|
|
|
case key == tomo.KeyLeft:
|
|
|
|
element.cursor = textmanip.MoveLeft (
|
|
|
|
element.text,
|
|
|
|
element.cursor,
|
|
|
|
modifiers.Control)
|
|
|
|
|
|
|
|
case key == tomo.KeyRight:
|
|
|
|
element.cursor = textmanip.MoveRight (
|
|
|
|
element.text,
|
|
|
|
element.cursor,
|
|
|
|
modifiers.Control)
|
|
|
|
|
2023-01-17 22:38:58 -07:00
|
|
|
case key.Printable():
|
2023-01-17 23:19:10 -07:00
|
|
|
element.text, element.cursor = textmanip.Type (
|
|
|
|
element.text,
|
|
|
|
element.cursor,
|
|
|
|
rune(key))
|
2023-01-18 13:56:36 -07:00
|
|
|
textChanged = true
|
2023-01-17 23:19:10 -07:00
|
|
|
|
|
|
|
default:
|
|
|
|
altered = false
|
|
|
|
}
|
|
|
|
|
2023-01-18 13:56:36 -07:00
|
|
|
if textChanged {
|
|
|
|
element.runOnChange()
|
2023-01-17 23:19:10 -07:00
|
|
|
element.valueDrawer.SetText(element.text)
|
|
|
|
}
|
2023-01-18 13:56:36 -07:00
|
|
|
|
|
|
|
if altered {
|
|
|
|
element.scrollToCursor()
|
|
|
|
}
|
2023-01-20 13:52:46 -07:00
|
|
|
|
|
|
|
if (textChanged || scrollMemory != element.scroll) &&
|
|
|
|
element.onScrollBoundsChange != nil {
|
|
|
|
element.onScrollBoundsChange()
|
|
|
|
}
|
2023-01-17 23:19:10 -07:00
|
|
|
|
|
|
|
if altered && element.core.HasImage () {
|
|
|
|
element.draw()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-17 22:38:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *TextBox) HandleKeyUp(key tomo.Key, modifiers tomo.Modifiers) { }
|
|
|
|
|
|
|
|
func (element *TextBox) SetPlaceholder (placeholder string) {
|
|
|
|
if element.placeholder == placeholder { return }
|
|
|
|
|
|
|
|
element.placeholder = placeholder
|
2023-01-17 22:42:04 -07:00
|
|
|
element.placeholderDrawer.SetText([]rune(placeholder))
|
2023-01-17 22:38:58 -07:00
|
|
|
|
|
|
|
element.updateMinimumSize()
|
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-17 22:38:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 09:56:14 -07:00
|
|
|
func (element *TextBox) SetValue (text string) {
|
2023-01-17 23:19:10 -07:00
|
|
|
// if element.text == text { return }
|
2023-01-17 22:38:58 -07:00
|
|
|
|
2023-01-17 23:19:10 -07:00
|
|
|
element.text = []rune(text)
|
2023-01-18 09:56:14 -07:00
|
|
|
element.runOnChange()
|
2023-01-17 23:19:10 -07:00
|
|
|
element.valueDrawer.SetText(element.text)
|
2023-01-17 22:38:58 -07:00
|
|
|
if element.cursor > element.valueDrawer.Length() {
|
|
|
|
element.cursor = element.valueDrawer.Length()
|
|
|
|
}
|
2023-01-18 13:56:36 -07:00
|
|
|
element.scrollToCursor()
|
2023-01-17 22:38:58 -07:00
|
|
|
|
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-17 22:38:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 23:29:59 -07:00
|
|
|
func (element *TextBox) Value () (value string) {
|
|
|
|
return string(element.text)
|
|
|
|
}
|
|
|
|
|
2023-01-18 09:56:14 -07:00
|
|
|
func (element *TextBox) Filled () (filled bool) {
|
|
|
|
return len(element.text) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *TextBox) OnKeyDown (
|
2023-01-20 15:40:28 -07:00
|
|
|
callback func (key tomo.Key, modifiers tomo.Modifiers) (handled bool),
|
2023-01-18 09:56:14 -07:00
|
|
|
) {
|
|
|
|
element.onKeyDown = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *TextBox) OnChange (callback func ()) {
|
|
|
|
element.onChange = callback
|
|
|
|
}
|
|
|
|
|
2023-01-18 15:52:05 -07:00
|
|
|
// ScrollContentBounds returns the full content size of the element.
|
|
|
|
func (element *TextBox) ScrollContentBounds () (bounds image.Rectangle) {
|
|
|
|
bounds = element.valueDrawer.LayoutBounds()
|
|
|
|
return bounds.Sub(bounds.Min)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollViewportBounds returns the size and position of the element's viewport
|
|
|
|
// relative to ScrollBounds.
|
|
|
|
func (element *TextBox) ScrollViewportBounds () (bounds image.Rectangle) {
|
|
|
|
return image.Rect (
|
|
|
|
element.scroll,
|
|
|
|
0,
|
2023-01-20 21:40:59 -07:00
|
|
|
element.scroll + element.scrollViewportWidth(),
|
2023-01-18 15:52:05 -07:00
|
|
|
0)
|
|
|
|
}
|
|
|
|
|
2023-01-20 21:40:59 -07:00
|
|
|
func (element *TextBox) scrollViewportWidth () (width int) {
|
|
|
|
return element.Bounds().Inset(theme.Padding()).Dx()
|
|
|
|
}
|
|
|
|
|
2023-01-18 15:52:05 -07:00
|
|
|
// ScrollTo scrolls the viewport to the specified point relative to
|
|
|
|
// ScrollBounds.
|
|
|
|
func (element *TextBox) ScrollTo (position image.Point) {
|
2023-01-20 21:40:59 -07:00
|
|
|
// constrain to minimum
|
2023-01-18 15:52:05 -07:00
|
|
|
element.scroll = position.X
|
|
|
|
if element.scroll < 0 { element.scroll = 0 }
|
2023-01-20 21:40:59 -07:00
|
|
|
|
|
|
|
// constrain to maximum
|
|
|
|
contentBounds := element.ScrollContentBounds()
|
|
|
|
maxPosition := contentBounds.Max.X - element.scrollViewportWidth()
|
|
|
|
if element.scroll > maxPosition { element.scroll = maxPosition }
|
2023-01-18 15:52:05 -07:00
|
|
|
|
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
if element.onScrollBoundsChange != nil {
|
|
|
|
element.onScrollBoundsChange()
|
2023-01-19 11:07:27 -07:00
|
|
|
}
|
|
|
|
}
|
2023-01-18 15:52:05 -07:00
|
|
|
|
|
|
|
// ScrollAxes returns the supported axes for scrolling.
|
|
|
|
func (element *TextBox) ScrollAxes () (horizontal, vertical bool) {
|
|
|
|
return true, false
|
|
|
|
}
|
|
|
|
|
2023-01-19 15:35:19 -07:00
|
|
|
func (element *TextBox) OnScrollBoundsChange (callback func ()) {
|
|
|
|
element.onScrollBoundsChange = callback
|
|
|
|
}
|
|
|
|
|
2023-01-18 15:32:33 -07:00
|
|
|
func (element *TextBox) updateMinimumSize () {
|
|
|
|
textBounds := element.placeholderDrawer.LayoutBounds()
|
2023-01-28 23:49:01 -07:00
|
|
|
_, inset := theme.InputPattern(theme.PatternState { })
|
2023-01-18 15:32:33 -07:00
|
|
|
element.core.SetMinimumSize (
|
|
|
|
textBounds.Dx() +
|
2023-01-28 23:49:01 -07:00
|
|
|
theme.Padding() * 2 + inset[3] + inset[1],
|
2023-01-18 15:32:33 -07:00
|
|
|
element.placeholderDrawer.LineHeight().Round() +
|
2023-01-28 23:49:01 -07:00
|
|
|
theme.Padding() * 2 + inset[0] + inset[2])
|
2023-01-18 15:32:33 -07:00
|
|
|
}
|
|
|
|
|
2023-01-18 09:56:14 -07:00
|
|
|
func (element *TextBox) runOnChange () {
|
|
|
|
if element.onChange != nil {
|
|
|
|
element.onChange()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 13:56:36 -07:00
|
|
|
func (element *TextBox) scrollToCursor () {
|
|
|
|
if !element.core.HasImage() { return }
|
|
|
|
|
|
|
|
bounds := element.core.Bounds().Inset(theme.Padding())
|
|
|
|
bounds.Max.X -= element.valueDrawer.Em().Round()
|
|
|
|
cursorPosition := element.valueDrawer.PositionOf(element.cursor)
|
|
|
|
cursorPosition.X -= element.scroll
|
|
|
|
maxX := bounds.Max.X
|
2023-01-18 14:01:31 -07:00
|
|
|
minX := maxX
|
2023-01-18 13:56:36 -07:00
|
|
|
if cursorPosition.X > maxX {
|
|
|
|
element.scroll += cursorPosition.X - maxX
|
|
|
|
} else if cursorPosition.X < minX {
|
|
|
|
element.scroll -= minX - cursorPosition.X
|
|
|
|
if element.scroll < 0 { element.scroll = 0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 22:38:58 -07:00
|
|
|
func (element *TextBox) draw () {
|
|
|
|
bounds := element.core.Bounds()
|
|
|
|
|
2023-01-28 23:49:01 -07:00
|
|
|
// FIXME: take index into account
|
|
|
|
pattern, inset := theme.InputPattern(theme.PatternState {
|
|
|
|
Disabled: !element.Enabled(),
|
|
|
|
Selected: element.Selected(),
|
|
|
|
})
|
|
|
|
artist.FillRectangle(element.core, pattern, bounds)
|
2023-01-17 22:38:58 -07:00
|
|
|
|
2023-01-27 15:55:49 -07:00
|
|
|
if len(element.text) == 0 && !element.Selected() {
|
2023-01-17 22:38:58 -07:00
|
|
|
// draw placeholder
|
|
|
|
textBounds := element.placeholderDrawer.LayoutBounds()
|
|
|
|
offset := image.Point {
|
2023-01-28 23:49:01 -07:00
|
|
|
X: theme.Padding() + inset[3],
|
|
|
|
Y: theme.Padding() + inset[0],
|
2023-01-17 22:38:58 -07:00
|
|
|
}
|
2023-01-28 23:49:01 -07:00
|
|
|
foreground, _ := theme.ForegroundPattern(theme.PatternState {
|
|
|
|
Disabled: true,
|
|
|
|
})
|
2023-01-17 22:38:58 -07:00
|
|
|
element.placeholderDrawer.Draw (
|
|
|
|
element.core,
|
|
|
|
foreground,
|
|
|
|
offset.Sub(textBounds.Min))
|
|
|
|
} else {
|
|
|
|
// draw input value
|
|
|
|
textBounds := element.valueDrawer.LayoutBounds()
|
|
|
|
offset := image.Point {
|
2023-01-28 23:49:01 -07:00
|
|
|
X: theme.Padding() + inset[3] - element.scroll,
|
|
|
|
Y: theme.Padding() + inset[0],
|
2023-01-17 22:38:58 -07:00
|
|
|
}
|
2023-01-28 23:49:01 -07:00
|
|
|
foreground, _ := theme.ForegroundPattern(theme.PatternState {
|
|
|
|
Disabled: !element.Enabled(),
|
|
|
|
})
|
2023-01-17 22:38:58 -07:00
|
|
|
element.valueDrawer.Draw (
|
|
|
|
element.core,
|
|
|
|
foreground,
|
|
|
|
offset.Sub(textBounds.Min))
|
|
|
|
|
2023-01-27 15:55:49 -07:00
|
|
|
if element.Selected() {
|
2023-01-17 22:38:58 -07:00
|
|
|
// cursor
|
|
|
|
cursorPosition := element.valueDrawer.PositionOf (
|
|
|
|
element.cursor)
|
2023-01-28 23:49:01 -07:00
|
|
|
foreground, _ := theme.ForegroundPattern(theme.PatternState { })
|
2023-01-17 22:38:58 -07:00
|
|
|
artist.Line (
|
|
|
|
element.core,
|
2023-01-28 23:49:01 -07:00
|
|
|
foreground, 1,
|
2023-01-17 22:38:58 -07:00
|
|
|
cursorPosition.Add(offset),
|
|
|
|
image.Pt (
|
|
|
|
cursorPosition.X,
|
|
|
|
cursorPosition.Y + element.valueDrawer.
|
|
|
|
LineHeight().Round()).Add(offset))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|