mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 01:45:36 +00:00
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>
This commit is contained in:
+51
-21
@@ -9,10 +9,16 @@ import (
|
|||||||
"gioui.org/ui/f32"
|
"gioui.org/ui/f32"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Flex lays out interface elements along an axis,
|
||||||
|
// according to alignment and weights.
|
||||||
type Flex struct {
|
type Flex struct {
|
||||||
Axis Axis
|
// Axis is the main axis, either Horizontal or Vertical.
|
||||||
MainAxisAlignment MainAxisAlignment
|
Axis Axis
|
||||||
CrossAxisAlignment CrossAxisAlignment
|
// 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
|
macro ui.MacroOp
|
||||||
ops *ui.Ops
|
ops *ui.Ops
|
||||||
@@ -29,21 +35,26 @@ type FlexChild struct {
|
|||||||
dims Dimens
|
dims Dimens
|
||||||
}
|
}
|
||||||
|
|
||||||
type MainAxisAlignment uint8
|
type Spacing uint8
|
||||||
type CrossAxisAlignment uint8
|
|
||||||
|
|
||||||
type flexMode uint8
|
type flexMode uint8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Start = 100 + iota
|
// SpaceEnd leaves space at the end.
|
||||||
End
|
SpaceEnd Spacing = iota
|
||||||
Center
|
// SpaceStart leaves space at the start.
|
||||||
|
SpaceStart
|
||||||
SpaceAround MainAxisAlignment = iota
|
// 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
|
SpaceBetween
|
||||||
|
// SpaceEvenly distributes space evenly between elements and
|
||||||
|
// at the start and end.
|
||||||
SpaceEvenly
|
SpaceEvenly
|
||||||
|
|
||||||
Baseline CrossAxisAlignment = iota
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -133,10 +144,10 @@ func (f *Flex) Layout(children ...FlexChild) Dimens {
|
|||||||
}
|
}
|
||||||
var mainSize int
|
var mainSize int
|
||||||
var baseline int
|
var baseline int
|
||||||
switch f.MainAxisAlignment {
|
switch f.Spacing {
|
||||||
case Center:
|
case SpaceSides:
|
||||||
mainSize += space / 2
|
mainSize += space / 2
|
||||||
case End:
|
case SpaceStart:
|
||||||
mainSize += space
|
mainSize += space
|
||||||
case SpaceEvenly:
|
case SpaceEvenly:
|
||||||
mainSize += space / (1 + len(children))
|
mainSize += space / (1 + len(children))
|
||||||
@@ -147,10 +158,10 @@ func (f *Flex) Layout(children ...FlexChild) Dimens {
|
|||||||
dims := child.dims
|
dims := child.dims
|
||||||
b := dims.Baseline
|
b := dims.Baseline
|
||||||
var cross int
|
var cross int
|
||||||
switch f.CrossAxisAlignment {
|
switch f.Alignment {
|
||||||
case End:
|
case End:
|
||||||
cross = crossSize - axisCross(f.Axis, dims.Size)
|
cross = crossSize - axisCross(f.Axis, dims.Size)
|
||||||
case Center:
|
case Middle:
|
||||||
cross = (crossSize - axisCross(f.Axis, dims.Size)) / 2
|
cross = (crossSize - axisCross(f.Axis, dims.Size)) / 2
|
||||||
case Baseline:
|
case Baseline:
|
||||||
if f.Axis == Horizontal {
|
if f.Axis == Horizontal {
|
||||||
@@ -164,7 +175,7 @@ func (f *Flex) Layout(children ...FlexChild) Dimens {
|
|||||||
stack.Pop()
|
stack.Pop()
|
||||||
mainSize += axisMain(f.Axis, dims.Size)
|
mainSize += axisMain(f.Axis, dims.Size)
|
||||||
if i < len(children)-1 {
|
if i < len(children)-1 {
|
||||||
switch f.MainAxisAlignment {
|
switch f.Spacing {
|
||||||
case SpaceEvenly:
|
case SpaceEvenly:
|
||||||
mainSize += space / (1 + len(children))
|
mainSize += space / (1 + len(children))
|
||||||
case SpaceAround:
|
case SpaceAround:
|
||||||
@@ -177,10 +188,10 @@ func (f *Flex) Layout(children ...FlexChild) Dimens {
|
|||||||
baseline = b
|
baseline = b
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch f.MainAxisAlignment {
|
switch f.Spacing {
|
||||||
case Center:
|
case SpaceSides:
|
||||||
mainSize += space / 2
|
mainSize += space / 2
|
||||||
case Start:
|
case SpaceEnd:
|
||||||
mainSize += space
|
mainSize += space
|
||||||
case SpaceEvenly:
|
case SpaceEvenly:
|
||||||
mainSize += space / (1 + len(children))
|
mainSize += space / (1 + len(children))
|
||||||
@@ -245,3 +256,22 @@ func axisConstraints(a Axis, mainc, crossc Constraint) Constraints {
|
|||||||
func toPointF(p image.Point) f32.Point {
|
func toPointF(p image.Point) f32.Point {
|
||||||
return f32.Point{X: float32(p.X), Y: float32(p.Y)}
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -27,6 +27,27 @@ type Dimens struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Axis uint8
|
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 (
|
const (
|
||||||
Horizontal Axis = iota
|
Horizontal Axis = iota
|
||||||
@@ -173,3 +194,54 @@ func (a *Align) End(dims Dimens) Dimens {
|
|||||||
Baseline: dims.Baseline,
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+6
-6
@@ -8,8 +8,8 @@ import (
|
|||||||
"gioui.org/ui"
|
"gioui.org/ui"
|
||||||
"gioui.org/ui/gesture"
|
"gioui.org/ui/gesture"
|
||||||
"gioui.org/ui/input"
|
"gioui.org/ui/input"
|
||||||
"gioui.org/ui/pointer"
|
|
||||||
"gioui.org/ui/paint"
|
"gioui.org/ui/paint"
|
||||||
|
"gioui.org/ui/pointer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type scrollChild struct {
|
type scrollChild struct {
|
||||||
@@ -18,9 +18,9 @@ type scrollChild struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type List struct {
|
type List struct {
|
||||||
Axis Axis
|
Axis Axis
|
||||||
Invert bool
|
Invert bool
|
||||||
CrossAxisAlignment CrossAxisAlignment
|
Alignment Alignment
|
||||||
|
|
||||||
// The distance scrolled since last call to Init.
|
// The distance scrolled since last call to Init.
|
||||||
Distance int
|
Distance int
|
||||||
@@ -204,10 +204,10 @@ func (l *List) Layout() Dimens {
|
|||||||
for _, child := range l.children {
|
for _, child := range l.children {
|
||||||
sz := child.size
|
sz := child.size
|
||||||
var cross int
|
var cross int
|
||||||
switch l.CrossAxisAlignment {
|
switch l.Alignment {
|
||||||
case End:
|
case End:
|
||||||
cross = maxCross - axisCross(l.Axis, sz)
|
cross = maxCross - axisCross(l.Axis, sz)
|
||||||
case Center:
|
case Middle:
|
||||||
cross = (maxCross - axisCross(l.Axis, sz)) / 2
|
cross = (maxCross - axisCross(l.Axis, sz)) / 2
|
||||||
}
|
}
|
||||||
childSize := axisMain(l.Axis, sz)
|
childSize := axisMain(l.Axis, sz)
|
||||||
|
|||||||
@@ -25,19 +25,6 @@ type StackChild struct {
|
|||||||
dims Dimens
|
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 {
|
func (s *Stack) Init(ops *ui.Ops, cs Constraints) *Stack {
|
||||||
s.ops = ops
|
s.ops = ops
|
||||||
s.cs = cs
|
s.cs = cs
|
||||||
|
|||||||
Reference in New Issue
Block a user