Skip to content

Commit

Permalink
make sure scroll bar shrinks at end of drag if mouse out of area
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Jun 25, 2024
1 parent 0e26564 commit 10905a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
15 changes: 10 additions & 5 deletions internal/widget/scroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (b *scrollBar) DragEnd() {
if fyne.CurrentDevice().IsMobile() {
b.area.MouseOut()
}
b.area.Refresh()
}

func (b *scrollBar) Dragged(e *fyne.DragEvent) {
Expand Down Expand Up @@ -129,6 +130,10 @@ func newScrollBar(area *scrollBarArea) *scrollBar {
return b
}

func (r *scrollBarArea) isLarge() bool {
return r.isMouseIn || r.isDragged
}

type scrollBarAreaRenderer struct {
BaseRenderer
area *scrollBarArea
Expand All @@ -149,7 +154,7 @@ func (r *scrollBarAreaRenderer) Layout(_ fyne.Size) {

func (r *scrollBarAreaRenderer) MinSize() fyne.Size {
min := theme.ScrollBarSize()
if !r.area.isLarge {
if !r.area.isLarge() {
min = theme.ScrollBarSmallSize() * 2
}
switch r.area.orientation {
Expand Down Expand Up @@ -177,7 +182,7 @@ func (r *scrollBarAreaRenderer) barSizeAndOffset(contentOffset, contentLength, s
if contentOffset != 0 {
lengthOffset = (scrollLength - length) * (contentOffset / (contentLength - scrollLength))
}
if r.area.isLarge {
if r.area.isLarge() {
width = scrollBarSize
} else {
widthOffset = theme.ScrollBarSmallSize()
Expand All @@ -192,7 +197,7 @@ type scrollBarArea struct {
Base

isDragged bool
isLarge bool
isMouseIn bool
scroll *Scroll
orientation scrollBarOrientation
}
Expand All @@ -203,19 +208,19 @@ func (a *scrollBarArea) CreateRenderer() fyne.WidgetRenderer {
}

func (a *scrollBarArea) MouseIn(*desktop.MouseEvent) {
a.isLarge = true
a.isMouseIn = true
a.scroll.Refresh()
}

func (a *scrollBarArea) MouseMoved(*desktop.MouseEvent) {
}

func (a *scrollBarArea) MouseOut() {
a.isMouseIn = false
if a.isDragged {
return
}

a.isLarge = false
a.scroll.Refresh()
}

Expand Down
12 changes: 6 additions & 6 deletions internal/widget/scroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,19 +791,19 @@ func TestScrollBar_LargeHandleWhileInDrag(t *testing.T) {

// Make sure that hovering makes the bar large.
mouseEvent := &desktop.MouseEvent{}
assert.False(t, scrollBarHoriz.area.isLarge)
assert.False(t, scrollBarHoriz.area.isLarge())
scrollBarHoriz.MouseIn(mouseEvent)
assert.True(t, scrollBarHoriz.area.isLarge)
assert.True(t, scrollBarHoriz.area.isLarge())
scrollBarHoriz.MouseOut()
assert.False(t, scrollBarHoriz.area.isLarge)
assert.False(t, scrollBarHoriz.area.isLarge())

// Make sure that the bar stays large when dragging, even if the mouse leaves the bar.
dragEvent := &fyne.DragEvent{Dragged: fyne.Delta{DX: 10}}
scrollBarHoriz.Dragged(dragEvent)
assert.True(t, scrollBarHoriz.area.isLarge)
assert.True(t, scrollBarHoriz.area.isLarge())
scrollBarHoriz.MouseOut()
assert.True(t, scrollBarHoriz.area.isLarge)
assert.True(t, scrollBarHoriz.area.isLarge())
scrollBarHoriz.DragEnd()
scrollBarHoriz.MouseOut()
assert.False(t, scrollBarHoriz.area.isLarge)
assert.False(t, scrollBarHoriz.area.isLarge())
}

0 comments on commit 10905a8

Please sign in to comment.