Files
gio/widget/icon.go
T
Elias Naur 94d242d18c op/paint: remove support for PaintOp.Rect
PaintOp.Rect is the wrong abstraction; it implies a clip operation
better handled by package clip, and not all paints need it (colors).
Furthermore, it's awkward to specify a PaintOp that fills up the
current clip area, regardless of its size.

Redefine PathOp to mean "fill current clip area".

API change. Replace uses of PaintOp.Rect with a TransformOp applied
before the PaintOp.

Leave a TODO for the PathOp infinity area.

Fixes gio#167

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-11-05 16:32:19 +01:00

61 lines
1.3 KiB
Go

// SPDX-License-Identifier: Unlicense OR MIT
package widget
import (
"image"
"image/color"
"image/draw"
"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) layout.Dimensions {
ico := ic.image(gtx.Px(sz))
ico.Add(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops)
return 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
}