ui/layout: document layout types and helpers

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-08-10 17:09:35 +02:00
parent 79233a3565
commit 8f37a565b9
+24 -11
View File
@@ -21,13 +21,22 @@ type Constraint struct {
Min, Max int Min, Max int
} }
// Dimens are the resolved size and baseline for a user
// interface element.
type Dimens struct { type Dimens struct {
Size image.Point Size image.Point
Baseline int Baseline int
} }
// Axis is the the Horizontal or Vertical direction.
type Axis uint8 type Axis uint8
// Alignment is the relative alignment of a list of
// interface elements.
type Alignment uint8 type Alignment uint8
// Direction is the alignment of a set of interface elements
// relative to a containing space.
type Direction uint8 type Direction uint8
const ( const (
@@ -54,6 +63,7 @@ const (
Vertical Vertical
) )
// Constrain a value to the range [Min; Max].
func (c Constraint) Constrain(v int) int { func (c Constraint) Constrain(v int) int {
if v < c.Min { if v < c.Min {
return c.Min return c.Min
@@ -63,8 +73,9 @@ func (c Constraint) Constrain(v int) int {
return v return v
} }
func (c Constraints) Constrain(p image.Point) image.Point { // Constrain a size to the Width and Height ranges.
return image.Point{X: c.Width.Constrain(p.X), Y: c.Height.Constrain(p.Y)} func (c Constraints) Constrain(size image.Point) image.Point {
return image.Point{X: c.Width.Constrain(size.X), Y: c.Height.Constrain(size.Y)}
} }
// RigidConstraints returns the constraints that can only be // RigidConstraints returns the constraints that can only be
@@ -76,6 +87,7 @@ func RigidConstraints(size image.Point) Constraints {
} }
} }
// Inset adds space around an interface element.
type Inset struct { type Inset struct {
Top, Right, Bottom, Left ui.Value Top, Right, Bottom, Left ui.Value
@@ -85,6 +97,16 @@ type Inset struct {
cs Constraints cs Constraints
} }
// Align aligns an interface element in the available space.
type Align struct {
Alignment Direction
macro ui.MacroOp
ops *ui.Ops
begun bool
cs Constraints
}
func (in *Inset) Begin(c ui.Config, ops *ui.Ops, cs Constraints) Constraints { func (in *Inset) Begin(c ui.Config, ops *ui.Ops, cs Constraints) Constraints {
if in.begun { if in.begun {
panic("must End before Begin") panic("must End before Begin")
@@ -135,15 +157,6 @@ func UniformInset(v ui.Value) Inset {
return Inset{Top: v, Right: v, Bottom: v, Left: v} return Inset{Top: v, Right: v, Bottom: v, Left: v}
} }
type Align struct {
Alignment Direction
macro ui.MacroOp
ops *ui.Ops
begun bool
cs Constraints
}
func (a *Align) Begin(ops *ui.Ops, cs Constraints) Constraints { func (a *Align) Begin(ops *ui.Ops, cs Constraints) Constraints {
if a.begun { if a.begun {
panic("must End before Begin") panic("must End before Begin")