diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2026-02-24 17:49:26 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2026-02-24 17:49:26 -0500 |
| commit | c99ff181003c565385c21b92ca15161fbdff2bd8 (patch) | |
| tree | e5f55594cfc6a5912807449166653ff049894293 | |
| parent | 1b48ef0a114dfb85f2a5b8c5bed7c2b9bc8b72cc (diff) | |
| download | gui-c99ff181003c565385c21b92ca15161fbdff2bd8.zip | |
add layout constraints and abs/rel units
| -rw-r--r-- | constraint.go | 34 | ||||
| -rw-r--r-- | len.go | 25 | ||||
| -rw-r--r-- | len_test.go | 16 |
3 files changed, 75 insertions, 0 deletions
diff --git a/constraint.go b/constraint.go new file mode 100644 index 0000000..c08e222 --- /dev/null +++ b/constraint.go @@ -0,0 +1,34 @@ +package gui + +// Constraint imposes a restriction on the size of a widget or layout. +type Constraint struct { + // Dim is the dimension to constrain: width/height. + Dim + + // Relation declares whether the constraint is an upper, lower, or exact bound. + Relation + + // Length is the target or threshold value. + Length +} + +// Dim is a dimension of a widget or layout that can be constrained. +type Dim int + +const ( + _ Dim = iota + Width + Height +) + +// Relation is an (in)equality. +type Relation int + +const ( + _ Relation = iota + Eq // == + Gteq // >= + Gt // > + Lteq // <= + Lt // < +) @@ -0,0 +1,25 @@ +package gui + +// TODO: add font-size-relative units once a text rendering package is added. + +// Length allows distance or size to be expressed in absolute or relative units. +type Length interface { + // Px resolves the Length to pixels for the given parent Env's size in pixels. + Px(parent int) int +} + +// Px is a Length expressed in pixels. +type Px int + +// Px implements the Length interface. +func (p Px) Px(parent int) int { return int(p) } + +// Relative is a Length relative to the width or height of the parent Env. +// +// Relative(0.10) <=> 10%. +type Relative float64 + +// Px implements the Length interface. +func (r Relative) Px(parent int) int { + return int(float64(r) * float64(parent)) +} diff --git a/len_test.go b/len_test.go new file mode 100644 index 0000000..91b0886 --- /dev/null +++ b/len_test.go @@ -0,0 +1,16 @@ +package gui_test + +import ( + "fmt" + "image" + + "github.com/faiface/gui" +) + +func ExampleRelative() { + var l Length = Relative(0.10) // 10% + r := image.Rect(0, 0, 100, 100) // 100x100 rectangle + fmt.Println(l.Px(r)) + // Output: + // 10 +} |