Text boxes now have properly constrained scrolling/autoscrolling

This commit is contained in:
Sasha Koshka 2023-09-09 20:22:10 -04:00
parent 2ecfafa469
commit 7fc2b1cd87
1 changed files with 58 additions and 10 deletions

View File

@ -59,7 +59,7 @@ func (this *textBox) ContentBounds () image.Rectangle {
}
func (this *textBox) ScrollTo (point image.Point) {
// TODO: constrain scroll
if this.scroll == point { return }
this.scroll = point
this.invalidateLayout()
}
@ -115,6 +115,7 @@ func (this *textBox) Select (dot text.Dot) {
if this.dot == dot { return }
this.SetFocused(true)
this.dot = dot
this.scrollToDot()
this.on.dotChange.Broadcast()
this.invalidateDraw()
}
@ -275,16 +276,13 @@ func (this *textBox) normalizedLayoutBoundsSpace () image.Rectangle {
}
func (this *textBox) contentMinimum () image.Point {
minimum := image.Pt (
this.drawer.Em().Round(),
this.drawer.LineHeight().Round())
minimum := this.drawer.MinimumSize()
textSize := this.drawer.MinimumSize()
if !this.hOverflow && !this.wrap {
minimum.X = textSize.X
if this.hOverflow || this.wrap {
minimum.X = this.drawer.Em().Round()
}
if !this.vOverflow {
minimum.Y = textSize.Y
if this.vOverflow {
minimum.Y = this.drawer.LineHeight().Round()
}
return minimum.Add(this.box.contentMinimum())
@ -298,9 +296,59 @@ func (this *textBox) doLayout () {
this.drawer.SetMaxWidth(innerBounds.Dx())
this.drawer.SetMaxHeight(innerBounds.Dy())
this.contentBounds = this.normalizedLayoutBoundsSpace().Add(this.scroll)
this.contentBounds = this.normalizedLayoutBoundsSpace()
this.constrainScroll()
this.contentBounds = this.contentBounds.Add(this.scroll)
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 > width - innerBounds.Dx() {
this.scroll.X = width - innerBounds.Dx()
}
// Y
if height <= innerBounds.Dy() {
this.scroll.Y = 0
} else if this.scroll.Y < 0 {
this.scroll.Y = 0
} else if this.scroll.Y > height - innerBounds.Dy() {
this.scroll.Y = height - innerBounds.Dy()
}
}
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
}
// 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)
}