From b1af8aa75e5e34e670fd89d02069cacf0cc6a5fc Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Tue, 9 Jul 2019 16:00:02 +0200 Subject: [PATCH] ui/widget: introduce Image.Scale And use a constant 72 DPI for the default scale, to ensure consistent image sizes across display densities. Signed-off-by: Elias Naur --- ui/widget/image.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/ui/widget/image.go b/ui/widget/image.go index 57628948..ffffed84 100644 --- a/ui/widget/image.go +++ b/ui/widget/image.go @@ -12,23 +12,26 @@ import ( ) type Image struct { - Src image.Image + // Src is the image to display. + Src image.Image + // Rect is the source rectangle. Rect image.Rectangle + // Scale is the ratio of image pixels to + // device pixels. If zero, a scale that + // makes the image appear at approximately + // 72 DPI is used. + Scale float32 } -func (im Image) Layout(ops *ui.Ops, cs layout.Constraints) layout.Dimens { +func (im Image) Layout(c *ui.Config, ops *ui.Ops, cs layout.Constraints) layout.Dimens { size := im.Src.Bounds() - w, h := size.Dx(), size.Dy() - if w == 0 || h == 0 { - return layout.Dimens{} - } - d := image.Point{X: cs.Width.Max, Y: cs.Height.Max} - if d.X == ui.Inf { - d.X = cs.Width.Min - } - if d.Y == ui.Inf { - d.Y = cs.Height.Min + scale := im.Scale + if scale == 0 { + const dpPrPx = 160 / 72 + scale = c.Val(ui.Dp(dpPrPx)) } + w, h := int(float32(size.Dx())*scale+.5), int(float32(size.Dy())*scale+.5) + d := image.Point{X: cs.Width.Constrain(w), Y: cs.Height.Constrain(h)} aspect := float32(w) / float32(h) dw, dh := float32(d.X), float32(d.Y) dAspect := dw / dh