Skip to content

Commit

Permalink
pull in early Fyne 2.5 updates to GridWrap and List
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Feb 23, 2024
1 parent b7a3502 commit 8ae51d5
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 45 deletions.
47 changes: 32 additions & 15 deletions widget/gridwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (l *GridWrap) scrollTo(id GridWrapItemID) {
if l.scroller == nil {
return
}
row := math.Floor(float64(id) / float64(l.getColCount()))
row := math.Floor(float64(id) / float64(l.ColumnCount()))
y := float32(row)*l.itemMin.Height + float32(row)*theme.Padding()
if y < l.scroller.Offset.Y {
l.scroller.Offset.Y = y
Expand Down Expand Up @@ -215,8 +215,17 @@ func (l *GridWrap) ScrollToTop() {

// ScrollToOffset scrolls the list to the given offset position
func (l *GridWrap) ScrollToOffset(offset float32) {
if l.scroller == nil {
return
}
if offset < 0 {
offset = 0
} else if contentH := l.contentMinSize().Height; offset > contentH {
offset = contentH
}
l.scroller.Offset.Y = offset
l.offsetUpdated(l.scroller.Offset)
l.Refresh()
}

// TypedKey is called if a key event happens while this GridWrap is focused.
Expand All @@ -232,7 +241,7 @@ func (l *GridWrap) TypedKey(event *fyne.KeyEvent) {
count = f()
}
l.RefreshItem(l.currentFocus)
l.currentFocus += l.getColCount()
l.currentFocus += l.ColumnCount()
if l.currentFocus >= count-1 {
l.currentFocus = count - 1
}
Expand All @@ -242,7 +251,7 @@ func (l *GridWrap) TypedKey(event *fyne.KeyEvent) {
if l.currentFocus <= 0 {
return
}
if l.currentFocus%l.getColCount() == 0 {
if l.currentFocus%l.ColumnCount() == 0 {
return
}

Expand All @@ -254,7 +263,7 @@ func (l *GridWrap) TypedKey(event *fyne.KeyEvent) {
if f := l.Length; f != nil && l.currentFocus >= f()-1 {
return
}
if (l.currentFocus+1)%l.getColCount() == 0 {
if (l.currentFocus+1)%l.ColumnCount() == 0 {
return
}

Expand All @@ -267,7 +276,7 @@ func (l *GridWrap) TypedKey(event *fyne.KeyEvent) {
return
}
l.RefreshItem(l.currentFocus)
l.currentFocus -= l.getColCount()
l.currentFocus -= l.ColumnCount()
if l.currentFocus < 0 {
l.currentFocus = 0
}
Expand Down Expand Up @@ -317,6 +326,17 @@ func (l *GridWrap) UnselectAll() {
}
}

func (l *GridWrap) contentMinSize() fyne.Size {
padding := theme.Padding()
if lenF := l.Length; lenF != nil {
cols := l.ColumnCount()
rows := float32(math.Ceil(float64(lenF()) / float64(cols)))
return fyne.NewSize(l.itemMin.Width,
(l.itemMin.Height+padding)*rows-padding)
}
return fyne.NewSize(0, 0)
}

// Declare conformity with WidgetRenderer interface.
var _ fyne.WidgetRenderer = (*gridWrapRenderer)(nil)

Expand Down Expand Up @@ -495,14 +515,7 @@ func (l *gridWrapLayout) Layout(_ []fyne.CanvasObject, _ fyne.Size) {
}

func (l *gridWrapLayout) MinSize(_ []fyne.CanvasObject) fyne.Size {
padding := theme.Padding()
if lenF := l.list.Length; lenF != nil {
cols := l.list.getColCount()
rows := float32(math.Ceil(float64(lenF()) / float64(cols)))
return fyne.NewSize(l.list.itemMin.Width,
(l.list.itemMin.Height+padding)*rows-padding)
}
return fyne.NewSize(0, 0)
return l.list.contentMinSize()
}

func (l *gridWrapLayout) getItem() *gridWrapItem {
Expand Down Expand Up @@ -557,7 +570,11 @@ func (l *gridWrapLayout) setupGridItem(li *gridWrapItem, id GridWrapItemID, focu
}
}

func (l *GridWrap) getColCount() int {
// ColumnCount returns the number of columns that are/will be shown
// in this GridWrap, based on the widget's current width.
//
// Since: 2.5
func (l *GridWrap) ColumnCount() int {
if l.colCountCache < 1 {
padding := theme.Padding()
l.colCountCache = 1
Expand All @@ -579,7 +596,7 @@ func (l *gridWrapLayout) updateGrid(refresh bool) {
length = f()
}

colCount := l.list.getColCount()
colCount := l.list.ColumnCount()
visibleRowsCount := int(math.Ceil(float64(l.list.scroller.Size().Height)/float64(l.list.itemMin.Height+padding))) + 1

offY := l.list.offsetY - float32(math.Mod(float64(l.list.offsetY), float64(l.list.itemMin.Height+padding)))
Expand Down
71 changes: 41 additions & 30 deletions widget/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,17 @@ func (l *List) ScrollToTop() {
//
// Since: 2.5
func (l *List) ScrollToOffset(offset float32) {
if l.scroller != nil {
l.scroller.Offset.Y = offset
l.offsetUpdated(l.scroller.Offset)
if l.scroller == nil {
return
}
if offset < 0 {
offset = 0
} else if h := l.contentMinSize().Height; offset > h {
offset = h
}
l.scroller.Offset.Y = offset
l.offsetUpdated(l.scroller.Offset)
l.Refresh()
}

// GetScrollOffset returns the current scroll offset position
Expand Down Expand Up @@ -338,6 +345,36 @@ func (l *List) UnselectAll() {
}
}

func (l *List) contentMinSize() fyne.Size {
l.propertyLock.Lock()
defer l.propertyLock.Unlock()
items := 0
if f := l.Length; f == nil {
return fyne.NewSize(0, 0)
} else {
items = f()
}

separatorThickness := theme.Padding()
if l.itemHeights == nil || len(l.itemHeights) == 0 {
return fyne.NewSize(l.itemMin.Width,
(l.itemMin.Height+separatorThickness)*float32(items)-separatorThickness)
}

height := float32(0)
templateHeight := l.itemMin.Height
for item := 0; item < items; item++ {
itemHeight, ok := l.itemHeights[item]
if ok {
height += itemHeight
} else {
height += templateHeight
}
}

return fyne.NewSize(l.itemMin.Width, height+separatorThickness*float32(items-1))
}

// fills l.visibleRowHeights and also returns offY and minRow
func (l *listLayout) calculateVisibleRowHeights(itemHeight float32, length int) (offY float32, minRow int) {
rowOffset := float32(0)
Expand Down Expand Up @@ -575,33 +612,7 @@ func (l *listLayout) Layout([]fyne.CanvasObject, fyne.Size) {
}

func (l *listLayout) MinSize([]fyne.CanvasObject) fyne.Size {
l.list.propertyLock.Lock()
defer l.list.propertyLock.Unlock()
items := 0
if f := l.list.Length; f == nil {
return fyne.NewSize(0, 0)
} else {
items = f()
}

separatorThickness := theme.Padding()
if l.list.itemHeights == nil || len(l.list.itemHeights) == 0 {
return fyne.NewSize(l.list.itemMin.Width,
(l.list.itemMin.Height+separatorThickness)*float32(items)-separatorThickness)
}

height := float32(0)
templateHeight := l.list.itemMin.Height
for item := 0; item < items; item++ {
itemHeight, ok := l.list.itemHeights[item]
if ok {
height += itemHeight
} else {
height += templateHeight
}
}

return fyne.NewSize(l.list.itemMin.Width, height+separatorThickness*float32(items-1))
return l.list.contentMinSize()
}

func (l *listLayout) getItem() *listItem {
Expand Down

0 comments on commit 8ae51d5

Please sign in to comment.