mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 15:45:38 +00:00
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 <mail@eliasnaur.com>
This commit is contained in:
+15
-12
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user