Files
gio/widget/material/checkbox.go
T
Elias Naur cd2ade0583 widget,widget/material: make Clickable widgets focusable
This change adds focus and keyboard control to Clickable widgets.
They now consider a press of the enter or return key equivalent to
a click. To keep the change simple, the focus indication is the
same as the hover indication.

References: https://todo.sr.ht/~eliasnaur/gio/195
References: https://github.com/tailscale/tailscale/issues/1611
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-02-27 15:31:50 +01:00

40 lines
1.0 KiB
Go

// SPDX-License-Identifier: Unlicense OR MIT
package material
import (
"gioui.org/io/semantic"
"gioui.org/layout"
"gioui.org/unit"
"gioui.org/widget"
)
type CheckBoxStyle struct {
checkable
CheckBox *widget.Bool
}
func CheckBox(th *Theme, checkBox *widget.Bool, label string) CheckBoxStyle {
return CheckBoxStyle{
CheckBox: checkBox,
checkable: checkable{
Label: label,
Color: th.Palette.Fg,
IconColor: th.Palette.ContrastBg,
TextSize: th.TextSize.Scale(14.0 / 16.0),
Size: unit.Dp(26),
shaper: th.Shaper,
checkedStateIcon: th.Icon.CheckBoxChecked,
uncheckedStateIcon: th.Icon.CheckBoxUnchecked,
},
}
}
// Layout updates the checkBox and displays it.
func (c CheckBoxStyle) Layout(gtx layout.Context) layout.Dimensions {
return c.CheckBox.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
semantic.CheckBox.Add(gtx.Ops)
return c.layout(gtx, c.CheckBox.Value, c.CheckBox.Hovered() || c.CheckBox.Focused())
})
}