1a15d7241ac4e58bfc6eb063d267cc692822ec10 — Elias Naur a month ago 2a0b007
ui/layout: rename and sanitize enums

Rename MainAxisAlignment to Spacing and CrossAxisAlignment to just
Alignment.

Drop the untyped Start, End, Center values and add them as Spacing
and Direction values. Center is both a Direction and Alignment, so
use the synonym "Middle" for the alignment.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
4 files changed, 129 insertions(+), 40 deletions(-)

M ui/layout/flex.go
M ui/layout/layout.go
M ui/layout/list.go
M ui/layout/stack.go
M ui/layout/flex.go => ui/layout/flex.go +51 -21
@@ 9,10 9,16 @@ "gioui.org/ui/f32"
  )
  
+ // Flex lays out interface elements along an axis,
+ // according to alignment and weights.
  type Flex struct {
- 	Axis               Axis
- 	MainAxisAlignment  MainAxisAlignment
- 	CrossAxisAlignment CrossAxisAlignment
+ 	// Axis is the main axis, either Horizontal or Vertical.
+ 	Axis Axis
+ 	// Spacing controls the distribution of any space left after
+ 	// layout.
+ 	Spacing Spacing
+ 	// Alignment is the alignment in the cross axis.
+ 	Alignment Alignment
  
  	macro       ui.MacroOp
  	ops         *ui.Ops


@@ 29,21 35,26 @@ dims  Dimens
  }
  
- type MainAxisAlignment uint8
- type CrossAxisAlignment uint8
+ type Spacing uint8
  
  type flexMode uint8
  
  const (
- 	Start = 100 + iota
- 	End
- 	Center
- 
- 	SpaceAround MainAxisAlignment = iota
+ 	// SpaceEnd leaves space at the end.
+ 	SpaceEnd Spacing = iota
+ 	// SpaceStart leaves space at the start.
+ 	SpaceStart
+ 	// SpaceSides shares space between the start and end.
+ 	SpaceSides
+ 	// SpaceAround distributes space evenly between elements,
+ 	// with half as much space at the start and end.
+ 	SpaceAround
+ 	// SpaceBetween distributes space evenly between elements,
+ 	// leaving no space at the start and end.
  	SpaceBetween
+ 	// SpaceEvenly distributes space evenly between elements and
+ 	// at the start and end.
  	SpaceEvenly
- 
- 	Baseline CrossAxisAlignment = iota
  )
  
  const (


@@ 133,10 144,10 @@ }
  	var mainSize int
  	var baseline int
