86b231ca287a08b75745f2dcc47e90c768397b72 — Elias Naur 3 months ago 5eef85f
ui: add Max, Add for Values

Finding the maximum or adding Values are particularly for adjusting
margins for the safe area insets returned in app.UpdateEvent.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
1 files changed, 37 insertions(+), 0 deletions(-)

M ui/unit.go
M ui/unit.go => ui/unit.go +37 -0
@@ 57,3 57,40 @@ func (u Unit) String() string {
 		panic("unknown unit")
 	}
 }
+
+// Add a list of Values.
+func Add(c Config, values ...Value) Value {
+	var sum Value
+	for _, v := range values {
+		sum, v = compatible(c, sum, v)
+		sum.V += v.V
+	}
+	return sum
+}
+
+// Max returns the maximum of a list of Values.
+func Max(c Config, values ...Value) Value {
+	var max Value
+	for _, v := range values {
+		max, v = compatible(c, max, v)
+		if v.V > max.V {
+			max.V = v.V
+		}
+	}
+	return max
+}
+
+func compatible(c Config, v1, v2 Value) (Value, Value) {
+	if v1.U == v2.U {
+		return v1, v2
+	}
+	if v1.V == 0 {
+		v1.U = v2.U
+		return v1, v2
+	}
+	if v2.V == 0 {
+		v2.U = v1.U
+		return v1, v2
+	}
+	return Px(float32(c.Px(v1))), Px(float32(c.Px(v2)))
+}