Files
gio-patched/layout/context.go
T
Elias Naur 7bf3265ccd layout,widget: transpose Constraints to use image.Points for limits
Instead of

    type Contraints struct {
	    Width, Height Constraint
    }

use

    type Constraints struct {
	    Min, Max image.Point
    }

which leads to simpler use. For example, the Min method is trivally replaced by
the field, and the RigidConstraints constructor is no longer a net win.

API Change. Rewrites:

    gofmt -r 'gtx.Constraints.Min() -> gtx.Constraints.Min'
    gofmt -r 'gtx.Constraints.Width.Min -> gtx.Constraints.Min.X'
    gofmt -r 'gtx.Constraints.Height.Min -> gtx.Constraints.Min.Y'
    gofmt -r 'gtx.Constraints.Height.Max -> gtx.Constraints.Max.Y'
    gofmt -r 'gtx.Constraints.Width.Max -> gtx.Constraints.Max.X'

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-19 09:58:07 +02:00

83 lines
2.0 KiB
Go

// SPDX-License-Identifier: Unlicense OR MIT
package layout
import (
"image"
"math"
"time"
"gioui.org/io/event"
"gioui.org/io/system"
"gioui.org/op"
"gioui.org/unit"
)
// Context carries the state needed by almost all layouts and widgets.
// A zero value Context never returns events, map units to pixels
// with a scale of 1.0, and returns the zero time from Now.
type Context struct {
// Constraints track the constraints for the active widget or
// layout.
Constraints Constraints
// Dimensions track the result of the most recent layout
// operation.
Dimensions Dimensions
cfg system.Config
queue event.Queue
*op.Ops
}
// layout a widget with a set of constraints and return its
// dimensions. The widget dimensions are constrained and the previous
// constraints are restored after layout.
func ctxLayout(gtx *Context, cs Constraints, w Widget) Dimensions {
saved := gtx.Constraints
gtx.Constraints = cs
gtx.Dimensions = Dimensions{}
w()
gtx.Dimensions.Size = cs.Constrain(gtx.Dimensions.Size)
gtx.Constraints = saved
return gtx.Dimensions
}
// Reset the context. The constraints' minimum and maximum values are
// set to the size.
func (c *Context) Reset(q event.Queue, cfg system.Config, size image.Point) {
c.Constraints = Constraints{Min: size, Max: size}
c.Dimensions = Dimensions{}
c.cfg = cfg
c.queue = q
if c.Ops == nil {
c.Ops = new(op.Ops)
}
c.Ops.Reset()
}
// Now returns the configuration time or the zero time.
func (c *Context) Now() time.Time {
if c.cfg == nil {
return time.Time{}
}
return c.cfg.Now()
}
// Px maps the value to pixels. If no configuration is set,
// Px returns the rounded value of v.
func (c *Context) Px(v unit.Value) int {
if c.cfg == nil {
return int(math.Round(float64(v.V)))
}
return c.cfg.Px(v)
}
// Events returns the events available for the key. If no
// queue is configured, Events returns nil.
func (c *Context) Events(k event.Tag) []event.Event {
if c.queue == nil {
return nil
}
return c.queue.Events(k)
}