mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 09:55:40 +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:
@@ -31,19 +31,19 @@ type SliderStyle struct {
|
||||
Color color.NRGBA
|
||||
Float *widget.Float
|
||||
|
||||
FingerSize unit.Value
|
||||
FingerSize unit.Dp
|
||||
}
|
||||
|
||||
func (s SliderStyle) Layout(gtx layout.Context) layout.Dimensions {
|
||||
thumbRadius := gtx.Px(unit.Dp(6))
|
||||
trackWidth := gtx.Px(unit.Dp(2))
|
||||
thumbRadius := gtx.Dp(6)
|
||||
trackWidth := gtx.Dp(2)
|
||||
|
||||
axis := s.Float.Axis
|
||||
// Keep a minimum length so that the track is always visible.
|
||||
minLength := thumbRadius + 3*thumbRadius + thumbRadius
|
||||
// Try to expand to finger size, but only if the constraints
|
||||
// allow for it.
|
||||
touchSizePx := min(gtx.Px(s.FingerSize), axis.Convert(gtx.Constraints.Max).Y)
|
||||
touchSizePx := min(gtx.Dp(s.FingerSize), axis.Convert(gtx.Constraints.Max).Y)
|
||||
sizeMain := max(axis.Convert(gtx.Constraints.Min).X, minLength)
|
||||
sizeCross := max(2*thumbRadius, touchSizePx)
|
||||
size := axis.Convert(image.Pt(sizeMain, sizeCross))
|
||||
|
||||
Reference in New Issue
Block a user