mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-05 17:35:36 +00:00
widget: make Icon honour its constraints
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>
This commit is contained in:
+17
-4
@@ -7,12 +7,14 @@ import (
|
||||
"image/color"
|
||||
"image/draw"
|
||||
|
||||
"golang.org/x/exp/shiny/iconvg"
|
||||
|
||||
"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 {
|
||||
@@ -24,6 +26,8 @@ type Icon struct {
|
||||
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)
|
||||
@@ -33,8 +37,17 @@ func NewIcon(data []byte) (*Icon, error) {
|
||||
return &Icon{src: data, Color: color.NRGBA{A: 0xff}}, nil
|
||||
}
|
||||
|
||||
func (ic *Icon) Layout(gtx layout.Context, sz unit.Value) layout.Dimensions {
|
||||
ico := ic.image(gtx.Px(sz))
|
||||
// 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{
|
||||
|
||||
Reference in New Issue
Block a user