Files
gio-patched/widget/material/list_test.go
T
Elias Naur 3d37491342 all: [API] replace unit.Value with separate unit.Dp, unit.Sp types
The unit.Value is a struct and thus more inconvenient to use than its
underlying float32 type. In addition, most uses don't need a general
value, but rather a specific unit given by the context. This change
replaces unit.Value with two float32 units, Dp and Sp. It also changes
variables and parameters of unit.Value to a specific unit type matching
the context. That is, unit.Dp everywhere except for text sizes which are
in Sp.

Switching to typed float32s has multiple advantages

- They can be constants:

const touchSlop = unit.Dp(16)

- Casting untyped constants is no longer necessary:

insets := layout.UniformInset(16)

- Calculation with values is natural:

func (s ScrollbarStyle) Width() unit.Dp {
	return s.Indicator.MinorWidth + s.Track.MinorPadding + s.Track.MinorPadding
}

The main API change is that calls to gtx.Px must be replaced with either
gtx.Dp or gtx.Sp depending on the unit.

Idea by Christophe Meessen.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-05-31 10:24:09 +02:00

72 lines
2.0 KiB
Go

package material_test
import (
"image"
"testing"
"time"
"gioui.org/font/gofont"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
)
func TestListAnchorStrategies(t *testing.T) {
var ops op.Ops
gtx := layout.NewContext(&ops, system.FrameEvent{
Metric: unit.Metric{
PxPerDp: 1,
PxPerSp: 1,
},
Now: time.Now(),
Size: image.Point{
X: 500,
Y: 500,
},
})
gtx.Constraints.Min = image.Point{}
var spaceConstraints layout.Constraints
space := func(gtx layout.Context, index int) layout.Dimensions {
spaceConstraints = gtx.Constraints
if spaceConstraints.Min.X < 0 || spaceConstraints.Min.Y < 0 ||
spaceConstraints.Max.X < 0 || spaceConstraints.Max.Y < 0 {
t.Errorf("invalid constraints at index %d: %#+v", index, spaceConstraints)
}
return layout.Dimensions{Size: image.Point{
X: gtx.Constraints.Max.X,
Y: gtx.Dp(20),
}}
}
var list widget.List
list.Axis = layout.Vertical
elements := 100
th := material.NewTheme(gofont.Collection())
materialList := material.List(th, &list)
indicatorWidth := gtx.Dp(materialList.Width())
materialList.AnchorStrategy = material.Occupy
occupyDims := materialList.Layout(gtx, elements, space)
occupyConstraints := spaceConstraints
materialList.AnchorStrategy = material.Overlay
overlayDims := materialList.Layout(gtx, elements, space)
overlayConstraints := spaceConstraints
// Both anchor strategies should use all space available if their elements do.
if occupyDims != overlayDims {
t.Errorf("expected occupy dims (%v) to be equal to overlay dims (%v)", occupyDims, overlayDims)
}
// The overlay strategy should not reserve any space for the scroll indicator,
// so the constraints that it presents to its elements should be larger than
// those presented by the occupy strategy.
if overlayConstraints.Max.X != occupyConstraints.Max.X+indicatorWidth {
t.Errorf("overlay max width (%d) != occupy max width (%d) + indicator width (%d)",
overlayConstraints.Max.X, occupyConstraints.Max.X, indicatorWidth)
}
}