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>
This commit is contained in:
Elias Naur
2022-04-25 10:08:01 +02:00
parent 48a8540a68
commit 3d37491342
35 changed files with 212 additions and 307 deletions
+12 -7
View File
@@ -52,15 +52,15 @@ func NewContext(ops *op.Ops, e system.FrameEvent) Context {
size := e.Size
if e.Insets != (system.Insets{}) {
left := e.Metric.Px(e.Insets.Left)
top := e.Metric.Px(e.Insets.Top)
left := e.Metric.Dp(e.Insets.Left)
top := e.Metric.Dp(e.Insets.Top)
op.Offset(image.Point{
X: left,
Y: top,
}).Add(ops)
size.X -= left + e.Metric.Px(e.Insets.Right)
size.Y -= top + e.Metric.Px(e.Insets.Bottom)
size.X -= left + e.Metric.Dp(e.Insets.Right)
size.Y -= top + e.Metric.Dp(e.Insets.Bottom)
}
return Context{
@@ -72,9 +72,14 @@ func NewContext(ops *op.Ops, e system.FrameEvent) Context {
}
}
// Px maps the value to pixels.
func (c Context) Px(v unit.Value) int {
return c.Metric.Px(v)
// Dp converts v to pixels.
func (c Context) Dp(v unit.Dp) int {
return c.Metric.Dp(v)
}
// Sp converts v to pixels.
func (c Context) Sp(v unit.Sp) int {
return c.Metric.Sp(v)
}
// Events returns the events available for the key. If no
+1 -1
View File
@@ -16,7 +16,7 @@ For example, to add space above a widget:
var gtx layout.Context
// Configure a top inset.
inset := layout.Inset{Top: unit.Dp(8), ...}
inset := layout.Inset{Top: 8, ...}
// Use the inset to lay out a widget.
inset.Layout(gtx, func() {
// Lay out widget and determine its size given the constraints
+1 -2
View File
@@ -6,7 +6,6 @@ import (
"gioui.org/layout"
"gioui.org/op"
"gioui.org/unit"
)
func ExampleInset() {
@@ -19,7 +18,7 @@ func ExampleInset() {
}
// Inset all edges by 10.
inset := layout.UniformInset(unit.Dp(10))
inset := layout.UniformInset(10)
dims := inset.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
// Lay out a 50x50 sized widget.
dims := layoutWidget(gtx, 50, 50)
+9 -9
View File
@@ -113,15 +113,15 @@ func (c Constraints) Constrain(size image.Point) image.Point {
// constraints. The minimum constraints will be adjusted to ensure
// they do not exceed the maximum.
type Inset struct {
Top, Bottom, Left, Right unit.Value
Top, Bottom, Left, Right unit.Dp
}
// Layout a widget.
func (in Inset) Layout(gtx Context, w Widget) Dimensions {
top := gtx.Px(in.Top)
right := gtx.Px(in.Right)
bottom := gtx.Px(in.Bottom)
left := gtx.Px(in.Left)
top := gtx.Dp(in.Top)
right := gtx.Dp(in.Right)
bottom := gtx.Dp(in.Bottom)
left := gtx.Dp(in.Left)
mcs := gtx.Constraints
mcs.Max.X -= left + right
if mcs.Max.X < 0 {
@@ -153,7 +153,7 @@ func (in Inset) Layout(gtx Context, w Widget) Dimensions {
// UniformInset returns an Inset with a single inset applied to all
// edges.
func UniformInset(v unit.Value) Inset {
func UniformInset(v unit.Dp) Inset {
return Inset{Top: v, Right: v, Bottom: v, Left: v}
}
@@ -213,14 +213,14 @@ func (d Direction) Position(widget, bounds image.Point) image.Point {
// Spacer adds space between widgets.
type Spacer struct {
Width, Height unit.Value
Width, Height unit.Dp
}
func (s Spacer) Layout(gtx Context) Dimensions {
return Dimensions{
Size: image.Point{
X: gtx.Px(s.Width),
Y: gtx.Px(s.Height),
X: gtx.Dp(s.Width),
Y: gtx.Dp(s.Height),
},
}
}