layout: simplified Axis methods

Removed the Main and Cross Axis methods in favor of Convert.

Signed-off-by: pierre <pierre.curto@gmail.com>
This commit is contained in:
pierre
2021-01-14 09:50:11 +01:00
committed by Elias Naur
parent d331dd2de8
commit e088833caf
3 changed files with 31 additions and 44 deletions
+11 -8
View File
@@ -3,6 +3,8 @@
package layout package layout
import ( import (
"image"
"gioui.org/op" "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) cgtx.Constraints = f.Axis.constraints(0, remaining, crossMin, crossMax)
dims := child.widget(cgtx) dims := child.widget(cgtx)
c := macro.Stop() c := macro.Stop()
sz := f.Axis.Main(dims.Size) sz := f.Axis.Convert(dims.Size).X
size += sz size += sz
remaining -= sz remaining -= sz
if remaining < 0 { 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) cgtx.Constraints = f.Axis.constraints(flexSize, flexSize, crossMin, crossMax)
dims := child.widget(cgtx) dims := child.widget(cgtx)
c := macro.Stop() c := macro.Stop()
sz := f.Axis.Main(dims.Size) sz := f.Axis.Convert(dims.Size).X
size += sz size += sz
remaining -= sz remaining -= sz
if remaining < 0 { if remaining < 0 {
@@ -143,7 +145,7 @@ func (f Flex) Layout(gtx Context, children ...FlexChild) Dimensions {
var maxCross int var maxCross int
var maxBaseline int var maxBaseline int
for _, child := range children { 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 maxCross = c
} }
if b := child.dims.Size.Y - child.dims.Baseline; b > maxBaseline { 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 var cross int
switch f.Alignment { switch f.Alignment {
case End: case End:
cross = maxCross - f.Axis.Cross(dims.Size) cross = maxCross - f.Axis.Convert(dims.Size).Y
case Middle: case Middle:
cross = (maxCross - f.Axis.Cross(dims.Size)) / 2 cross = (maxCross - f.Axis.Convert(dims.Size).Y) / 2
case Baseline: case Baseline:
if f.Axis == Horizontal { if f.Axis == Horizontal {
cross = maxBaseline - b cross = maxBaseline - b
} }
} }
stack := op.Save(gtx.Ops) 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) child.call.Add(gtx.Ops)
stack.Load() stack.Load()
mainSize += f.Axis.Main(dims.Size) mainSize += f.Axis.Convert(dims.Size).X
if i < len(children)-1 { if i < len(children)-1 {
switch f.Spacing { switch f.Spacing {
case SpaceEvenly: case SpaceEvenly:
@@ -213,7 +216,7 @@ func (f Flex) Layout(gtx Context, children ...FlexChild) Dimensions {
mainSize += space / (len(children) * 2) 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} return Dimensions{Size: sz, Baseline: sz.Y - maxBaseline}
} }
+6 -23
View File
@@ -225,31 +225,14 @@ func (a Alignment) String() string {
} }
} }
// Main returns the main axis of p. // Convert a point in (x, y) coordinates to (main, cross) coordinates,
// I.e. if a is Horizontal, then the returned value is p.X. // or vice versa. Specifically, Convert((x, y)) returns (x, y) unchanged
func (a Axis) Main(p image.Point) int { // for the horizontal axis, or (y, x) for the vertical axis.
func (a Axis) Convert(pt image.Point) image.Point {
if a == Horizontal { if a == Horizontal {
return p.X return pt
} }
return p.Y return image.Pt(pt.Y, pt.X)
}
// 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)
} }
// mainConstraint returns the min and max main constraints for axis a. // mainConstraint returns the min and max main constraints for axis a.
+14 -13
View File
@@ -178,7 +178,7 @@ func (l *List) nextDir() iterationDir {
// End the current child by specifying its dimensions. // End the current child by specifying its dimensions.
func (l *List) end(dims Dimensions, call op.CallOp) { func (l *List) end(dims Dimensions, call op.CallOp) {
child := scrollChild{dims.Size, call} child := scrollChild{dims.Size, call}
mainSize := l.Axis.Main(child.size) mainSize := l.Axis.Convert(child.size).X
l.maxSize += mainSize l.maxSize += mainSize
switch l.dir { switch l.dir {
case iterateForward: case iterateForward:
@@ -205,7 +205,7 @@ func (l *List) layout(ops *op.Ops, macro op.MacroOp) Dimensions {
// Skip invisible children // Skip invisible children
for len(children) > 0 { for len(children) > 0 {
sz := children[0].size sz := children[0].size
mainSize := l.Axis.Main(sz) mainSize := l.Axis.Convert(sz).X
if l.Position.Offset <= mainSize { if l.Position.Offset <= mainSize {
break break
} }
@@ -216,11 +216,11 @@ func (l *List) layout(ops *op.Ops, macro op.MacroOp) Dimensions {
size := -l.Position.Offset size := -l.Position.Offset
var maxCross int var maxCross int
for i, child := range children { for i, child := range children {
sz := child.size sz := l.Axis.Convert(child.size)
if c := l.Axis.Cross(sz); c > maxCross { if c := sz.Y; c > maxCross {
maxCross = c maxCross = c
} }
size += l.Axis.Main(sz) size += sz.X
if size >= mainMax { if size >= mainMax {
children = children[:i+1] children = children[:i+1]
break break
@@ -232,15 +232,15 @@ func (l *List) layout(ops *op.Ops, macro op.MacroOp) Dimensions {
pos += space pos += space
} }
for _, child := range children { for _, child := range children {
sz := child.size sz := l.Axis.Convert(child.size)
var cross int var cross int
switch l.Alignment { switch l.Alignment {
case End: case End:
cross = maxCross - l.Axis.Cross(sz) cross = maxCross - sz.Y
case Middle: 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 max := childSize + pos
if max > mainMax { if max > mainMax {
max = mainMax max = mainMax
@@ -250,12 +250,13 @@ func (l *List) layout(ops *op.Ops, macro op.MacroOp) Dimensions {
min = 0 min = 0
} }
r := image.Rectangle{ r := image.Rectangle{
Min: l.Axis.point(min, -inf), Min: l.Axis.Convert(image.Pt(min, -inf)),
Max: l.Axis.point(max, inf), Max: l.Axis.Convert(image.Pt(max, inf)),
} }
stack := op.Save(ops) stack := op.Save(ops)
clip.Rect(r).Add(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) child.call.Add(ops)
stack.Load() stack.Load()
pos += childSize pos += childSize
@@ -272,7 +273,7 @@ func (l *List) layout(ops *op.Ops, macro op.MacroOp) Dimensions {
if pos > mainMax { if pos > mainMax {
pos = mainMax pos = mainMax
} }
dims := l.Axis.point(pos, maxCross) dims := l.Axis.Convert(image.Pt(pos, maxCross))
call := macro.Stop() call := macro.Stop()
defer op.Save(ops).Load() defer op.Save(ops).Load()
pointer.Rect(image.Rectangle{Max: dims}).Add(ops) pointer.Rect(image.Rectangle{Max: dims}).Add(ops)