| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package widget |
| 6 |
|
| 7 |
import ( |
| 8 |
"image" |
| 9 |
"image/draw" |
| 10 |
|
| 11 |
"golang.org/x/exp/shiny/screen" |
| 12 |
"golang.org/x/exp/shiny/widget/node" |
| 13 |
"golang.org/x/image/math/f64" |
| 14 |
"golang.org/x/mobile/event/lifecycle" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
type Sheet struct { |
| 31 |
node.ShellEmbed |
| 32 |
buf screen.Buffer |
| 33 |
tex screen.Texture |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
func NewSheet(inner node.Node) *Sheet { |
| 38 |
w := &Sheet{} |
| 39 |
w.Wrapper = w |
| 40 |
if inner != nil { |
| 41 |
w.Insert(inner, nil) |
| 42 |
} |
| 43 |
return w |
| 44 |
} |
| 45 |
|
| 46 |
func (w *Sheet) release() { |
| 47 |
if w.buf != nil { |
| 48 |
w.buf.Release() |
| 49 |
w.buf = nil |
| 50 |
} |
| 51 |
if w.tex != nil { |
| 52 |
w.tex.Release() |
| 53 |
w.tex = nil |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func (w *Sheet) Paint(ctx *node.PaintContext, origin image.Point) (retErr error) { |
| 58 |
w.Marks.UnmarkNeedsPaint() |
| 59 |
c := w.FirstChild |
| 60 |
if c == nil { |
| 61 |
w.release() |
| 62 |
return nil |
| 63 |
} |
| 64 |
|
| 65 |
fresh, size := false, w.Rect.Size() |
| 66 |
if w.buf != nil && w.buf.Size() != size { |
| 67 |
w.release() |
| 68 |
} |
| 69 |
if w.buf == nil { |
| 70 |
w.buf, retErr = ctx.Screen.NewBuffer(size) |
| 71 |
if retErr != nil { |
| 72 |
w.release() |
| 73 |
return retErr |
| 74 |
} |
| 75 |
w.tex, retErr = ctx.Screen.NewTexture(size) |
| 76 |
if retErr != nil { |
| 77 |
w.release() |
| 78 |
return retErr |
| 79 |
} |
| 80 |
fresh = true |
| 81 |
} |
| 82 |
if fresh || c.Marks.NeedsPaintBase() { |
| 83 |
c.Wrapper.PaintBase(&node.PaintBaseContext{ |
| 84 |
Theme: ctx.Theme, |
| 85 |
Dst: w.buf.RGBA(), |
| 86 |
}, image.Point{}) |
| 87 |
} |
| 88 |
|
| 89 |
w.tex.Upload(image.Point{}, w.buf, w.buf.Bounds()) |
| 90 |
|
| 91 |
src2dst := ctx.Src2Dst |
| 92 |
translate(&src2dst, |
| 93 |
float64(origin.X+w.Rect.Min.X), |
| 94 |
float64(origin.Y+w.Rect.Min.Y), |
| 95 |
) |
| 96 |
|
| 97 |
ctx.Drawer.Draw(src2dst, w.tex, w.tex.Bounds(), draw.Over, nil) |
| 98 |
|
| 99 |
return c.Wrapper.Paint(ctx, origin.Add(w.Rect.Min)) |
| 100 |
} |
| 101 |
|
| 102 |
func translate(a *f64.Aff3, tx, ty float64) { |
| 103 |
a[2] += a[0]*tx + a[1]*ty |
| 104 |
a[5] += a[3]*tx + a[4]*ty |
| 105 |
} |
| 106 |
|
| 107 |
func (w *Sheet) PaintBase(ctx *node.PaintBaseContext, origin image.Point) error { |
| 108 |
w.Marks.UnmarkNeedsPaintBase() |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
return nil |
| 113 |
} |
| 114 |
|
| 115 |
func (w *Sheet) OnChildMarked(child node.Node, newMarks node.Marks) { |
| 116 |
if newMarks&node.MarkNeedsPaintBase != 0 { |
| 117 |
newMarks &^= node.MarkNeedsPaintBase |
| 118 |
newMarks |= node.MarkNeedsPaint |
| 119 |
} |
| 120 |
w.Mark(newMarks) |
| 121 |
} |
| 122 |
|
| 123 |
func (w *Sheet) OnLifecycleEvent(e lifecycle.Event) { |
| 124 |
if e.Crosses(lifecycle.StageVisible) == lifecycle.CrossOff { |
| 125 |
w.release() |
| 126 |
} |
| 127 |
} |