From 3e8523f37a03c3ae6ff74d5c97ef016b8fdfc2f6 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Fri, 20 Jan 2023 23:40:59 -0500 Subject: [PATCH] TextBox contstrains scroll positions to max --- elements/basic/textbox.go | 13 +++++++++++-- elements/testing/artist.go | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/elements/basic/textbox.go b/elements/basic/textbox.go index f3d3854..4323a85 100644 --- a/elements/basic/textbox.go +++ b/elements/basic/textbox.go @@ -249,16 +249,25 @@ func (element *TextBox) ScrollViewportBounds () (bounds image.Rectangle) { return image.Rect ( element.scroll, 0, - element.scroll + element.Bounds().Inset(theme.Padding()).Dx(), + element.scroll + element.scrollViewportWidth(), 0) } +func (element *TextBox) scrollViewportWidth () (width int) { + return element.Bounds().Inset(theme.Padding()).Dx() +} + // ScrollTo scrolls the viewport to the specified point relative to // ScrollBounds. func (element *TextBox) ScrollTo (position image.Point) { + // constrain to minimum element.scroll = position.X if element.scroll < 0 { element.scroll = 0 } - // TODO: constrain to max + + // constrain to maximum + contentBounds := element.ScrollContentBounds() + maxPosition := contentBounds.Max.X - element.scrollViewportWidth() + if element.scroll > maxPosition { element.scroll = maxPosition } if element.core.HasImage () { element.draw() diff --git a/elements/testing/artist.go b/elements/testing/artist.go index 84c88b4..220a236 100644 --- a/elements/testing/artist.go +++ b/elements/testing/artist.go @@ -131,7 +131,7 @@ func (element *Artist) Resize (width, height int) { } func (element *Artist) lines (weight int, bounds image.Rectangle) { - bounds = bounds.Inset(8) + bounds = bounds.Inset(4) c := uhex(0xFFFFFFFF) artist.Line(element, c, weight, bounds.Min, bounds.Max) artist.Line (