~eliasnaur/gio

5f2adf9b2f2290e1ed82e96ca864ede354c25b43 — Elias Naur 1 year, 3 months ago 1b684ff
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 <mail@eliasnaur.com>
M ui/app/app.go => ui/app/app.go +0 -3
@@ 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)))
}


M ui/app/internal/gpu/gpu.go => ui/app/internal/gpu/gpu.go +2 -16
@@ 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) {

M ui/draw/draw.go => ui/draw/draw.go +2 -13
@@ 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)},
	}
}

M ui/layout/flex.go => ui/layout/flex.go +1 -4
@@ 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)

M ui/layout/layout.go => ui/layout/layout.go +15 -19
@@ 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
		}
	}
	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
		}
	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.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)

M ui/layout/list.go => ui/layout/list.go +5 -3
@@ 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)

M ui/measure/measure.go => ui/measure/measure.go +1 -3
@@ 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

M ui/text/editor.go => ui/text/editor.go +2 -2
@@ 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 {

M ui/text/label.go => ui/text/label.go +6 -16
@@ 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)},
	}
}


M ui/ui.go => ui/ui.go +0 -3
@@ 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)