From e088833caf08ef8f8fd6d984b887e90fe8daabdf Mon Sep 17 00:00:00 2001 From: pierre Date: Thu, 14 Jan 2021 09:50:11 +0100 Subject: [PATCH] layout: simplified Axis methods Removed the Main and Cross Axis methods in favor of Convert. Signed-off-by: pierre --- layout/flex.go | 19 +++++++++++-------- layout/layout.go | 29 ++++++----------------------- layout/list.go | 27 ++++++++++++++------------- 3 files changed, 31 insertions(+), 44 deletions(-) diff --git a/layout/flex.go b/layout/flex.go index d197d755..1aaab715 100644 --- a/layout/flex.go +++ b/layout/flex.go @@ -3,6 +3,8 @@ package layout import ( + "image" + "gioui.org/op" ) @@ -96,7 +98,7 @@ func (f Flex) Layout(gtx Context, children ...FlexChild) Dimensions { cgtx.Constraints = f.Axis.constraints(0, remaining, crossMin, crossMax) dims := child.widget(cgtx) c := macro.Stop() - sz := f.Axis.Main(dims.Size) + sz := f.Axis.Convert(dims.Size).X size += sz remaining -= sz if remaining < 0 { @@ -131,7 +133,7 @@ func (f Flex) Layout(gtx Context, children ...FlexChild) Dimensions { cgtx.Constraints = f.Axis.constraints(flexSize, flexSize, crossMin, crossMax) dims := child.widget(cgtx) c := macro.Stop() - sz := f.Axis.Main(dims.Size) + sz := f.Axis.Convert(dims.Size).X size += sz remaining -= sz if remaining < 0 { @@ -143,7 +145,7 @@ func (f Flex) Layout(gtx Context, children ...FlexChild) Dimensions { var maxCross int var maxBaseline int for _, child := range children { - if c := f.Axis.Cross(child.dims.Size); c > maxCross { + if c := f.Axis.Convert(child.dims.Size).Y; c > maxCross { maxCross = c } if b := child.dims.Size.Y - child.dims.Baseline; b > maxBaseline { @@ -173,19 +175,20 @@ func (f Flex) Layout(gtx Context, children ...FlexChild) Dimensions { var cross int switch f.Alignment { case End: - cross = maxCross - f.Axis.Cross(dims.Size) + cross = maxCross - f.Axis.Convert(dims.Size).Y case Middle: - cross = (maxCross - f.Axis.Cross(dims.Size)) / 2 + cross = (maxCross - f.Axis.Convert(dims.Size).Y) / 2 case Baseline: if f.Axis == Horizontal { cross = maxBaseline - b } } stack := op.Save(gtx.Ops) - op.Offset(FPt(f.Axis.point(mainSize, cross))).Add(gtx.Ops) + pt := f.Axis.Convert(image.Pt(mainSize, cross)) + op.Offset(FPt(pt)).Add(gtx.Ops) child.call.Add(gtx.Ops) stack.Load() - mainSize += f.Axis.Main(dims.Size) + mainSize += f.Axis.Convert(dims.Size).X if i < len(children)-1 { switch f.Spacing { case SpaceEvenly: @@ -213,7 +216,7 @@ func (f Flex) Layout(gtx Context, children ...FlexChild) Dimensions { mainSize += space / (len(children) * 2) } } - sz := f.Axis.point(mainSize, maxCross) + sz := f.Axis.Convert(image.Pt(mainSize, maxCross)) return Dimensions{Size: sz, Baseline: sz.Y - maxBaseline} } diff --git a/layout/layout.go b/layout/layout.go index dc3074f5..eb854927 100644 --- a/layout/layout.go +++ b/layout/layout.go @@ -225,31 +225,14 @@ func (a Alignment) String() string { } } -// Main returns the main axis of p. -// I.e. if a is Horizontal, then the returned value is p.X. -func (a Axis) Main(p image.Point) int { +// Convert a point in (x, y) coordinates to (main, cross) coordinates, +// or vice versa. Specifically, Convert((x, y)) returns (x, y) unchanged +// for the horizontal axis, or (y, x) for the vertical axis. +func (a Axis) Convert(pt image.Point) image.Point { if a == Horizontal { - return p.X + return pt } - return p.Y -} - -// Cross returns the cross axis of p. -// I.e. if a is Horizontal, then the returned value is p.Y. -func (a Axis) Cross(p image.Point) int { - if a == Horizontal { - return p.Y - } - return p.X -} - -// point returns the point having its values set based on the axis. -// I.e. if a is Horizontal, then the returned value is image.Point{X: main, Y: cross}. -func (a Axis) point(main, cross int) image.Point { - if a == Horizontal { - return image.Pt(main, cross) - } - return image.Pt(cross, main) + return image.Pt(pt.Y, pt.X) } // mainConstraint returns the min and max main constraints for axis a. diff --git a/layout/list.go b/layout/list.go index 201a3d0a..85eb9114 100644 --- a/layout/list.go +++ b/layout/list.go @@ -178,7 +178,7 @@ func (l *List) nextDir() iterationDir { // End the current child by specifying its dimensions. func (l *List) end(dims Dimensions, call op.CallOp) { child := scrollChild{dims.Size, call} - mainSize := l.Axis.Main(child.size) + mainSize := l.Axis.Convert(child.size).X l.maxSize += mainSize switch l.dir { case iterateForward: @@ -205,7 +205,7 @@ func (l *List) layout(ops *op.Ops, macro op.MacroOp) Dimensions { // Skip invisible children for len(children) > 0 { sz := children[0].size - mainSize := l.Axis.Main(sz) + mainSize := l.Axis.Convert(sz).X if l.Position.Offset <= mainSize { break } @@ -216,11 +216,11 @@ func (l *List) layout(ops *op.Ops, macro op.MacroOp) Dimensions { size := -l.Position.Offset var maxCross int for i, child := range children { - sz := child.size - if c := l.Axis.Cross(sz); c > maxCross { + sz := l.Axis.Convert(child.size) + if c := sz.Y; c > maxCross { maxCross = c } - size += l.Axis.Main(sz) + size += sz.X if size >= mainMax { children = children[:i+1] break @@ -232,15 +232,15 @@ func (l *List) layout(ops *op.Ops, macro op.MacroOp) Dimensions { pos += space } for _, child := range children { - sz := child.size + sz := l.Axis.Convert(child.size) var cross int switch l.Alignment { case End: - cross = maxCross - l.Axis.Cross(sz) + cross = maxCross - sz.Y case Middle: - cross = (maxCross - l.Axis.Cross(sz)) / 2 + cross = (maxCross - sz.Y) / 2 } - childSize := l.Axis.Main(sz) + childSize := sz.X max := childSize + pos if max > mainMax { max = mainMax @@ -250,12 +250,13 @@ func (l *List) layout(ops *op.Ops, macro op.MacroOp) Dimensions { min = 0 } r := image.Rectangle{ - Min: l.Axis.point(min, -inf), - Max: l.Axis.point(max, inf), + Min: l.Axis.Convert(image.Pt(min, -inf)), + Max: l.Axis.Convert(image.Pt(max, inf)), } stack := op.Save(ops) clip.Rect(r).Add(ops) - op.Offset(FPt(l.Axis.point(pos, cross))).Add(ops) + pt := l.Axis.Convert(image.Pt(pos, cross)) + op.Offset(FPt(pt)).Add(ops) child.call.Add(ops) stack.Load() pos += childSize @@ -272,7 +273,7 @@ func (l *List) layout(ops *op.Ops, macro op.MacroOp) Dimensions { if pos > mainMax { pos = mainMax } - dims := l.Axis.point(pos, maxCross) + dims := l.Axis.Convert(image.Pt(pos, maxCross)) call := macro.Stop() defer op.Save(ops).Load() pointer.Rect(image.Rectangle{Max: dims}).Add(ops)