~eliasnaur/gio

31d722d9eb15678a18988f627ce637931ad8913d — Elias Naur a month ago 3164a3f
widget: change Bool.Layout to follow layout protocol

Just like Clickable, Bool.Layout should respect constraints and
return its dimensions.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
3 files changed, 15 insertions(+), 9 deletions(-)

M widget/bool.go
M widget/material/checkbox.go
M widget/material/switch.go
M widget/bool.go => widget/bool.go +10 -1
@@ 1,8 1,12 @@
package widget

import (
	"image"

	"gioui.org/gesture"
	"gioui.org/io/pointer"
	"gioui.org/layout"
	"gioui.org/op"
)

type Bool struct {


@@ 24,7 28,7 @@ func (b *Bool) Changed() bool {
	return changed
}

func (b *Bool) Layout(gtx layout.Context) {
func (b *Bool) Layout(gtx layout.Context) layout.Dimensions {
	for _, e := range b.gesture.Events(gtx) {
		switch e.Type {
		case gesture.TypeClick:


@@ 35,5 39,10 @@ func (b *Bool) Layout(gtx layout.Context) {
			b.Value = !b.Value
		}
	}
	var st op.StackOp
	st.Push(gtx.Ops)
	defer st.Pop()
	pointer.Rect(image.Rectangle{Max: gtx.Constraints.Min}).Add(gtx.Ops)
	b.gesture.Add(gtx.Ops)
	return layout.Dimensions{Size: gtx.Constraints.Min}
}

M widget/material/checkbox.go => widget/material/checkbox.go +1 -0
@@ 32,6 32,7 @@ func CheckBox(th *Theme, checkBox *widget.Bool, label string) CheckBoxStyle {
// Layout updates the checkBox and displays it.
func (c CheckBoxStyle) Layout(gtx layout.Context) layout.Dimensions {
	dims := c.layout(gtx, c.CheckBox.Value)
	gtx.Constraints.Min = dims.Size
	c.CheckBox.Layout(gtx)
	return dims
}

M widget/material/switch.go => widget/material/switch.go +4 -8
@@ 104,17 104,13 @@ func (s SwitchStyle) Layout(gtx layout.Context) layout.Dimensions {
		Y: (float32(trackHeight)-float32(clickSize))*.5 + trackOff,
	}
	op.TransformOp{}.Offset(clickOff).Add(gtx.Ops)
	pointer.Ellipse(image.Rectangle{
		Max: image.Point{
			X: clickSize, Y: clickSize,
		},
	}).Add(gtx.Ops)
	sz := image.Pt(clickSize, clickSize)
	pointer.Ellipse(image.Rectangle{Max: sz}).Add(gtx.Ops)
	gtx.Constraints.Min = sz
	s.Switch.Layout(gtx)
	stack.Pop()

	return layout.Dimensions{
		Size: image.Point{X: trackWidth, Y: trackHeight},
	}
	return layout.Dimensions{Size: image.Point{X: trackWidth, Y: thumbSize}}
}

func drawDisc(ops *op.Ops, sz float32, col color.RGBA) {