gridwrap.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. package widget
  2. import (
  3. "fmt"
  4. "math"
  5. "sort"
  6. "sync"
  7. "fyne.io/fyne/v2"
  8. "fyne.io/fyne/v2/canvas"
  9. "fyne.io/fyne/v2/data/binding"
  10. "fyne.io/fyne/v2/driver/desktop"
  11. "fyne.io/fyne/v2/internal/widget"
  12. "fyne.io/fyne/v2/theme"
  13. )
  14. // Declare conformity with interfaces.
  15. var _ fyne.Widget = (*GridWrap)(nil)
  16. var _ fyne.Focusable = (*GridWrap)(nil)
  17. // GridWrapItemID is the ID of an individual item in the GridWrap widget.
  18. //
  19. // Since: 2.4
  20. type GridWrapItemID = int
  21. // GridWrap is a widget with an API very similar to widget.List,
  22. // that lays out items in a scrollable wrapping grid similar to container.NewGridWrap.
  23. // It caches and reuses widgets for performance.
  24. //
  25. // Since: 2.4
  26. type GridWrap struct {
  27. BaseWidget
  28. Length func() int `json:"-"`
  29. CreateItem func() fyne.CanvasObject `json:"-"`
  30. UpdateItem func(id GridWrapItemID, item fyne.CanvasObject) `json:"-"`
  31. OnSelected func(id GridWrapItemID) `json:"-"`
  32. OnUnselected func(id GridWrapItemID) `json:"-"`
  33. currentFocus ListItemID
  34. focused bool
  35. scroller *widget.Scroll
  36. selected []GridWrapItemID
  37. itemMin fyne.Size
  38. offsetY float32
  39. offsetUpdated func(fyne.Position)
  40. }
  41. // NewGridWrap creates and returns a GridWrap widget for displaying items in
  42. // a wrapping grid layout with scrolling and caching for performance.
  43. //
  44. // Since: 2.4
  45. func NewGridWrap(length func() int, createItem func() fyne.CanvasObject, updateItem func(GridWrapItemID, fyne.CanvasObject)) *GridWrap {
  46. gwList := &GridWrap{Length: length, CreateItem: createItem, UpdateItem: updateItem}
  47. gwList.ExtendBaseWidget(gwList)
  48. return gwList
  49. }
  50. // NewGridWrapWithData creates a new GridWrap widget that will display the contents of the provided data.
  51. //
  52. // Since: 2.4
  53. func NewGridWrapWithData(data binding.DataList, createItem func() fyne.CanvasObject, updateItem func(binding.DataItem, fyne.CanvasObject)) *GridWrap {
  54. gwList := NewGridWrap(
  55. data.Length,
  56. createItem,
  57. func(i GridWrapItemID, o fyne.CanvasObject) {
  58. item, err := data.GetItem(int(i))
  59. if err != nil {
  60. fyne.LogError(fmt.Sprintf("Error getting data item %d", i), err)
  61. return
  62. }
  63. updateItem(item, o)
  64. })
  65. data.AddListener(binding.NewDataListener(gwList.Refresh))
  66. return gwList
  67. }
  68. // CreateRenderer is a private method to Fyne which links this widget to its renderer.
  69. func (l *GridWrap) CreateRenderer() fyne.WidgetRenderer {
  70. l.ExtendBaseWidget(l)
  71. if f := l.CreateItem; f != nil && l.itemMin.IsZero() {
  72. l.itemMin = f().MinSize()
  73. }
  74. layout := &fyne.Container{Layout: newGridWrapLayout(l)}
  75. l.scroller = widget.NewVScroll(layout)
  76. layout.Resize(layout.MinSize())
  77. return newGridWrapRenderer([]fyne.CanvasObject{l.scroller}, l, l.scroller, layout)
  78. }
  79. // FocusGained is called after this GridWrap has gained focus.
  80. //
  81. // Implements: fyne.Focusable
  82. func (l *GridWrap) FocusGained() {
  83. l.focused = true
  84. l.scrollTo(l.currentFocus)
  85. l.RefreshItem(l.currentFocus)
  86. }
  87. // FocusLost is called after this GridWrap has lost focus.
  88. //
  89. // Implements: fyne.Focusable
  90. func (l *GridWrap) FocusLost() {
  91. l.focused = false
  92. l.RefreshItem(l.currentFocus)
  93. }
  94. // MinSize returns the size that this widget should not shrink below.
  95. func (l *GridWrap) MinSize() fyne.Size {
  96. l.ExtendBaseWidget(l)
  97. return l.BaseWidget.MinSize()
  98. }
  99. func (l *GridWrap) scrollTo(id GridWrapItemID) {
  100. if l.scroller == nil {
  101. return
  102. }
  103. row := math.Floor(float64(id) / float64(l.getColCount()))
  104. y := float32(row)*l.itemMin.Height + float32(row)*theme.Padding()
  105. if y < l.scroller.Offset.Y {
  106. l.scroller.Offset.Y = y
  107. } else if y+l.itemMin.Height > l.scroller.Offset.Y+l.scroller.Size().Height {
  108. l.scroller.Offset.Y = y + l.itemMin.Height - l.scroller.Size().Height
  109. }
  110. l.offsetUpdated(l.scroller.Offset)
  111. }
  112. // RefreshItem refreshes a single item, specified by the item ID passed in.
  113. //
  114. // Since: 2.4
  115. func (l *GridWrap) RefreshItem(id GridWrapItemID) {
  116. if l.scroller == nil {
  117. return
  118. }
  119. l.BaseWidget.Refresh()
  120. lo := l.scroller.Content.(*fyne.Container).Layout.(*gridWrapLayout)
  121. lo.renderLock.Lock() // ensures we are not changing visible info in render code during the search
  122. item, ok := lo.searchVisible(lo.visible, id)
  123. lo.renderLock.Unlock()
  124. if ok {
  125. lo.setupGridItem(item, id, l.focused && l.currentFocus == id)
  126. }
  127. }
  128. // Resize is called when this GridWrap should change size. We refresh to ensure invisible items are drawn.
  129. func (l *GridWrap) Resize(s fyne.Size) {
  130. l.BaseWidget.Resize(s)
  131. l.offsetUpdated(l.scroller.Offset)
  132. l.scroller.Content.(*fyne.Container).Layout.(*gridWrapLayout).updateGrid(true)
  133. }
  134. // Select adds the item identified by the given ID to the selection.
  135. func (l *GridWrap) Select(id GridWrapItemID) {
  136. if len(l.selected) > 0 && id == l.selected[0] {
  137. return
  138. }
  139. length := 0
  140. if f := l.Length; f != nil {
  141. length = f()
  142. }
  143. if id < 0 || id >= length {
  144. return
  145. }
  146. old := l.selected
  147. l.selected = []GridWrapItemID{id}
  148. defer func() {
  149. if f := l.OnUnselected; f != nil && len(old) > 0 {
  150. f(old[0])
  151. }
  152. if f := l.OnSelected; f != nil {
  153. f(id)
  154. }
  155. }()
  156. l.scrollTo(id)
  157. l.Refresh()
  158. }
  159. // ScrollTo scrolls to the item represented by id
  160. func (l *GridWrap) ScrollTo(id GridWrapItemID) {
  161. length := 0
  162. if f := l.Length; f != nil {
  163. length = f()
  164. }
  165. if id < 0 || int(id) >= length {
  166. return
  167. }
  168. l.scrollTo(id)
  169. l.Refresh()
  170. }
  171. // ScrollToBottom scrolls to the end of the list
  172. func (l *GridWrap) ScrollToBottom() {
  173. length := 0
  174. if f := l.Length; f != nil {
  175. length = f()
  176. }
  177. if length > 0 {
  178. length--
  179. }
  180. l.scrollTo(GridWrapItemID(length))
  181. l.Refresh()
  182. }
  183. // ScrollToTop scrolls to the start of the list
  184. func (l *GridWrap) ScrollToTop() {
  185. l.scrollTo(0)
  186. l.Refresh()
  187. }
  188. // ScrollToOffset scrolls the list to the given offset position
  189. func (l *GridWrap) ScrollToOffset(offset float32) {
  190. l.scroller.Offset.Y = offset
  191. l.offsetUpdated(l.scroller.Offset)
  192. }
  193. // TypedKey is called if a key event happens while this GridWrap is focused.
  194. //
  195. // Implements: fyne.Focusable
  196. func (l *GridWrap) TypedKey(event *fyne.KeyEvent) {
  197. switch event.Name {
  198. case fyne.KeySpace:
  199. l.Select(l.currentFocus)
  200. case fyne.KeyDown:
  201. count := 0
  202. if f := l.Length; f != nil {
  203. count = f()
  204. }
  205. l.RefreshItem(l.currentFocus)
  206. l.currentFocus += l.getColCount()
  207. if l.currentFocus >= count-1 {
  208. l.currentFocus = count - 1
  209. }
  210. l.scrollTo(l.currentFocus)
  211. l.RefreshItem(l.currentFocus)
  212. case fyne.KeyLeft:
  213. if l.currentFocus <= 0 {
  214. return
  215. }
  216. if l.currentFocus%l.getColCount() == 0 {
  217. return
  218. }
  219. l.RefreshItem(l.currentFocus)
  220. l.currentFocus--
  221. l.scrollTo(l.currentFocus)
  222. l.RefreshItem(l.currentFocus)
  223. case fyne.KeyRight:
  224. if f := l.Length; f != nil && l.currentFocus >= f()-1 {
  225. return
  226. }
  227. if (l.currentFocus+1)%l.getColCount() == 0 {
  228. return
  229. }
  230. l.RefreshItem(l.currentFocus)
  231. l.currentFocus++
  232. l.scrollTo(l.currentFocus)
  233. l.RefreshItem(l.currentFocus)
  234. case fyne.KeyUp:
  235. if l.currentFocus <= 0 {
  236. return
  237. }
  238. l.RefreshItem(l.currentFocus)
  239. l.currentFocus -= l.getColCount()
  240. if l.currentFocus < 0 {
  241. l.currentFocus = 0
  242. }
  243. l.scrollTo(l.currentFocus)
  244. l.RefreshItem(l.currentFocus)
  245. }
  246. }
  247. // TypedRune is called if a text event happens while this GridWrap is focused.
  248. //
  249. // Implements: fyne.Focusable
  250. func (l *GridWrap) TypedRune(_ rune) {
  251. // intentionally left blank
  252. }
  253. // GetScrollOffset returns the current scroll offset position
  254. func (l *GridWrap) GetScrollOffset() float32 {
  255. return l.offsetY
  256. }
  257. // Unselect removes the item identified by the given ID from the selection.
  258. func (l *GridWrap) Unselect(id GridWrapItemID) {
  259. if len(l.selected) == 0 || l.selected[0] != id {
  260. return
  261. }
  262. l.selected = nil
  263. l.Refresh()
  264. if f := l.OnUnselected; f != nil {
  265. f(id)
  266. }
  267. }
  268. // UnselectAll removes all items from the selection.
  269. func (l *GridWrap) UnselectAll() {
  270. if len(l.selected) == 0 {
  271. return
  272. }
  273. selected := l.selected
  274. l.selected = nil
  275. l.Refresh()
  276. if f := l.OnUnselected; f != nil {
  277. for _, id := range selected {
  278. f(id)
  279. }
  280. }
  281. }
  282. // Declare conformity with WidgetRenderer interface.
  283. var _ fyne.WidgetRenderer = (*gridWrapRenderer)(nil)
  284. type gridWrapRenderer struct {
  285. objects []fyne.CanvasObject
  286. list *GridWrap
  287. scroller *widget.Scroll
  288. layout *fyne.Container
  289. }
  290. func newGridWrapRenderer(objects []fyne.CanvasObject, l *GridWrap, scroller *widget.Scroll, layout *fyne.Container) *gridWrapRenderer {
  291. lr := &gridWrapRenderer{objects: objects, list: l, scroller: scroller, layout: layout}
  292. lr.scroller.OnScrolled = l.offsetUpdated
  293. return lr
  294. }
  295. func (l *gridWrapRenderer) Layout(size fyne.Size) {
  296. l.scroller.Resize(size)
  297. }
  298. func (l *gridWrapRenderer) MinSize() fyne.Size {
  299. return l.scroller.MinSize().Max(l.list.itemMin)
  300. }
  301. func (l *gridWrapRenderer) Refresh() {
  302. if f := l.list.CreateItem; f != nil {
  303. l.list.itemMin = f().MinSize()
  304. }
  305. l.Layout(l.list.Size())
  306. l.scroller.Refresh()
  307. l.layout.Layout.(*gridWrapLayout).updateGrid(true)
  308. canvas.Refresh(l.list)
  309. }
  310. func (l *gridWrapRenderer) Destroy() {
  311. }
  312. func (l *gridWrapRenderer) Objects() []fyne.CanvasObject {
  313. return l.objects
  314. }
  315. // Declare conformity with interfaces.
  316. var _ fyne.Widget = (*gridWrapItem)(nil)
  317. var _ fyne.Tappable = (*gridWrapItem)(nil)
  318. var _ desktop.Hoverable = (*gridWrapItem)(nil)
  319. type gridWrapItem struct {
  320. BaseWidget
  321. onTapped func()
  322. background *canvas.Rectangle
  323. child fyne.CanvasObject
  324. hovered, selected bool
  325. }
  326. func newGridWrapItem(child fyne.CanvasObject, tapped func()) *gridWrapItem {
  327. gw := &gridWrapItem{
  328. child: child,
  329. onTapped: tapped,
  330. }
  331. gw.ExtendBaseWidget(gw)
  332. return gw
  333. }
  334. // CreateRenderer is a private method to Fyne which links this widget to its renderer.
  335. func (gw *gridWrapItem) CreateRenderer() fyne.WidgetRenderer {
  336. gw.ExtendBaseWidget(gw)
  337. gw.background = canvas.NewRectangle(theme.HoverColor())
  338. gw.background.CornerRadius = theme.SelectionRadiusSize()
  339. gw.background.Hide()
  340. objects := []fyne.CanvasObject{gw.background, gw.child}
  341. return &gridWrapItemRenderer{widget.NewBaseRenderer(objects), gw}
  342. }
  343. // MinSize returns the size that this widget should not shrink below.
  344. func (gw *gridWrapItem) MinSize() fyne.Size {
  345. gw.ExtendBaseWidget(gw)
  346. return gw.BaseWidget.MinSize()
  347. }
  348. // MouseIn is called when a desktop pointer enters the widget.
  349. func (gw *gridWrapItem) MouseIn(*desktop.MouseEvent) {
  350. gw.hovered = true
  351. gw.Refresh()
  352. }
  353. // MouseMoved is called when a desktop pointer hovers over the widget.
  354. func (gw *gridWrapItem) MouseMoved(*desktop.MouseEvent) {
  355. }
  356. // MouseOut is called when a desktop pointer exits the widget.
  357. func (gw *gridWrapItem) MouseOut() {
  358. gw.hovered = false
  359. gw.Refresh()
  360. }
  361. // Tapped is called when a pointer tapped event is captured and triggers any tap handler.
  362. func (gw *gridWrapItem) Tapped(*fyne.PointEvent) {
  363. if gw.onTapped != nil {
  364. gw.selected = true
  365. gw.Refresh()
  366. gw.onTapped()
  367. }
  368. }
  369. // Declare conformity with the WidgetRenderer interface.
  370. var _ fyne.WidgetRenderer = (*gridWrapItemRenderer)(nil)
  371. type gridWrapItemRenderer struct {
  372. widget.BaseRenderer
  373. item *gridWrapItem
  374. }
  375. // MinSize calculates the minimum size of a listItem.
  376. // This is based on the size of the status indicator and the size of the child object.
  377. func (gw *gridWrapItemRenderer) MinSize() fyne.Size {
  378. return gw.item.child.MinSize()
  379. }
  380. // Layout the components of the listItem widget.
  381. func (gw *gridWrapItemRenderer) Layout(size fyne.Size) {
  382. gw.item.background.Resize(size)
  383. gw.item.child.Resize(size)
  384. }
  385. func (gw *gridWrapItemRenderer) Refresh() {
  386. gw.item.background.CornerRadius = theme.SelectionRadiusSize()
  387. if gw.item.selected {
  388. gw.item.background.FillColor = theme.SelectionColor()
  389. gw.item.background.Show()
  390. } else if gw.item.hovered {
  391. gw.item.background.FillColor = theme.HoverColor()
  392. gw.item.background.Show()
  393. } else {
  394. gw.item.background.Hide()
  395. }
  396. gw.item.background.Refresh()
  397. canvas.Refresh(gw.item.super())
  398. }
  399. // Declare conformity with Layout interface.
  400. var _ fyne.Layout = (*gridWrapLayout)(nil)
  401. type gridItemAndID struct {
  402. item *gridWrapItem
  403. id GridWrapItemID
  404. }
  405. type gridWrapLayout struct {
  406. list *GridWrap
  407. itemPool syncPool
  408. slicePool sync.Pool // *[]itemAndID
  409. visible []gridItemAndID
  410. renderLock sync.Mutex
  411. }
  412. func newGridWrapLayout(list *GridWrap) fyne.Layout {
  413. l := &gridWrapLayout{list: list}
  414. l.slicePool.New = func() interface{} {
  415. s := make([]gridItemAndID, 0)
  416. return &s
  417. }
  418. list.offsetUpdated = l.offsetUpdated
  419. return l
  420. }
  421. func (l *gridWrapLayout) Layout(_ []fyne.CanvasObject, _ fyne.Size) {
  422. l.updateGrid(true)
  423. }
  424. func (l *gridWrapLayout) MinSize(_ []fyne.CanvasObject) fyne.Size {
  425. if lenF := l.list.Length; lenF != nil {
  426. cols := l.list.getColCount()
  427. rows := float32(math.Ceil(float64(lenF()) / float64(cols)))
  428. return fyne.NewSize(l.list.itemMin.Width,
  429. (l.list.itemMin.Height+theme.Padding())*rows-theme.Padding())
  430. }
  431. return fyne.NewSize(0, 0)
  432. }
  433. func (l *gridWrapLayout) getItem() *gridWrapItem {
  434. item := l.itemPool.Obtain()
  435. if item == nil {
  436. if f := l.list.CreateItem; f != nil {
  437. item = newGridWrapItem(f(), nil)
  438. }
  439. }
  440. return item.(*gridWrapItem)
  441. }
  442. func (l *gridWrapLayout) offsetUpdated(pos fyne.Position) {
  443. if l.list.offsetY == pos.Y {
  444. return
  445. }
  446. l.list.offsetY = pos.Y
  447. l.updateGrid(false)
  448. }
  449. func (l *gridWrapLayout) setupGridItem(li *gridWrapItem, id GridWrapItemID, focus bool) {
  450. previousIndicator := li.selected
  451. li.selected = false
  452. for _, s := range l.list.selected {
  453. if id == s {
  454. li.selected = true
  455. break
  456. }
  457. }
  458. if focus {
  459. li.hovered = true
  460. li.Refresh()
  461. } else if previousIndicator != li.selected || li.hovered {
  462. li.hovered = false
  463. li.Refresh()
  464. }
  465. if f := l.list.UpdateItem; f != nil {
  466. f(id, li.child)
  467. }
  468. li.onTapped = func() {
  469. if !fyne.CurrentDevice().IsMobile() {
  470. l.list.RefreshItem(l.list.currentFocus)
  471. canvas := fyne.CurrentApp().Driver().CanvasForObject(l.list)
  472. if canvas != nil {
  473. canvas.Focus(l.list)
  474. }
  475. l.list.currentFocus = id
  476. }
  477. l.list.Select(id)
  478. }
  479. }
  480. func (l *GridWrap) getColCount() int {
  481. colCount := 1
  482. width := l.Size().Width
  483. if width > l.itemMin.Width {
  484. colCount = int(math.Floor(float64(width+theme.Padding()) / float64(l.itemMin.Width+theme.Padding())))
  485. }
  486. return colCount
  487. }
  488. func (l *gridWrapLayout) updateGrid(refresh bool) {
  489. // code here is a mashup of listLayout.updateList and gridWrapLayout.Layout
  490. l.renderLock.Lock()
  491. length := 0
  492. if f := l.list.Length; f != nil {
  493. length = f()
  494. }
  495. colCount := l.list.getColCount()
  496. visibleRowsCount := int(math.Ceil(float64(l.list.scroller.Size().Height)/float64(l.list.itemMin.Height+theme.Padding()))) + 1
  497. offY := l.list.offsetY - float32(math.Mod(float64(l.list.offsetY), float64(l.list.itemMin.Height+theme.Padding())))
  498. minRow := int(offY / (l.list.itemMin.Height + theme.Padding()))
  499. minItem := GridWrapItemID(minRow * colCount)
  500. maxRow := int(math.Min(float64(minRow+visibleRowsCount), math.Ceil(float64(length)/float64(colCount))))
  501. maxItem := GridWrapItemID(math.Min(float64(maxRow*colCount), float64(length-1)))
  502. if l.list.UpdateItem == nil {
  503. fyne.LogError("Missing UpdateCell callback required for GridWrap", nil)
  504. }
  505. // Keep pointer reference for copying slice header when returning to the pool
  506. // https://blog.mike.norgate.xyz/unlocking-go-slice-performance-navigating-sync-pool-for-enhanced-efficiency-7cb63b0b453e
  507. wasVisiblePtr := l.slicePool.Get().(*[]gridItemAndID)
  508. wasVisible := (*wasVisiblePtr)[:0]
  509. wasVisible = append(wasVisible, l.visible...)
  510. oldVisibleLen := len(l.visible)
  511. l.visible = l.visible[:0]
  512. c := l.list.scroller.Content.(*fyne.Container)
  513. oldObjLen := len(c.Objects)
  514. c.Objects = c.Objects[:0]
  515. y := offY
  516. curItemID := minItem
  517. for row := minRow; row <= maxRow && curItemID <= maxItem; row++ {
  518. x := float32(0)
  519. for col := 0; col < colCount && curItemID <= maxItem; col++ {
  520. item, ok := l.searchVisible(wasVisible, curItemID)
  521. if !ok {
  522. item = l.getItem()
  523. if item == nil {
  524. continue
  525. }
  526. item.Resize(l.list.itemMin)
  527. }
  528. item.Move(fyne.NewPos(x, y))
  529. if refresh {
  530. item.Resize(l.list.itemMin)
  531. }
  532. x += l.list.itemMin.Width + theme.Padding()
  533. l.visible = append(l.visible, gridItemAndID{item: item, id: curItemID})
  534. c.Objects = append(c.Objects, item)
  535. curItemID++
  536. }
  537. y += l.list.itemMin.Height + theme.Padding()
  538. }
  539. l.nilOldSliceData(c.Objects, len(c.Objects), oldObjLen)
  540. l.nilOldVisibleSliceData(l.visible, len(l.visible), oldVisibleLen)
  541. for _, old := range wasVisible {
  542. if _, ok := l.searchVisible(l.visible, old.id); !ok {
  543. l.itemPool.Release(old.item)
  544. }
  545. }
  546. // make a local deep copy of l.visible since rest of this function is unlocked
  547. // and cannot safely access l.visible
  548. visiblePtr := l.slicePool.Get().(*[]gridItemAndID)
  549. visible := (*visiblePtr)[:0]
  550. visible = append(visible, l.visible...)
  551. l.renderLock.Unlock() // user code should not be locked
  552. for _, obj := range visible {
  553. l.setupGridItem(obj.item, obj.id, l.list.focused && l.list.currentFocus == obj.id)
  554. }
  555. // nil out all references before returning slices to pool
  556. for i := 0; i < len(wasVisible); i++ {
  557. wasVisible[i].item = nil
  558. }
  559. for i := 0; i < len(visible); i++ {
  560. visible[i].item = nil
  561. }
  562. *wasVisiblePtr = wasVisible // Copy the slice header over to the heap
  563. *visiblePtr = visible
  564. l.slicePool.Put(wasVisiblePtr)
  565. l.slicePool.Put(visiblePtr)
  566. }
  567. // invariant: visible is in ascending order of IDs
  568. func (l *gridWrapLayout) searchVisible(visible []gridItemAndID, id GridWrapItemID) (*gridWrapItem, bool) {
  569. ln := len(visible)
  570. idx := sort.Search(ln, func(i int) bool { return visible[i].id >= id })
  571. if idx < ln && visible[idx].id == id {
  572. return visible[idx].item, true
  573. }
  574. return nil, false
  575. }
  576. func (l *gridWrapLayout) nilOldSliceData(objs []fyne.CanvasObject, len, oldLen int) {
  577. if oldLen > len {
  578. objs = objs[:oldLen] // gain view into old data
  579. for i := len; i < oldLen; i++ {
  580. objs[i] = nil
  581. }
  582. }
  583. }
  584. func (l *gridWrapLayout) nilOldVisibleSliceData(objs []gridItemAndID, len, oldLen int) {
  585. if oldLen > len {
  586. objs = objs[:oldLen] // gain view into old data
  587. for i := len; i < oldLen; i++ {
  588. objs[i].item = nil
  589. }
  590. }
  591. }