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
+13 -13
View File
@@ -22,47 +22,47 @@ type LabelStyle struct {
// MaxLines limits the number of lines. Zero means no limit.
MaxLines int
Text string
TextSize unit.Value
TextSize unit.Sp
shaper text.Shaper
}
func H1(th *Theme, txt string) LabelStyle {
label := Label(th, th.TextSize.Scale(96.0/16.0), txt)
label := Label(th, th.TextSize*96.0/16.0, txt)
label.Font.Weight = text.Light
return label
}
func H2(th *Theme, txt string) LabelStyle {
label := Label(th, th.TextSize.Scale(60.0/16.0), txt)
label := Label(th, th.TextSize*60.0/16.0, txt)
label.Font.Weight = text.Light
return label
}
func H3(th *Theme, txt string) LabelStyle {
return Label(th, th.TextSize.Scale(48.0/16.0), txt)
return Label(th, th.TextSize*48.0/16.0, txt)
}
func H4(th *Theme, txt string) LabelStyle {
return Label(th, th.TextSize.Scale(34.0/16.0), txt)
return Label(th, th.TextSize*34.0/16.0, txt)
}
func H5(th *Theme, txt string) LabelStyle {
return Label(th, th.TextSize.Scale(24.0/16.0), txt)
return Label(th, th.TextSize*24.0/16.0, txt)
}
func H6(th *Theme, txt string) LabelStyle {
label := Label(th, th.TextSize.Scale(20.0/16.0), txt)
label := Label(th, th.TextSize*20.0/16.0, txt)
label.Font.Weight = text.Medium
return label
}
func Subtitle1(th *Theme, txt string) LabelStyle {
return Label(th, th.TextSize.Scale(16.0/16.0), txt)
return Label(th, th.TextSize*16.0/16.0, txt)
}
func Subtitle2(th *Theme, txt string) LabelStyle {
label := Label(th, th.TextSize.Scale(14.0/16.0), txt)
label := Label(th, th.TextSize*14.0/16.0, txt)
label.Font.Weight = text.Medium
return label
}
@@ -72,18 +72,18 @@ func Body1(th *Theme, txt string) LabelStyle {
}
func Body2(th *Theme, txt string) LabelStyle {
return Label(th, th.TextSize.Scale(14.0/16.0), txt)
return Label(th, th.TextSize*14.0/16.0, txt)
}
func Caption(th *Theme, txt string) LabelStyle {
return Label(th, th.TextSize.Scale(12.0/16.0), txt)
return Label(th, th.TextSize*12.0/16.0, txt)
}
func Overline(th *Theme, txt string) LabelStyle {
return Label(th, th.TextSize.Scale(10.0/16.0), txt)
return Label(th, th.TextSize*10.0/16.0, txt)
}
func Label(th *Theme, size unit.Value, txt string) LabelStyle {
func Label(th *Theme, size unit.Sp, txt string) LabelStyle {
return LabelStyle{
Text: txt,
Color: th.Palette.Fg,