Skip to content

Commit

Permalink
Fix locking order so we update UI before running user code in List/Table
Browse files Browse the repository at this point in the history
Fix #3487
  • Loading branch information
andydotxyz committed Dec 20, 2022
1 parent 18e7e29 commit 5cc7bc7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
19 changes: 9 additions & 10 deletions widget/list.go
Expand Up @@ -572,8 +572,8 @@ func (l *listLayout) updateList(refresh bool) {
l.list.propertyLock.Lock()
visibleRowHeights, offY, minRow, maxRow := l.list.visibleItemHeights(l.list.itemMin.Height, length)
l.list.propertyLock.Unlock()
l.renderLock.Unlock() // user code should not be locked
if len(visibleRowHeights) == 0 && length > 0 { // we can't show anything until we have some dimensions
l.renderLock.Unlock() // user code should not be locked
return
}

Expand All @@ -589,24 +589,16 @@ func (l *listLayout) updateList(refresh bool) {
continue
}
c.Resize(size)
l.setupListItem(c, row)
}

c.Move(fyne.NewPos(0, y))
if refresh {
c.Resize(size)
if ok { // refresh visible
l.setupListItem(c, row)
}
}
c.Resize(size)

y += itemHeight + separatorThickness
visible[row] = c
cells = append(cells, c)
}

l.renderLock.Lock()
defer l.renderLock.Unlock()
l.visible = visible

for id, old := range wasVisible {
Expand All @@ -621,6 +613,13 @@ func (l *listLayout) updateList(refresh bool) {
objects := l.children
objects = append(objects, l.separators...)
l.list.scroller.Content.(*fyne.Container).Objects = objects
l.renderLock.Unlock() // user code should not be locked

if refresh {
for row, obj := range visible {
l.setupListItem(obj, row)
}
}
}

func (l *listLayout) updateSeparators() {
Expand Down
14 changes: 10 additions & 4 deletions widget/table.go
Expand Up @@ -723,7 +723,6 @@ func (r *tableCellsRenderer) MinSize() fyne.Size {

func (r *tableCellsRenderer) Refresh() {
r.cells.propertyLock.Lock()
defer r.cells.propertyLock.Unlock()
oldSize := r.cells.cellSize
r.cells.cellSize = r.cells.t.templateSize()
if oldSize != r.cells.cellSize { // theme changed probably
Expand All @@ -737,10 +736,12 @@ func (r *tableCellsRenderer) Refresh() {
}
visibleColWidths, offX, minCol, maxCol := r.cells.t.visibleColumnWidths(r.cells.cellSize.Width, dataCols)
if len(visibleColWidths) == 0 { // we can't show anything until we have some dimensions
r.cells.propertyLock.Unlock()
return
}
visibleRowHeights, offY, minRow, maxRow := r.cells.t.visibleRowHeights(r.cells.cellSize.Height, dataRows)
if len(visibleRowHeights) == 0 { // we can't show anything until we have some dimensions
r.cells.propertyLock.Unlock()
return
}

Expand Down Expand Up @@ -773,9 +774,6 @@ func (r *tableCellsRenderer) Refresh() {
c.Move(fyne.NewPos(cellXOffset, cellYOffset))
c.Resize(fyne.NewSize(colWidth, rowHeight))

if updateCell != nil {
updateCell(TableCellID{row, col}, c)
}
r.visible[id] = c
cells = append(cells, c)
cellXOffset += colWidth + separatorThickness
Expand All @@ -788,7 +786,15 @@ func (r *tableCellsRenderer) Refresh() {
r.pool.Release(old)
}
}
visible := r.visible
r.cells.propertyLock.Unlock()
r.SetObjects(cells)

if updateCell != nil {
for id, cell := range visible {
updateCell(TableCellID{id.Row, id.Col}, cell)
}
}
}

func (r *tableCellsRenderer) returnAllToPool() {
Expand Down

0 comments on commit 5cc7bc7

Please sign in to comment.