| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package widget |
| 9 |
|
| 10 |
import ( |
| 11 |
"image" |
| 12 |
|
| 13 |
"golang.org/x/exp/shiny/gesture" |
| 14 |
"golang.org/x/exp/shiny/screen" |
| 15 |
"golang.org/x/exp/shiny/unit" |
| 16 |
"golang.org/x/exp/shiny/widget/node" |
| 17 |
"golang.org/x/exp/shiny/widget/theme" |
| 18 |
"golang.org/x/image/math/f64" |
| 19 |
"golang.org/x/mobile/event/lifecycle" |
| 20 |
"golang.org/x/mobile/event/mouse" |
| 21 |
"golang.org/x/mobile/event/paint" |
| 22 |
"golang.org/x/mobile/event/size" |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
type Axis uint8 |
| 28 |
|
| 29 |
const ( |
| 30 |
AxisNone = Axis(0) |
| 31 |
AxisHorizontal = Axis(1) |
| 32 |
AxisVertical = Axis(2) |
| 33 |
AxisBoth = Axis(3) |
| 34 |
) |
| 35 |
|
| 36 |
func (a Axis) Horizontal() bool { return a&AxisHorizontal != 0 } |
| 37 |
func (a Axis) Vertical() bool { return a&AxisVertical != 0 } |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
func WithLayoutData(n node.Node, layoutData interface{}) node.Node { |
| 42 |
n.Wrappee().LayoutData = layoutData |
| 43 |
return n |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
type RunWindowOptions struct { |
| 48 |
NewWindowOptions screen.NewWindowOptions |
| 49 |
Theme theme.Theme |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
func RunWindow(s screen.Screen, root node.Node, opts *RunWindowOptions) error { |
| 69 |
var ( |
| 70 |
nwo *screen.NewWindowOptions |
| 71 |
t *theme.Theme |
| 72 |
) |
| 73 |
if opts != nil { |
| 74 |
nwo = &opts.NewWindowOptions |
| 75 |
t = &opts.Theme |
| 76 |
} |
| 77 |
w, err := s.NewWindow(nwo) |
| 78 |
if err != nil { |
| 79 |
return err |
| 80 |
} |
| 81 |
defer w.Release() |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
paintPending := false |
| 94 |
|
| 95 |
gef := gesture.EventFilter{EventDeque: w} |
| 96 |
for { |
| 97 |
e := w.NextEvent() |
| 98 |
|
| 99 |
if e = gef.Filter(e); e == nil { |
| 100 |
continue |
| 101 |
} |
| 102 |
|
| 103 |
switch e := e.(type) { |
| 104 |
case lifecycle.Event: |
| 105 |
root.OnLifecycleEvent(e) |
| 106 |
if e.To == lifecycle.StageDead { |
| 107 |
return nil |
| 108 |
} |
| 109 |
|
| 110 |
case gesture.Event, mouse.Event: |
| 111 |
root.OnInputEvent(e, image.Point{}) |
| 112 |
|
| 113 |
case paint.Event: |
| 114 |
ctx := &node.PaintContext{ |
| 115 |
Theme: t, |
| 116 |
Screen: s, |
| 117 |
Drawer: w, |
| 118 |
Src2Dst: f64.Aff3{ |
| 119 |
1, 0, 0, |
| 120 |
0, 1, 0, |
| 121 |
}, |
| 122 |
} |
| 123 |
if err := root.Paint(ctx, image.Point{}); err != nil { |
| 124 |
return err |
| 125 |
} |
| 126 |
w.Publish() |
| 127 |
paintPending = false |
| 128 |
|
| 129 |
case size.Event: |
| 130 |
if dpi := float64(e.PixelsPerPt) * unit.PointsPerInch; dpi != t.GetDPI() { |
| 131 |
newT := new(theme.Theme) |
| 132 |
if t != nil { |
| 133 |
*newT = *t |
| 134 |
} |
| 135 |
newT.DPI = dpi |
| 136 |
t = newT |
| 137 |
} |
| 138 |
|
| 139 |
size := e.Size() |
| 140 |
root.Measure(t, size.X, size.Y) |
| 141 |
root.Wrappee().Rect = e.Bounds() |
| 142 |
root.Layout(t) |
| 143 |
|
| 144 |
|
| 145 |
case error: |
| 146 |
return e |
| 147 |
} |
| 148 |
|
| 149 |
if !paintPending && root.Wrappee().Marks.NeedsPaint() { |
| 150 |
paintPending = true |
| 151 |
w.Send(paint.Event{}) |
| 152 |
} |
| 153 |
} |
| 154 |
} |