Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 69 additions & 5 deletions internal/driver/mobile/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ type driver struct {
animation animation.Runner
currentSize size.Event

theme fyne.ThemeVariant
onConfigChanged func(*Configuration)
painting bool
running bool
queuedFuncs *async.UnboundedChan[func()]
theme fyne.ThemeVariant
onConfigChanged func(*Configuration)
painting bool
running bool
queuedFuncs *async.UnboundedChan[func()]
queuedFuncsLowPriority *async.UnboundedChan[func()]
prerenderChecked bool
}

// Declare conformity with Driver
Expand Down Expand Up @@ -182,6 +184,7 @@ func (d *driver) Run() {
async.SetMainGoroutine()
d.app = a
d.queuedFuncs = async.NewUnboundedChan[func()]()
d.queuedFuncsLowPriority = async.NewUnboundedChan[func()]()

fyne.CurrentApp().Settings().AddListener(func(s fyne.Settings) {
painter.ClearFontCache()
Expand Down Expand Up @@ -276,6 +279,8 @@ func (d *driver) Run() {
d.typeUpCanvas(c, e.Rune, e.Code, e.Modifiers)
}
}
case fn := <-d.queuedFuncsLowPriority.Out():
fn()
}
}
})
Expand Down Expand Up @@ -402,6 +407,65 @@ func (d *driver) paintWindow(window fyne.Window, size fyne.Size) {
}

c.WalkTrees(draw, afterDraw)

if !d.prerenderChecked {
d.findTextsForPrerender(size, c)
}
}

func (d *driver) findTextsForPrerender(size fyne.Size, c *canvas) {
if size.Width <= 0 || size.Height <= 0 {
return
}
clips := &internal.ClipStack{}
frame := size
p := c.Painter()
check := func(node *common.RenderCacheNode, pos fyne.Position) {
obj := node.Obj()
if !obj.Visible() {
return
}
if intdriver.IsClip(obj) {
clips.Push(pos, obj.Size())
}
if text, ok := obj.(*fynecanvas.Text); ok {
if text.Text == "" {
return
}
decorated := text.TextStyle.Underline || text.TextStyle.Strikethrough
if text.Text == " " && !decorated {
return
}

size := text.MinSize()
containerSize := text.Size()
switch text.Alignment {
case fyne.TextAlignTrailing:
pos = fyne.NewPos(pos.X+containerSize.Width-size.Width, pos.Y)
case fyne.TextAlignCenter:
pos = fyne.NewPos(pos.X+(containerSize.Width-size.Width)/2, pos.Y)
}

if containerSize.Height > size.Height {
pos = fyne.NewPos(pos.X, pos.Y+(containerSize.Height-size.Height)/2)
}

var clipPos fyne.Position
var clipSize fyne.Size
clip := clips.Top()
if clip != nil {
clipPos, clipSize = clip.Rect()
} else {
clipSize = frame
}
if pos.Y > clipPos.Y+clipSize.Height || pos.Y+size.Height < clipPos.Y ||
pos.X > clipPos.X+clipSize.Width || pos.X+size.Width < clipPos.X {
p.CreateTextTexture(text, d.queuedFuncsLowPriority.In())
}
}
}
c.WalkTrees(check, nil)
d.prerenderChecked = true
}

func (d *driver) sendPaintEvent() {
Expand Down
2 changes: 2 additions & 0 deletions internal/painter/gl/painter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type Painter interface {
StartClipping(fyne.Position, fyne.Size)
// StopClipping stops clipping paint actions.
StopClipping()
// CreateTextTexture creates a texture from a canvas.Text object
CreateTextTexture(t *canvas.Text, queue chan<- func())
}

// NewPainter creates a new GL based renderer for the provided canvas.
Expand Down
26 changes: 26 additions & 0 deletions internal/painter/gl/texture.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,32 @@ func (p *painter) freeTexture(obj fyne.CanvasObject) {
cache.DeleteTexture(obj)
}

func (p *painter) CreateTextTexture(t *canvas.Text, queue chan<- func()) {
custom := ""
if t.FontSource != nil {
custom = t.FontSource.Name()
}
ent := cache.FontCacheEntry{Color: t.Color, Canvas: p.canvas}
ent.Text = t.Text
ent.Size = t.TextSize
ent.Style = t.TextStyle
ent.Source = custom

if _, ok := cache.GetTextTexture(ent); !ok {
queue <- func() {
_, ok := cache.GetTextTexture(ent)
if !ok {
tex := p.newGlTextTexture(t)
texture := cache.TextureType(tex)
cache.SetTextTexture(ent, texture, p.canvas, func() {
p.ctx.DeleteTexture(tex)
p.logError()
})
}
}
}
}

func (p *painter) getTexture(object fyne.CanvasObject, creator func(canvasObject fyne.CanvasObject) Texture) (Texture, error) {
if t, ok := object.(*canvas.Text); ok {
custom := ""
Expand Down
Loading