Debian Codegraph @

shiny/widget/widget.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 provides graphical user interface widgets.
6 //
7 // TODO: give an overview and some example code.
8 package widget // import "golang.org/x/exp/shiny/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 // Axis is zero, one or both of the horizontal and vertical axes. For example,
26 // a widget may be scrollable in one of the four AxisXxx values.
27 type Axis uint8
28
29 const (
30 AxisNone = Axis(0)
31 AxisHorizontal = Axis(1)
32 AxisVertical = Axis(2)
33 AxisBoth = Axis(3) // AxisBoth equals AxisHorizontal | AxisVertical.
34 )
35
36 func (a Axis) Horizontal() bool { return a&AxisHorizontal != 0 }
37 func (a Axis) Vertical() bool { return a&AxisVertical != 0 }
38
39 // WithLayoutData returns the given node after setting its embedded LayoutData
40 // field.
41 func WithLayoutData(n node.Node, layoutData interface{}) node.Node {
42 n.Wrappee().LayoutData = layoutData
43 return n
44 }
45
46 // RunWindowOptions are optional arguments to RunWindow.
47 type RunWindowOptions struct {
48 NewWindowOptions screen.NewWindowOptions
49 Theme theme.Theme
50
51 // TODO: some mechanism to process, filter and inject events. Perhaps a
52 // screen.EventFilter interface, and note that the zero value in this
53 // RunWindowOptions implicitly includes the gesture.EventFilter?
54 }
55
56 // TODO: how does RunWindow's caller inject or process events (whether general
57 // like lifecycle events or app-specific)? How does it stop the event loop when
58 // the app's work is done?
59
60 // TODO: how do widgets signal that they need repaint or relayout?
61
62 // TODO: propagate keyboard / mouse / touch events.
63
64 // RunWindow creates a new window for s, with the given widget tree, and runs
65 // its event loop.
66 //
67 // A nil opts is valid and means to use the default option values.
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 // paintPending batches up multiple NeedsPaint observations so that we
84 // paint only once (which can be relatively expensive) even when there are
85 // multiple input events in the queue, such as from a rapidly moving mouse
86 // or from the user typing many keys.
87 //
88 // TODO: determine somehow if there's an external paint event in the queue,
89 // not just internal paint events?
90 //
91 // TODO: if every package that uses package screen should basically
92 // throttle like this, should it be provided at a lower level?
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 // TODO: call Mark(node.MarkNeedsPaint)?
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 }