mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 02:15:34 +00:00
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:
+9
-9
@@ -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),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user