Use GridWrap from fyne-x
With https://github.com/fyne-io/fyne-x/pull/56 merged, the local implementation can be removed to rely on the upstream version.
This commit is contained in:
committed by
Drew Weymouth
parent
39b17abd7d
commit
7b15fe95e4
@@ -3,7 +3,8 @@ module github.com/dweymouth/supersonic
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
fyne.io/fyne/v2 v2.3.4
|
||||
fyne.io/fyne/v2 v2.3.5
|
||||
fyne.io/x/fyne v0.0.0-20230611151101-afdcd6b92cf3
|
||||
github.com/20after4/configdir v0.1.1
|
||||
github.com/dweymouth/go-mpv v0.0.0-20230406003141-7f1858e503ee
|
||||
github.com/dweymouth/go-subsonic v0.0.0-20230513020020-0790f53c2868
|
||||
@@ -14,7 +15,7 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
fyne.io/systray v1.10.1-0.20230403195833-7dc3c09283d6 // indirect
|
||||
fyne.io/systray v1.10.1-0.20230602210930-b6a2d6ca2a7b // indirect
|
||||
github.com/alessio/shellescape v1.4.1 // indirect
|
||||
github.com/danieljoos/wincred v1.1.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
@@ -30,6 +31,7 @@ require (
|
||||
github.com/goki/freetype v0.0.0-20220119013949-7a161fd3728c // indirect
|
||||
github.com/gopherjs/gopherjs v1.17.2 // indirect
|
||||
github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e // indirect
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/srwiley/oksvg v0.0.0-20220731023508-a61f04f16b76 // indirect
|
||||
github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780 // indirect
|
||||
@@ -44,4 +46,4 @@ require (
|
||||
honnef.co/go/js/dom v0.0.0-20210725211120-f030747120f2 // indirect
|
||||
)
|
||||
|
||||
replace fyne.io/fyne/v2 v2.3.4 => github.com/dweymouth/fyne/v2 v2.3.0-rc1.0.20230505012127-ca61c153b2a5
|
||||
replace fyne.io/fyne/v2 v2.3.5 => github.com/dweymouth/fyne/v2 v2.3.0-rc1.0.20230505012127-ca61c153b2a5
|
||||
|
||||
@@ -37,10 +37,14 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
|
||||
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
|
||||
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
|
||||
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
|
||||
fyne.io/systray v1.10.1-0.20230403195833-7dc3c09283d6 h1:lHt8dm97Uy9ggtnt9N6XOlsp76wXmRAh3SjReWm1e2Q=
|
||||
fyne.io/systray v1.10.1-0.20230403195833-7dc3c09283d6/go.mod h1:oM2AQqGJ1AMo4nNqZFYU8xYygSBZkW2hmdJ7n4yjedE=
|
||||
fyne.io/systray v1.10.1-0.20230602210930-b6a2d6ca2a7b h1:MP1cUnIdF1cxrMhK9iw9H0JP3zopyD1zi84BqU6WTsE=
|
||||
fyne.io/systray v1.10.1-0.20230602210930-b6a2d6ca2a7b/go.mod h1:oM2AQqGJ1AMo4nNqZFYU8xYygSBZkW2hmdJ7n4yjedE=
|
||||
fyne.io/x/fyne v0.0.0-20230611151101-afdcd6b92cf3 h1:PYQpybIWYSfheDLt5qvd0acmDQIPs8Npem3INFXX72w=
|
||||
fyne.io/x/fyne v0.0.0-20230611151101-afdcd6b92cf3/go.mod h1:Ygr+Jy2eAg35VdG3qX9WDyTnj8ruxJX3M4eh+PjRsoE=
|
||||
github.com/20after4/configdir v0.1.1 h1:ylL5dO+aGxBV4jDtG9Ej9hZfeE8Fw9jULQVq0N+ErJ8=
|
||||
github.com/20after4/configdir v0.1.1/go.mod h1:kZ7yOiD6MFUABqBI2/N62QA1wyEB8sGeI+mygme1pl4=
|
||||
github.com/Andrew-M-C/go.jsonvalue v1.1.2-0.20211223013816-e873b56b4a84/go.mod h1:oTJGG91FhtsxvUFVwHSvr6zuaTcAuroj/ToxfT7Ox8U=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
@@ -76,6 +80,7 @@ github.com/dweymouth/go-mpv v0.0.0-20230406003141-7f1858e503ee h1:ZGyJ6wp7CAfT31
|
||||
github.com/dweymouth/go-mpv v0.0.0-20230406003141-7f1858e503ee/go.mod h1:Ov0ieN90M7i+0k3OxhA/g1dozGs+UcPHDsMKqPgRDk0=
|
||||
github.com/dweymouth/go-subsonic v0.0.0-20230513020020-0790f53c2868 h1:403Dden/cdQyDM8ydonHLxTBPjgQXZ76PUfmBG92+Dw=
|
||||
github.com/dweymouth/go-subsonic v0.0.0-20230513020020-0790f53c2868/go.mod h1:fUez6NFiEJiQTZizZ1BThZr5GJXAbigzGYjEPNm4tdI=
|
||||
github.com/eclipse/paho.mqtt.golang v1.3.5/go.mod h1:eTzb4gxwwyWpqBUHGQZ4ABAV7+Jgm1PklsYT/eo8Hcc=
|
||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
||||
@@ -184,6 +189,7 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORR
|
||||
github.com/gopherjs/gopherjs v0.0.0-20211219123610-ec9572f70e60/go.mod h1:cz9oNYuRUWGdHmLF2IodMLkAhcPtXeULvcBNagUrxTI=
|
||||
github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g=
|
||||
github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/goxjs/gl v0.0.0-20210104184919-e3fafc6f8f2a/go.mod h1:dy/f2gjY09hwVfIyATps4G2ai7/hLwLkc5TrPqONuXY=
|
||||
github.com/goxjs/glfw v0.0.0-20191126052801-d2efb5f20838/go.mod h1:oS8P8gVOT4ywTcjV6wZlOU4GuVFQ8F5328KY3MJ79CY=
|
||||
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
|
||||
@@ -244,6 +250,7 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo=
|
||||
github.com/neelance/sourcemap v0.0.0-20200213170602-2833bce08e4c/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM=
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
|
||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
|
||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||
@@ -295,6 +302,7 @@ github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69
|
||||
github.com/tevino/abool v1.2.0 h1:heAkClL8H6w+mK5md9dzsuohKeXHUpY7Vw0ZCKW+huA=
|
||||
github.com/tevino/abool v1.2.0/go.mod h1:qc66Pna1RiIsPa7O4Egxxs9OqkuxDX55zznh9K07Tzg=
|
||||
github.com/urfave/cli/v2 v2.4.0/go.mod h1:NX9W0zmTvedE5oDoOMs2RTC8RvdK98NTYZE5LbaEYPg=
|
||||
github.com/wagslane/go-password-validator v0.3.0/go.mod h1:TI1XJ6T5fRdRnHqHt14pvy1tNVnrwe7m3/f1f2fDphQ=
|
||||
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
@@ -390,6 +398,7 @@ golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/go.mod h1:z5CRVTTTmAJ677TzLL
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
golang.org/x/net v0.0.0-20200501053045-e0ff5e5a1de5/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
xwidget "fyne.io/x/fyne/widget"
|
||||
)
|
||||
|
||||
const batchFetchSize = 6
|
||||
@@ -72,7 +73,7 @@ type GridView struct {
|
||||
|
||||
GridViewState
|
||||
|
||||
grid *GridWrap
|
||||
grid *xwidget.GridWrap
|
||||
}
|
||||
|
||||
type GridViewState struct {
|
||||
@@ -184,7 +185,7 @@ func (g *GridView) ScrollToOffset(offs float32) {
|
||||
}
|
||||
|
||||
func (g *GridView) createGridWrap() {
|
||||
g.grid = NewGridWrap(
|
||||
g.grid = xwidget.NewGridWrap(
|
||||
func() int {
|
||||
return g.lenItems()
|
||||
},
|
||||
@@ -219,7 +220,7 @@ func (g *GridView) createGridWrap() {
|
||||
return card
|
||||
},
|
||||
// update func
|
||||
func(itemID GridWrapItemID, obj fyne.CanvasObject) {
|
||||
func(itemID xwidget.GridWrapItemID, obj fyne.CanvasObject) {
|
||||
ac := obj.(*GridViewItem)
|
||||
g.doUpdateItemCard(int(itemID), ac)
|
||||
},
|
||||
|
||||
@@ -1,351 +0,0 @@
|
||||
// TODO: remove this file once https://github.com/fyne-io/fyne-x/pull/56 lands in fyne-x
|
||||
// and use the fyne-x version.
|
||||
package widgets
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sync"
|
||||
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/canvas"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"fyne.io/fyne/v2/data/binding"
|
||||
"fyne.io/fyne/v2/theme"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
)
|
||||
|
||||
// Declare conformity with Widget interface.
|
||||
var _ fyne.Widget = (*GridWrap)(nil)
|
||||
|
||||
// GridWrapItemID is the ID of an individual item in the GridWrap widget.
|
||||
type GridWrapItemID int
|
||||
|
||||
// GridWrap is a widget with an API very similar to widget.List,
|
||||
// that lays out items in a scrollable wrapping grid similar to container.NewGridWrap.
|
||||
// It caches and reuses widgets for performance.
|
||||
type GridWrap struct {
|
||||
widget.BaseWidget
|
||||
|
||||
Length func() int `json:"-"`
|
||||
CreateItem func() fyne.CanvasObject `json:"-"`
|
||||
UpdateItem func(id GridWrapItemID, item fyne.CanvasObject) `json:"-"`
|
||||
|
||||
scroller *container.Scroll
|
||||
itemMin fyne.Size
|
||||
offsetY float32
|
||||
offsetUpdated func(fyne.Position)
|
||||
}
|
||||
|
||||
// NewGridWrap creates and returns a GridWrap widget for displaying items in
|
||||
// a wrapping grid layout with scrolling and caching for performance.
|
||||
func NewGridWrap(length func() int, createItem func() fyne.CanvasObject, updateItem func(GridWrapItemID, fyne.CanvasObject)) *GridWrap {
|
||||
gwList := &GridWrap{Length: length, CreateItem: createItem, UpdateItem: updateItem}
|
||||
gwList.ExtendBaseWidget(gwList)
|
||||
return gwList
|
||||
}
|
||||
|
||||
// NewGridWrapWithData creates a new GridWrap widget that will display the contents of the provided data.
|
||||
func NewGridWrapWithData(data binding.DataList, createItem func() fyne.CanvasObject, updateItem func(binding.DataItem, fyne.CanvasObject)) *GridWrap {
|
||||
gwList := NewGridWrap(
|
||||
data.Length,
|
||||
createItem,
|
||||
func(i GridWrapItemID, o fyne.CanvasObject) {
|
||||
item, err := data.GetItem(int(i))
|
||||
if err != nil {
|
||||
fyne.LogError(fmt.Sprintf("Error getting data item %d", i), err)
|
||||
return
|
||||
}
|
||||
updateItem(item, o)
|
||||
})
|
||||
|
||||
data.AddListener(binding.NewDataListener(gwList.Refresh))
|
||||
return gwList
|
||||
}
|
||||
|
||||
// CreateRenderer is a private method to Fyne which links this widget to its renderer.
|
||||
func (l *GridWrap) CreateRenderer() fyne.WidgetRenderer {
|
||||
l.ExtendBaseWidget(l)
|
||||
|
||||
if f := l.CreateItem; f != nil {
|
||||
if l.itemMin.IsZero() {
|
||||
l.itemMin = f().MinSize()
|
||||
}
|
||||
}
|
||||
layout := &fyne.Container{Layout: newGridWrapLayout(l)}
|
||||
l.scroller = container.NewVScroll(layout)
|
||||
layout.Resize(layout.MinSize())
|
||||
|
||||
return newGridWrapRenderer([]fyne.CanvasObject{l.scroller}, l, l.scroller, layout)
|
||||
}
|
||||
|
||||
// MinSize returns the size that this widget should not shrink below.
|
||||
func (l *GridWrap) MinSize() fyne.Size {
|
||||
l.ExtendBaseWidget(l)
|
||||
|
||||
return l.BaseWidget.MinSize()
|
||||
}
|
||||
|
||||
func (l *GridWrap) scrollTo(id GridWrapItemID) {
|
||||
if l.scroller == nil {
|
||||
return
|
||||
}
|
||||
row := math.Floor(float64(id) / float64(l.getColCount()))
|
||||
y := float32(row)*l.itemMin.Height + float32(row)*theme.Padding()
|
||||
if y < l.scroller.Offset.Y {
|
||||
l.scroller.Offset.Y = y
|
||||
} else if y+l.itemMin.Height > l.scroller.Offset.Y+l.scroller.Size().Height {
|
||||
l.scroller.Offset.Y = y + l.itemMin.Height - l.scroller.Size().Height
|
||||
}
|
||||
l.offsetUpdated(l.scroller.Offset)
|
||||
}
|
||||
|
||||
// Resize is called when this GridWrap should change size. We refresh to ensure invisible items are drawn.
|
||||
func (l *GridWrap) Resize(s fyne.Size) {
|
||||
l.BaseWidget.Resize(s)
|
||||
l.offsetUpdated(l.scroller.Offset)
|
||||
l.scroller.Content.(*fyne.Container).Layout.(*gridWrapLayout).updateList(true)
|
||||
}
|
||||
|
||||
// ScrollTo scrolls to the item represented by id
|
||||
func (l *GridWrap) ScrollTo(id GridWrapItemID) {
|
||||
length := 0
|
||||
if f := l.Length; f != nil {
|
||||
length = f()
|
||||
}
|
||||
if id < 0 || int(id) >= length {
|
||||
return
|
||||
}
|
||||
l.scrollTo(id)
|
||||
l.Refresh()
|
||||
}
|
||||
|
||||
// ScrollToBottom scrolls to the end of the list
|
||||
func (l *GridWrap) ScrollToBottom() {
|
||||
length := 0
|
||||
if f := l.Length; f != nil {
|
||||
length = f()
|
||||
}
|
||||
if length > 0 {
|
||||
length--
|
||||
}
|
||||
l.scrollTo(GridWrapItemID(length))
|
||||
l.Refresh()
|
||||
}
|
||||
|
||||
// ScrollToTop scrolls to the start of the list
|
||||
func (l *GridWrap) ScrollToTop() {
|
||||
l.scrollTo(0)
|
||||
l.Refresh()
|
||||
}
|
||||
|
||||
// ScrollToOffset scrolls the list to the given offset position
|
||||
func (l *GridWrap) ScrollToOffset(offset float32) {
|
||||
l.scroller.Offset.Y = offset
|
||||
l.offsetUpdated(l.scroller.Offset)
|
||||
}
|
||||
|
||||
// GetScrollOffset returns the current scroll offset position
|
||||
func (l *GridWrap) GetScrollOffset() float32 {
|
||||
return l.offsetY
|
||||
}
|
||||
|
||||
// Declare conformity with WidgetRenderer interface.
|
||||
var _ fyne.WidgetRenderer = (*gridWrapRenderer)(nil)
|
||||
|
||||
type gridWrapRenderer struct {
|
||||
objects []fyne.CanvasObject
|
||||
|
||||
list *GridWrap
|
||||
scroller *container.Scroll
|
||||
layout *fyne.Container
|
||||
}
|
||||
|
||||
func newGridWrapRenderer(objects []fyne.CanvasObject, l *GridWrap, scroller *container.Scroll, layout *fyne.Container) *gridWrapRenderer {
|
||||
lr := &gridWrapRenderer{objects: objects, list: l, scroller: scroller, layout: layout}
|
||||
lr.scroller.OnScrolled = l.offsetUpdated
|
||||
return lr
|
||||
}
|
||||
|
||||
func (l *gridWrapRenderer) Layout(size fyne.Size) {
|
||||
l.scroller.Resize(size)
|
||||
}
|
||||
|
||||
func (l *gridWrapRenderer) MinSize() fyne.Size {
|
||||
return l.scroller.MinSize().Max(l.list.itemMin)
|
||||
}
|
||||
|
||||
func (l *gridWrapRenderer) Refresh() {
|
||||
if f := l.list.CreateItem; f != nil {
|
||||
l.list.itemMin = f().MinSize()
|
||||
}
|
||||
l.Layout(l.list.Size())
|
||||
l.scroller.Refresh()
|
||||
l.layout.Layout.(*gridWrapLayout).updateList(true)
|
||||
canvas.Refresh(l.list)
|
||||
}
|
||||
|
||||
func (l *gridWrapRenderer) Destroy() {
|
||||
}
|
||||
|
||||
func (l *gridWrapRenderer) Objects() []fyne.CanvasObject {
|
||||
return l.objects
|
||||
}
|
||||
|
||||
// Declare conformity with Layout interface.
|
||||
var _ fyne.Layout = (*gridWrapLayout)(nil)
|
||||
|
||||
type gridWrapLayout struct {
|
||||
list *GridWrap
|
||||
children []fyne.CanvasObject
|
||||
|
||||
itemPool *syncPool
|
||||
visible map[GridWrapItemID]fyne.CanvasObject
|
||||
renderLock sync.Mutex
|
||||
}
|
||||
|
||||
func newGridWrapLayout(list *GridWrap) fyne.Layout {
|
||||
l := &gridWrapLayout{list: list, itemPool: &syncPool{}, visible: make(map[GridWrapItemID]fyne.CanvasObject)}
|
||||
list.offsetUpdated = l.offsetUpdated
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *gridWrapLayout) Layout([]fyne.CanvasObject, fyne.Size) {
|
||||
l.updateList(true)
|
||||
}
|
||||
|
||||
func (l *gridWrapLayout) MinSize([]fyne.CanvasObject) fyne.Size {
|
||||
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+theme.Padding())*rows-theme.Padding())
|
||||
}
|
||||
return fyne.NewSize(0, 0)
|
||||
}
|
||||
|
||||
func (l *gridWrapLayout) getItem() fyne.CanvasObject {
|
||||
item := l.itemPool.Obtain()
|
||||
if item == nil {
|
||||
if f := l.list.CreateItem; f != nil {
|
||||
item = f()
|
||||
}
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
func (l *gridWrapLayout) offsetUpdated(pos fyne.Position) {
|
||||
if l.list.offsetY == pos.Y {
|
||||
return
|
||||
}
|
||||
l.list.offsetY = pos.Y
|
||||
l.updateList(false)
|
||||
}
|
||||
|
||||
func (l *gridWrapLayout) setupListItem(li fyne.CanvasObject, id GridWrapItemID) {
|
||||
if f := l.list.UpdateItem; f != nil {
|
||||
f(id, li)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GridWrap) getColCount() int {
|
||||
colCount := 1
|
||||
width := l.Size().Width
|
||||
if width > l.itemMin.Width {
|
||||
colCount = int(math.Floor(float64(width+theme.Padding()) / float64(l.itemMin.Width+theme.Padding())))
|
||||
}
|
||||
return colCount
|
||||
}
|
||||
|
||||
func (l *gridWrapLayout) updateList(refresh bool) {
|
||||
// code here is a mashup of listLayout.updateList and gridWrapLayout.Layout
|
||||
|
||||
l.renderLock.Lock()
|
||||
defer l.renderLock.Unlock()
|
||||
length := 0
|
||||
if f := l.list.Length; f != nil {
|
||||
length = f()
|
||||
}
|
||||
|
||||
colCount := l.list.getColCount()
|
||||
visibleRowsCount := int(math.Ceil(float64(l.list.scroller.Size().Height)/float64(l.list.itemMin.Height+theme.Padding()))) + 1
|
||||
|
||||
offY := l.list.offsetY - float32(math.Mod(float64(l.list.offsetY), float64(l.list.itemMin.Height+theme.Padding())))
|
||||
minRow := int(offY / (l.list.itemMin.Height + theme.Padding()))
|
||||
minItem := GridWrapItemID(minRow * colCount)
|
||||
maxRow := int(math.Min(float64(minRow+visibleRowsCount), math.Ceil(float64(length)/float64(colCount))))
|
||||
maxItem := GridWrapItemID(math.Min(float64(maxRow*colCount), float64(length-1)))
|
||||
|
||||
if l.list.UpdateItem == nil {
|
||||
fyne.LogError("Missing UpdateCell callback required for GridWrap", nil)
|
||||
}
|
||||
|
||||
wasVisible := l.visible
|
||||
l.visible = make(map[GridWrapItemID]fyne.CanvasObject)
|
||||
var cells []fyne.CanvasObject
|
||||
y := offY
|
||||
curItem := minItem
|
||||
for row := minRow; row <= maxRow && curItem <= maxItem; row++ {
|
||||
x := float32(0)
|
||||
for col := 0; col < colCount && curItem <= maxItem; col++ {
|
||||
c, ok := wasVisible[curItem]
|
||||
if !ok {
|
||||
c = l.getItem()
|
||||
if c == nil {
|
||||
continue
|
||||
}
|
||||
c.Resize(l.list.itemMin)
|
||||
l.setupListItem(c, curItem)
|
||||
}
|
||||
|
||||
c.Move(fyne.NewPos(x, y))
|
||||
if refresh {
|
||||
c.Resize(l.list.itemMin)
|
||||
if ok { // refresh visible
|
||||
l.setupListItem(c, curItem)
|
||||
}
|
||||
}
|
||||
|
||||
x += l.list.itemMin.Width + theme.Padding()
|
||||
l.visible[curItem] = c
|
||||
cells = append(cells, c)
|
||||
curItem++
|
||||
}
|
||||
y += l.list.itemMin.Height + theme.Padding()
|
||||
}
|
||||
|
||||
for id, old := range wasVisible {
|
||||
if _, ok := l.visible[id]; !ok {
|
||||
l.itemPool.Release(old)
|
||||
}
|
||||
}
|
||||
l.children = cells
|
||||
|
||||
objects := l.children
|
||||
l.list.scroller.Content.(*fyne.Container).Objects = objects
|
||||
}
|
||||
|
||||
type pool interface {
|
||||
Obtain() fyne.CanvasObject
|
||||
Release(fyne.CanvasObject)
|
||||
}
|
||||
|
||||
var _ pool = (*syncPool)(nil)
|
||||
|
||||
type syncPool struct {
|
||||
sync.Pool
|
||||
}
|
||||
|
||||
// Obtain returns an item from the pool for use
|
||||
func (p *syncPool) Obtain() (item fyne.CanvasObject) {
|
||||
o := p.Get()
|
||||
if o != nil {
|
||||
item = o.(fyne.CanvasObject)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Release adds an item into the pool to be used later
|
||||
func (p *syncPool) Release(item fyne.CanvasObject) {
|
||||
p.Put(item)
|
||||
}
|
||||
Reference in New Issue
Block a user