mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
2e991f31be
This is a breaking change as Icon.Layout no longer requests a size. Before: sz := unit.Dp(20) ic.Layout(gtx, sz) After: sz := gtx.Metric.Px(unit.Dp(20)) gtx.Constraints.Min = image.Pt(sz, 0) ic.Layout(gtx) Fixes gio#240 Signed-off-by: pierre <pierre.curto@gmail.com>
76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package widget
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
"image/draw"
|
|
|
|
"gioui.org/internal/f32color"
|
|
"gioui.org/layout"
|
|
"gioui.org/op"
|
|
"gioui.org/op/clip"
|
|
"gioui.org/op/paint"
|
|
"gioui.org/unit"
|
|
|
|
"golang.org/x/exp/shiny/iconvg"
|
|
)
|
|
|
|
type Icon struct {
|
|
Color color.NRGBA
|
|
src []byte
|
|
// Cached values.
|
|
op paint.ImageOp
|
|
imgSize int
|
|
imgColor color.NRGBA
|
|
}
|
|
|
|
var defaultIconSize = unit.Dp(24)
|
|
|
|
// 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.NRGBA{A: 0xff}}, nil
|
|
}
|
|
|
|
// Layout displays the icon with its size set to the X minimum constraint.
|
|
func (ic *Icon) Layout(gtx layout.Context) layout.Dimensions {
|
|
sz := gtx.Constraints.Min.X
|
|
if sz == 0 {
|
|
sz = gtx.Metric.Px(defaultIconSize)
|
|
}
|
|
size := gtx.Constraints.Constrain(image.Pt(sz, sz))
|
|
defer op.Save(gtx.Ops).Load()
|
|
clip.Rect{Max: size}.Add(gtx.Ops)
|
|
|
|
ico := ic.image(size.X)
|
|
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] = f32color.NRGBAToLinearRGBA(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
|
|
}
|