Skip to content

Commit

Permalink
fix ScrollToOffset with viewport larger than content
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Mar 12, 2024
1 parent 417de2f commit a3dd415
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
9 changes: 7 additions & 2 deletions widget/gridwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,13 @@ func (l *GridWrap) ScrollToOffset(offset float32) {
}
if offset < 0 {
offset = 0
} else if contentH := l.contentMinSize().Height; offset > contentH {
offset = contentH
}
contentHeight := l.contentMinSize().Height
if l.Size().Height >= contentHeight {
return // content fully visible - no need to scroll
}
if offset > contentHeight {
offset = contentHeight
}
l.scroller.Offset.Y = offset
l.offsetUpdated(l.scroller.Offset)
Expand Down
8 changes: 7 additions & 1 deletion widget/gridwrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func TestGridWrap_ScrollTo(t *testing.T) {

func TestGridWrap_ScrollToOffset(t *testing.T) {
g := createGridWrap(10)
g.Resize(fyne.NewSize(10, 10))

g.ScrollToOffset(2)
assert.Equal(t, float32(2), g.GetScrollOffset())
Expand All @@ -109,7 +110,12 @@ func TestGridWrap_ScrollToOffset(t *testing.T) {
assert.Equal(t, float32(0), g.GetScrollOffset())

g.ScrollToOffset(10000)
assert.LessOrEqual(t, g.GetScrollOffset(), float32(50) /*upper bound on content height*/)
assert.LessOrEqual(t, g.GetScrollOffset(), float32(500) /*upper bound on content height*/)

// GridWrap viewport is larger than content size
g.Resize(fyne.NewSize(50, 250))
g.ScrollToOffset(20)
assert.Equal(t, float32(0), g.GetScrollOffset()) // doesn't scroll
}

func TestGridWrap_ScrollToTop(t *testing.T) {
Expand Down
9 changes: 7 additions & 2 deletions widget/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,13 @@ func (l *List) ScrollToOffset(offset float32) {
}
if offset < 0 {
offset = 0
} else if h := l.contentMinSize().Height; offset > h {
offset = h
}
contentHeight := l.contentMinSize().Height
if l.Size().Height >= contentHeight {
return // content fully visible - no need to scroll
}
if offset > contentHeight {
offset = contentHeight
}
l.scroller.Offset.Y = offset
l.offsetUpdated(l.scroller.Offset)
Expand Down
6 changes: 6 additions & 0 deletions widget/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ func TestList_ScrollToTop(t *testing.T) {

func TestList_ScrollOffset(t *testing.T) {
list := createList(10)
list.Resize(fyne.NewSize(20, 15))

offset := float32(25)
list.ScrollToOffset(25)
Expand All @@ -252,6 +253,11 @@ func TestList_ScrollOffset(t *testing.T) {

list.ScrollToOffset(1000)
assert.LessOrEqual(t, list.GetScrollOffset(), float32(500) /*upper bound on content height*/)

// list viewport is larger than content size
list.Resize(fyne.NewSize(100, 500))
list.ScrollToOffset(20)
assert.Equal(t, float32(0), list.GetScrollOffset()) // doesn't scroll
}

func TestList_Selection(t *testing.T) {
Expand Down

0 comments on commit a3dd415

Please sign in to comment.