2024-06-01 14:39:14 -06:00
|
|
|
package system
|
|
|
|
|
2024-06-02 11:23:03 -06:00
|
|
|
import "image"
|
|
|
|
import "image/color"
|
2024-08-10 18:24:25 -06:00
|
|
|
import "golang.org/x/image/font"
|
2024-06-01 14:39:14 -06:00
|
|
|
import "git.tebibyte.media/tomo/tomo"
|
2024-06-02 11:23:03 -06:00
|
|
|
import "golang.org/x/image/math/fixed"
|
|
|
|
import "git.tebibyte.media/tomo/typeset"
|
|
|
|
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/tomo/canvas"
|
2024-08-10 18:24:25 -06:00
|
|
|
import "git.tebibyte.media/tomo/backend/internal/util"
|
2024-06-02 11:23:03 -06:00
|
|
|
|
|
|
|
type textBox struct {
|
|
|
|
*box
|
|
|
|
|
|
|
|
contentBounds image.Rectangle
|
|
|
|
scroll image.Point
|
|
|
|
|
2024-07-25 11:01:15 -06:00
|
|
|
attrTextColor attrHierarchy[tomo.AttrTextColor]
|
|
|
|
attrDotColor attrHierarchy[tomo.AttrDotColor]
|
|
|
|
attrFace attrHierarchy[tomo.AttrFace]
|
|
|
|
attrWrap attrHierarchy[tomo.AttrWrap]
|
|
|
|
attrAlign attrHierarchy[tomo.AttrAlign]
|
|
|
|
attrOverflow attrHierarchy[tomo.AttrOverflow]
|
2024-06-02 11:23:03 -06:00
|
|
|
|
2024-07-25 11:01:15 -06:00
|
|
|
text string
|
2024-07-26 16:43:12 -06:00
|
|
|
runes []rune
|
2024-06-02 11:23:03 -06:00
|
|
|
selectable bool
|
|
|
|
selecting bool
|
|
|
|
selectStart int
|
|
|
|
dot text.Dot
|
|
|
|
|
|
|
|
drawer typeset.Drawer
|
2024-08-10 18:24:25 -06:00
|
|
|
face util.Cycler[font.Face]
|
2024-06-02 11:23:03 -06:00
|
|
|
|
|
|
|
on struct {
|
|
|
|
contentBoundsChange event.FuncBroadcaster
|
|
|
|
dotChange event.FuncBroadcaster
|
|
|
|
}
|
|
|
|
}
|
2024-06-01 14:39:14 -06:00
|
|
|
|
|
|
|
func (this *System) NewTextBox () tomo.TextBox {
|
2024-07-25 11:01:15 -06:00
|
|
|
box := &textBox { }
|
2024-06-02 11:23:03 -06:00
|
|
|
box.box = this.newBox(box)
|
2024-08-10 18:24:25 -06:00
|
|
|
box.attrTextColor.SetFallback(tomo.ATextColor(color.Black))
|
|
|
|
box.attrDotColor.SetFallback(tomo.ADotColor(color.RGBA { G: 255, B: 255, A: 255}))
|
2024-06-02 11:23:03 -06:00
|
|
|
return box
|
|
|
|
}
|
|
|
|
|
2024-07-25 11:01:15 -06:00
|
|
|
// ----- public methods ----------------------------------------------------- //
|
2024-06-02 11:23:03 -06:00
|
|
|
|
|
|
|
func (this *textBox) ContentBounds () image.Rectangle {
|
|
|
|
return this.contentBounds
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *textBox) ScrollTo (point image.Point) {
|
|
|
|
if this.scroll == point { return }
|
|
|
|
this.scroll = point
|
|
|
|
this.invalidateLayout()
|
|
|
|
}
|
|
|
|
|
2024-06-11 20:45:40 -06:00
|
|
|
func (this *textBox) RecommendedHeight (width int) int {
|
|
|
|
return this.drawer.ReccomendedHeightFor(width) + this.borderAndPaddingSum().Vertical()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *textBox) RecommendedWidth (height int) int {
|
|
|
|
// TODO maybe not the best idea?
|
2024-07-25 11:01:15 -06:00
|
|
|
return this.minimumSize().X
|
2024-06-11 20:45:40 -06:00
|
|
|
}
|
|
|
|
|
2024-06-02 11:23:03 -06:00
|
|
|
func (this *textBox) OnContentBoundsChange (callback func()) event.Cookie {
|
|
|
|
return this.on.contentBoundsChange.Connect(callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *textBox) SetText (text string) {
|
|
|
|
if this.text == text { return }
|
2024-07-26 16:43:12 -06:00
|
|
|
this.text = text
|
|
|
|
this.runes = []rune(text)
|
|
|
|
this.drawer.SetText(this.runes)
|
2024-06-02 11:23:03 -06:00
|
|
|
this.invalidateMinimum()
|
|
|
|
this.invalidateLayout()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *textBox) SetSelectable (selectable bool) {
|
|
|
|
if this.selectable == selectable { return }
|
|
|
|
this.selectable = selectable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *textBox) Select (dot text.Dot) {
|
|
|
|
if !this.selectable { return }
|
|
|
|
if this.dot == dot { return }
|
|
|
|
this.SetFocused(true)
|
|
|
|
this.dot = dot
|
|
|
|
this.scrollToDot()
|
|
|
|
this.on.dotChange.Broadcast()
|
|
|
|
this.invalidateDraw()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *textBox) Dot () text.Dot {
|
|
|
|
return this.dot
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *textBox) OnDotChange (callback func ()) event.Cookie {
|
|
|
|
return this.on.dotChange.Connect(callback)
|
|
|
|
}
|
|
|
|
|
2024-07-25 11:01:15 -06:00
|
|
|
// ----- private methods ---------------------------------------------------- //
|
2024-06-02 11:23:03 -06:00
|
|
|
|
|
|
|
func (this *textBox) Draw (can canvas.Canvas) {
|
|
|
|
if can == nil { return }
|
2024-07-25 11:01:15 -06:00
|
|
|
|
2024-08-10 18:24:25 -06:00
|
|
|
texture := this.attrTexture.Value().Texture
|
|
|
|
col := this.attrColor.Value().Color
|
2024-07-25 11:01:15 -06:00
|
|
|
|
2024-06-02 11:23:03 -06:00
|
|
|
this.drawBorders(can)
|
2024-07-25 22:27:32 -06:00
|
|
|
|
2024-06-02 11:23:03 -06:00
|
|
|
pen := can.Pen()
|
2024-07-25 11:01:15 -06:00
|
|
|
pen.Fill(col)
|
|
|
|
pen.Texture(texture)
|
2024-06-02 11:23:03 -06:00
|
|
|
if this.transparent() && this.parent != nil {
|
|
|
|
this.parent.drawBackgroundPart(can)
|
|
|
|
}
|
|
|
|
pen.Rectangle(can.Bounds())
|
|
|
|
|
|
|
|
if this.selectable && this.Focused() {
|
|
|
|
this.drawDot(can)
|
|
|
|
}
|
|
|
|
|
2024-08-10 18:24:25 -06:00
|
|
|
if this.face.Value() != nil {
|
2024-07-25 11:01:15 -06:00
|
|
|
textColor := this.attrTextColor.Value().Color
|
|
|
|
this.drawer.Draw(can, textColor, this.textOffset())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *textBox) setAttr (attr tomo.Attr, user bool) {
|
|
|
|
switch attr := attr.(type) {
|
|
|
|
case tomo.AttrTextColor:
|
|
|
|
if this.attrTextColor.Set(attr, user) && !this.dot.Empty() {
|
|
|
|
this.invalidateDraw()
|
|
|
|
}
|
|
|
|
|
|
|
|
case tomo.AttrDotColor:
|
|
|
|
if this.attrDotColor.Set(attr, user) && !this.dot.Empty() {
|
|
|
|
this.invalidateDraw()
|
|
|
|
}
|
|
|
|
|
|
|
|
case tomo.AttrFace:
|
|
|
|
if this.attrFace.Set(attr, user) {
|
2024-08-10 18:24:25 -06:00
|
|
|
this.handleFaceChange()
|
2024-07-25 11:01:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
case tomo.AttrWrap:
|
|
|
|
if this.attrWrap.Set(attr, user) {
|
2024-08-10 18:24:25 -06:00
|
|
|
this.drawer.SetWrap(bool(this.attrWrap.Value()))
|
2024-07-25 11:01:15 -06:00
|
|
|
this.invalidateMinimum()
|
|
|
|
this.invalidateLayout()
|
|
|
|
}
|
|
|
|
|
|
|
|
case tomo.AttrAlign:
|
|
|
|
if this.attrAlign.Set(attr, user) {
|
2024-08-10 18:24:25 -06:00
|
|
|
align := this.attrAlign.Value()
|
2024-07-25 22:27:32 -06:00
|
|
|
this.drawer.SetAlign (
|
2024-08-10 18:24:25 -06:00
|
|
|
typeset.Align(align.X),
|
|
|
|
typeset.Align(align.Y))
|
2024-07-25 11:01:15 -06:00
|
|
|
this.invalidateDraw()
|
|
|
|
}
|
|
|
|
|
|
|
|
case tomo.AttrOverflow:
|
|
|
|
if this.attrOverflow.Set(attr, user) {
|
2024-07-27 11:47:22 -06:00
|
|
|
this.invalidateMinimum()
|
2024-07-25 11:01:15 -06:00
|
|
|
this.invalidateLayout()
|
|
|
|
}
|
|
|
|
|
|
|
|
default: this.box.setAttr(attr, user)
|
|
|
|
}
|
2024-06-02 11:23:03 -06:00
|
|
|
}
|
|
|
|
|
2024-08-10 18:24:25 -06:00
|
|
|
func (this *textBox) unsetAttr (kind tomo.AttrKind, user bool) {
|
|
|
|
switch kind {
|
|
|
|
case tomo.AttrKindTextColor:
|
|
|
|
if this.attrTextColor.Unset(user) && !this.dot.Empty() {
|
|
|
|
this.invalidateDraw()
|
|
|
|
}
|
|
|
|
|
|
|
|
case tomo.AttrKindDotColor:
|
|
|
|
if this.attrDotColor.Unset(user) && !this.dot.Empty() {
|
|
|
|
this.invalidateDraw()
|
|
|
|
}
|
|
|
|
|
|
|
|
case tomo.AttrKindFace:
|
|
|
|
if this.attrFace.Unset(user) {
|
|
|
|
this.handleFaceChange()
|
|
|
|
}
|
|
|
|
|
|
|
|
case tomo.AttrKindWrap:
|
|
|
|
if this.attrWrap.Unset(user) {
|
|
|
|
this.drawer.SetWrap(bool(this.attrWrap.Value()))
|
|
|
|
this.invalidateMinimum()
|
|
|
|
this.invalidateLayout()
|
|
|
|
}
|
|
|
|
|
|
|
|
case tomo.AttrKindAlign:
|
|
|
|
if this.attrAlign.Unset(user) {
|
|
|
|
align := this.attrAlign.Value()
|
|
|
|
this.drawer.SetAlign (
|
|
|
|
typeset.Align(align.X),
|
|
|
|
typeset.Align(align.Y))
|
|
|
|
this.invalidateDraw()
|
|
|
|
}
|
|
|
|
|
|
|
|
case tomo.AttrKindOverflow:
|
|
|
|
if this.attrOverflow.Unset(user) {
|
|
|
|
this.invalidateMinimum()
|
|
|
|
this.invalidateLayout()
|
|
|
|
}
|
|
|
|
|
|
|
|
default: this.box.unsetAttr(kind, user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-02 11:23:03 -06:00
|
|
|
func roundPt (point fixed.Point26_6) image.Point {
|
|
|
|
return image.Pt(point.X.Round(), point.Y.Round())
|
|
|
|
}
|
|
|
|
|
|
|
|
func fixPt (point image.Point) fixed.Point26_6 {
|
|
|
|
return fixed.P(point.X, point.Y)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *textBox) drawDot (can canvas.Canvas) {
|
2024-08-10 18:24:25 -06:00
|
|
|
face := this.face.Value()
|
|
|
|
if face == nil { return }
|
2024-06-02 11:23:03 -06:00
|
|
|
|
2024-07-25 11:01:15 -06:00
|
|
|
textColor := this.attrTextColor.Value().Color
|
|
|
|
dotColor := this.attrDotColor.Value().Color
|
|
|
|
|
2024-06-02 11:23:03 -06:00
|
|
|
pen := can.Pen()
|
|
|
|
|
|
|
|
bounds := this.InnerBounds()
|
2024-07-25 11:01:15 -06:00
|
|
|
metrics := face.Metrics()
|
2024-06-02 11:23:03 -06:00
|
|
|
dot := this.dot.Canon()
|
|
|
|
start := this.drawer.PositionAt(dot.Start).Add(fixPt(this.textOffset()))
|
|
|
|
end := this.drawer.PositionAt(dot.End ).Add(fixPt(this.textOffset()))
|
|
|
|
height := this.drawer.LineHeight().Round()
|
|
|
|
ascent := fixed.Point26_6 { Y: metrics.Descent }
|
|
|
|
descent := fixed.Point26_6 { Y: metrics.Ascent }
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case dot.Empty():
|
2024-07-26 15:34:33 -06:00
|
|
|
pen.Stroke(textColor)
|
2024-06-02 11:23:03 -06:00
|
|
|
pen.StrokeWeight(1)
|
|
|
|
pen.Path(roundPt(start.Add(ascent)), roundPt(start.Sub(descent)))
|
|
|
|
|
|
|
|
case start.Y == end.Y:
|
2024-07-25 11:01:15 -06:00
|
|
|
pen.Fill(dotColor)
|
2024-06-02 11:23:03 -06:00
|
|
|
pen.StrokeWeight(0)
|
|
|
|
pen.Rectangle(image.Rectangle {
|
|
|
|
Min: roundPt(start.Add(ascent)),
|
|
|
|
Max: roundPt(end.Sub(descent)),
|
|
|
|
})
|
|
|
|
|
|
|
|
default:
|
2024-07-25 11:01:15 -06:00
|
|
|
pen.Fill(dotColor)
|
2024-06-02 11:23:03 -06:00
|
|
|
pen.StrokeWeight(0)
|
|
|
|
|
|
|
|
rect := image.Rectangle {
|
|
|
|
Min: roundPt(start.Add(ascent)),
|
|
|
|
Max: roundPt(start.Sub(descent)),
|
|
|
|
}
|
|
|
|
rect.Max.X = bounds.Max.X
|
|
|
|
pen.Rectangle(rect)
|
|
|
|
if end.Y - start.Y > fixed.I(height) {
|
|
|
|
rect.Min.X = bounds.Min.X
|
|
|
|
rect.Min.Y = roundPt(start.Sub(descent)).Y + height
|
|
|
|
rect.Max.X = bounds.Max.X
|
|
|
|
rect.Max.Y = roundPt(end.Add(ascent)).Y - height
|
|
|
|
pen.Rectangle(rect)
|
|
|
|
}
|
|
|
|
rect = image.Rectangle {
|
|
|
|
Min: roundPt(end.Add(ascent)),
|
|
|
|
Max: roundPt(end.Sub(descent)),
|
|
|
|
}
|
|
|
|
rect.Min.X = bounds.Min.X
|
|
|
|
pen.Rectangle(rect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *textBox) textOffset () image.Point {
|
|
|
|
return this.InnerBounds().Min.
|
|
|
|
Add(this.scroll).
|
|
|
|
Sub(this.drawer.LayoutBoundsSpace().Min)
|
|
|
|
}
|
|
|
|
|
2024-07-26 19:17:30 -06:00
|
|
|
func (this *textBox) handleFocusEnter () {
|
|
|
|
this.invalidateDraw()
|
|
|
|
this.box.handleFocusEnter()
|
|
|
|
}
|
|
|
|
|
2024-06-02 11:23:03 -06:00
|
|
|
func (this *textBox) handleFocusLeave () {
|
|
|
|
this.invalidateDraw()
|
|
|
|
this.box.handleFocusLeave()
|
|
|
|
}
|
|
|
|
|
2024-07-25 11:01:15 -06:00
|
|
|
func (this *textBox) handleMouseDown (button input.Button) bool {
|
2024-07-26 19:17:30 -06:00
|
|
|
if this.mouseButtonCanDrag(button) {
|
2024-06-02 11:23:03 -06:00
|
|
|
index := this.runeUnderMouse()
|
|
|
|
this.selectStart = index
|
|
|
|
this.selecting = true
|
|
|
|
this.Select(text.Dot { Start: this.selectStart, End: index })
|
|
|
|
}
|
2024-07-25 11:01:15 -06:00
|
|
|
return this.box.handleMouseDown(button)
|
2024-06-02 11:23:03 -06:00
|
|
|
}
|
|
|
|
|
2024-07-25 11:01:15 -06:00
|
|
|
func (this *textBox) handleMouseUp (button input.Button) bool {
|
2024-07-26 19:17:30 -06:00
|
|
|
if this.mouseButtonCanDrag(button) && this.selecting {
|
2024-06-02 11:23:03 -06:00
|
|
|
index := this.runeUnderMouse()
|
|
|
|
this.selecting = false
|
|
|
|
this.Select(text.Dot { Start: this.selectStart, End: index })
|
|
|
|
}
|
2024-07-25 11:01:15 -06:00
|
|
|
return this.box.handleMouseUp(button)
|
2024-06-02 11:23:03 -06:00
|
|
|
}
|
|
|
|
|
2024-07-26 19:17:30 -06:00
|
|
|
func (this *textBox) mouseButtonCanDrag (button input.Button) bool {
|
|
|
|
return button == input.ButtonLeft ||
|
|
|
|
button == input.ButtonMiddle ||
|
|
|
|
button == input.ButtonRight
|
|
|
|
}
|
|
|
|
|
2024-07-25 11:01:15 -06:00
|
|
|
func (this *textBox) handleMouseMove () bool {
|
2024-06-02 11:23:03 -06:00
|
|
|
if this.selecting {
|
|
|
|
index := this.runeUnderMouse()
|
|
|
|
this.Select(text.Dot { Start: this.selectStart, End: index })
|
|
|
|
}
|
2024-07-25 11:01:15 -06:00
|
|
|
return this.box.handleMouseMove()
|
2024-06-02 11:23:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *textBox) runeUnderMouse () int {
|
2024-07-25 11:01:15 -06:00
|
|
|
window := this.Window()
|
|
|
|
if window == nil { return 0 }
|
|
|
|
position := window.MousePosition().Sub(this.textOffset())
|
2024-06-02 11:23:03 -06:00
|
|
|
return this.drawer.AtPosition(fixPt(position))
|
|
|
|
}
|
|
|
|
|
2024-07-26 16:43:12 -06:00
|
|
|
func (this *textBox) handleKeyDown (key input.Key, numberPad bool) bool {
|
|
|
|
if this.box.handleKeyDown(key, numberPad) { return true }
|
2024-07-26 18:48:44 -06:00
|
|
|
if !this.selectable { return false }
|
2024-07-26 16:43:12 -06:00
|
|
|
|
|
|
|
// because fuck you thats why!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
modifiers := this.Window().Modifiers()
|
|
|
|
dot := this.Dot()
|
|
|
|
sel := modifiers.Shift
|
|
|
|
word := modifiers.Control
|
|
|
|
switch {
|
|
|
|
case key == input.KeyHome || (modifiers.Alt && key == input.KeyLeft):
|
|
|
|
dot.End = 0
|
|
|
|
if !sel { dot.Start = dot.End }
|
|
|
|
this.Select(dot)
|
|
|
|
return true
|
|
|
|
case key == input.KeyEnd || (modifiers.Alt && key == input.KeyRight):
|
|
|
|
dot.End = len(this.text)
|
|
|
|
if !sel { dot.Start = dot.End }
|
|
|
|
this.Select(dot)
|
|
|
|
return true
|
|
|
|
case key == input.KeyLeft:
|
|
|
|
if sel {
|
|
|
|
this.Select(text.SelectLeft(this.runes, dot, word))
|
|
|
|
} else {
|
|
|
|
this.Select(text.MoveLeft(this.runes, dot, word))
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
case key == input.KeyRight:
|
|
|
|
if sel {
|
|
|
|
this.Select(text.SelectRight(this.runes, dot, word))
|
|
|
|
} else {
|
|
|
|
this.Select(text.MoveRight(this.runes, dot, word))
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
case key == input.Key('a') && modifiers.Control:
|
|
|
|
dot.Start = 0
|
|
|
|
dot.End = len(this.text)
|
|
|
|
this.Select(dot)
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *textBox) handleKeyUp (key input.Key, numberPad bool) bool {
|
|
|
|
if this.box.handleKeyUp(key, numberPad) { return true }
|
2024-07-26 18:48:44 -06:00
|
|
|
if !this.selectable { return false }
|
2024-07-26 16:43:12 -06:00
|
|
|
|
|
|
|
modifiers := this.Window().Modifiers()
|
|
|
|
switch {
|
|
|
|
case key == input.KeyHome || (modifiers.Alt && key == input.KeyLeft):
|
|
|
|
return true
|
|
|
|
case key == input.KeyEnd || (modifiers.Alt && key == input.KeyRight):
|
|
|
|
return true
|
|
|
|
case key == input.KeyLeft:
|
|
|
|
return true
|
|
|
|
case key == input.KeyRight:
|
|
|
|
return true
|
|
|
|
case key == input.Key('a') && modifiers.Control:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-02 11:23:03 -06:00
|
|
|
func (this *textBox) normalizedLayoutBoundsSpace () image.Rectangle {
|
|
|
|
bounds := this.drawer.LayoutBoundsSpace()
|
|
|
|
return bounds.Sub(bounds.Min)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *textBox) contentMinimum () image.Point {
|
|
|
|
minimum := this.drawer.MinimumSize()
|
|
|
|
|
2024-07-25 11:01:15 -06:00
|
|
|
if this.attrOverflow.Value().X || bool(this.attrWrap.Value()) {
|
2024-06-02 11:23:03 -06:00
|
|
|
minimum.X = this.drawer.Em().Round()
|
|
|
|
}
|
2024-07-25 11:01:15 -06:00
|
|
|
if this.attrOverflow.Value().Y {
|
2024-06-02 11:23:03 -06:00
|
|
|
minimum.Y = this.drawer.LineHeight().Round()
|
|
|
|
}
|
|
|
|
|
|
|
|
return minimum.Add(this.box.contentMinimum())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *textBox) doLayout () {
|
|
|
|
this.box.doLayout()
|
|
|
|
previousContentBounds := this.contentBounds
|
|
|
|
|
|
|
|
innerBounds := this.InnerBounds()
|
|
|
|
this.drawer.SetWidth(innerBounds.Dx())
|
|
|
|
this.drawer.SetHeight(innerBounds.Dy())
|
|
|
|
|
|
|
|
this.contentBounds = this.normalizedLayoutBoundsSpace()
|
|
|
|
this.constrainScroll()
|
|
|
|
this.contentBounds = this.contentBounds.Add(this.scroll)
|
|
|
|
// println(this.InnerBounds().String(), this.contentBounds.String())
|
|
|
|
|
|
|
|
if previousContentBounds != this.contentBounds {
|
|
|
|
this.on.contentBoundsChange.Broadcast()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *textBox) constrainScroll () {
|
|
|
|
innerBounds := this.InnerBounds()
|
|
|
|
width := this.contentBounds.Dx()
|
|
|
|
height := this.contentBounds.Dy()
|
|
|
|
|
|
|
|
// X
|
|
|
|
if width <= innerBounds.Dx() {
|
|
|
|
this.scroll.X = 0
|
|
|
|
} else if this.scroll.X > 0 {
|
|
|
|
this.scroll.X = 0
|
|
|
|
} else if this.scroll.X < innerBounds.Dx() - width {
|
|
|
|
this.scroll.X = innerBounds.Dx() - width
|
|
|
|
}
|
|
|
|
|
|
|
|
// Y
|
|
|
|
if height <= innerBounds.Dy() {
|
|
|
|
this.scroll.Y = 0
|
|
|
|
} else if this.scroll.Y > 0 {
|
|
|
|
this.scroll.Y = 0
|
|
|
|
} else if this.scroll.Y < innerBounds.Dy() - height {
|
|
|
|
this.scroll.Y = innerBounds.Dy() - height
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *textBox) scrollToDot () {
|
|
|
|
dot := roundPt(this.drawer.PositionAt(this.dot.End)).Add(this.textOffset())
|
|
|
|
innerBounds := this.InnerBounds()
|
|
|
|
scroll := this.scroll
|
|
|
|
em := this.drawer.Em().Round()
|
|
|
|
lineHeight := this.drawer.LineHeight().Round()
|
|
|
|
|
|
|
|
// X
|
|
|
|
if dot.X < innerBounds.Min.X + em {
|
|
|
|
scroll.X += innerBounds.Min.X - dot.X + em
|
|
|
|
} else if dot.X > innerBounds.Max.X - em {
|
|
|
|
scroll.X -= dot.X - innerBounds.Max.X + em
|
|
|
|
}
|
2024-06-01 14:39:14 -06:00
|
|
|
|
2024-06-02 11:23:03 -06:00
|
|
|
// Y
|
|
|
|
if dot.Y < innerBounds.Min.Y + lineHeight {
|
|
|
|
scroll.Y += innerBounds.Min.Y - dot.Y + lineHeight
|
|
|
|
} else if dot.Y > innerBounds.Max.Y - lineHeight {
|
|
|
|
scroll.Y -= dot.Y - innerBounds.Max.Y + lineHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
this.ScrollTo(scroll)
|
2024-06-01 14:39:14 -06:00
|
|
|
}
|
2024-08-10 18:24:25 -06:00
|
|
|
|
|
|
|
func (this *textBox) handleFaceChange () {
|
|
|
|
hierarchy := this.getHierarchy()
|
2024-08-11 09:55:13 -06:00
|
|
|
if hierarchy == nil { return }
|
2024-08-10 18:24:25 -06:00
|
|
|
faceSet := hierarchy.getFaceSet()
|
2024-08-11 09:55:13 -06:00
|
|
|
if faceSet == nil { return }
|
2024-08-10 18:24:25 -06:00
|
|
|
|
|
|
|
face := faceSet.Face(tomo.Face(this.attrFace.Value()))
|
|
|
|
this.face.Set(face, face)
|
|
|
|
this.drawer.SetFace(face)
|
|
|
|
this.invalidateMinimum()
|
|
|
|
this.invalidateLayout()
|
|
|
|
}
|
2024-08-11 09:55:13 -06:00
|
|
|
|
|
|
|
func (this *textBox) recursiveReApply () {
|
|
|
|
this.box.recursiveReApply()
|
|
|
|
|
|
|
|
hierarchy := this.getHierarchy()
|
|
|
|
if hierarchy == nil { return }
|
|
|
|
|
|
|
|
previousFace := this.face.Value()
|
|
|
|
if previousFace == nil {
|
|
|
|
faceSet := hierarchy.getFaceSet()
|
|
|
|
if faceSet == nil { return }
|
|
|
|
face := faceSet.Face(tomo.Face(this.attrFace.Value()))
|
|
|
|
if face != previousFace {
|
|
|
|
this.face.Set(face, face)
|
|
|
|
this.drawer.SetFace(face)
|
|
|
|
this.invalidateMinimum()
|
|
|
|
this.invalidateLayout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|