From 5f2adf9b2f2290e1ed82e96ca864ede354c25b43 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Mon, 29 Jul 2019 12:12:51 +0200 Subject: [PATCH] ui: get rid of Inf It's not worth the special cases. Use a large value where needed (layout.List, text.Editor...) instead. Signed-off-by: Elias Naur --- ui/app/app.go | 3 --- ui/app/internal/gpu/gpu.go | 18 ++---------------- ui/draw/draw.go | 15 ++------------- ui/layout/flex.go | 5 +---- ui/layout/layout.go | 32 ++++++++++++++------------------ ui/layout/list.go | 8 +++++--- ui/measure/measure.go | 4 +--- ui/text/editor.go | 4 ++-- ui/text/label.go | 22 ++++++---------------- ui/ui.go | 3 --- 10 files changed, 33 insertions(+), 81 deletions(-) diff --git a/ui/app/app.go b/ui/app/app.go index 57dcb85e..1f2ac07d 100644 --- a/ui/app/app.go +++ b/ui/app/app.go @@ -153,9 +153,6 @@ func (c *Config) Px(v ui.Value) int { default: panic("unknown unit") } - if math.IsInf(float64(r), +1) { - return ui.Inf - } return int(math.Round(float64(r))) } diff --git a/ui/app/internal/gpu/gpu.go b/ui/app/internal/gpu/gpu.go index 88a3e7c1..90b3ed5c 100644 --- a/ui/app/internal/gpu/gpu.go +++ b/ui/app/internal/gpu/gpu.go @@ -606,25 +606,11 @@ func boundRectF(r f32.Rectangle) image.Rectangle { } func ceil(v float32) int { - switch { - case math.IsInf(float64(v), +1): - return ui.Inf - case math.IsInf(float64(v), -1): - return -ui.Inf - default: - return int(math.Ceil(float64(v))) - } + return int(math.Ceil(float64(v))) } func floor(v float32) int { - switch { - case math.IsInf(float64(v), +1): - return ui.Inf - case math.IsInf(float64(v), -1): - return -ui.Inf - default: - return int(math.Floor(float64(v))) - } + return int(math.Floor(float64(v))) } func (d *drawOps) reset(cache *resourceCache, viewport image.Point) { diff --git a/ui/draw/draw.go b/ui/draw/draw.go index 9967edcb..52ba241e 100644 --- a/ui/draw/draw.go +++ b/ui/draw/draw.go @@ -119,20 +119,9 @@ func RectClip(r image.Rectangle) ClipOp { return ClipOp{bounds: toRectF(r)} } -func itof(i int) float32 { - switch i { - case ui.Inf: - return float32(math.Inf(+1)) - case -ui.Inf: - return float32(math.Inf(-1)) - default: - return float32(i) - } -} - func toRectF(r image.Rectangle) f32.Rectangle { return f32.Rectangle{ - Min: f32.Point{X: itof(r.Min.X), Y: itof(r.Min.Y)}, - Max: f32.Point{X: itof(r.Max.X), Y: itof(r.Max.Y)}, + Min: f32.Point{X: float32(r.Min.X), Y: float32(r.Min.Y)}, + Max: f32.Point{X: float32(r.Max.X), Y: float32(r.Max.Y)}, } } diff --git a/ui/layout/flex.go b/ui/layout/flex.go index 1b1fdc79..0c5bbe88 100644 --- a/ui/layout/flex.go +++ b/ui/layout/flex.go @@ -82,9 +82,6 @@ func (f *Flex) Rigid() Constraints { f.begin(modeRigid) mainc := axisMainConstraint(f.Axis, f.cs) mainMax := mainc.Max - if mainc.Max != ui.Inf { - mainMax -= f.size - } return axisConstraints(f.Axis, Constraint{Max: mainMax}, axisCrossConstraint(f.Axis, f.cs)) } @@ -92,7 +89,7 @@ func (f *Flex) Flexible(weight float32) Constraints { f.begin(modeFlex) mainc := axisMainConstraint(f.Axis, f.cs) var flexSize int - if mainc.Max != ui.Inf && mainc.Max > f.size { + if mainc.Max > f.size { maxSize := mainc.Max - f.size flexSize = mainc.Max - f.rigidSize flexSize = int(float32(flexSize)*weight + .5) diff --git a/ui/layout/layout.go b/ui/layout/layout.go index 9e5460a3..ef6b2b17 100644 --- a/ui/layout/layout.go +++ b/ui/layout/layout.go @@ -75,25 +75,21 @@ func (in *Inset) Begin(c ui.Config, ops *ui.Ops, cs Constraints) Constraints { in.begun = true in.cs = cs mcs := cs - if mcs.Width.Max != ui.Inf { - mcs.Width.Min -= in.left + in.right - mcs.Width.Max -= in.left + in.right - if mcs.Width.Min < 0 { - mcs.Width.Min = 0 - } - if mcs.Width.Max < mcs.Width.Min { - mcs.Width.Max = mcs.Width.Min - } + mcs.Width.Min -= in.left + in.right + mcs.Width.Max -= in.left + in.right + if mcs.Width.Min < 0 { + mcs.Width.Min = 0 } - if mcs.Height.Max != ui.Inf { - mcs.Height.Min -= in.top + in.bottom - mcs.Height.Max -= in.top + in.bottom - if mcs.Height.Min < 0 { - mcs.Height.Min = 0 - } - if mcs.Height.Max < mcs.Height.Min { - mcs.Height.Max = mcs.Height.Min - } + if mcs.Width.Max < mcs.Width.Min { + mcs.Width.Max = mcs.Width.Min + } + mcs.Height.Min -= in.top + in.bottom + mcs.Height.Max -= in.top + in.bottom + if mcs.Height.Min < 0 { + mcs.Height.Min = 0 + } + if mcs.Height.Max < mcs.Height.Min { + mcs.Height.Max = mcs.Height.Min } in.stack.Push(ops) ui.TransformOp{}.Offset(toPointF(image.Point{X: in.left, Y: in.top})).Add(ops) diff --git a/ui/layout/list.go b/ui/layout/list.go index 50a397a8..2df153db 100644 --- a/ui/layout/list.go +++ b/ui/layout/list.go @@ -56,6 +56,8 @@ const ( iterateBackward ) +const inf = 1e6 + // Init prepares the list for iterating through its elements with Next. func (l *List) Init(cfg ui.Config, q input.Queue, ops *ui.Ops, cs Constraints, len int) { if l.more { @@ -117,7 +119,7 @@ func (l *List) Index() int { // Constraints is the constraints for the current element. func (l *List) Constraints() Constraints { - return axisConstraints(l.Axis, Constraint{Max: ui.Inf}, axisCrossConstraint(l.Axis, l.cs)) + return axisConstraints(l.Axis, Constraint{Max: inf}, axisCrossConstraint(l.Axis, l.cs)) } func (l *List) More() bool { @@ -223,8 +225,8 @@ func (l *List) Layout() Dimens { min, max = mainc.Max-max, mainc.Max-min } r := image.Rectangle{ - Min: axisPoint(l.Axis, min, -ui.Inf), - Max: axisPoint(l.Axis, max, ui.Inf), + Min: axisPoint(l.Axis, min, -inf), + Max: axisPoint(l.Axis, max, inf), } var stack ui.StackOp stack.Push(ops) diff --git a/ui/measure/measure.go b/ui/measure/measure.go index 75381325..5213c0ed 100644 --- a/ui/measure/measure.go +++ b/ui/measure/measure.go @@ -148,9 +148,7 @@ func layoutText(ppem fixed.Int26_6, str string, f *opentype, opts text.LayoutOpt } var lines []text.Line maxDotX := fixed.Int26_6(math.MaxInt32) - if opts.MaxWidth != ui.Inf { - maxDotX = fixed.I(opts.MaxWidth) - } + maxDotX = fixed.I(opts.MaxWidth) type state struct { r rune advs []fixed.Int26_6 diff --git a/ui/text/editor.go b/ui/text/editor.go index 737f3943..3659ee5b 100644 --- a/ui/text/editor.go +++ b/ui/text/editor.go @@ -159,9 +159,9 @@ func (e *Editor) Layout(cfg ui.Config, queue input.Queue, ops *ui.Ops, cs layout e.padLeft, e.padRight = twoDp, twoDp maxWidth := cs.Width.Max if e.SingleLine { - maxWidth = ui.Inf + maxWidth = inf } - if maxWidth != ui.Inf { + if maxWidth != inf { maxWidth -= e.padLeft + e.padRight } if maxWidth != e.maxWidth { diff --git a/ui/text/label.go b/ui/text/label.go index 3a67d232..86b034fe 100644 --- a/ui/text/label.go +++ b/ui/text/label.go @@ -5,7 +5,6 @@ package text import ( "image" "image/color" - "math" "unicode/utf8" "gioui.org/ui" @@ -36,6 +35,8 @@ type lineIterator struct { y, prevDesc fixed.Int26_6 } +const inf = 1e6 + func (l *lineIterator) Next() (String, f32.Point, bool) { for len(l.Lines) > 0 { line := l.Lines[0] @@ -92,8 +93,8 @@ func (l Label) Layout(ops *ui.Ops, cs layout.Constraints) layout.Dimens { dims.Size = cs.Constrain(dims.Size) padTop, padBottom := textPadding(lines) clip := image.Rectangle{ - Min: image.Point{X: -ui.Inf, Y: -padTop}, - Max: image.Point{X: ui.Inf, Y: dims.Size.Y + padBottom}, + Min: image.Point{X: -inf, Y: -padTop}, + Max: image.Point{X: inf, Y: dims.Size.Y + padBottom}, } l.it = lineIterator{ Lines: lines, @@ -120,21 +121,10 @@ func (l Label) Layout(ops *ui.Ops, cs layout.Constraints) layout.Dimens { return dims } -func itof(i int) float32 { - switch i { - case ui.Inf: - return float32(math.Inf(+1)) - case -ui.Inf: - return float32(math.Inf(-1)) - default: - return float32(i) - } -} - func toRectF(r image.Rectangle) f32.Rectangle { return f32.Rectangle{ - Min: f32.Point{X: itof(r.Min.X), Y: itof(r.Min.Y)}, - Max: f32.Point{X: itof(r.Max.X), Y: itof(r.Max.Y)}, + Min: f32.Point{X: float32(r.Min.X), Y: float32(r.Min.Y)}, + Max: f32.Point{X: float32(r.Max.X), Y: float32(r.Max.Y)}, } } diff --git a/ui/ui.go b/ui/ui.go index b399f8e1..7f9a78f6 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -32,9 +32,6 @@ type TransformOp struct { offset f32.Point } -// Inf is the int value that represents an unbounded maximum constraint. -const Inf = int(^uint(0) >> 1) - func (r InvalidateOp) Add(o *Ops) { data := make([]byte, ops.TypeRedrawLen) data[0] = byte(ops.TypeInvalidate)