Scrolling over a ScrollContainer will now scroll it

This commit is contained in:
Sasha Koshka 2023-04-16 14:12:55 -04:00
parent b9c8350677
commit 7d4ddaf387
4 changed files with 49 additions and 9 deletions

View File

@ -81,6 +81,21 @@ func (entity *entity) childAt (point image.Point) *entity {
return entity return entity
} }
func (entity *entity) scrollTargetChildAt (point image.Point) *entity {
for _, child := range entity.children {
if point.In(child.bounds) {
result := child.scrollTargetChildAt(point)
if result != nil { return result }
break
}
}
if _, ok := entity.element.(tomo.ScrollTarget); ok {
return entity
}
return nil
}
// ----------- Entity ----------- // // ----------- Entity ----------- //
func (entity *entity) Invalidate () { func (entity *entity) Invalidate () {

View File

@ -190,20 +190,22 @@ func (window *window) handleButtonPress (
insideWindow := point.In(window.canvas.Bounds()) insideWindow := point.In(window.canvas.Bounds())
scrolling := buttonEvent.Detail >= 4 && buttonEvent.Detail <= 7 scrolling := buttonEvent.Detail >= 4 && buttonEvent.Detail <= 7
underneath := window.system.childAt(point)
if !insideWindow && window.shy && !scrolling { if !insideWindow && window.shy && !scrolling {
window.Close() window.Close()
} else if scrolling { } else if scrolling {
if child, ok := underneath.element.(tomo.ScrollTarget); ok { underneath := window.system.scrollTargetChildAt(point)
sum := scrollSum { } if underneath != nil {
sum.add(buttonEvent.Detail, window, buttonEvent.State) if child, ok := underneath.element.(tomo.ScrollTarget); ok {
window.compressScrollSum(buttonEvent, &sum) sum := scrollSum { }
child.HandleScroll ( sum.add(buttonEvent.Detail, window, buttonEvent.State)
point.X, point.Y, window.compressScrollSum(buttonEvent, &sum)
float64(sum.x), float64(sum.y)) child.HandleScroll (
point.X, point.Y,
float64(sum.x), float64(sum.y))
}
} }
} else { } else {
underneath := window.system.childAt(point)
if child, ok := underneath.element.(tomo.MouseTarget); ok { if child, ok := underneath.element.(tomo.MouseTarget); ok {
window.system.drags[buttonEvent.Detail] = child window.system.drags[buttonEvent.Detail] = child
child.HandleMouseDown ( child.HandleMouseDown (

View File

@ -123,6 +123,11 @@ func (system *system) childAt (point image.Point) *entity {
return system.child.childAt(point) return system.child.childAt(point)
} }
func (system *system) scrollTargetChildAt (point image.Point) *entity {
if system.child == nil { return nil }
return system.child.scrollTargetChildAt(point)
}
func (system *system) resizeChildToFit () { func (system *system) resizeChildToFit () {
system.child.bounds = system.canvas.Bounds() system.child.bounds = system.canvas.Bounds()
system.child.clippedBounds = system.child.bounds system.child.clippedBounds = system.child.bounds

View File

@ -148,6 +148,16 @@ func (element *Scroll) HandleChildScrollBoundsChange (tomo.Scrollable) {
} }
} }
func (element *Scroll) HandleScroll (
x, y int,
deltaX, deltaY float64,
) {
horizontal, vertical := element.child.ScrollAxes()
if !horizontal { deltaX = 0 }
if !vertical { deltaY = 0 }
element.scrollChildBy(int(deltaX), int(deltaY))
}
func (element *Scroll) SetTheme (theme tomo.Theme) { func (element *Scroll) SetTheme (theme tomo.Theme) {
if theme == element.theme.Theme { return } if theme == element.theme.Theme { return }
element.theme.Theme = theme element.theme.Theme = theme
@ -195,3 +205,11 @@ func (element *Scroll) updateEnabled () {
element.vertical.SetEnabled(vertical) element.vertical.SetEnabled(vertical)
} }
} }
func (element *Scroll) scrollChildBy (x, y int) {
if element.child == nil { return }
scrollPoint :=
element.child.ScrollViewportBounds().Min.
Add(image.Pt(x, y))
element.child.ScrollTo(scrollPoint)
}