radiobutton.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 RadioButtonStyle struct {
  9. checkable
  10. Key string
  11. Group *widget.Enum
  12. }
  13. // RadioButton returns a RadioButton with a label. The key specifies
  14. // the value for the Enum.
  15. func RadioButton(th *Theme, group *widget.Enum, key, label string) RadioButtonStyle {
  16. r := RadioButtonStyle{
  17. Group: group,
  18. checkable: checkable{
  19. Label: label,
  20. Color: th.Palette.Fg,
  21. IconColor: th.Palette.ContrastBg,
  22. TextSize: th.TextSize * 14.0 / 16.0,
  23. Size: 26,
  24. shaper: th.Shaper,
  25. checkedStateIcon: th.Icon.RadioChecked,
  26. uncheckedStateIcon: th.Icon.RadioUnchecked,
  27. },
  28. Key: key,
  29. }
  30. r.checkable.Font.Typeface = th.Face
  31. return r
  32. }
  33. // Layout updates enum and displays the radio button.
  34. func (r RadioButtonStyle) Layout(gtx layout.Context) layout.Dimensions {
  35. r.Group.Update(gtx)
  36. hovered, hovering := r.Group.Hovered()
  37. focus, focused := r.Group.Focused()
  38. return r.Group.Layout(gtx, r.Key, func(gtx layout.Context) layout.Dimensions {
  39. semantic.RadioButton.Add(gtx.Ops)
  40. highlight := hovering && hovered == r.Key || focused && focus == r.Key
  41. return r.layout(gtx, r.Group.Value == r.Key, highlight)
  42. })
  43. }