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,13 +10,15 @@ import (
type RadioButtonStyle struct {
checkable
Key string
Key string
Group *widget.Enum
}
// RadioButton returns a RadioButton with a label. The key specifies
// the value for the Enum.
func RadioButton(th *Theme, key, label string) RadioButtonStyle {
func RadioButton(th *Theme, group *widget.Enum, key, label string) RadioButtonStyle {
return RadioButtonStyle{
Group: group,
checkable: checkable{
Label: label,
@@ -33,9 +35,9 @@ func RadioButton(th *Theme, key, label string) RadioButtonStyle {
}
// Layout updates enum and displays the radio button.
func (r RadioButtonStyle) Layout(gtx layout.Context, enum *widget.Enum) layout.Dimensions {
enum.Update(gtx)
dims := r.layout(gtx, enum.Value == r.Key)
enum.Layout(gtx, r.Key)
func (r RadioButtonStyle) Layout(gtx layout.Context) layout.Dimensions {
r.Group.Update(gtx)
dims := r.layout(gtx, r.Group.Value == r.Key)
r.Group.Layout(gtx, r.Key)
return dims
}