2023-09-14 12:48:08 -06:00
|
|
|
package objects
|
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "git.tebibyte.media/tomo/tomo"
|
|
|
|
import "git.tebibyte.media/tomo/tomo/input"
|
|
|
|
import "git.tebibyte.media/tomo/tomo/event"
|
|
|
|
|
2023-09-14 19:49:45 -06:00
|
|
|
// Scrollbar is a special type of slider that can control the scroll of any
|
|
|
|
// overflowing ContainerBox.
|
2023-09-14 12:48:08 -06:00
|
|
|
type Scrollbar struct {
|
|
|
|
tomo.ContainerBox
|
|
|
|
handle *SliderHandle
|
|
|
|
layout scrollbarLayout
|
|
|
|
dragging bool
|
|
|
|
dragOffset image.Point
|
|
|
|
|
|
|
|
linkCookie event.Cookie
|
|
|
|
|
|
|
|
on struct {
|
|
|
|
valueChange event.FuncBroadcaster
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newScrollbar (orient string) *Scrollbar {
|
|
|
|
this := &Scrollbar {
|
|
|
|
ContainerBox: tomo.NewContainerBox(),
|
|
|
|
handle: &SliderHandle {
|
|
|
|
Box: tomo.NewBox(),
|
|
|
|
},
|
|
|
|
layout: scrollbarLayout {
|
|
|
|
vertical: orient == "vertical",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
this.Add(this.handle)
|
|
|
|
this.SetFocusable(true)
|
2023-09-14 15:03:19 -06:00
|
|
|
|
2023-09-14 12:48:08 -06:00
|
|
|
this.CaptureDND(true)
|
|
|
|
this.CaptureMouse(true)
|
|
|
|
this.CaptureScroll(true)
|
|
|
|
this.CaptureKeyboard(true)
|
2023-09-14 15:03:19 -06:00
|
|
|
|
2023-09-14 12:48:08 -06:00
|
|
|
this.OnKeyDown(this.handleKeyDown)
|
|
|
|
this.OnMouseDown(this.handleMouseDown)
|
|
|
|
this.OnMouseUp(this.handleMouseUp)
|
|
|
|
this.OnMouseMove(this.handleMouseMove)
|
2023-09-14 19:04:59 -06:00
|
|
|
this.OnScroll(this.handleScroll)
|
2024-06-03 19:13:18 -06:00
|
|
|
|
|
|
|
this.handle.SetRole(tomo.R("objects", "SliderHandle", orient))
|
|
|
|
this.SetRole(tomo.R("objects", "Slider", orient))
|
2023-09-14 12:48:08 -06:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2023-09-14 19:49:45 -06:00
|
|
|
// NewVerticalScrollbar creates a new vertical scrollbar.
|
2023-09-14 12:48:08 -06:00
|
|
|
func NewVerticalScrollbar () *Scrollbar {
|
|
|
|
return newScrollbar("vertical")
|
|
|
|
}
|
|
|
|
|
2023-09-14 19:49:45 -06:00
|
|
|
// NewHorizontalScrollbar creates a new horizontal scrollbar.
|
2023-09-14 12:48:08 -06:00
|
|
|
func NewHorizontalScrollbar () *Scrollbar {
|
|
|
|
return newScrollbar("horizontal")
|
|
|
|
}
|
|
|
|
|
2024-05-27 14:28:48 -06:00
|
|
|
// Link assigns this scrollbar to a ContentObject. Closing the returned cookie
|
|
|
|
// will unlink it.
|
|
|
|
func (this *Scrollbar) Link (box tomo.ContentObject) event.Cookie {
|
2023-09-14 12:48:08 -06:00
|
|
|
this.layout.linked = box
|
|
|
|
this.linkCookie = this.newLinkCookie (
|
|
|
|
box.OnContentBoundsChange(this.handleLinkedContentBoundsChange))
|
|
|
|
this.SetLayout(this.layout)
|
|
|
|
return this.linkCookie
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Scrollbar) handleLinkedContentBoundsChange () {
|
2023-09-14 19:04:59 -06:00
|
|
|
if this.layout.linked == nil { return }
|
2023-09-14 19:40:06 -06:00
|
|
|
previousValue := this.layout.value
|
2023-09-14 19:04:59 -06:00
|
|
|
trackLength := this.layout.contentLength() - this.layout.viewportLength()
|
|
|
|
if trackLength == 0 {
|
|
|
|
this.layout.value = 0
|
|
|
|
} else {
|
|
|
|
this.layout.value = this.layout.contentPos() / trackLength
|
|
|
|
}
|
2023-09-14 18:34:56 -06:00
|
|
|
this.SetLayout(this.layout)
|
2023-09-14 19:40:06 -06:00
|
|
|
if this.layout.value != previousValue {
|
|
|
|
this.on.valueChange.Broadcast()
|
|
|
|
}
|
2023-09-14 12:48:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2023-09-14 19:40:06 -06:00
|
|
|
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 {
|
2024-05-13 17:45:18 -06:00
|
|
|
point.Y = -int(position)
|
2023-09-14 19:40:06 -06:00
|
|
|
} else {
|
2024-05-13 17:45:18 -06:00
|
|
|
point.X = -int(position)
|
2023-09-14 19:40:06 -06:00
|
|
|
}
|
|
|
|
this.layout.linked.ScrollTo(point)
|
|
|
|
|
2023-09-14 12:48:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns 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) Value () float64 {
|
2023-09-14 18:34:56 -06:00
|
|
|
if this.layout.linked == nil { return 0 }
|
|
|
|
return this.layout.value
|
2023-09-14 15:03:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnValueChange specifies a function to be called when the position of the
|
|
|
|
// scrollbar changes.
|
2023-09-14 18:34:56 -06:00
|
|
|
func (this *Scrollbar) OnValueChange (callback func ()) event.Cookie {
|
2023-09-14 15:03:19 -06:00
|
|
|
return this.on.valueChange.Connect(callback)
|
2023-09-14 12:48:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Scrollbar) handleKeyDown (key input.Key, numpad bool) {
|
|
|
|
var increment float64; if this.layout.vertical {
|
|
|
|
increment = -0.05
|
|
|
|
} else {
|
|
|
|
increment = 0.05
|
|
|
|
}
|
|
|
|
|
|
|
|
switch key {
|
|
|
|
case input.KeyUp, input.KeyLeft:
|
|
|
|
if this.Modifiers().Alt {
|
|
|
|
this.SetValue(0)
|
|
|
|
} else {
|
|
|
|
this.SetValue(this.Value() - increment)
|
|
|
|
}
|
|
|
|
case input.KeyDown, input.KeyRight:
|
|
|
|
if this.Modifiers().Alt {
|
|
|
|
this.SetValue(1)
|
|
|
|
} else {
|
|
|
|
this.SetValue(this.Value() + increment)
|
|
|
|
}
|
|
|
|
case input.KeyHome:
|
|
|
|
this.SetValue(0)
|
|
|
|
case input.KeyEnd:
|
|
|
|
this.SetValue(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Scrollbar) handleMouseDown (button input.Button) {
|
|
|
|
pointer := this.MousePosition()
|
|
|
|
handle := this.handle.Bounds()
|
|
|
|
|
2023-09-14 19:40:06 -06:00
|
|
|
within := pointer.In(handle)
|
|
|
|
var above bool; if this.layout.vertical {
|
|
|
|
above = pointer.Y < handle.Min.Y + handle.Dy() / 2
|
2023-09-14 12:48:08 -06:00
|
|
|
} else {
|
2023-09-14 19:40:06 -06:00
|
|
|
above = pointer.X < handle.Min.X + handle.Dx() / 2
|
2023-09-14 12:48:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
switch button {
|
|
|
|
case input.ButtonLeft:
|
|
|
|
if within {
|
|
|
|
this.dragging = true
|
|
|
|
this.dragOffset =
|
|
|
|
pointer.Sub(this.handle.Bounds().Min).
|
|
|
|
Add(this.InnerBounds().Min)
|
|
|
|
this.drag()
|
|
|
|
} else {
|
|
|
|
this.dragOffset = this.fallbackDragOffset()
|
|
|
|
this.dragging = true
|
|
|
|
this.drag()
|
|
|
|
}
|
|
|
|
case input.ButtonMiddle:
|
|
|
|
if above {
|
2023-09-15 14:11:59 -06:00
|
|
|
this.scrollBy(this.pageSize())
|
2024-05-13 17:45:18 -06:00
|
|
|
} else {
|
|
|
|
this.scrollBy(-this.pageSize())
|
2023-09-14 12:48:08 -06:00
|
|
|
}
|
|
|
|
case input.ButtonRight:
|
|
|
|
if above {
|
2023-09-15 14:11:59 -06:00
|
|
|
this.scrollBy(this.stepSize())
|
2024-05-13 17:45:18 -06:00
|
|
|
} else {
|
|
|
|
this.scrollBy(-this.stepSize())
|
2023-09-14 12:48:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Scrollbar) handleMouseUp (button input.Button) {
|
|
|
|
if button != input.ButtonLeft || !this.dragging { return }
|
|
|
|
this.dragging = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Scrollbar) handleMouseMove () {
|
|
|
|
if !this.dragging { return }
|
|
|
|
this.drag()
|
|
|
|
}
|
|
|
|
|
2023-09-14 19:04:59 -06:00
|
|
|
func (this *Scrollbar) handleScroll (x, y float64) {
|
|
|
|
if this.layout.linked == nil { return }
|
|
|
|
this.layout.linked.ScrollTo (
|
|
|
|
this.layout.linked.ContentBounds().Min.
|
2024-05-13 17:45:18 -06:00
|
|
|
Sub(image.Pt(int(x), int(y))))
|
2023-09-14 19:04:59 -06:00
|
|
|
}
|
|
|
|
|
2023-09-14 12:48:08 -06:00
|
|
|
func (this *Scrollbar) drag () {
|
|
|
|
pointer := this.MousePosition().Sub(this.dragOffset)
|
|
|
|
gutter := this.InnerBounds()
|
|
|
|
handle := this.handle.Bounds()
|
|
|
|
|
|
|
|
if this.layout.vertical {
|
|
|
|
this.SetValue (
|
|
|
|
float64(pointer.Y) /
|
|
|
|
float64(gutter.Dy() - handle.Dy()))
|
|
|
|
} else {
|
|
|
|
this.SetValue (
|
|
|
|
float64(pointer.X) /
|
|
|
|
float64(gutter.Dx() - handle.Dx()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Scrollbar) fallbackDragOffset () image.Point {
|
|
|
|
if this.layout.vertical {
|
|
|
|
return this.InnerBounds().Min.
|
|
|
|
Add(image.Pt(0, this.handle.Bounds().Dy() / 2))
|
|
|
|
} else {
|
|
|
|
return this.InnerBounds().Min.
|
|
|
|
Add(image.Pt(this.handle.Bounds().Dx() / 2, 0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 14:11:59 -06:00
|
|
|
func (this *Scrollbar) pageSize () int {
|
|
|
|
if this.layout.linked == nil { return 0 }
|
2024-05-27 14:28:48 -06:00
|
|
|
viewport := this.layout.linked.GetBox().InnerBounds()
|
2023-09-15 14:11:59 -06:00
|
|
|
if this.layout.vertical {
|
|
|
|
return viewport.Dy()
|
|
|
|
} else {
|
|
|
|
return viewport.Dx()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Scrollbar) stepSize () int {
|
|
|
|
// FIXME: this should not be hardcoded, need to get base font metrics
|
2024-05-26 15:21:58 -06:00
|
|
|
// from tomo.somehow. should be (emspace, lineheight)
|
2023-09-15 14:11:59 -06:00
|
|
|
return 16
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Scrollbar) scrollBy (distance int) {
|
|
|
|
if this.layout.linked == nil { return }
|
|
|
|
var vector image.Point; if this.layout.vertical {
|
|
|
|
vector.Y = distance
|
|
|
|
} else {
|
|
|
|
vector.X = distance
|
|
|
|
}
|
|
|
|
this.layout.linked.ScrollTo (
|
|
|
|
this.layout.linked.ContentBounds().Min.
|
|
|
|
Add(vector))
|
|
|
|
}
|
|
|
|
|
2023-09-14 12:48:08 -06:00
|
|
|
type scrollbarCookie struct {
|
|
|
|
owner *Scrollbar
|
|
|
|
subCookies []event.Cookie
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Scrollbar) newLinkCookie (subCookies ...event.Cookie) *scrollbarCookie {
|
|
|
|
return &scrollbarCookie {
|
|
|
|
owner: this,
|
|
|
|
subCookies: subCookies,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *scrollbarCookie) Close () {
|
|
|
|
for _, cookie := range this.subCookies {
|
|
|
|
cookie.Close()
|
|
|
|
}
|
|
|
|
this.owner.layout.linked = nil
|
|
|
|
this.owner.SetLayout(this.owner.layout)
|
|
|
|
}
|
|
|
|
|
|
|
|
type scrollbarLayout struct {
|
|
|
|
vertical bool
|
2023-09-14 18:34:56 -06:00
|
|
|
value float64
|
2024-05-27 14:28:48 -06:00
|
|
|
linked tomo.ContentObject
|
2023-09-14 12:48:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (scrollbarLayout) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
|
|
|
|
if len(boxes) != 1 { return image.Pt(0, 0) }
|
|
|
|
return boxes[0].MinimumSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this scrollbarLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
|
|
|
if len(boxes) != 1 { return }
|
|
|
|
handle := image.Rectangle { Max: boxes[0].MinimumSize() }
|
|
|
|
gutter := hints.Bounds
|
|
|
|
|
2023-09-14 19:04:59 -06:00
|
|
|
var gutterLength float64;
|
|
|
|
var handleMin float64;
|
|
|
|
if this.vertical {
|
|
|
|
gutterLength = float64(gutter.Dy())
|
|
|
|
handleMin = float64(handle.Dy())
|
|
|
|
} else {
|
|
|
|
gutterLength = float64(gutter.Dx())
|
|
|
|
handleMin = float64(handle.Dx())
|
|
|
|
}
|
2023-09-14 18:34:56 -06:00
|
|
|
|
2023-09-14 19:04:59 -06:00
|
|
|
// calculate handle length
|
|
|
|
handleLength := gutterLength * this.viewportContentRatio()
|
|
|
|
if handleLength < handleMin { handleLength = handleMin }
|
2023-09-14 23:47:58 -06:00
|
|
|
if handleLength >= gutterLength {
|
2024-05-27 13:22:18 -06:00
|
|
|
// just hide the handle if it isn't needed. we are the layout
|
|
|
|
// and we shouldn't be adding and removing boxes, so this is
|
|
|
|
// really the only good way to hide things.
|
|
|
|
// TODO perhaps have a "Hidden" rectangle in the Tomo API?
|
2023-09-14 23:47:58 -06:00
|
|
|
boxes[0].SetBounds(image.Rect(-16, -16, 0, 0))
|
|
|
|
return
|
|
|
|
}
|
2023-09-14 19:04:59 -06:00
|
|
|
if this.vertical {
|
|
|
|
handle.Max.Y = int(handleLength)
|
|
|
|
} else {
|
|
|
|
handle.Max.X = int(handleLength)
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate handle position
|
|
|
|
handlePosition := (gutterLength - handleLength) * this.value
|
|
|
|
var handleOffset image.Point
|
|
|
|
if this.vertical {
|
|
|
|
handleOffset = image.Pt(0, int(handlePosition))
|
|
|
|
} else {
|
|
|
|
handleOffset = image.Pt(int(handlePosition), 0)
|
|
|
|
}
|
2024-05-13 17:45:18 -06:00
|
|
|
handle = handle.Sub(handleOffset).Add(gutter.Min)
|
2023-09-14 19:04:59 -06:00
|
|
|
|
|
|
|
// place handle
|
|
|
|
boxes[0].SetBounds(handle)
|
|
|
|
|
2023-09-14 12:48:08 -06:00
|
|
|
}
|
2023-09-14 18:34:56 -06:00
|
|
|
|
|
|
|
func (this scrollbarLayout) viewportContentRatio () float64 {
|
|
|
|
if this.linked == nil { return 0 }
|
2023-09-14 19:04:59 -06:00
|
|
|
return this.viewportLength() / this.contentLength()
|
2023-09-14 18:34:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this scrollbarLayout) viewportLength () float64 {
|
|
|
|
if this.vertical {
|
2024-05-27 14:28:48 -06:00
|
|
|
return float64(this.linked.GetBox().InnerBounds().Dy())
|
2023-09-14 18:34:56 -06:00
|
|
|
} else {
|
2024-05-27 14:28:48 -06:00
|
|
|
return float64(this.linked.GetBox().InnerBounds().Dx())
|
2023-09-14 18:34:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this scrollbarLayout) contentLength () float64 {
|
|
|
|
if this.vertical {
|
|
|
|
return float64(this.linked.ContentBounds().Dy())
|
|
|
|
} else {
|
|
|
|
return float64(this.linked.ContentBounds().Dx())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this scrollbarLayout) contentPos () float64 {
|
|
|
|
if this.vertical {
|
|
|
|
return float64(this.linked.ContentBounds().Min.Y)
|
|
|
|
} else {
|
|
|
|
return float64(this.linked.ContentBounds().Min.X)
|
|
|
|
}
|
|
|
|
}
|