Skip to content

Commit

Permalink
Cache the canvas lookup for huge speedup
Browse files Browse the repository at this point in the history
We could be calling this 100s or 1000s of times if the entire text grid was refreshed
(up to twice for each cell, possibly faster than refresh rate because this in the event thread not draw).

Result is huge boost (see fyneterm usage for example)
  • Loading branch information
andydotxyz committed Jul 4, 2023
1 parent 56db81e commit 47cac54
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions widget/textgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func (t *textGridRenderer) setCellRune(str rune, pos int, style, rowStyle TextGr
if text.Text != newStr || text.Color != fg {
text.Text = newStr
text.Color = fg
canvas.Refresh(text)
t.refresh(text)
}

rect := t.objects[pos*2].(*canvas.Rectangle)
Expand All @@ -386,7 +386,7 @@ func (t *textGridRenderer) setCellRune(str rune, pos int, style, rowStyle TextGr
}
if rect.FillColor != bg {
rect.FillColor = bg
canvas.Refresh(rect)
t.refresh(rect)
}
}

Expand Down Expand Up @@ -527,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 @@ -545,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 47cac54

Please sign in to comment.