mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-05 17:35:36 +00:00
layout: move Context to its own file
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -0,0 +1,50 @@
|
|||||||
|
// SPDX-License-Identifier: Unlicense OR MIT
|
||||||
|
|
||||||
|
package layout
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
|
||||||
|
"gioui.org/io/event"
|
||||||
|
"gioui.org/io/system"
|
||||||
|
"gioui.org/op"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Context carries the state needed by almost all layouts and widgets.
|
||||||
|
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
|
||||||
|
|
||||||
|
system.Config
|
||||||
|
event.Queue
|
||||||
|
*op.Ops
|
||||||
|
}
|
||||||
|
|
||||||
|
// layout a widget with a set of constraints and return its
|
||||||
|
// dimensions. The widget dimensions are constrained abd 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(cfg system.Config, size image.Point) {
|
||||||
|
c.Constraints = RigidConstraints(size)
|
||||||
|
c.Dimensions = Dimensions{}
|
||||||
|
c.Config = cfg
|
||||||
|
if c.Ops == nil {
|
||||||
|
c.Ops = new(op.Ops)
|
||||||
|
}
|
||||||
|
c.Ops.Reset()
|
||||||
|
}
|
||||||
@@ -5,8 +5,6 @@ package layout
|
|||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
|
|
||||||
"gioui.org/io/event"
|
|
||||||
"gioui.org/io/system"
|
|
||||||
"gioui.org/op"
|
"gioui.org/op"
|
||||||
"gioui.org/unit"
|
"gioui.org/unit"
|
||||||
)
|
)
|
||||||
@@ -44,20 +42,6 @@ type Direction uint8
|
|||||||
// computing dimensions for a user interface element.
|
// computing dimensions for a user interface element.
|
||||||
type Widget func()
|
type Widget func()
|
||||||
|
|
||||||
// Context carries the state needed by almost all layouts and widgets.
|
|
||||||
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
|
|
||||||
|
|
||||||
system.Config
|
|
||||||
event.Queue
|
|
||||||
*op.Ops
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Start Alignment = iota
|
Start Alignment = iota
|
||||||
End
|
End
|
||||||
@@ -82,31 +66,6 @@ const (
|
|||||||
Vertical
|
Vertical
|
||||||
)
|
)
|
||||||
|
|
||||||
// layout a widget with a set of constraints and return its
|
|
||||||
// dimensions. The widget dimensions are constrained abd 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(cfg system.Config, size image.Point) {
|
|
||||||
c.Constraints = RigidConstraints(size)
|
|
||||||
c.Dimensions = Dimensions{}
|
|
||||||
c.Config = cfg
|
|
||||||
if c.Ops == nil {
|
|
||||||
c.Ops = new(op.Ops)
|
|
||||||
}
|
|
||||||
c.Ops.Reset()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Constrain a value to the range [Min; Max].
|
// Constrain a value to the range [Min; Max].
|
||||||
func (c Constraint) Constrain(v int) int {
|
func (c Constraint) Constrain(v int) int {
|
||||||
if v < c.Min {
|
if v < c.Min {
|
||||||
|
|||||||
Reference in New Issue
Block a user