Debian Codegraph @

shiny/widget/sheet.go

raw symbols SCIP
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
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 // TODO: scrolling.
18
19 // Sheet is a shell widget that provides *image.RGBA pixel buffers (analogous
20 // to blank sheets of paper) for its descendent widgets to paint on, via their
21 // PaintBase methods. Such buffers may be cached and their contents reused for
22 // multiple paints, which can make scrolling and animation smoother and more
23 // efficient.
24 //
25 // A simple app may have only one Sheet, near the root of its widget tree. A
26 // more complicated app may have multiple Sheets. For example, consider a text
27 // editor consisting of a small header bar and a large text widget. Those two
28 // nodes may be backed by two separate Sheets, since scrolling the latter
29 // should not scroll the former.
30 type Sheet struct {
31 node.ShellEmbed
32 buf screen.Buffer
33 tex screen.Texture
34 }
35
36 // NewSheet returns a new Sheet widget.
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 // TODO: should draw.Over be configurable?
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 // Do not recursively call PaintBase on our children. We create our own
110 // buffers, and Sheet.Paint will call PaintBase with our PaintBaseContext
111 // instead of our ancestor's.
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 }