| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
type Flow struct { |
| 21 |
node.ContainerEmbed |
| 22 |
Axis Axis |
| 23 |
} |
| 24 |
|
| 25 |
|
| 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 |
|
| 164 |
type FlowLayoutData struct { |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
AlongWeight int |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
ExpandAlong bool |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
ShrinkAlong bool |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
ExpandAcross bool |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
ShrinkAcross bool |
| 207 |
} |