widget/material: move widget state object from Layout methods to constructors

Instead of, say,

	var th *material.Theme
	var btn *widget.Clickable

	material.Button(th, "Click me").Layout(gtx, btn)

move the widget state objects to the constructor:

	material.Button(th, btn, "Click me").Layout(gtx)

The advatage is that several widgets can now be used without
wrapping them in function literals. For example,

	layout.Inset{}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
		material.Button(th, "Click me").Layout(gtx, btn)
	})

collapses to just

	layout.Inset{}.Layout(gtx, material.Button(th, btn, "Click me").Layout)

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-05-23 20:09:08 +02:00
parent 3af01a3f43
commit 2451750782
6 changed files with 62 additions and 44 deletions
+8 -6
View File
@@ -10,11 +10,13 @@ import (
type CheckBoxStyle struct {
checkable
CheckBox *widget.Bool
}
func CheckBox(th *Theme, label string) CheckBoxStyle {
func CheckBox(th *Theme, checkBox *widget.Bool, label string) CheckBoxStyle {
return CheckBoxStyle{
checkable{
CheckBox: checkBox,
checkable: checkable{
Label: label,
Color: th.Color.Text,
IconColor: th.Color.Primary,
@@ -28,9 +30,9 @@ func CheckBox(th *Theme, label string) CheckBoxStyle {
}
// Layout updates the checkBox and displays it.
func (c CheckBoxStyle) Layout(gtx layout.Context, checkBox *widget.Bool) layout.Dimensions {
checkBox.Update(gtx)
dims := c.layout(gtx, checkBox.Value)
checkBox.Layout(gtx)
func (c CheckBoxStyle) Layout(gtx layout.Context) layout.Dimensions {
c.CheckBox.Update(gtx)
dims := c.layout(gtx, c.CheckBox.Value)
c.CheckBox.Layout(gtx)
return dims
}