semantic.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // SPDX-License-Identifier: Unlicense OR MIT
  2. // Package semantic provides operations for semantic descriptions of a user
  3. // interface, to facilitate presentation and interaction in external software
  4. // such as screen readers.
  5. //
  6. // Semantic descriptions are organized in a tree, with clip operations as
  7. // nodes. Operations in this package are associated with the current semantic
  8. // node, that is the most recent pushed clip operation.
  9. package semantic
  10. import (
  11. "gioui.org/internal/ops"
  12. "gioui.org/op"
  13. )
  14. // LabelOp provides the content of a textual component.
  15. type LabelOp string
  16. // DescriptionOp describes a component.
  17. type DescriptionOp string
  18. // ClassOp provides the component class.
  19. type ClassOp int
  20. const (
  21. Unknown ClassOp = iota
  22. Button
  23. CheckBox
  24. Editor
  25. RadioButton
  26. Switch
  27. )
  28. // SelectedOp describes the selected state for components that have
  29. // boolean state.
  30. type SelectedOp bool
  31. // EnabledOp describes the enabled state.
  32. type EnabledOp bool
  33. func (l LabelOp) Add(o *op.Ops) {
  34. data := ops.Write1String(&o.Internal, ops.TypeSemanticLabelLen, string(l))
  35. data[0] = byte(ops.TypeSemanticLabel)
  36. }
  37. func (d DescriptionOp) Add(o *op.Ops) {
  38. data := ops.Write1String(&o.Internal, ops.TypeSemanticDescLen, string(d))
  39. data[0] = byte(ops.TypeSemanticDesc)
  40. }
  41. func (c ClassOp) Add(o *op.Ops) {
  42. data := ops.Write(&o.Internal, ops.TypeSemanticClassLen)
  43. data[0] = byte(ops.TypeSemanticClass)
  44. data[1] = byte(c)
  45. }
  46. func (s SelectedOp) Add(o *op.Ops) {
  47. data := ops.Write(&o.Internal, ops.TypeSemanticSelectedLen)
  48. data[0] = byte(ops.TypeSemanticSelected)
  49. if s {
  50. data[1] = 1
  51. }
  52. }
  53. func (e EnabledOp) Add(o *op.Ops) {
  54. data := ops.Write(&o.Internal, ops.TypeSemanticEnabledLen)
  55. data[0] = byte(ops.TypeSemanticEnabled)
  56. if e {
  57. data[1] = 1
  58. }
  59. }
  60. func (c ClassOp) String() string {
  61. switch c {
  62. case Unknown:
  63. return "Unknown"
  64. case Button:
  65. return "Button"
  66. case CheckBox:
  67. return "CheckBox"
  68. case Editor:
  69. return "Editor"
  70. case RadioButton:
  71. return "RadioButton"
  72. case Switch:
  73. return "Switch"
  74. default:
  75. panic("invalid ClassOp")
  76. }
  77. }