~eliasnaur/gio

e1248651c8717e686f664279bc0a994b160a5ead — Egon Elbre 2 months ago 6faed7e
layout: automatically add system inset for NewContext

Currently every user needs to manually adjust for system insets.
This is rather verbose and most don't need to deviate from this behavior.

To disable the automatic adjustment, use:

  e.Insets = system.Insets{}
  ctx := layout.NewContext(ops, e)

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
1 files changed, 18 insertions(+), 2 deletions(-)

M layout/context.go
M layout/context.go => layout/context.go +18 -2
@@ 5,6 5,7 @@ package layout
import (
	"time"

	"gioui.org/f32"
	"gioui.org/io/event"
	"gioui.org/io/system"
	"gioui.org/op"


@@ 39,15 40,30 @@ type Context struct {
//     Constraints: Exact(e.Size),
//   }
//
// NewContext calls ops.Reset.
// NewContext calls ops.Reset and adjusts ops for e.Insets.
func NewContext(ops *op.Ops, e system.FrameEvent) Context {
	ops.Reset()

	size := e.Size

	if e.Insets != (system.Insets{}) {
		left := e.Metric.Px(e.Insets.Left)
		top := e.Metric.Px(e.Insets.Top)
		op.Offset(f32.Point{
			X: float32(left),
			Y: float32(top),
		}).Add(ops)

		size.X -= left + e.Metric.Px(e.Insets.Right)
		size.Y -= top + e.Metric.Px(e.Insets.Bottom)
	}

	return Context{
		Ops:         ops,
		Now:         e.Now,
		Queue:       e.Queue,
		Metric:      e.Metric,
		Constraints: Exact(e.Size),
		Constraints: Exact(size),
	}
}