Skip to content

Commit

Permalink
Merge pull request #1485 from andydotxyz/fix/tablerefresh
Browse files Browse the repository at this point in the history
Make sure that visible table cells refresh when asked as well
  • Loading branch information
andydotxyz committed Oct 29, 2020
2 parents 651dddc + 5df118c commit 53d82cc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
24 changes: 13 additions & 11 deletions widget/table.go
Expand Up @@ -437,12 +437,17 @@ func (r *tableCellsRenderer) Refresh() {
minRow := offY / (r.cells.cellSize.Height + tableDividerThickness)
maxRow := fyne.Min(minRow+rows, dataRows)

updateCell := r.cells.t.UpdateCell
if updateCell == nil {
fyne.LogError("Missing UpdateCell callback required for Table", nil)
}

wasVisible := r.visible
r.visible = make(map[TableCellID]fyne.CanvasObject)
var cells []fyne.CanvasObject
for y := minRow; y < maxRow; y++ {
for x := minCol; x < maxCol; x++ {
id := TableCellID{x, y}
for row := minRow; row < maxRow; row++ {
for col := minCol; col < maxCol; col++ {
id := TableCellID{row, col}
c, ok := wasVisible[id]
if !ok {
c = r.pool.Obtain()
Expand All @@ -454,16 +459,13 @@ func (r *tableCellsRenderer) Refresh() {
continue
}

c.Move(fyne.NewPos(theme.Padding()+x*r.cells.cellSize.Width+(x-1)*tableDividerThickness,
theme.Padding()+y*r.cells.cellSize.Height+(y-1)*tableDividerThickness))

if f := r.cells.t.UpdateCell; f != nil {
f(TableCellID{y, x}, c)
} else {
fyne.LogError("Missing UpdateCell callback required for Table", nil)
}
c.Move(fyne.NewPos(theme.Padding()+col*r.cells.cellSize.Width+(col-1)*tableDividerThickness,
theme.Padding()+row*r.cells.cellSize.Height+(row-1)*tableDividerThickness))
}

if updateCell != nil {
updateCell(TableCellID{row, col}, c)
}
r.visible[id] = c
cells = append(cells, c)
}
Expand Down
21 changes: 21 additions & 0 deletions widget/table_test.go
Expand Up @@ -142,6 +142,27 @@ func TestTable_Hovered(t *testing.T) {
test.AssertImageMatches(t, "table/hovered.png", w.Canvas().Capture())
}

func TestTable_Refresh(t *testing.T) {
displayText := "placeholder"
table := NewTable(
func() (int, int) { return 5, 5 },
func() fyne.CanvasObject {
return NewLabel("template")
},
func(_ TableCellID, obj fyne.CanvasObject) {
obj.(*Label).SetText(displayText)
})
table.Resize(fyne.NewSize(120, 120))

renderer := test.WidgetRenderer(table).(*tableRenderer)
cellRenderer := test.WidgetRenderer(renderer.scroll.Content.(*tableCells))
assert.Equal(t, "placeholder", cellRenderer.(*tableCellsRenderer).Objects()[7].(*Label).Text)

displayText = "replaced"
table.Refresh()
assert.Equal(t, "replaced", cellRenderer.(*tableCellsRenderer).Objects()[7].(*Label).Text)
}

func TestTable_Selection(t *testing.T) {
app := test.NewApp()
defer test.NewApp()
Expand Down

0 comments on commit 53d82cc

Please sign in to comment.