mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 15:45:38 +00:00
3d37491342
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>
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package widget
|
|
|
|
import (
|
|
"image"
|
|
|
|
"gioui.org/f32"
|
|
"gioui.org/layout"
|
|
"gioui.org/op"
|
|
"gioui.org/op/clip"
|
|
"gioui.org/op/paint"
|
|
"gioui.org/unit"
|
|
)
|
|
|
|
// Image is a widget that displays an image.
|
|
type Image struct {
|
|
// Src is the image to display.
|
|
Src paint.ImageOp
|
|
// Fit specifies how to scale the image to the constraints.
|
|
// By default it does not do any scaling.
|
|
Fit Fit
|
|
// Position specifies where to position the image within
|
|
// the constraints.
|
|
Position layout.Direction
|
|
// Scale is the ratio of image pixels to
|
|
// dps. If Scale is zero Image falls back to
|
|
// a scale that match a standard 72 DPI.
|
|
Scale float32
|
|
}
|
|
|
|
const defaultScale = float32(160.0 / 72.0)
|
|
|
|
func (im Image) Layout(gtx layout.Context) layout.Dimensions {
|
|
scale := im.Scale
|
|
if scale == 0 {
|
|
scale = defaultScale
|
|
}
|
|
|
|
size := im.Src.Size()
|
|
wf, hf := float32(size.X), float32(size.Y)
|
|
w, h := gtx.Dp(unit.Dp(wf*scale)), gtx.Dp(unit.Dp(hf*scale))
|
|
|
|
dims, trans := im.Fit.scale(gtx.Constraints, im.Position, layout.Dimensions{Size: image.Pt(w, h)})
|
|
defer clip.Rect{Max: dims.Size}.Push(gtx.Ops).Pop()
|
|
|
|
pixelScale := scale * gtx.Metric.PxPerDp
|
|
trans = trans.Mul(f32.Affine2D{}.Scale(f32.Point{}, f32.Pt(pixelScale, pixelScale)))
|
|
defer op.Affine(trans).Push(gtx.Ops).Pop()
|
|
|
|
im.Src.Add(gtx.Ops)
|
|
paint.PaintOp{}.Add(gtx.Ops)
|
|
|
|
return dims
|
|
}
|