~eliasnaur/gio

013ea395b4cd396faad3e8d1bfd480956098ae07 — Elias Naur 10 days ago d82eb8f
all: use new rectangle and point convenience functions

Signed-off-by: Elias Naur <mail@eliasnaur.com>
M gpu/gpu.go => gpu/gpu.go +2 -1
@@ 24,6 24,7 @@ import (
	"gioui.org/internal/ops"
	"gioui.org/internal/path"
	gunsafe "gioui.org/internal/unsafe"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/op/paint"
)


@@ 796,7 797,7 @@ func (d *drawState) materialFor(cache *resourceCache, rect f32.Rectangle, off f3
		m.material = materialTexture
		dr := boundRectF(rect.Add(off))
		sz := d.image.src.Bounds().Size()
		sr := toRectF(d.image.rect)
		sr := layout.FRect(d.image.rect)
		if dx := float32(dr.Dx()); dx != 0 {
			// Don't clip 1 px width sources.
			if sdx := sr.Dx(); sdx > 1 {

M layout/flex.go => layout/flex.go +1 -6
@@ 5,7 5,6 @@ package layout
import (
	"image"

	"gioui.org/f32"
	"gioui.org/op"
)



@@ 175,7 174,7 @@ func (f Flex) Layout(gtx *Context, children ...FlexChild) {
		}
		var stack op.StackOp
		stack.Push(gtx.Ops)
		op.TransformOp{}.Offset(toPointF(axisPoint(f.Axis, mainSize, cross))).Add(gtx.Ops)
		op.TransformOp{}.Offset(FPt(axisPoint(f.Axis, mainSize, cross))).Add(gtx.Ops)
		child.macro.Add()
		stack.Pop()
		mainSize += axisMain(f.Axis, dims.Size)


@@ 252,10 251,6 @@ func axisConstraints(a Axis, mainMin, mainMax, crossMin, crossMax int) Constrain
	}
}

func toPointF(p image.Point) f32.Point {
	return f32.Point{X: float32(p.X), Y: float32(p.Y)}
}

func (s Spacing) String() string {
	switch s {
	case SpaceEnd:

M layout/layout.go => layout/layout.go +2 -2
@@ 130,7 130,7 @@ func (in Inset) Layout(gtx *Context, w Widget) {
	}
	var stack op.StackOp
	stack.Push(gtx.Ops)
	op.TransformOp{}.Offset(toPointF(image.Point{X: left, Y: top})).Add(gtx.Ops)
	op.TransformOp{}.Offset(FPt(image.Point{X: left, Y: top})).Add(gtx.Ops)
	dims := ctxLayout(gtx, mcs, w)
	stack.Pop()
	gtx.Dimensions = Dimensions{


@@ 176,7 176,7 @@ func (a Direction) Layout(gtx *Context, w Widget) {
	}
	var stack op.StackOp
	stack.Push(gtx.Ops)
	op.TransformOp{}.Offset(toPointF(p)).Add(gtx.Ops)
	op.TransformOp{}.Offset(FPt(p)).Add(gtx.Ops)
	macro.Add()
	stack.Pop()
	gtx.Dimensions = Dimensions{

M layout/list.go => layout/list.go +2 -10
@@ 5,7 5,6 @@ package layout
import (
	"image"

	"gioui.org/f32"
	"gioui.org/gesture"
	"gioui.org/io/pointer"
	"gioui.org/op"


@@ 261,8 260,8 @@ func (l *List) layout() Dimensions {
		}
		var stack op.StackOp
		stack.Push(ops)
		clip.Rect{Rect: toRectF(r)}.Op(ops).Add(ops)
		op.TransformOp{}.Offset(toPointF(axisPoint(l.Axis, pos, cross))).Add(ops)
		clip.Rect{Rect: FRect(r)}.Op(ops).Add(ops)
		op.TransformOp{}.Offset(FPt(axisPoint(l.Axis, pos, cross))).Add(ops)
		child.macro.Add()
		stack.Pop()
		pos += childSize


@@ 286,10 285,3 @@ func (l *List) layout() Dimensions {
	l.macro.Add()
	return Dimensions{Size: dims}
}

func toRectF(r image.Rectangle) f32.Rectangle {
	return f32.Rectangle{
		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 layout/stack.go => layout/stack.go +1 -1
@@ 110,7 110,7 @@ func (s Stack) Layout(gtx *Context, children ...StackChild) {
		}
		var stack op.StackOp
		stack.Push(gtx.Ops)
		op.TransformOp{}.Offset(toPointF(p)).Add(gtx.Ops)
		op.TransformOp{}.Offset(FPt(p)).Add(gtx.Ops)
		ch.macro.Add()
		stack.Pop()
		if baseline == 0 {

M op/clip/clip.go => op/clip/clip.go +11 -4
@@ 310,7 310,7 @@ func (rr Rect) Op(ops *op.Ops) Op {
			Max: image.Point{X: int(r.Max.X), Y: int(r.Max.Y)},
		}
		// Optimize pixel-aligned rectangles to just its bounds.
		if r == toRectF(ri) {
		if r == fRect(ri) {
			return Op{bounds: r}
		}
	}


@@ 338,9 338,16 @@ func roundRect(ops *op.Ops, r f32.Rectangle, se, sw, nw, ne float32) Op {
	return p.End()
}

func toRectF(r image.Rectangle) f32.Rectangle {
// fRect converts a rectangle to a f32.Rectangle.
func fRect(r image.Rectangle) f32.Rectangle {
	return f32.Rectangle{
		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)},
		Min: fPt(r.Min), Max: fPt(r.Max),
	}
}

// fPt converts an point to a f32.Point.
func fPt(p image.Point) f32.Point {
	return f32.Point{
		X: float32(p.X), Y: float32(p.Y),
	}
}

M widget/editor.go => widget/editor.go +2 -2
@@ 332,7 332,7 @@ func (e *Editor) PaintText(gtx *layout.Context) {
		stack.Push(gtx.Ops)
		op.TransformOp{}.Offset(shape.offset).Add(gtx.Ops)
		shape.clip.Add(gtx.Ops)
		paint.PaintOp{Rect: toRectF(clip).Sub(shape.offset)}.Add(gtx.Ops)
		paint.PaintOp{Rect: layout.FRect(clip).Sub(shape.offset)}.Add(gtx.Ops)
		stack.Pop()
	}
}


@@ 368,7 368,7 @@ func (e *Editor) PaintCaret(gtx *layout.Context) {
	clip.Max = clip.Max.Add(e.viewSize)
	carRect = clip.Intersect(carRect)
	if !carRect.Empty() {
		paint.PaintOp{Rect: toRectF(carRect)}.Add(gtx.Ops)
		paint.PaintOp{Rect: layout.FRect(carRect)}.Add(gtx.Ops)
	}
	stack.Pop()
}

M widget/icon.go => widget/icon.go +1 -1
@@ 37,7 37,7 @@ func (ic *Icon) Layout(gtx *layout.Context, sz unit.Value) {
	ico.Add(gtx.Ops)
	paint.PaintOp{
		Rect: f32.Rectangle{
			Max: toPointF(ico.Size()),
			Max: layout.FPt(ico.Size()),
		},
	}.Add(gtx.Ops)
	gtx.Dimensions = layout.Dimensions{

M widget/image.go => widget/image.go +1 -5
@@ 35,13 35,9 @@ func (im Image) Layout(gtx *layout.Context) {
	d := cs.Constrain(image.Pt(w, h))
	var s op.StackOp
	s.Push(gtx.Ops)
	clip.Rect{Rect: f32.Rectangle{Max: toPointF(d)}}.Op(gtx.Ops).Add(gtx.Ops)
	clip.Rect{Rect: f32.Rectangle{Max: layout.FPt(d)}}.Op(gtx.Ops).Add(gtx.Ops)
	im.Src.Add(gtx.Ops)
	paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{X: float32(w), Y: float32(h)}}}.Add(gtx.Ops)
	s.Pop()
	gtx.Dimensions = layout.Dimensions{Size: d}
}

func toPointF(p image.Point) f32.Point {
	return f32.Point{X: float32(p.X), Y: float32(p.Y)}
}

M widget/label.go => widget/label.go +3 -10
@@ 102,29 102,22 @@ func (l Label) Layout(gtx *layout.Context, s text.Shaper, font text.Font, size u
		Width:     dims.Size.X,
	}
	for {
		start, end, layout, off, ok := it.Next()
		start, end, l, off, ok := it.Next()
		if !ok {
			break
		}
		lclip := toRectF(clip).Sub(off)
		lclip := layout.FRect(clip).Sub(off)
		var stack op.StackOp
		stack.Push(gtx.Ops)
		op.TransformOp{}.Offset(off).Add(gtx.Ops)
		str := txt[start:end]
		s.ShapeString(font, textSize, str, layout).Add(gtx.Ops)
		s.ShapeString(font, textSize, str, l).Add(gtx.Ops)
		paint.PaintOp{Rect: lclip}.Add(gtx.Ops)
		stack.Pop()
	}
	gtx.Dimensions = dims
}

func toRectF(r image.Rectangle) f32.Rectangle {
	return f32.Rectangle{
		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)},
	}
}

func textPadding(lines []text.Line) (padding image.Rectangle) {
	if len(lines) == 0 {
		return

M widget/material/button.go => widget/material/button.go +0 -4
@@ 175,10 175,6 @@ func (b IconButtonStyle) Layout(gtx *layout.Context, button *widget.Clickable) {
	)
}

func toPointF(p image.Point) f32.Point {
	return f32.Point{X: float32(p.X), Y: float32(p.Y)}
}

func drawInk(gtx *layout.Context, c widget.Click) {
	d := gtx.Now().Sub(c.Time)
	t := float32(d.Seconds())