mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +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:
+46
-2
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op"
|
||||
"gioui.org/unit"
|
||||
|
||||
"golang.org/x/exp/shiny/materialdesign/icons"
|
||||
)
|
||||
|
||||
@@ -26,5 +26,49 @@ func TestIcon_Alpha(t *testing.T) {
|
||||
Constraints: layout.Exact(image.Pt(100, 100)),
|
||||
}
|
||||
|
||||
_ = icon.Layout(gtx, unit.Sp(18))
|
||||
_ = icon.Layout(gtx)
|
||||
}
|
||||
|
||||
// TestWidgetConstraints tests that widgets returns dimensions within their constraints.
|
||||
func TestWidgetConstraints(t *testing.T) {
|
||||
_cs := func(v ...layout.Constraints) []layout.Constraints { return v }
|
||||
for _, tc := range []struct {
|
||||
label string
|
||||
widget layout.Widget
|
||||
constraints []layout.Constraints
|
||||
}{
|
||||
{
|
||||
label: "Icon",
|
||||
widget: func(gtx layout.Context) layout.Dimensions {
|
||||
ic, _ := NewIcon(icons.ToggleCheckBox)
|
||||
return ic.Layout(gtx)
|
||||
},
|
||||
constraints: _cs(
|
||||
layout.Constraints{
|
||||
Min: image.Pt(20, 0),
|
||||
Max: image.Pt(100, 100),
|
||||
},
|
||||
layout.Constraints{
|
||||
Max: image.Pt(100, 100),
|
||||
},
|
||||
),
|
||||
},
|
||||
} {
|
||||
t.Run(tc.label, func(t *testing.T) {
|
||||
for _, cs := range tc.constraints {
|
||||
gtx := layout.Context{
|
||||
Constraints: cs,
|
||||
Ops: new(op.Ops),
|
||||
}
|
||||
dims := tc.widget(gtx)
|
||||
csr := image.Rectangle{
|
||||
Min: cs.Min,
|
||||
Max: cs.Max,
|
||||
}
|
||||
if !dims.Size.In(csr) {
|
||||
t.Errorf("dims size %v not within constraints %v", dims.Size, csr)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user