2023-02-01 23:48:16 -07:00
|
|
|
package basicElements
|
2023-01-19 11:07:27 -07:00
|
|
|
|
|
|
|
import "image"
|
2023-02-01 23:48:16 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/input"
|
2023-01-19 11:07:27 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
2023-02-07 22:22:40 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/config"
|
2023-02-01 23:48:16 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
2023-01-19 11:07:27 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
2023-02-01 23:48:16 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements"
|
2023-01-19 11:07:27 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
|
|
|
|
|
|
|
// ScrollContainer is a container that is capable of holding a scrollable
|
|
|
|
// element.
|
|
|
|
type ScrollContainer struct {
|
|
|
|
*core.Core
|
|
|
|
core core.CoreControl
|
2023-01-30 15:01:47 -07:00
|
|
|
focused bool
|
2023-01-20 13:35:43 -07:00
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
child elements.Scrollable
|
2023-01-20 13:35:43 -07:00
|
|
|
childWidth, childHeight int
|
|
|
|
|
2023-01-19 11:07:27 -07:00
|
|
|
horizontal struct {
|
2023-02-08 12:36:14 -07:00
|
|
|
theme theme.Wrapped
|
2023-01-20 13:35:43 -07:00
|
|
|
exists bool
|
2023-01-19 11:07:27 -07:00
|
|
|
enabled bool
|
2023-01-20 23:15:30 -07:00
|
|
|
dragging bool
|
2023-01-20 23:24:24 -07:00
|
|
|
dragOffset int
|
2023-01-20 13:35:43 -07:00
|
|
|
gutter image.Rectangle
|
2023-01-28 23:49:01 -07:00
|
|
|
track image.Rectangle
|
2023-01-20 13:35:43 -07:00
|
|
|
bar image.Rectangle
|
2023-01-19 11:07:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
vertical struct {
|
2023-02-08 12:36:14 -07:00
|
|
|
theme theme.Wrapped
|
2023-01-20 13:35:43 -07:00
|
|
|
exists bool
|
2023-01-19 11:07:27 -07:00
|
|
|
enabled bool
|
2023-01-20 23:15:30 -07:00
|
|
|
dragging bool
|
2023-01-20 23:24:24 -07:00
|
|
|
dragOffset int
|
2023-01-20 13:35:43 -07:00
|
|
|
gutter image.Rectangle
|
2023-01-28 23:49:01 -07:00
|
|
|
track image.Rectangle
|
2023-01-20 13:35:43 -07:00
|
|
|
bar image.Rectangle
|
2023-01-19 11:07:27 -07:00
|
|
|
}
|
2023-02-07 22:22:40 -07:00
|
|
|
|
2023-02-08 12:36:14 -07:00
|
|
|
config config.Wrapped
|
|
|
|
theme theme.Wrapped
|
2023-01-19 14:49:34 -07:00
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
onFocusRequest func () (granted bool)
|
2023-02-01 23:48:16 -07:00
|
|
|
onFocusMotionRequest func (input.KeynavDirection) (granted bool)
|
2023-01-19 11:07:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewScrollContainer creates a new scroll container with the specified scroll
|
|
|
|
// bars.
|
|
|
|
func NewScrollContainer (horizontal, vertical bool) (element *ScrollContainer) {
|
2023-02-08 12:36:14 -07:00
|
|
|
element = &ScrollContainer { }
|
|
|
|
element.theme.Case = theme.C("basic", "scrollContainer")
|
|
|
|
element.horizontal.theme.Case = theme.C("basic", "scrollBarHorizontal")
|
|
|
|
element.vertical.theme.Case = theme.C("basic", "scrollBarVertical")
|
2023-02-07 22:22:40 -07:00
|
|
|
|
2023-01-31 12:54:43 -07:00
|
|
|
element.Core, element.core = core.NewCore(element.handleResize)
|
2023-01-20 13:35:43 -07:00
|
|
|
element.horizontal.exists = horizontal
|
|
|
|
element.vertical.exists = vertical
|
2023-01-19 11:07:27 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:54:43 -07:00
|
|
|
func (element *ScrollContainer) handleResize () {
|
2023-01-19 11:07:27 -07:00
|
|
|
element.recalculate()
|
2023-01-31 16:39:17 -07:00
|
|
|
element.resizeChildToFit()
|
2023-01-19 11:07:27 -07:00
|
|
|
element.draw()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adopt adds a scrollable element to the scroll container. The container can
|
|
|
|
// only contain one scrollable element at a time, and when a new one is adopted
|
|
|
|
// it replaces the last one.
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *ScrollContainer) Adopt (child elements.Scrollable) {
|
2023-01-19 11:07:27 -07:00
|
|
|
// disown previous child if it exists
|
|
|
|
if element.child != nil {
|
2023-01-19 14:49:34 -07:00
|
|
|
element.clearChildEventHandlers(child)
|
2023-01-19 11:07:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// adopt new child
|
|
|
|
element.child = child
|
|
|
|
if child != nil {
|
2023-02-08 12:36:14 -07:00
|
|
|
if child0, ok := child.(elements.Themeable); ok {
|
|
|
|
child0.SetTheme(element.theme.Theme)
|
|
|
|
}
|
|
|
|
if child0, ok := child.(elements.Configurable); ok {
|
|
|
|
child0.SetConfig(element.config.Config)
|
|
|
|
}
|
2023-01-19 15:35:19 -07:00
|
|
|
child.OnDamage(element.childDamageCallback)
|
|
|
|
child.OnMinimumSizeChange(element.updateMinimumSize)
|
2023-01-20 13:35:43 -07:00
|
|
|
child.OnScrollBoundsChange(element.childScrollBoundsChangeCallback)
|
2023-02-01 23:48:16 -07:00
|
|
|
if newChild, ok := child.(elements.Focusable); ok {
|
2023-01-30 15:01:47 -07:00
|
|
|
newChild.OnFocusRequest (
|
|
|
|
element.childFocusRequestCallback)
|
|
|
|
newChild.OnFocusMotionRequest (
|
|
|
|
element.childFocusMotionRequestCallback)
|
2023-01-19 15:35:19 -07:00
|
|
|
}
|
2023-01-19 11:07:27 -07:00
|
|
|
|
|
|
|
element.updateMinimumSize()
|
2023-01-20 13:35:43 -07:00
|
|
|
|
|
|
|
element.horizontal.enabled,
|
|
|
|
element.vertical.enabled = element.child.ScrollAxes()
|
2023-01-19 11:07:27 -07:00
|
|
|
|
|
|
|
if element.core.HasImage() {
|
2023-01-31 16:39:17 -07:00
|
|
|
element.resizeChildToFit()
|
2023-01-19 11:07:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 12:36:14 -07:00
|
|
|
// SetTheme sets the element's theme.
|
|
|
|
func (element *ScrollContainer) SetTheme (new theme.Theme) {
|
|
|
|
if new == element.theme.Theme { return }
|
|
|
|
element.theme.Theme = new
|
|
|
|
if child, ok := element.child.(elements.Themeable); ok {
|
|
|
|
child.SetTheme(element.theme.Theme)
|
|
|
|
}
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.recalculate()
|
|
|
|
element.resizeChildToFit()
|
|
|
|
element.draw()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig sets the element's configuration.
|
|
|
|
func (element *ScrollContainer) SetConfig (new config.Config) {
|
|
|
|
if new == element.config.Config { return }
|
|
|
|
element.config.Config = new
|
|
|
|
if child, ok := element.child.(elements.Configurable); ok {
|
|
|
|
child.SetConfig(element.config.Config)
|
|
|
|
}
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.recalculate()
|
|
|
|
element.resizeChildToFit()
|
|
|
|
element.draw()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *ScrollContainer) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
|
|
|
|
if child, ok := element.child.(elements.KeyboardTarget); ok {
|
2023-01-20 15:40:28 -07:00
|
|
|
child.HandleKeyDown(key, modifiers)
|
2023-01-19 15:35:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *ScrollContainer) HandleKeyUp (key input.Key, modifiers input.Modifiers) {
|
|
|
|
if child, ok := element.child.(elements.KeyboardTarget); ok {
|
2023-01-19 15:35:19 -07:00
|
|
|
child.HandleKeyUp(key, modifiers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *ScrollContainer) HandleMouseDown (x, y int, button input.Button) {
|
2023-02-07 22:22:40 -07:00
|
|
|
velocity := element.config.ScrollVelocity()
|
2023-01-20 23:15:30 -07:00
|
|
|
point := image.Pt(x, y)
|
2023-01-20 23:24:24 -07:00
|
|
|
if point.In(element.horizontal.bar) {
|
2023-01-20 23:15:30 -07:00
|
|
|
element.horizontal.dragging = true
|
2023-01-20 23:24:24 -07:00
|
|
|
element.horizontal.dragOffset =
|
2023-01-31 16:39:17 -07:00
|
|
|
x - element.horizontal.bar.Min.X +
|
|
|
|
element.Bounds().Min.X
|
2023-01-20 23:15:30 -07:00
|
|
|
element.dragHorizontalBar(point)
|
|
|
|
|
2023-01-21 19:05:51 -07:00
|
|
|
} else if point.In(element.horizontal.gutter) {
|
2023-02-11 20:17:03 -07:00
|
|
|
switch button {
|
|
|
|
case input.ButtonLeft:
|
|
|
|
element.horizontal.dragging = true
|
|
|
|
element.horizontal.dragOffset =
|
|
|
|
element.horizontal.bar.Dx() / 2 +
|
|
|
|
element.Bounds().Min.X
|
|
|
|
element.dragHorizontalBar(point)
|
|
|
|
case input.ButtonMiddle:
|
|
|
|
viewport := element.child.ScrollViewportBounds().Dx()
|
|
|
|
if x > element.horizontal.bar.Min.X {
|
|
|
|
element.scrollChildBy(viewport, 0)
|
|
|
|
} else {
|
|
|
|
element.scrollChildBy(-viewport, 0)
|
|
|
|
}
|
|
|
|
case input.ButtonRight:
|
|
|
|
if x > element.horizontal.bar.Min.X {
|
|
|
|
element.scrollChildBy(velocity, 0)
|
|
|
|
} else {
|
|
|
|
element.scrollChildBy(-velocity, 0)
|
|
|
|
}
|
2023-01-21 19:05:51 -07:00
|
|
|
}
|
|
|
|
|
2023-01-20 23:24:24 -07:00
|
|
|
} else if point.In(element.vertical.bar) {
|
2023-01-20 23:15:30 -07:00
|
|
|
element.vertical.dragging = true
|
2023-01-20 23:24:24 -07:00
|
|
|
element.vertical.dragOffset =
|
2023-01-31 16:39:17 -07:00
|
|
|
y - element.vertical.bar.Min.Y +
|
|
|
|
element.Bounds().Min.Y
|
2023-01-20 23:15:30 -07:00
|
|
|
element.dragVerticalBar(point)
|
|
|
|
|
2023-01-21 19:05:51 -07:00
|
|
|
} else if point.In(element.vertical.gutter) {
|
2023-02-11 20:17:03 -07:00
|
|
|
switch button {
|
|
|
|
case input.ButtonLeft:
|
|
|
|
element.vertical.dragging = true
|
|
|
|
element.vertical.dragOffset =
|
|
|
|
element.vertical.bar.Dy() / 2 +
|
|
|
|
element.Bounds().Min.Y
|
|
|
|
element.dragVerticalBar(point)
|
|
|
|
case input.ButtonMiddle:
|
|
|
|
viewport := element.child.ScrollViewportBounds().Dy()
|
|
|
|
if y > element.vertical.bar.Min.Y {
|
|
|
|
element.scrollChildBy(0, viewport)
|
|
|
|
} else {
|
|
|
|
element.scrollChildBy(0, -viewport)
|
|
|
|
}
|
|
|
|
case input.ButtonRight:
|
|
|
|
if y > element.vertical.bar.Min.Y {
|
|
|
|
element.scrollChildBy(0, velocity)
|
|
|
|
} else {
|
|
|
|
element.scrollChildBy(0, -velocity)
|
|
|
|
}
|
2023-01-21 19:05:51 -07:00
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
} else if child, ok := element.child.(elements.MouseTarget); ok {
|
2023-01-19 16:03:50 -07:00
|
|
|
child.HandleMouseDown(x, y, button)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *ScrollContainer) HandleMouseUp (x, y int, button input.Button) {
|
2023-01-20 23:15:30 -07:00
|
|
|
if element.horizontal.dragging {
|
|
|
|
element.horizontal.dragging = false
|
2023-01-21 18:58:25 -07:00
|
|
|
element.drawHorizontalBar()
|
|
|
|
element.core.DamageRegion(element.horizontal.bar)
|
2023-01-20 23:15:30 -07:00
|
|
|
|
|
|
|
} else if element.vertical.dragging {
|
|
|
|
element.vertical.dragging = false
|
2023-01-21 18:58:25 -07:00
|
|
|
element.drawVerticalBar()
|
|
|
|
element.core.DamageRegion(element.vertical.bar)
|
2023-01-20 23:15:30 -07:00
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
} else if child, ok := element.child.(elements.MouseTarget); ok {
|
2023-01-19 16:03:50 -07:00
|
|
|
child.HandleMouseUp(x, y, button)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *ScrollContainer) HandleMouseMove (x, y int) {
|
2023-01-20 23:15:30 -07:00
|
|
|
if element.horizontal.dragging {
|
|
|
|
element.dragHorizontalBar(image.Pt(x, y))
|
|
|
|
|
|
|
|
} else if element.vertical.dragging {
|
|
|
|
element.dragVerticalBar(image.Pt(x, y))
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
} else if child, ok := element.child.(elements.MouseTarget); ok {
|
2023-01-19 16:03:50 -07:00
|
|
|
child.HandleMouseMove(x, y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *ScrollContainer) HandleMouseScroll (
|
|
|
|
x, y int,
|
|
|
|
deltaX, deltaY float64,
|
|
|
|
) {
|
2023-01-21 19:05:51 -07:00
|
|
|
element.scrollChildBy(int(deltaX), int(deltaY))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *ScrollContainer) scrollChildBy (x, y int) {
|
|
|
|
if element.child == nil { return }
|
|
|
|
scrollPoint :=
|
|
|
|
element.child.ScrollViewportBounds().Min.
|
|
|
|
Add(image.Pt(x, y))
|
2023-01-20 14:44:07 -07:00
|
|
|
element.child.ScrollTo(scrollPoint)
|
2023-01-19 16:03:50 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
func (element *ScrollContainer) Focused () (focused bool) {
|
|
|
|
return element.focused
|
2023-01-19 15:35:19 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
func (element *ScrollContainer) Focus () {
|
|
|
|
if element.onFocusRequest != nil {
|
2023-02-10 22:58:54 -07:00
|
|
|
if element.onFocusRequest() {
|
|
|
|
element.focused = true
|
|
|
|
}
|
2023-01-19 15:35:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
func (element *ScrollContainer) HandleFocus (
|
2023-02-01 23:48:16 -07:00
|
|
|
direction input.KeynavDirection,
|
2023-01-19 15:35:19 -07:00
|
|
|
) (
|
|
|
|
accepted bool,
|
|
|
|
) {
|
2023-02-01 23:48:16 -07:00
|
|
|
if child, ok := element.child.(elements.Focusable); ok {
|
2023-02-10 22:58:54 -07:00
|
|
|
element.focused = child.HandleFocus(direction)
|
|
|
|
return element.focused
|
2023-01-19 15:35:19 -07:00
|
|
|
} else {
|
2023-01-30 15:01:47 -07:00
|
|
|
element.focused = false
|
2023-01-19 15:35:19 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
func (element *ScrollContainer) HandleUnfocus () {
|
2023-02-01 23:48:16 -07:00
|
|
|
if child, ok := element.child.(elements.Focusable); ok {
|
2023-01-30 15:01:47 -07:00
|
|
|
child.HandleUnfocus()
|
2023-01-19 15:35:19 -07:00
|
|
|
}
|
2023-01-30 15:01:47 -07:00
|
|
|
element.focused = false
|
2023-01-19 15:35:19 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
func (element *ScrollContainer) OnFocusRequest (callback func () (granted bool)) {
|
|
|
|
element.onFocusRequest = callback
|
2023-01-19 15:35:19 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
func (element *ScrollContainer) OnFocusMotionRequest (
|
2023-02-01 23:48:16 -07:00
|
|
|
callback func (direction input.KeynavDirection) (granted bool),
|
2023-01-19 15:35:19 -07:00
|
|
|
) {
|
2023-01-30 15:01:47 -07:00
|
|
|
element.onFocusMotionRequest = callback
|
2023-01-19 15:35:19 -07:00
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *ScrollContainer) childDamageCallback (region canvas.Canvas) {
|
2023-02-26 20:20:17 -07:00
|
|
|
element.core.DamageRegion(region.Bounds())
|
2023-01-19 15:35:19 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
func (element *ScrollContainer) childFocusRequestCallback () (granted bool) {
|
2023-02-10 22:58:54 -07:00
|
|
|
if element.onFocusRequest != nil {
|
|
|
|
element.focused = element.onFocusRequest()
|
|
|
|
return element.focused
|
2023-01-19 15:35:19 -07:00
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
func (element *ScrollContainer) childFocusMotionRequestCallback (
|
2023-02-01 23:48:16 -07:00
|
|
|
direction input.KeynavDirection,
|
2023-01-19 15:35:19 -07:00
|
|
|
) (
|
|
|
|
granted bool,
|
|
|
|
) {
|
2023-01-30 15:01:47 -07:00
|
|
|
if element.onFocusMotionRequest == nil { return }
|
|
|
|
return element.onFocusMotionRequest(direction)
|
2023-01-19 15:35:19 -07:00
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *ScrollContainer) clearChildEventHandlers (child elements.Scrollable) {
|
2023-01-31 12:54:43 -07:00
|
|
|
child.DrawTo(nil)
|
2023-01-19 14:49:34 -07:00
|
|
|
child.OnDamage(nil)
|
|
|
|
child.OnMinimumSizeChange(nil)
|
2023-01-20 13:35:43 -07:00
|
|
|
child.OnScrollBoundsChange(nil)
|
2023-02-01 23:48:16 -07:00
|
|
|
if child0, ok := child.(elements.Focusable); ok {
|
2023-01-30 15:01:47 -07:00
|
|
|
child0.OnFocusRequest(nil)
|
|
|
|
child0.OnFocusMotionRequest(nil)
|
|
|
|
if child0.Focused() {
|
|
|
|
child0.HandleUnfocus()
|
2023-01-19 14:49:34 -07:00
|
|
|
}
|
|
|
|
}
|
2023-02-01 23:48:16 -07:00
|
|
|
if child0, ok := child.(elements.Flexible); ok {
|
2023-01-19 14:49:34 -07:00
|
|
|
child0.OnFlexibleHeightChange(nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 16:39:17 -07:00
|
|
|
func (element *ScrollContainer) resizeChildToFit () {
|
|
|
|
childBounds := image.Rect (
|
|
|
|
0, 0,
|
|
|
|
element.childWidth,
|
|
|
|
element.childHeight).Add(element.Bounds().Min)
|
2023-02-12 23:49:33 -07:00
|
|
|
element.child.DrawTo(canvas.Cut(element.core, childBounds))
|
2023-01-31 16:39:17 -07:00
|
|
|
}
|
|
|
|
|
2023-01-19 11:07:27 -07:00
|
|
|
func (element *ScrollContainer) recalculate () {
|
|
|
|
horizontal := &element.horizontal
|
|
|
|
vertical := &element.vertical
|
2023-02-07 22:22:40 -07:00
|
|
|
|
2023-02-26 20:20:17 -07:00
|
|
|
gutterInsetHorizontal := horizontal.theme.Padding(theme.PatternGutter)
|
|
|
|
gutterInsetVertical := vertical.theme.Padding(theme.PatternGutter)
|
2023-02-07 22:22:40 -07:00
|
|
|
|
2023-01-19 11:07:27 -07:00
|
|
|
bounds := element.Bounds()
|
2023-01-29 23:30:13 -07:00
|
|
|
thicknessHorizontal :=
|
2023-02-07 22:22:40 -07:00
|
|
|
element.config.HandleWidth() +
|
2023-01-29 23:30:13 -07:00
|
|
|
gutterInsetHorizontal[3] +
|
|
|
|
gutterInsetHorizontal[1]
|
|
|
|
thicknessVertical :=
|
2023-02-07 22:22:40 -07:00
|
|
|
element.config.HandleWidth() +
|
2023-01-29 23:30:13 -07:00
|
|
|
gutterInsetVertical[3] +
|
|
|
|
gutterInsetVertical[1]
|
2023-01-19 11:07:27 -07:00
|
|
|
|
2023-01-20 13:35:43 -07:00
|
|
|
// calculate child size
|
|
|
|
element.childWidth = bounds.Dx()
|
|
|
|
element.childHeight = bounds.Dy()
|
|
|
|
|
2023-01-19 11:07:27 -07:00
|
|
|
// reset bounds
|
2023-01-20 13:35:43 -07:00
|
|
|
horizontal.gutter = image.Rectangle { }
|
|
|
|
vertical.gutter = image.Rectangle { }
|
|
|
|
horizontal.bar = image.Rectangle { }
|
|
|
|
vertical.bar = image.Rectangle { }
|
|
|
|
|
|
|
|
// if enabled, give substance to the gutters
|
|
|
|
if horizontal.exists {
|
2023-01-31 16:39:17 -07:00
|
|
|
horizontal.gutter.Min.X = bounds.Min.X
|
2023-01-29 23:30:13 -07:00
|
|
|
horizontal.gutter.Min.Y = bounds.Max.Y - thicknessHorizontal
|
2023-01-20 13:35:43 -07:00
|
|
|
horizontal.gutter.Max.X = bounds.Max.X
|
|
|
|
horizontal.gutter.Max.Y = bounds.Max.Y
|
|
|
|
if vertical.exists {
|
2023-01-29 23:30:13 -07:00
|
|
|
horizontal.gutter.Max.X -= thicknessVertical
|
2023-01-20 13:35:43 -07:00
|
|
|
}
|
2023-01-29 23:30:13 -07:00
|
|
|
element.childHeight -= thicknessHorizontal
|
|
|
|
horizontal.track = gutterInsetHorizontal.Apply(horizontal.gutter)
|
2023-01-20 13:35:43 -07:00
|
|
|
}
|
|
|
|
if vertical.exists {
|
2023-01-29 23:30:13 -07:00
|
|
|
vertical.gutter.Min.X = bounds.Max.X - thicknessVertical
|
2023-01-20 13:35:43 -07:00
|
|
|
vertical.gutter.Max.X = bounds.Max.X
|
2023-01-31 16:39:17 -07:00
|
|
|
vertical.gutter.Min.Y = bounds.Min.Y
|
2023-01-20 13:35:43 -07:00
|
|
|
vertical.gutter.Max.Y = bounds.Max.Y
|
|
|
|
if horizontal.exists {
|
2023-01-29 23:30:13 -07:00
|
|
|
vertical.gutter.Max.Y -= thicknessHorizontal
|
2023-01-20 13:35:43 -07:00
|
|
|
}
|
2023-01-29 23:30:13 -07:00
|
|
|
element.childWidth -= thicknessVertical
|
|
|
|
vertical.track = gutterInsetVertical.Apply(vertical.gutter)
|
2023-01-20 13:35:43 -07:00
|
|
|
}
|
2023-01-19 11:07:27 -07:00
|
|
|
|
2023-01-20 13:35:43 -07:00
|
|
|
// if enabled, calculate the positions of the bars
|
|
|
|
contentBounds := element.child.ScrollContentBounds()
|
|
|
|
viewportBounds := element.child.ScrollViewportBounds()
|
|
|
|
if horizontal.exists && horizontal.enabled {
|
2023-01-28 23:49:01 -07:00
|
|
|
horizontal.bar.Min.Y = horizontal.track.Min.Y
|
|
|
|
horizontal.bar.Max.Y = horizontal.track.Max.Y
|
2023-01-20 13:35:43 -07:00
|
|
|
|
2023-01-28 23:49:01 -07:00
|
|
|
scale := float64(horizontal.track.Dx()) /
|
2023-01-20 13:35:43 -07:00
|
|
|
float64(contentBounds.Dx())
|
|
|
|
horizontal.bar.Min.X = int(float64(viewportBounds.Min.X) * scale)
|
|
|
|
horizontal.bar.Max.X = int(float64(viewportBounds.Max.X) * scale)
|
|
|
|
|
2023-01-28 23:49:01 -07:00
|
|
|
horizontal.bar.Min.X += horizontal.track.Min.X
|
|
|
|
horizontal.bar.Max.X += horizontal.track.Min.X
|
2023-01-19 11:07:27 -07:00
|
|
|
}
|
2023-01-20 13:35:43 -07:00
|
|
|
if vertical.exists && vertical.enabled {
|
2023-01-28 23:49:01 -07:00
|
|
|
vertical.bar.Min.X = vertical.track.Min.X
|
|
|
|
vertical.bar.Max.X = vertical.track.Max.X
|
2023-01-20 13:35:43 -07:00
|
|
|
|
2023-01-28 23:49:01 -07:00
|
|
|
scale := float64(vertical.track.Dy()) /
|
2023-01-20 13:35:43 -07:00
|
|
|
float64(contentBounds.Dy())
|
|
|
|
vertical.bar.Min.Y = int(float64(viewportBounds.Min.Y) * scale)
|
|
|
|
vertical.bar.Max.Y = int(float64(viewportBounds.Max.Y) * scale)
|
|
|
|
|
2023-01-28 23:49:01 -07:00
|
|
|
vertical.bar.Min.Y += vertical.track.Min.Y
|
|
|
|
vertical.bar.Max.Y += vertical.track.Min.Y
|
2023-01-19 11:07:27 -07:00
|
|
|
}
|
|
|
|
|
2023-01-20 13:35:43 -07:00
|
|
|
// if the scroll bars are out of bounds, don't display them.
|
2023-01-28 23:49:01 -07:00
|
|
|
if horizontal.bar.Dx() >= horizontal.track.Dx() {
|
2023-01-20 13:35:43 -07:00
|
|
|
horizontal.bar = image.Rectangle { }
|
|
|
|
}
|
2023-01-28 23:49:01 -07:00
|
|
|
if vertical.bar.Dy() >= vertical.track.Dy() {
|
2023-01-20 13:35:43 -07:00
|
|
|
vertical.bar = image.Rectangle { }
|
|
|
|
}
|
2023-01-19 11:07:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *ScrollContainer) draw () {
|
2023-02-07 22:22:40 -07:00
|
|
|
deadPattern := element.theme.Pattern (
|
2023-02-26 20:20:17 -07:00
|
|
|
theme.PatternDead, theme.State { })
|
|
|
|
artist.DrawBounds (
|
2023-02-12 23:49:33 -07:00
|
|
|
element.core, deadPattern,
|
2023-01-19 16:03:50 -07:00
|
|
|
image.Rect (
|
2023-01-20 13:35:43 -07:00
|
|
|
element.vertical.gutter.Min.X,
|
|
|
|
element.horizontal.gutter.Min.Y,
|
|
|
|
element.vertical.gutter.Max.X,
|
|
|
|
element.horizontal.gutter.Max.Y))
|
2023-01-19 16:29:57 -07:00
|
|
|
element.drawHorizontalBar()
|
|
|
|
element.drawVerticalBar()
|
2023-01-19 11:07:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *ScrollContainer) drawHorizontalBar () {
|
2023-02-26 20:20:17 -07:00
|
|
|
state := theme.State {
|
2023-01-28 23:49:01 -07:00
|
|
|
Disabled: !element.horizontal.enabled,
|
2023-02-07 22:22:40 -07:00
|
|
|
Pressed: element.horizontal.dragging,
|
|
|
|
}
|
2023-02-08 12:36:14 -07:00
|
|
|
gutterPattern := element.horizontal.theme.Pattern(theme.PatternGutter, state)
|
2023-02-26 20:20:17 -07:00
|
|
|
artist.DrawBounds(element.core, gutterPattern, element.horizontal.gutter)
|
2023-01-28 23:49:01 -07:00
|
|
|
|
2023-02-08 12:36:14 -07:00
|
|
|
handlePattern := element.horizontal.theme.Pattern(theme.PatternHandle, state)
|
2023-02-26 20:20:17 -07:00
|
|
|
artist.DrawBounds(element.core, handlePattern, element.horizontal.bar)
|
2023-01-19 11:07:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *ScrollContainer) drawVerticalBar () {
|
2023-02-26 20:20:17 -07:00
|
|
|
state := theme.State {
|
2023-01-28 23:49:01 -07:00
|
|
|
Disabled: !element.vertical.enabled,
|
2023-02-07 22:22:40 -07:00
|
|
|
Pressed: element.vertical.dragging,
|
|
|
|
}
|
2023-02-08 12:36:14 -07:00
|
|
|
gutterPattern := element.vertical.theme.Pattern(theme.PatternGutter, state)
|
2023-02-26 20:20:17 -07:00
|
|
|
artist.DrawBounds(element.core, gutterPattern, element.vertical.gutter)
|
2023-01-28 23:49:01 -07:00
|
|
|
|
2023-02-08 12:36:14 -07:00
|
|
|
handlePattern := element.vertical.theme.Pattern(theme.PatternHandle, state)
|
2023-02-26 20:20:17 -07:00
|
|
|
artist.DrawBounds(element.core, handlePattern, element.vertical.bar)
|
2023-01-19 11:07:27 -07:00
|
|
|
}
|
|
|
|
|
2023-01-20 23:15:30 -07:00
|
|
|
func (element *ScrollContainer) dragHorizontalBar (mousePosition image.Point) {
|
|
|
|
scrollX :=
|
|
|
|
float64(element.child.ScrollContentBounds().Dx()) /
|
2023-01-28 23:49:01 -07:00
|
|
|
float64(element.horizontal.track.Dx()) *
|
2023-01-20 23:24:24 -07:00
|
|
|
float64(mousePosition.X - element.horizontal.dragOffset)
|
2023-01-20 23:15:30 -07:00
|
|
|
scrollY := element.child.ScrollViewportBounds().Min.Y
|
|
|
|
element.child.ScrollTo(image.Pt(int(scrollX), scrollY))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *ScrollContainer) dragVerticalBar (mousePosition image.Point) {
|
|
|
|
scrollY :=
|
|
|
|
float64(element.child.ScrollContentBounds().Dy()) /
|
2023-01-28 23:49:01 -07:00
|
|
|
float64(element.vertical.track.Dy()) *
|
2023-01-20 23:24:24 -07:00
|
|
|
float64(mousePosition.Y - element.vertical.dragOffset)
|
2023-01-20 23:15:30 -07:00
|
|
|
scrollX := element.child.ScrollViewportBounds().Min.X
|
|
|
|
element.child.ScrollTo(image.Pt(scrollX, int(scrollY)))
|
|
|
|
}
|
|
|
|
|
2023-01-19 11:07:27 -07:00
|
|
|
func (element *ScrollContainer) updateMinimumSize () {
|
2023-02-26 20:20:17 -07:00
|
|
|
gutterInsetHorizontal := element.horizontal.theme.Padding(theme.PatternGutter)
|
|
|
|
gutterInsetVertical := element.vertical.theme.Padding(theme.PatternGutter)
|
2023-01-29 23:30:13 -07:00
|
|
|
|
|
|
|
thicknessHorizontal :=
|
2023-02-07 22:22:40 -07:00
|
|
|
element.config.HandleWidth() +
|
2023-01-29 23:30:13 -07:00
|
|
|
gutterInsetHorizontal[3] +
|
|
|
|
gutterInsetHorizontal[1]
|
|
|
|
thicknessVertical :=
|
2023-02-07 22:22:40 -07:00
|
|
|
element.config.HandleWidth() +
|
2023-01-29 23:30:13 -07:00
|
|
|
gutterInsetVertical[3] +
|
|
|
|
gutterInsetVertical[1]
|
2023-01-28 23:49:01 -07:00
|
|
|
|
2023-01-29 23:30:13 -07:00
|
|
|
width := thicknessHorizontal
|
|
|
|
height := thicknessVertical
|
2023-01-19 11:07:27 -07:00
|
|
|
if element.child != nil {
|
|
|
|
childWidth, childHeight := element.child.MinimumSize()
|
|
|
|
width += childWidth
|
|
|
|
height += childHeight
|
|
|
|
}
|
|
|
|
element.core.SetMinimumSize(width, height)
|
|
|
|
}
|
2023-01-20 13:35:43 -07:00
|
|
|
|
|
|
|
func (element *ScrollContainer) childScrollBoundsChangeCallback () {
|
|
|
|
element.horizontal.enabled,
|
|
|
|
element.vertical.enabled = element.child.ScrollAxes()
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.recalculate()
|
|
|
|
element.drawHorizontalBar()
|
|
|
|
element.drawVerticalBar()
|
2023-01-20 13:52:46 -07:00
|
|
|
element.core.DamageRegion(element.horizontal.gutter)
|
|
|
|
element.core.DamageRegion(element.vertical.gutter)
|
2023-01-20 13:35:43 -07:00
|
|
|
}
|
|
|
|
}
|