forked from joejulian/gio
ui/widget: preserve image aspect in Image.Layout
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -17,6 +17,11 @@ type Image struct {
|
||||
}
|
||||
|
||||
func (im Image) Layout(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
|
||||
@@ -24,6 +29,14 @@ func (im Image) Layout(ops *ui.Ops, cs layout.Constraints) layout.Dimens {
|
||||
if d.Y == ui.Inf {
|
||||
d.Y = cs.Height.Min
|
||||
}
|
||||
aspect := float32(w) / float32(h)
|
||||
dw, dh := float32(d.X), float32(d.Y)
|
||||
dAspect := dw / dh
|
||||
if aspect < dAspect {
|
||||
d.X = int(dh*aspect + 0.5)
|
||||
} else {
|
||||
d.Y = int(dw/aspect + 0.5)
|
||||
}
|
||||
dr := f32.Rectangle{
|
||||
Max: f32.Point{X: float32(d.X), Y: float32(d.Y)},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user