Skip to content

Commit

Permalink
Merge pull request fyne-io#4021 from andydotxyz/fix/slowtextgrid
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Jul 4, 2023
2 parents f73816f + 47cac54 commit e050d79
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions widget/textgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ type textGridRenderer struct {

cellSize fyne.Size
objects []fyne.CanvasObject
current fyne.Canvas
}

func (t *textGridRenderer) appendTextCell(str rune) {
Expand Down Expand Up @@ -369,9 +370,12 @@ func (t *textGridRenderer) setCellRune(str rune, pos int, style, rowStyle TextGr
} else if rowStyle != nil && rowStyle.TextColor() != nil {
fg = rowStyle.TextColor()
}
text.Text = string(str)
text.Color = fg
canvas.Refresh(text)
newStr := string(str)
if text.Text != newStr || text.Color != fg {
text.Text = newStr
text.Color = fg
t.refresh(text)
}

rect := t.objects[pos*2].(*canvas.Rectangle)
bg := color.Color(color.Transparent)
Expand All @@ -380,8 +384,10 @@ func (t *textGridRenderer) setCellRune(str rune, pos int, style, rowStyle TextGr
} else if rowStyle != nil && rowStyle.BackgroundColor() != nil {
bg = rowStyle.BackgroundColor()
}
rect.FillColor = bg
canvas.Refresh(rect)
if rect.FillColor != bg {
rect.FillColor = bg
t.refresh(rect)
}
}

func (t *textGridRenderer) addCellsIfRequired() {
Expand Down Expand Up @@ -521,6 +527,11 @@ func (t *textGridRenderer) MinSize() fyne.Size {
}

func (t *textGridRenderer) Refresh() {
// we may be on a new canvas, so just update it to be sure
if fyne.CurrentApp() != nil && fyne.CurrentApp().Driver() != nil {
t.current = fyne.CurrentApp().Driver().CanvasForObject(t.text)
}

// theme could change text size
t.updateCellSize()

Expand All @@ -539,6 +550,21 @@ func (t *textGridRenderer) Objects() []fyne.CanvasObject {
func (t *textGridRenderer) Destroy() {
}

func (t *textGridRenderer) refresh(obj fyne.CanvasObject) {
if t.current == nil {
if fyne.CurrentApp() != nil && fyne.CurrentApp().Driver() != nil {
// cache canvas for this widget, so we don't look it up many times for every cell/row refresh!
t.current = fyne.CurrentApp().Driver().CanvasForObject(t.text)
}

if t.current == nil {
return // not yet set up perhaps?
}
}

t.current.Refresh(obj)
}

func (t *textGridRenderer) updateCellSize() {
size := fyne.MeasureText("M", theme.TextSize(), fyne.TextStyle{Monospace: true})

Expand Down

0 comments on commit e050d79

Please sign in to comment.