This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/elements/basic/textbox.go

419 lines
11 KiB
Go
Raw Normal View History

2023-02-01 23:48:16 -07:00
package basicElements
2023-01-17 22:38:58 -07:00
import "image"
2023-02-01 23:48:16 -07:00
import "git.tebibyte.media/sashakoshka/tomo/input"
2023-01-17 22:38:58 -07:00
import "git.tebibyte.media/sashakoshka/tomo/theme"
2023-02-07 22:22:40 -07:00
import "git.tebibyte.media/sashakoshka/tomo/config"
2023-01-17 22:38:58 -07:00
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/canvas"
2023-02-15 16:45:58 -07:00
import "git.tebibyte.media/sashakoshka/tomo/textdraw"
import "git.tebibyte.media/sashakoshka/tomo/textmanip"
2023-02-15 16:45:58 -07:00
import "git.tebibyte.media/sashakoshka/tomo/fixedutil"
2023-02-26 20:20:17 -07:00
import "git.tebibyte.media/sashakoshka/tomo/artist/shapes"
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-30 15:01:47 -07:00
*core.FocusableCore
2023-01-17 22:38:58 -07:00
core core.CoreControl
2023-01-30 15:01:47 -07:00
focusableControl core.FocusableCoreControl
dragging bool
dot textmanip.Dot
scroll int
2023-01-17 22:38:58 -07:00
placeholder string
text []rune
2023-02-15 16:45:58 -07:00
placeholderDrawer textdraw.Drawer
valueDrawer textdraw.Drawer
2023-02-08 12:36:14 -07:00
config config.Wrapped
theme theme.Wrapped
2023-02-07 22:22:40 -07:00
2023-02-01 23:48:16 -07:00
onKeyDown func (key input.Key, modifiers input.Modifiers) (handled bool)
onChange func ()
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-02-08 12:36:14 -07:00
element = &TextBox { }
element.theme.Case = theme.C("basic", "textBox")
2023-01-31 12:54:43 -07:00
element.Core, element.core = core.NewCore(element.handleResize)
2023-01-30 15:01:47 -07:00
element.FocusableCore,
element.focusableControl = core.NewFocusableCore (func () {
if element.core.HasImage () {
element.draw()
element.core.DamageAll()
}
})
2023-01-17 22:38:58 -07:00
element.placeholder = placeholder
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
}
2023-01-31 12:54:43 -07:00
func (element *TextBox) handleResize () {
element.scrollToCursor()
2023-01-17 22:38:58 -07:00
element.draw()
if element.onScrollBoundsChange != nil {
element.onScrollBoundsChange()
}
2023-01-17 22:38:58 -07:00
}
2023-02-01 23:48:16 -07:00
func (element *TextBox) HandleMouseDown (x, y int, button input.Button) {
2023-01-30 15:01:47 -07:00
if !element.Enabled() { return }
if !element.Focused() { element.Focus() }
if button == input.ButtonLeft {
2023-02-16 10:35:31 -07:00
runeIndex := element.atPosition(image.Pt(x, y))
element.dragging = true
if runeIndex > -1 {
element.dot = textmanip.EmptyDot(runeIndex)
element.redo()
}
}
}
func (element *TextBox) HandleMouseMove (x, y int) {
if !element.Enabled() { return }
if !element.Focused() { element.Focus() }
if element.dragging {
2023-02-16 10:35:31 -07:00
runeIndex := element.atPosition(image.Pt(x, y))
if runeIndex > -1 {
element.dot.End = runeIndex
element.redo()
}
}
}
2023-02-16 10:35:31 -07:00
func (element *TextBox) atPosition (position image.Point) int {
2023-02-27 10:48:44 -07:00
padding := element.theme.Padding(theme.PatternInput)
2023-02-16 10:35:31 -07:00
offset := element.Bounds().Min.Add (image.Pt (
2023-02-26 20:20:17 -07:00
padding[artist.SideLeft] - element.scroll,
padding[artist.SideTop]))
2023-02-16 10:35:31 -07:00
textBoundsMin := element.valueDrawer.LayoutBounds().Min
return element.valueDrawer.AtPosition (
fixedutil.Pt(position.Sub(offset).Add(textBoundsMin)))
}
func (element *TextBox) HandleMouseUp (x, y int, button input.Button) {
if button == input.ButtonLeft {
element.dragging = false
}
2023-01-17 22:38:58 -07:00
}
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-02-01 23:48:16 -07:00
func (element *TextBox) HandleKeyDown(key input.Key, modifiers input.Modifiers) {
if element.onKeyDown != nil && element.onKeyDown(key, modifiers) {
2023-01-18 09:56:14 -07:00
return
}
// TODO: text selection with shift
scrollMemory := element.scroll
altered := true
textChanged := false
2023-01-17 22:38:58 -07:00
switch {
2023-02-01 23:48:16 -07:00
case key == input.KeyBackspace:
2023-01-17 22:38:58 -07:00
if len(element.text) < 1 { break }
element.text, element.dot = textmanip.Backspace (
element.text,
element.dot,
modifiers.Control)
textChanged = true
2023-02-01 23:48:16 -07:00
case key == input.KeyDelete:
if len(element.text) < 1 { break }
element.text, element.dot = textmanip.Delete (
element.text,
element.dot,
modifiers.Control)
textChanged = true
2023-02-01 23:48:16 -07:00
case key == input.KeyLeft:
if modifiers.Shift {
element.dot = textmanip.SelectLeft (
element.text,
element.dot,
modifiers.Control)
} else {
element.dot = textmanip.MoveLeft (
element.text,
element.dot,
modifiers.Control)
}
2023-02-01 23:48:16 -07:00
case key == input.KeyRight:
if modifiers.Shift {
element.dot = textmanip.SelectRight (
element.text,
element.dot,
modifiers.Control)
} else {
element.dot = textmanip.MoveRight (
element.text,
element.dot,
modifiers.Control)
}
2023-01-17 22:38:58 -07:00
case key.Printable():
element.text, element.dot = textmanip.Type (
element.text,
element.dot,
rune(key))
textChanged = true
default:
altered = false
}
if textChanged {
element.runOnChange()
element.valueDrawer.SetText(element.text)
}
if altered {
element.scrollToCursor()
}
if (textChanged || scrollMemory != element.scroll) &&
element.onScrollBoundsChange != nil {
element.onScrollBoundsChange()
}
2023-02-07 22:22:40 -07:00
if altered {
element.redo()
2023-01-17 22:38:58 -07:00
}
}
2023-02-01 23:48:16 -07:00
func (element *TextBox) HandleKeyUp(key input.Key, modifiers input.Modifiers) { }
2023-01-17 22:38:58 -07:00
func (element *TextBox) SetPlaceholder (placeholder string) {
if element.placeholder == placeholder { return }
element.placeholder = placeholder
element.placeholderDrawer.SetText([]rune(placeholder))
2023-01-17 22:38:58 -07:00
element.updateMinimumSize()
2023-02-07 22:22:40 -07:00
element.redo()
2023-01-17 22:38:58 -07:00
}
2023-01-18 09:56:14 -07:00
func (element *TextBox) SetValue (text string) {
// if element.text == text { return }
2023-01-17 22:38:58 -07:00
element.text = []rune(text)
2023-01-18 09:56:14 -07:00
element.runOnChange()
element.valueDrawer.SetText(element.text)
if element.dot.End > element.valueDrawer.Length() {
element.dot = textmanip.EmptyDot(element.valueDrawer.Length())
2023-01-17 22:38:58 -07:00
}
element.scrollToCursor()
2023-02-07 22:22:40 -07:00
element.redo()
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-02-01 23:48:16 -07:00
callback func (key input.Key, modifiers input.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,
element.scroll + element.scrollViewportWidth(),
2023-01-18 15:52:05 -07:00
0)
}
func (element *TextBox) scrollViewportWidth () (width int) {
2023-02-27 10:48:44 -07:00
padding := element.theme.Padding(theme.PatternInput)
2023-02-26 20:20:17 -07:00
return padding.Apply(element.Bounds()).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) {
// constrain to minimum
2023-01-18 15:52:05 -07:00
element.scroll = position.X
if element.scroll < 0 { element.scroll = 0 }
// 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
2023-02-07 22:22:40 -07:00
element.redo()
if element.onScrollBoundsChange != nil {
element.onScrollBoundsChange()
}
}
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
}
func (element *TextBox) OnScrollBoundsChange (callback func ()) {
element.onScrollBoundsChange = callback
}
2023-01-18 09:56:14 -07:00
func (element *TextBox) runOnChange () {
if element.onChange != nil {
element.onChange()
}
}
func (element *TextBox) scrollToCursor () {
if !element.core.HasImage() { return }
2023-02-27 10:48:44 -07:00
padding := element.theme.Padding(theme.PatternInput)
2023-02-26 20:20:17 -07:00
bounds := padding.Apply(element.Bounds())
2023-01-31 16:39:17 -07:00
bounds = bounds.Sub(bounds.Min)
bounds.Max.X -= element.valueDrawer.Em().Round()
2023-02-15 16:45:58 -07:00
cursorPosition := fixedutil.RoundPt (
element.valueDrawer.PositionAt(element.dot.End))
cursorPosition.X -= element.scroll
maxX := bounds.Max.X
2023-01-18 14:01:31 -07:00
minX := maxX
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-02-07 22:22:40 -07:00
// SetTheme sets the element's theme.
func (element *TextBox) SetTheme (new theme.Theme) {
2023-02-08 12:36:14 -07:00
if new == element.theme.Theme { return }
element.theme.Theme = new
2023-02-07 22:22:40 -07:00
face := element.theme.FontFace (
theme.FontStyleRegular,
2023-02-08 12:36:14 -07:00
theme.FontSizeNormal)
2023-02-07 22:22:40 -07:00
element.placeholderDrawer.SetFace(face)
element.valueDrawer.SetFace(face)
element.updateMinimumSize()
element.redo()
}
// SetConfig sets the element's configuration.
func (element *TextBox) SetConfig (new config.Config) {
2023-02-08 12:36:14 -07:00
if new == element.config.Config { return }
element.config.Config = new
2023-02-07 22:22:40 -07:00
element.updateMinimumSize()
element.redo()
}
func (element *TextBox) updateMinimumSize () {
textBounds := element.placeholderDrawer.LayoutBounds()
2023-02-27 10:48:44 -07:00
padding := element.theme.Padding(theme.PatternInput)
2023-02-07 22:22:40 -07:00
element.core.SetMinimumSize (
2023-02-26 20:20:17 -07:00
padding.Horizontal() + textBounds.Dx(),
padding.Vertical() +
element.placeholderDrawer.LineHeight().Round())
2023-02-07 22:22:40 -07:00
}
func (element *TextBox) redo () {
if element.core.HasImage () {
element.draw()
element.core.DamageAll()
}
}
2023-01-17 22:38:58 -07:00
func (element *TextBox) draw () {
bounds := element.Bounds()
2023-01-17 22:38:58 -07:00
2023-02-26 20:20:17 -07:00
state := theme.State {
Disabled: !element.Enabled(),
2023-01-30 15:01:47 -07:00
Focused: element.Focused(),
2023-02-07 22:22:40 -07:00
}
2023-02-27 10:48:44 -07:00
pattern := element.theme.Pattern(theme.PatternInput, state)
padding := element.theme.Padding(theme.PatternInput)
2023-02-26 20:20:17 -07:00
innerCanvas := canvas.Cut(element.core, padding.Apply(bounds))
pattern.Draw(element.core, bounds)
offset := bounds.Min.Add (image.Point {
2023-02-26 20:48:14 -07:00
X: padding[artist.SideLeft] - element.scroll,
Y: padding[artist.SideTop],
})
2023-01-17 22:38:58 -07:00
if element.Focused() && !element.dot.Empty() {
// draw selection bounds
2023-02-26 20:20:17 -07:00
accent := element.theme.Color(theme.ColorAccent, state)
canon := element.dot.Canon()
2023-02-15 16:45:58 -07:00
foff := fixedutil.Pt(offset)
start := element.valueDrawer.PositionAt(canon.Start).Add(foff)
end := element.valueDrawer.PositionAt(canon.End).Add(foff)
end.Y += element.valueDrawer.LineHeight()
2023-02-26 20:20:17 -07:00
shapes.FillColorRectangle (
innerCanvas,
accent,
2023-02-15 16:45:58 -07:00
image.Rectangle {
fixedutil.RoundPt(start),
fixedutil.RoundPt(end),
})
}
if len(element.text) == 0 {
2023-01-17 22:38:58 -07:00
// draw placeholder
textBounds := element.placeholderDrawer.LayoutBounds()
2023-02-26 20:20:17 -07:00
foreground := element.theme.Color (
theme.ColorForeground,
theme.State { Disabled: true })
2023-01-17 22:38:58 -07:00
element.placeholderDrawer.Draw (
innerCanvas,
2023-01-17 22:38:58 -07:00
foreground,
offset.Sub(textBounds.Min))
} else {
// draw input value
textBounds := element.valueDrawer.LayoutBounds()
2023-02-26 20:20:17 -07:00
foreground := element.theme.Color(theme.ColorForeground, state)
2023-01-17 22:38:58 -07:00
element.valueDrawer.Draw (
innerCanvas,
2023-01-17 22:38:58 -07:00
foreground,
offset.Sub(textBounds.Min))
}
if element.Focused() && element.dot.Empty() {
// draw cursor
2023-02-26 20:20:17 -07:00
foreground := element.theme.Color(theme.ColorForeground, state)
2023-02-15 16:45:58 -07:00
cursorPosition := fixedutil.RoundPt (
element.valueDrawer.PositionAt(element.dot.End))
2023-02-26 20:20:17 -07:00
shapes.ColorLine (
innerCanvas,
foreground, 1,
cursorPosition.Add(offset),
image.Pt (
cursorPosition.X,
cursorPosition.Y + element.valueDrawer.
LineHeight().Round()).Add(offset))
2023-01-17 22:38:58 -07:00
}
}