forked from fyne-io/fyne
/
painter.go
62 lines (52 loc) · 1.65 KB
/
painter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package software
import (
"image"
"image/draw"
"fyne.io/fyne"
"fyne.io/fyne/canvas"
"fyne.io/fyne/internal"
"fyne.io/fyne/internal/driver"
)
// Painter is a simple software painter that can paint a canvas in memory.
type Painter struct {
}
// NewPainter creates a new Painter.
func NewPainter() *Painter {
return &Painter{}
}
// Paint is the main entry point for a simple software painter.
// The canvas to be drawn is passed in as a parameter and the return is an
// image containing the result of rendering.
func (*Painter) Paint(c fyne.Canvas) image.Image {
theme := fyne.CurrentApp().Settings().Theme()
size := c.Size().Max(c.Content().MinSize())
bounds := image.Rect(0, 0, internal.ScaleInt(c, size.Width), internal.ScaleInt(c, size.Height))
base := image.NewNRGBA(bounds)
draw.Draw(base, bounds, image.NewUniform(theme.BackgroundColor()), image.Point{}, draw.Src)
paint := func(obj fyne.CanvasObject, pos, clipPos fyne.Position, clipSize fyne.Size) bool {
clip := image.Rect(
internal.ScaleInt(c, clipPos.X),
internal.ScaleInt(c, clipPos.Y),
internal.ScaleInt(c, clipPos.X+clipSize.Width),
internal.ScaleInt(c, clipPos.Y+clipSize.Height),
)
switch o := obj.(type) {
case *canvas.Image:
drawImage(c, o, pos, base, clip)
case *canvas.Text:
drawText(c, o, pos, base, clip)
case gradient:
drawGradient(c, o, pos, base, clip)
case *canvas.Rectangle:
drawRectangle(c, o, pos, base, clip)
case fyne.Widget:
drawWidget(c, o, pos, base, clip)
}
return false
}
driver.WalkVisibleObjectTree(c.Content(), paint, nil)
for _, o := range c.Overlays().List() {
driver.WalkVisibleObjectTree(o, paint, nil)
}
return base
}