From 86b231ca287a08b75745f2dcc47e90c768397b72 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Sun, 8 Sep 2019 22:45:39 +0200 Subject: [PATCH] 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 --- ui/unit.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/ui/unit.go b/ui/unit.go index 665aa58d..418fbfdf 100644 --- a/ui/unit.go +++ b/ui/unit.go @@ -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))) +}