mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 01:45:36 +00:00
widget/material: change Image to take an ImageOp, not an image.Image
Using ImageOps directly avoids the image copy by NewImageOp. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -14,13 +14,13 @@ import (
|
|||||||
// Image is a widget that displays an image.
|
// Image is a widget that displays an image.
|
||||||
type Image struct {
|
type Image struct {
|
||||||
// Src is the image to display.
|
// Src is the image to display.
|
||||||
Src image.Image
|
Src paint.ImageOp
|
||||||
// Scale is the ratio of image pixels to
|
// Scale is the ratio of image pixels to
|
||||||
// dps.
|
// dps.
|
||||||
Scale float32
|
Scale float32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Theme) Image(img image.Image) Image {
|
func (t *Theme) Image(img paint.ImageOp) Image {
|
||||||
return Image{
|
return Image{
|
||||||
Src: img,
|
Src: img,
|
||||||
Scale: 160 / 72, // About 72 DPI.
|
Scale: 160 / 72, // About 72 DPI.
|
||||||
@@ -28,8 +28,8 @@ func (t *Theme) Image(img image.Image) Image {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (im Image) Layout(gtx *layout.Context) {
|
func (im Image) Layout(gtx *layout.Context) {
|
||||||
size := im.Src.Bounds()
|
size := im.Src.Size()
|
||||||
wf, hf := float32(size.Dx()), float32(size.Dy())
|
wf, hf := float32(size.X), float32(size.Y)
|
||||||
w, h := gtx.Px(unit.Dp(wf*im.Scale)), gtx.Px(unit.Dp(hf*im.Scale))
|
w, h := gtx.Px(unit.Dp(wf*im.Scale)), gtx.Px(unit.Dp(hf*im.Scale))
|
||||||
cs := gtx.Constraints
|
cs := gtx.Constraints
|
||||||
d := image.Point{X: cs.Width.Constrain(w), Y: cs.Height.Constrain(h)}
|
d := image.Point{X: cs.Width.Constrain(w), Y: cs.Height.Constrain(h)}
|
||||||
@@ -44,7 +44,7 @@ func (im Image) Layout(gtx *layout.Context) {
|
|||||||
dr := f32.Rectangle{
|
dr := f32.Rectangle{
|
||||||
Max: f32.Point{X: float32(d.X), Y: float32(d.Y)},
|
Max: f32.Point{X: float32(d.X), Y: float32(d.Y)},
|
||||||
}
|
}
|
||||||
paint.NewImageOp(im.Src).Add(gtx.Ops)
|
im.Src.Add(gtx.Ops)
|
||||||
paint.PaintOp{Rect: dr}.Add(gtx.Ops)
|
paint.PaintOp{Rect: dr}.Add(gtx.Ops)
|
||||||
gtx.Dimensions = layout.Dimensions{Size: d, Baseline: d.Y}
|
gtx.Dimensions = layout.Dimensions{Size: d, Baseline: d.Y}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user