## ~eliasnaur/gio

25af3e3701deaa38aac55718e0865c18defd80de — Elias Naur 1 year, 4 months ago
```ui/layout: replace Sized struct with simpler Constraint.Exact method

While here, add String methods to ui.Value and ui.Unit.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
```
```2 files changed, 41 insertions(+), 25 deletions(-)

M ui/layout/layout.go
M ui/unit.go
```
`M ui/layout/layout.go => ui/layout/layout.go +22 -25`
```@@ 50,15 50,36 @@ func (c Constraints) Expand() Constraints {
func (c Constraint) Expand() Constraint {
return Constraint{Min: c.Max, Max: c.Max}
}
+
func (c Constraints) Loose() Constraints {
return Constraints{Width: c.Width.Loose(), Height: c.Height.Loose()}
}

+func (c Constraints) Exact(width, height int) Constraints {
+	if height != 0 {
+		if c.Height.Min < height {
+			c.Height.Min = height
+		}
+		if height < c.Height.Max {
+			c.Height.Max = height
+		}
+	}
+	if width != 0 {
+		if c.Width.Min < width {
+			c.Width.Min = width
+		}
+		if width < c.Width.Max {
+			c.Width.Max = width
+		}
+	}
+	return c
+}
+
func (c Constraint) Loose() Constraint {
return Constraint{Max: c.Max}
}

-// ExactConstraints returns the constraints that exactly represents the
+// ExactConstraints returns the rigid constraints that represents the
// given dimensions.
func ExactConstraints(size image.Point) Constraints {
return Constraints{

@@ 130,30 151,6 @@ func EqualInsets(v ui.Value) Insets {
return Insets{Top: v, Right: v, Bottom: v, Left: v}
}

-type Sized struct {
-	Width, Height ui.Value
-}
-
-func (s Sized) Constrain(c *ui.Config, cs Constraints) Constraints {
-	if h := int(c.Val(s.Height) + 0.5); h != 0 {
-		if cs.Height.Min < h {
-			cs.Height.Min = h
-		}
-		if h < cs.Height.Max {
-			cs.Height.Max = h
-		}
-	}
-	if w := int(c.Val(s.Width) + .5); w != 0 {
-		if cs.Width.Min < w {
-			cs.Width.Min = w
-		}
-		if w < cs.Width.Max {
-			cs.Width.Max = w
-		}
-	}
-	return cs
-}
-
type Align struct {
Alignment Direction

```
`M ui/unit.go => ui/unit.go +19 -0`
```@@ 2,6 2,8 @@

package ui

+import "fmt"
+
// Value is a value with a unit.
type Value struct {
V float32

@@ 34,3 36,20 @@ func Dp(v float32) Value {
func Sp(v float32) Value {
return Value{V: v, U: UnitSp}
}
+
+func (v Value) String() string {
+	return fmt.Sprintf("%g%s", v.V, v.U)
+}
+
+func (u Unit) String() string {
+	switch u {
+	case UnitPx:
+		return "px"
+	case UnitDp:
+		return "dp"
+	case UnitSp:
+		return "sp"
+	default:
+		panic("unknown unit")
+	}
+}

```