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>
This commit is contained in:
Elias Naur
2019-07-10 11:14:28 +02:00
parent 4ec352727e
commit 25af3e3701
2 changed files with 41 additions and 25 deletions
+22 -25
View File
@@ -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
+19
View File
@@ -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")
}
}