Debian Codegraph @

shiny/widget/flow.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
10 "golang.org/x/exp/shiny/widget/node"
11 "golang.org/x/exp/shiny/widget/theme"
12 )
13
14 // TODO: padding, alignment.
15
16 // Flow is a container widget that lays out its children in sequence along an
17 // axis, either horizontally or vertically. The children's laid out size may
18 // differ from their natural size, along or across that axis, if a child's
19 // LayoutData is a FlowLayoutData.
20 type Flow struct {
21 node.ContainerEmbed
22 Axis Axis
23 }
24
25 // NewFlow returns a new Flow widget containing the given children.
26 func NewFlow(a Axis, children ...node.Node) *Flow {
27 w := &Flow{
28 Axis: a,
29 }
30 w.Wrapper = w
31 for _, c := range children {
32 w.Insert(c, nil)
33 }
34 return w
35 }
36
37 func (w *Flow) Measure(t *theme.Theme, widthHint, heightHint int) {
38 if w.Axis != AxisHorizontal && w.Axis != AxisVertical {
39 w.ContainerEmbed.Measure(t, widthHint, heightHint)
40 return
41 }
42
43 if w.Axis == AxisHorizontal {
44 widthHint = node.NoHint
45 }
46 if w.Axis == AxisVertical {
47 heightHint = node.NoHint
48 }
49
50 mSize := image.Point{}
51 for c := w.FirstChild; c != nil; c = c.NextSibling {
52 c.Wrapper.Measure(t, widthHint, heightHint)
53 if w.Axis == AxisHorizontal {
54 mSize.X += c.MeasuredSize.X
55 if mSize.Y < c.MeasuredSize.Y {
56 mSize.Y = c.MeasuredSize.Y
57 }
58 } else {
59 mSize.Y += c.MeasuredSize.Y
60 if mSize.X < c.MeasuredSize.X {
61 mSize.X = c.MeasuredSize.X
62 }
63 }
64 }
65 w.MeasuredSize = mSize
66 }
67
68 func (w *Flow) Layout(t *theme.Theme) {
69 if w.Axis != AxisHorizontal && w.Axis != AxisVertical {
70 w.ContainerEmbed.Layout(t)
71 return
72 }
73
74 extra, totalExpandWeight, totalShrinkWeight := 0, 0, 0
75 if w.Axis == AxisHorizontal {
76 extra = w.Rect.Dx()
77 } else {
78 extra = w.Rect.Dy()
79 }
80 for c := w.FirstChild; c != nil; c = c.NextSibling {
81 if d, ok := c.LayoutData.(FlowLayoutData); ok && d.AlongWeight > 0 {
82 if d.AlongWeight <= 0 {
83 continue
84 }
85 if d.ExpandAlong {
86 totalExpandWeight += d.AlongWeight
87 }
88 if d.ShrinkAlong {
89 totalShrinkWeight += d.AlongWeight
90 }
91 }
92 if w.Axis == AxisHorizontal {
93 extra -= c.MeasuredSize.X
94 } else {
95 extra -= c.MeasuredSize.Y
96 }
97 }
98 expand, shrink, totalWeight := extra > 0, extra < 0, 0
99 if expand {
100 if totalExpandWeight == 0 {
101 expand = false
102 } else {
103 totalWeight = totalExpandWeight
104 }
105 }
106 if shrink {
107 if totalShrinkWeight == 0 {
108 shrink = false
109 } else {
110 totalWeight = totalShrinkWeight
111 }
112 }
113
114 p := image.Point{}
115 for c := w.FirstChild; c != nil; c = c.NextSibling {
116 q := p.Add(c.MeasuredSize)
117 if d, ok := c.LayoutData.(FlowLayoutData); ok {
118 if d.AlongWeight > 0 {
119 if (expand && d.ExpandAlong) || (shrink && d.ShrinkAlong) {
120 delta := extra * d.AlongWeight / totalWeight
121 extra -= delta
122 totalWeight -= d.AlongWeight
123 if w.Axis == AxisHorizontal {
124 q.X += delta
125 if q.X < p.X {
126 q.X = p.X
127 }
128 } else {
129 q.Y += delta
130 if q.Y < p.Y {
131 q.Y = p.Y
132 }
133 }
134 }
135 }
136
137 if w.Axis == AxisHorizontal {
138 q.Y = stretchAcross(q.Y, w.Rect.Dy(), d.ExpandAcross, d.ShrinkAcross)
139 } else {
140 q.X = stretchAcross(q.X, w.Rect.Dx(), d.ExpandAcross, d.ShrinkAcross)
141 }
142 }
143 c.Rect = image.Rectangle{
144 Min: p,
145 Max: q,
146 }
147 c.Wrapper.Layout(t)
148 if w.Axis == AxisHorizontal {
149 p.X = q.X
150 } else {
151 p.Y = q.Y
152 }
153 }
154 }
155
156 func stretchAcross(child, parent int, expand, shrink bool) int {
157 if (expand && child < parent) || (shrink && child > parent) {
158 return parent
159 }
160 return child
161 }
162
163 // FlowLayoutData is the node LayoutData type for a Flow's children.
164 type FlowLayoutData struct {
165 // AlongWeight is the relative weight for distributing any space surplus or
166 // deficit along the Flow's axis. For example, if an AxisHorizontal Flow's
167 // Rect width was 100 pixels greater than the sum of its children's natural
168 // widths, and three children had non-zero FlowLayoutData.AlongWeight
169 // values 6, 3 and 1 (and their FlowLayoutData.ExpandAlong values were
170 // true) then those children's laid out widths would be larger than their
171 // natural widths by 60, 30 and 10 pixels.
172 //
173 // A negative AlongWeight is equivalent to zero.
174 AlongWeight int
175
176 // ExpandAlong is whether the child's laid out size should increase along
177 // the Flow's axis, based on AlongWeight, if there is a space surplus (the
178 // children's measured size total less than the parent's size). To allow
179 // size decreases as well as increases, set ShrinkAlong.
180 ExpandAlong bool
181
182 // ShrinkAlong is whether the child's laid out size should decrease along
183 // the Flow's axis, based on AlongWeight, if there is a space deficit (the
184 // children's measured size total more than the parent's size). To allow
185 // size increases as well as decreases, set ExpandAlong.
186 ShrinkAlong bool
187
188 // ExpandAcross is whether the child's laid out size should increase along
189 // the Flow's cross-axis if there is a space surplus (the child's measured
190 // size is less than the parent's size). To allow size decreases as well as
191 // increases, set ShrinkAcross.
192 //
193 // For example, if an AxisHorizontal Flow's Rect height was 80 pixels, any
194 // child whose FlowLayoutData.ExpandAcross was true would also be laid out
195 // with at least an 80 pixel height.
196 ExpandAcross bool
197
198 // ShrinkAcross is whether the child's laid out size should decrease along
199 // the Flow's cross-axis if there is a space deficit (the child's measured
200 // size is more than the parent's size). To allow size increases as well as
201 // decreases, set ExpandAcross.
202 //
203 // For example, if an AxisHorizontal Flow's Rect height was 80 pixels, any
204 // child whose FlowLayoutData.ShrinkAcross was true would also be laid out
205 // with at most an 80 pixel height.
206 ShrinkAcross bool
207 }