2023-03-08 18:24:43 -07:00
|
|
|
package basicElements
|
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/input"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/config"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
|
|
|
|
2023-03-09 16:15:52 -07:00
|
|
|
// ScrollBar is an element similar to Slider, but it has special behavior that
|
|
|
|
// makes it well suited for controlling the viewport position on one axis of a
|
|
|
|
// scrollable element. Instead of having a value from zero to one, it stores
|
|
|
|
// viewport and content boundaries. When the user drags the scroll bar handle,
|
|
|
|
// the scroll bar calls the OnScroll callback assigned to it with the position
|
|
|
|
// the user is trying to move the handle to. A program can check to see if this
|
|
|
|
// value is valid, move the viewport, and give the scroll bar the new viewport
|
|
|
|
// bounds (which will then cause it to move the handle).
|
2023-03-09 20:23:09 -07:00
|
|
|
//
|
|
|
|
// Typically, you wont't want to use a ScrollBar by itself. A ScrollContainer is
|
|
|
|
// better for most cases.
|
2023-03-08 18:24:43 -07:00
|
|
|
type ScrollBar struct {
|
|
|
|
*core.Core
|
|
|
|
core core.CoreControl
|
|
|
|
|
|
|
|
vertical bool
|
|
|
|
enabled bool
|
|
|
|
dragging bool
|
2023-03-09 16:15:52 -07:00
|
|
|
dragOffset image.Point
|
2023-03-08 18:24:43 -07:00
|
|
|
track image.Rectangle
|
|
|
|
bar image.Rectangle
|
|
|
|
|
|
|
|
contentBounds image.Rectangle
|
|
|
|
viewportBounds image.Rectangle
|
|
|
|
|
|
|
|
config config.Wrapped
|
|
|
|
theme theme.Wrapped
|
|
|
|
|
2023-03-09 16:15:52 -07:00
|
|
|
onScroll func (viewport image.Point)
|
2023-03-08 18:24:43 -07:00
|
|
|
}
|
|
|
|
|
2023-03-09 16:15:52 -07:00
|
|
|
// NewScrollBar creates a new scroll bar. If vertical is set to true, the scroll
|
|
|
|
// bar will be vertical instead of horizontal.
|
2023-03-08 18:24:43 -07:00
|
|
|
func NewScrollBar (vertical bool) (element *ScrollBar) {
|
|
|
|
element = &ScrollBar {
|
|
|
|
vertical: vertical,
|
|
|
|
enabled: true,
|
|
|
|
}
|
|
|
|
if vertical {
|
|
|
|
element.theme.Case = theme.C("basic", "scrollBarHorizontal")
|
|
|
|
} else {
|
|
|
|
element.theme.Case = theme.C("basic", "scrollBarVertical")
|
|
|
|
}
|
2023-03-14 23:41:23 -06:00
|
|
|
element.Core, element.core = core.NewCore(element, element.handleResize)
|
2023-03-08 18:24:43 -07:00
|
|
|
element.updateMinimumSize()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-09 20:23:09 -07:00
|
|
|
func (element *ScrollBar) handleResize () {
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.recalculate()
|
|
|
|
element.draw()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 18:24:43 -07:00
|
|
|
func (element *ScrollBar) HandleMouseDown (x, y int, button input.Button) {
|
2023-03-09 16:15:52 -07:00
|
|
|
velocity := element.config.ScrollVelocity()
|
|
|
|
point := image.Pt(x, y)
|
|
|
|
|
|
|
|
if point.In(element.bar) {
|
|
|
|
// the mouse is pressed down within the bar's handle
|
|
|
|
element.dragging = true
|
2023-03-09 20:23:09 -07:00
|
|
|
element.drawAndPush()
|
2023-03-09 21:27:08 -07:00
|
|
|
element.dragOffset =
|
|
|
|
point.Sub(element.bar.Min).
|
|
|
|
Add(element.Bounds().Min)
|
2023-03-09 16:15:52 -07:00
|
|
|
element.dragTo(point)
|
|
|
|
} else {
|
|
|
|
// the mouse is pressed down within the bar's gutter
|
|
|
|
switch button {
|
|
|
|
case input.ButtonLeft:
|
|
|
|
// start scrolling at this point, but set the offset to
|
|
|
|
// the middle of the handle
|
|
|
|
element.dragging = true
|
|
|
|
element.dragOffset = element.fallbackDragOffset()
|
|
|
|
element.dragTo(point)
|
|
|
|
|
|
|
|
case input.ButtonMiddle:
|
|
|
|
// page up/down on middle click
|
|
|
|
viewport := 0
|
|
|
|
if element.vertical {
|
|
|
|
viewport = element.viewportBounds.Dy()
|
|
|
|
} else {
|
|
|
|
viewport = element.viewportBounds.Dx()
|
|
|
|
}
|
|
|
|
if element.isAfterHandle(point) {
|
|
|
|
element.scrollBy(viewport)
|
|
|
|
} else {
|
|
|
|
element.scrollBy(-viewport)
|
|
|
|
}
|
|
|
|
|
|
|
|
case input.ButtonRight:
|
|
|
|
// inch up/down on right click
|
|
|
|
if element.isAfterHandle(point) {
|
|
|
|
element.scrollBy(velocity)
|
|
|
|
} else {
|
|
|
|
element.scrollBy(-velocity)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-08 18:24:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *ScrollBar) HandleMouseUp (x, y int, button input.Button) {
|
2023-03-09 16:15:52 -07:00
|
|
|
if element.dragging {
|
|
|
|
element.dragging = false
|
2023-03-09 20:23:09 -07:00
|
|
|
element.drawAndPush()
|
2023-03-09 16:15:52 -07:00
|
|
|
}
|
2023-03-08 18:24:43 -07:00
|
|
|
}
|
|
|
|
|
2023-03-15 22:24:40 -06:00
|
|
|
func (element *ScrollBar) HandleMotion (x, y int) {
|
2023-03-09 16:15:52 -07:00
|
|
|
if element.dragging {
|
|
|
|
element.dragTo(image.Pt(x, y))
|
|
|
|
}
|
2023-03-08 18:24:43 -07:00
|
|
|
}
|
|
|
|
|
2023-03-15 22:24:40 -06:00
|
|
|
func (element *ScrollBar) HandleScroll (x, y int, deltaX, deltaY float64) {
|
2023-03-10 11:41:47 -07:00
|
|
|
if element.vertical {
|
|
|
|
element.scrollBy(int(deltaY))
|
|
|
|
} else {
|
|
|
|
element.scrollBy(int(deltaX))
|
|
|
|
}
|
2023-03-08 18:24:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetEnabled sets whether or not the scroll bar can be interacted with.
|
|
|
|
func (element *ScrollBar) SetEnabled (enabled bool) {
|
|
|
|
if element.enabled == enabled { return }
|
|
|
|
element.enabled = enabled
|
2023-03-09 20:23:09 -07:00
|
|
|
element.drawAndPush()
|
2023-03-08 18:24:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enabled returns whether or not the element is enabled.
|
|
|
|
func (element *ScrollBar) Enabled () (enabled bool) {
|
|
|
|
return element.enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBounds sets the content and viewport bounds of the scroll bar.
|
|
|
|
func (element *ScrollBar) SetBounds (content, viewport image.Rectangle) {
|
|
|
|
element.contentBounds = content
|
|
|
|
element.viewportBounds = viewport
|
2023-03-09 20:23:09 -07:00
|
|
|
element.recalculate()
|
|
|
|
element.drawAndPush()
|
2023-03-08 18:24:43 -07:00
|
|
|
}
|
|
|
|
|
2023-03-09 16:15:52 -07:00
|
|
|
// OnScroll sets a function to be called when the user tries to move the scroll
|
|
|
|
// bar's handle. The callback is passed a point representing the new viewport
|
|
|
|
// position. For the scroll bar's position to visually update, the callback must
|
|
|
|
// check if the position is valid and call ScrollBar.SetBounds with the new
|
|
|
|
// viewport bounds.
|
|
|
|
func (element *ScrollBar) OnScroll (callback func (viewport image.Point)) {
|
|
|
|
element.onScroll = callback
|
|
|
|
}
|
|
|
|
|
2023-03-08 18:24:43 -07:00
|
|
|
// SetTheme sets the element's theme.
|
|
|
|
func (element *ScrollBar) SetTheme (new theme.Theme) {
|
|
|
|
if new == element.theme.Theme { return }
|
|
|
|
element.theme.Theme = new
|
2023-03-09 20:23:09 -07:00
|
|
|
element.drawAndPush()
|
2023-03-08 18:24:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig sets the element's configuration.
|
|
|
|
func (element *ScrollBar) SetConfig (new config.Config) {
|
|
|
|
if new == element.config.Config { return }
|
|
|
|
element.config.Config = new
|
|
|
|
element.updateMinimumSize()
|
2023-03-09 20:23:09 -07:00
|
|
|
element.drawAndPush()
|
2023-03-08 18:24:43 -07:00
|
|
|
}
|
|
|
|
|
2023-03-09 16:15:52 -07:00
|
|
|
func (element *ScrollBar) isAfterHandle (point image.Point) bool {
|
|
|
|
if element.vertical {
|
|
|
|
return point.Y > element.bar.Min.Y
|
|
|
|
} else {
|
|
|
|
return point.X > element.bar.Min.X
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *ScrollBar) fallbackDragOffset () image.Point {
|
|
|
|
if element.vertical {
|
2023-03-09 21:27:08 -07:00
|
|
|
return element.Bounds().Min.
|
|
|
|
Add(image.Pt(0, element.bar.Dy() / 2))
|
2023-03-09 16:15:52 -07:00
|
|
|
} else {
|
2023-03-09 21:27:08 -07:00
|
|
|
return element.Bounds().Min.
|
|
|
|
Add(image.Pt(element.bar.Dx() / 2, 0))
|
2023-03-09 16:15:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *ScrollBar) scrollBy (delta int) {
|
|
|
|
deltaPoint := image.Point { }
|
|
|
|
if element.vertical {
|
|
|
|
deltaPoint.Y = delta
|
|
|
|
} else {
|
|
|
|
deltaPoint.X = delta
|
|
|
|
}
|
|
|
|
if element.onScroll != nil {
|
|
|
|
element.onScroll(element.viewportBounds.Min.Add(deltaPoint))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *ScrollBar) dragTo (point image.Point) {
|
|
|
|
point = point.Sub(element.dragOffset)
|
|
|
|
var scrollX, scrollY float64
|
|
|
|
|
|
|
|
if element.vertical {
|
|
|
|
ratio :=
|
|
|
|
float64(element.contentBounds.Dy()) /
|
|
|
|
float64(element.track.Dy())
|
|
|
|
scrollX = float64(element.viewportBounds.Min.X)
|
|
|
|
scrollY = float64(point.Y) * ratio
|
|
|
|
} else {
|
|
|
|
ratio :=
|
|
|
|
float64(element.contentBounds.Dx()) /
|
|
|
|
float64(element.track.Dx())
|
|
|
|
scrollX = float64(point.X) * ratio
|
|
|
|
scrollY = float64(element.viewportBounds.Min.Y)
|
|
|
|
}
|
|
|
|
|
|
|
|
if element.onScroll != nil {
|
|
|
|
element.onScroll(image.Pt(int(scrollX), int(scrollY)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 18:24:43 -07:00
|
|
|
func (element *ScrollBar) recalculate () {
|
|
|
|
if element.vertical {
|
|
|
|
element.recalculateVertical()
|
|
|
|
} else {
|
|
|
|
element.recalculateHorizontal()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *ScrollBar) recalculateVertical () {
|
|
|
|
bounds := element.Bounds()
|
|
|
|
padding := element.theme.Padding(theme.PatternGutter)
|
|
|
|
element.track = padding.Apply(bounds)
|
|
|
|
|
|
|
|
contentBounds := element.contentBounds
|
|
|
|
viewportBounds := element.viewportBounds
|
|
|
|
if element.Enabled() {
|
|
|
|
element.bar.Min.X = element.track.Min.X
|
|
|
|
element.bar.Max.X = element.track.Max.X
|
|
|
|
|
2023-03-09 20:23:09 -07:00
|
|
|
ratio :=
|
|
|
|
float64(element.track.Dy()) /
|
2023-03-08 18:24:43 -07:00
|
|
|
float64(contentBounds.Dy())
|
2023-03-09 20:23:09 -07:00
|
|
|
element.bar.Min.Y = int(float64(viewportBounds.Min.Y) * ratio)
|
|
|
|
element.bar.Max.Y = int(float64(viewportBounds.Max.Y) * ratio)
|
2023-03-08 18:24:43 -07:00
|
|
|
|
|
|
|
element.bar.Min.Y += element.track.Min.Y
|
|
|
|
element.bar.Max.Y += element.track.Min.Y
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the handle is out of bounds, don't display it
|
|
|
|
if element.bar.Dy() >= element.track.Dy() {
|
|
|
|
element.bar = image.Rectangle { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *ScrollBar) recalculateHorizontal () {
|
2023-03-09 20:23:09 -07:00
|
|
|
bounds := element.Bounds()
|
|
|
|
padding := element.theme.Padding(theme.PatternGutter)
|
|
|
|
element.track = padding.Apply(bounds)
|
|
|
|
|
|
|
|
contentBounds := element.contentBounds
|
|
|
|
viewportBounds := element.viewportBounds
|
|
|
|
if element.Enabled() {
|
|
|
|
element.bar.Min.Y = element.track.Min.Y
|
|
|
|
element.bar.Max.Y = element.track.Max.Y
|
|
|
|
|
|
|
|
ratio :=
|
2023-03-10 22:21:54 -07:00
|
|
|
float64(element.track.Dx()) /
|
|
|
|
float64(contentBounds.Dx())
|
2023-03-09 20:23:09 -07:00
|
|
|
element.bar.Min.X = int(float64(viewportBounds.Min.X) * ratio)
|
|
|
|
element.bar.Max.X = int(float64(viewportBounds.Max.X) * ratio)
|
|
|
|
|
|
|
|
element.bar.Min.X += element.track.Min.X
|
|
|
|
element.bar.Max.X += element.track.Min.X
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the handle is out of bounds, don't display it
|
|
|
|
if element.bar.Dx() >= element.track.Dx() {
|
|
|
|
element.bar = image.Rectangle { }
|
|
|
|
}
|
2023-03-08 18:24:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *ScrollBar) updateMinimumSize () {
|
|
|
|
padding := element.theme.Padding(theme.PatternGutter)
|
|
|
|
if element.vertical {
|
|
|
|
element.core.SetMinimumSize (
|
|
|
|
padding.Horizontal() + element.config.HandleWidth(),
|
|
|
|
padding.Vertical() + element.config.HandleWidth() * 2)
|
|
|
|
} else {
|
|
|
|
element.core.SetMinimumSize (
|
|
|
|
padding.Horizontal() + element.config.HandleWidth() * 2,
|
|
|
|
padding.Vertical() + element.config.HandleWidth())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 20:23:09 -07:00
|
|
|
func (element *ScrollBar) drawAndPush () {
|
2023-03-08 18:24:43 -07:00
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *ScrollBar) draw () {
|
|
|
|
bounds := element.Bounds()
|
|
|
|
state := theme.State {
|
|
|
|
Disabled: !element.Enabled(),
|
|
|
|
Pressed: element.dragging,
|
|
|
|
}
|
2023-03-11 23:15:36 -07:00
|
|
|
element.theme.Pattern(theme.PatternGutter, state).Draw (
|
2023-03-08 18:24:43 -07:00
|
|
|
element.core,
|
|
|
|
bounds)
|
2023-03-11 23:15:36 -07:00
|
|
|
element.theme.Pattern(theme.PatternHandle, state).Draw (
|
2023-03-08 18:24:43 -07:00
|
|
|
element.core,
|
|
|
|
element.bar)
|
|
|
|
}
|