checkbox.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // SPDX-License-Identifier: Unlicense OR MIT
  2. package material
  3. import (
  4. "gioui.org/io/semantic"
  5. "gioui.org/layout"
  6. "gioui.org/widget"
  7. )
  8. type CheckBoxStyle struct {
  9. checkable
  10. CheckBox *widget.Bool
  11. }
  12. func CheckBox(th *Theme, checkBox *widget.Bool, label string) CheckBoxStyle {
  13. c := CheckBoxStyle{
  14. CheckBox: checkBox,
  15. checkable: checkable{
  16. Label: label,
  17. Color: th.Palette.Fg,
  18. IconColor: th.Palette.ContrastBg,
  19. TextSize: th.TextSize * 14.0 / 16.0,
  20. Size: 26,
  21. shaper: th.Shaper,
  22. checkedStateIcon: th.Icon.CheckBoxChecked,
  23. uncheckedStateIcon: th.Icon.CheckBoxUnchecked,
  24. },
  25. }
  26. c.checkable.Font.Typeface = th.Face
  27. return c
  28. }
  29. // Layout updates the checkBox and displays it.
  30. func (c CheckBoxStyle) Layout(gtx layout.Context) layout.Dimensions {
  31. return c.CheckBox.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
  32. semantic.CheckBox.Add(gtx.Ops)
  33. return c.layout(gtx, c.CheckBox.Value, c.CheckBox.Hovered() || gtx.Focused(c.CheckBox))
  34. })
  35. }