mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
widget,widget/material: move Image and Icon to widget package
There is nothing theme-specific about displaying images and icons, so move the types from the material package to the generic widget package. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
// SPDX-License-Identifier: Unlicense OR MIT
|
||||
|
||||
package widget
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
|
||||
"gioui.org/f32"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op/paint"
|
||||
"gioui.org/unit"
|
||||
"golang.org/x/exp/shiny/iconvg"
|
||||
)
|
||||
|
||||
type Icon struct {
|
||||
Color color.RGBA
|
||||
src []byte
|
||||
// Cached values.
|
||||
op paint.ImageOp
|
||||
imgSize int
|
||||
imgColor color.RGBA
|
||||
}
|
||||
|
||||
// NewIcon returns a new Icon from IconVG data.
|
||||
func NewIcon(data []byte) (*Icon, error) {
|
||||
_, err := iconvg.DecodeMetadata(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Icon{src: data, Color: color.RGBA{A: 0xff}}, nil
|
||||
}
|
||||
|
||||
func (ic *Icon) Layout(gtx *layout.Context, sz unit.Value) {
|
||||
ico := ic.image(gtx.Px(sz))
|
||||
ico.Add(gtx.Ops)
|
||||
paint.PaintOp{
|
||||
Rect: f32.Rectangle{
|
||||
Max: toPointF(ico.Size()),
|
||||
},
|
||||
}.Add(gtx.Ops)
|
||||
gtx.Dimensions = layout.Dimensions{
|
||||
Size: ico.Size(),
|
||||
}
|
||||
}
|
||||
|
||||
func (ic *Icon) image(sz int) paint.ImageOp {
|
||||
if sz == ic.imgSize && ic.Color == ic.imgColor {
|
||||
return ic.op
|
||||
}
|
||||
m, _ := iconvg.DecodeMetadata(ic.src)
|
||||
dx, dy := m.ViewBox.AspectRatio()
|
||||
img := image.NewRGBA(image.Rectangle{Max: image.Point{X: sz, Y: int(float32(sz) * dy / dx)}})
|
||||
var ico iconvg.Rasterizer
|
||||
ico.SetDstImage(img, img.Bounds(), draw.Src)
|
||||
m.Palette[0] = ic.Color
|
||||
iconvg.Decode(&ico, ic.src, &iconvg.DecodeOptions{
|
||||
Palette: &m.Palette,
|
||||
})
|
||||
ic.op = paint.NewImageOp(img)
|
||||
ic.imgSize = sz
|
||||
ic.imgColor = ic.Color
|
||||
return ic.op
|
||||
}
|
||||
Reference in New Issue
Block a user