From 1c286139814c339b812a6995c1690312618d78f8 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 9 Mar 2023 23:27:08 -0500 Subject: [PATCH] The scroll bar is better --- elements/basic/scrollbar.go | 10 ++++++--- examples/scroll/main.go | 41 ++++++++++++++++++++++++------------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/elements/basic/scrollbar.go b/elements/basic/scrollbar.go index a820aaa..8f75938 100644 --- a/elements/basic/scrollbar.go +++ b/elements/basic/scrollbar.go @@ -70,7 +70,9 @@ func (element *ScrollBar) HandleMouseDown (x, y int, button input.Button) { // the mouse is pressed down within the bar's handle element.dragging = true element.drawAndPush() - element.dragOffset = point + element.dragOffset = + point.Sub(element.bar.Min). + Add(element.Bounds().Min) element.dragTo(point) } else { // the mouse is pressed down within the bar's gutter @@ -178,9 +180,11 @@ func (element *ScrollBar) isAfterHandle (point image.Point) bool { func (element *ScrollBar) fallbackDragOffset () image.Point { if element.vertical { - return element.bar.Min.Add(image.Pt(0, element.bar.Dy() / 2)) + return element.Bounds().Min. + Add(image.Pt(0, element.bar.Dy() / 2)) } else { - return element.bar.Min.Add(image.Pt(element.bar.Dx() / 2, 0)) + return element.Bounds().Min. + Add(image.Pt(element.bar.Dx() / 2, 0)) } } diff --git a/examples/scroll/main.go b/examples/scroll/main.go index e17c7d2..ae97064 100644 --- a/examples/scroll/main.go +++ b/examples/scroll/main.go @@ -24,17 +24,27 @@ func run () { Gap: true, }) list := basicElements.NewList ( - basicElements.NewListEntry("something", nil), - basicElements.NewListEntry("something", nil), - basicElements.NewListEntry("something", nil), - basicElements.NewListEntry("something", nil), - basicElements.NewListEntry("something", nil), - basicElements.NewListEntry("something", nil), - basicElements.NewListEntry("something", nil), - basicElements.NewListEntry("something", nil), - basicElements.NewListEntry("something", nil), - basicElements.NewListEntry("something", nil), - basicElements.NewListEntry("something", nil)) + basicElements.NewListEntry("This is list item 0", nil), + basicElements.NewListEntry("This is list item 1", nil), + basicElements.NewListEntry("This is list item 2", nil), + basicElements.NewListEntry("This is list item 3", nil), + basicElements.NewListEntry("This is list item 4", nil), + basicElements.NewListEntry("This is list item 5", nil), + basicElements.NewListEntry("This is list item 6", nil), + basicElements.NewListEntry("This is list item 7", nil), + basicElements.NewListEntry("This is list item 8", nil), + basicElements.NewListEntry("This is list item 9", nil), + basicElements.NewListEntry("This is list item 10", nil), + basicElements.NewListEntry("This is list item 11", nil), + basicElements.NewListEntry("This is list item 12", nil), + basicElements.NewListEntry("This is list item 13", nil), + basicElements.NewListEntry("This is list item 14", nil), + basicElements.NewListEntry("This is list item 15", nil), + basicElements.NewListEntry("This is list item 16", nil), + basicElements.NewListEntry("This is list item 17", nil), + basicElements.NewListEntry("This is list item 18", nil), + basicElements.NewListEntry("This is list item 19", nil), + basicElements.NewListEntry("This is list item 20", nil)) list.Collapse(0, 32) scrollBar := basicElements.NewScrollBar(true) list.OnScrollBoundsChange (func () { @@ -46,12 +56,15 @@ func run () { list.ScrollTo(viewport) }) - container.Adopt(basicElements.NewLabel("look at this non sense", false), false) + container.Adopt(basicElements.NewLabel("A ScrollContainer:", false), false) scrollContainer.Adopt(textBox) container.Adopt(scrollContainer, false) - container.Adopt(basicElements.NewLabel("what does that scrollbar do?", false), false) disconnectedContainer.Adopt(list, false) - disconnectedContainer.Adopt(basicElements.NewSpacer(true), true) + disconnectedContainer.Adopt (basicElements.NewLabel ( + "Notice how the scroll bar to the right can be used to " + + "control the list, despite not even touching it. It is " + + "indeed a thing you can do. It is also terrible UI design so " + + "don't do it.", true), true) disconnectedContainer.Adopt(scrollBar, false) container.Adopt(disconnectedContainer, true)