- 	switch f.MainAxisAlignment {
- 	case Center:
+ 	switch f.Spacing {
+ 	case SpaceSides:
  		mainSize += space / 2
- 	case End:
+ 	case SpaceStart:
  		mainSize += space
  	case SpaceEvenly:
  		mainSize += space / (1 + len(children))


@@ 147,10 158,10 @@ dims := child.dims
  		b := dims.Baseline
  		var cross int
- 		switch f.CrossAxisAlignment {
+ 		switch f.Alignment {
  		case End:
  			cross = crossSize - axisCross(f.Axis, dims.Size)
- 		case Center:
+ 		case Middle:
  			cross = (crossSize - axisCross(f.Axis, dims.Size)) / 2
  		case Baseline:
  			if f.Axis == Horizontal {


@@ 164,7 175,7 @@ stack.Pop()
  		mainSize += axisMain(f.Axis, dims.Size)
  		if i < len(children)-1 {
- 			switch f.MainAxisAlignment {
+ 			switch f.Spacing {
  			case SpaceEvenly:
  				mainSize += space / (1 + len(children))
  			case SpaceAround:


@@ 177,10 188,10 @@ baseline = b
  		}
  	}
- 	switch f.MainAxisAlignment {
- 	case Center:
+ 	switch f.Spacing {
+ 	case SpaceSides:
  		mainSize += space / 2
- 	case Start:
+ 	case SpaceEnd:
  		mainSize += space
  	case SpaceEvenly:
  		mainSize += space / (1 + len(children))


@@ 245,3 256,22 @@ func toPointF(p image.Point) f32.Point {
  	return f32.Point{X: float32(p.X), Y: float32(p.Y)}
  }
+ 
+ func (s Spacing) String() string {
+ 	switch s {
+ 	case SpaceEnd:
+ 		return "SpaceEnd"
+ 	case SpaceStart:
+ 		return "SpaceStart"
+ 	case SpaceSides:
+ 		return "SpaceSides"
+ 	case SpaceAround:
+ 		return "SpaceAround"
+ 	case SpaceBetween:
+ 		return "SpaceAround"
+ 	case SpaceEvenly:
+ 		return "SpaceEvenly"
+ 	default:
+ 		panic("unreachable")
+ 	}
+ }

M ui/layout/layout.go => ui/layout/layout.go +72 -0
@@ 27,6 27,27 @@ }
  
  type Axis uint8
+ type Alignment uint8
+ type Direction uint8
+ 
+ const (
+ 	Start Alignment = iota
+ 	End
+ 	Middle
+ 	Baseline
+ )
+ 
+ const (
+ 	NW Direction = iota
+ 	N
+ 	NE
+ 	E
+ 	SE
+ 	S
+ 	SW
+ 	W
+ 	Center
+ )
  
  const (
  	Horizontal Axis = iota


@@ 173,3 194,54 @@ Baseline: dims.Baseline,
  	}
  }
+ 
+ func (a Alignment) String() string {
+ 	switch a {
+ 	case Start:
+ 		return "Start"
+ 	case End:
+ 		return "End"
+ 	case Middle:
+ 		return "Middle"
+ 	case Baseline:
+ 		return "Baseline"
+ 	default:
+ 		panic("unreachable")
+ 	}
+ }
+ 
+ func (a Axis) String() string {
+ 	switch a {
+ 	case Horizontal:
+ 		return "Horizontal"
+ 	case Vertical:
+ 		return "Vertical"
+ 	default:
+ 		panic("unreachable")
+ 	}
+ }
+ 
+ func (d Direction) String() string {
+ 	switch d {
+ 	case NW:
+ 		return "NW"
+ 	case N:
+ 		return "N"
+ 	case NE:
+ 		return "NE"
+ 	case E:
+ 		return "E"
+ 	case SE:
+ 		return "SE"
+ 	case S:
+ 		return "S"
+ 	case SW:
+ 		return "SW"
+ 	case W:
+ 		return "W"
+ 	case Center:
+ 		return "Center"
+ 	default:
+ 		panic("unreachable")
+ 	}
+ }

M ui/layout/list.go => ui/layout/list.go +6 -6
@@ 8,8 8,8 @@ "gioui.org/ui"
  	"gioui.org/ui/gesture"
  	"gioui.org/ui/input"
- 	"gioui.org/ui/pointer"
  	"gioui.org/ui/paint"
+ 	"gioui.org/ui/pointer"
  )
  
  type scrollChild struct {


@@ 18,9 18,9 @@ }
  
  type List struct {
- 	Axis               Axis
- 	Invert             bool
- 	CrossAxisAlignment CrossAxisAlignment
+ 	Axis      Axis
+ 	Invert    bool
+ 	Alignment Alignment
  
  	// The distance scrolled since last call to Init.
  	Distance int


@@ 204,10 204,10 @@ for _, child := range l.children {
  		sz := child.size
  		var cross int
- 		switch l.CrossAxisAlignment {
+ 		switch l.Alignment {
  		case End:
  			cross = maxCross - axisCross(l.Axis, sz)
- 		case Center:
+ 		case Middle:
  			cross = (maxCross - axisCross(l.Axis, sz)) / 2
  		}
  		childSize := axisMain(l.Axis, sz)

M ui/layout/stack.go => ui/layout/stack.go +0 -13
@@ 25,19 25,6 @@ dims  Dimens
  }
  
- type Direction uint8
- 
- const (
- 	NW Direction = iota
- 	N
- 	NE
- 	E
- 	SE
- 	S
- 	SW
- 	W
- )
- 
  func (s *Stack) Init(ops *ui.Ops, cs Constraints) *Stack {
  	s.ops = ops
  	s.cs = cs