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>
This commit is contained in:
Elias Naur
2020-05-24 13:50:03 +02:00
parent 3164a3fee7
commit 31d722d9eb
3 changed files with 15 additions and 9 deletions
+10 -1
View File
@@ -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}
}
+1
View File
@@ -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
}
+4 -8
View File
@@ -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) {