bool.go 983 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // SPDX-License-Identifier: Unlicense OR MIT
  2. package widget
  3. import (
  4. "gioui.org/io/semantic"
  5. "gioui.org/layout"
  6. )
  7. type Bool struct {
  8. Value bool
  9. clk Clickable
  10. }
  11. // Update the widget state and report whether Value was changed.
  12. func (b *Bool) Update(gtx layout.Context) bool {
  13. changed := false
  14. for b.clk.clicked(b, gtx) {
  15. b.Value = !b.Value
  16. changed = true
  17. }
  18. return changed
  19. }
  20. // Hovered reports whether pointer is over the element.
  21. func (b *Bool) Hovered() bool {
  22. return b.clk.Hovered()
  23. }
  24. // Pressed reports whether pointer is pressing the element.
  25. func (b *Bool) Pressed() bool {
  26. return b.clk.Pressed()
  27. }
  28. func (b *Bool) History() []Press {
  29. return b.clk.History()
  30. }
  31. func (b *Bool) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions {
  32. b.Update(gtx)
  33. dims := b.clk.layout(b, gtx, func(gtx layout.Context) layout.Dimensions {
  34. semantic.SelectedOp(b.Value).Add(gtx.Ops)
  35. semantic.EnabledOp(gtx.Enabled()).Add(gtx.Ops)
  36. return w(gtx)
  37. })
  38. return dims
  39. }