Scrollbars can be dragged

This commit is contained in:
Sasha Koshka 2023-09-14 21:40:06 -04:00
parent 15d7031e22
commit c9e556f47b
2 changed files with 27 additions and 15 deletions

View File

@ -67,6 +67,7 @@ func (this *Scrollbar) Link (box tomo.ContentBox) event.Cookie {
func (this *Scrollbar) handleLinkedContentBoundsChange () {
if this.layout.linked == nil { return }
previousValue := this.layout.value
trackLength := this.layout.contentLength() - this.layout.viewportLength()
if trackLength == 0 {
this.layout.value = 0
@ -74,13 +75,29 @@ func (this *Scrollbar) handleLinkedContentBoundsChange () {
this.layout.value = this.layout.contentPos() / trackLength
}
this.SetLayout(this.layout)
if this.layout.value != previousValue {
this.on.valueChange.Broadcast()
}
}
// SetValue sets the value of the scrollbar between 0 and 1, where 0 is scrolled
// all the way to the left/top, and 1 is scrolled all the way to the
// right/bottom.
func (this *Scrollbar) SetValue (value float64) {
// TODO
if this.layout.linked == nil { return }
if value > 1 { value = 1 }
if value < 0 { value = 0 }
trackLength := this.layout.contentLength() - this.layout.viewportLength()
position := trackLength * value
point := this.layout.linked.ContentBounds().Min
if this.layout.vertical {
point.Y = int(position)
} else {
point.X = int(position)
}
this.layout.linked.ScrollTo(point)
}
// Value returns the value of the scrollbar between 0 and 1 where 0 is scrolled
@ -127,14 +144,12 @@ func (this *Scrollbar) handleKeyDown (key input.Key, numpad bool) {
func (this *Scrollbar) handleMouseDown (button input.Button) {
pointer := this.MousePosition()
handle := this.handle.Bounds()
var above, within bool
if pointer.In(handle) {
within = true
} else if this.layout.vertical {
above = pointer.Y < handle.Min.Y
within := pointer.In(handle)
var above bool; if this.layout.vertical {
above = pointer.Y < handle.Min.Y + handle.Dy() / 2
} else {
above = pointer.X < handle.Min.X
above = pointer.X < handle.Min.X + handle.Dx() / 2
}
switch button {
@ -189,7 +204,6 @@ func (this *Scrollbar) drag () {
if this.layout.vertical {
this.SetValue (
1 -
float64(pointer.Y) /
float64(gutter.Dy() - handle.Dy()))
} else {

View File

@ -115,14 +115,12 @@ func (this *Slider) handleKeyDown (key input.Key, numpad bool) {
func (this *Slider) handleMouseDown (button input.Button) {
pointer := this.MousePosition()
handle := this.handle.Bounds()
var above, within bool
if pointer.In(handle) {
within = true
} else if this.layout.vertical {
above = pointer.Y < handle.Min.Y
within := pointer.In(handle)
var above bool; if this.layout.vertical {
above = pointer.Y < handle.Min.Y + handle.Dy() / 2
} else {
above = pointer.X < handle.Min.X
above = pointer.X < handle.Min.X + handle.Dx() / 2
}
switch button {