doc.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // SPDX-License-Identifier: Unlicense OR MIT
  2. /*
  3. Package pointer implements pointer events and operations.
  4. A pointer is either a mouse controlled cursor or a touch
  5. object such as a finger.
  6. The [event.Op] operation is used to declare a handler ready for pointer
  7. events.
  8. # Hit areas
  9. Clip operations from package [op/clip] are used for specifying
  10. hit areas where handlers may receive events.
  11. For example, to set up a handler with a rectangular hit area:
  12. r := image.Rectangle{...}
  13. area := clip.Rect(r).Push(ops)
  14. event.Op{Tag: h}.Add(ops)
  15. area.Pop()
  16. Note that hit areas behave similar to painting: the effective area of a stack
  17. of multiple area operations is the intersection of the areas.
  18. BUG: Clip operations other than clip.Rect and clip.Ellipse are approximated
  19. with their bounding boxes.
  20. # Matching events
  21. Areas form an implicit tree, with input handlers as leaves. The children of
  22. an area is every area and handler added between its Push and corresponding Pop.
  23. For example:
  24. ops := new(op.Ops)
  25. var h1, h2 *Handler
  26. area := clip.Rect(...).Push(ops)
  27. event.Op(Ops, h1)
  28. area.Pop()
  29. area := clip.Rect(...).Push(ops)
  30. event.Op(Ops, h2)
  31. area.Pop()
  32. implies a tree of two inner nodes, each with one pointer handler attached.
  33. The matching proceeds as follows.
  34. First, the foremost area that contains the event is found. Only areas whose
  35. parent areas all contain the event is considered.
  36. Then, every handler attached to the area is matched with the event.
  37. If all attached handlers are marked pass-through or if no handlers are
  38. attached, the matching repeats with the next foremost (sibling) area. Otherwise
  39. the matching repeats with the parent area.
  40. In the example above, all events will go to h2 because it and h1 are siblings
  41. and none are pass-through.
  42. # Pass-through
  43. The PassOp operations controls the pass-through setting. All handlers added
  44. inside one or more PassOp scopes are marked pass-through.
  45. Pass-through is useful for overlay widgets. Consider a hidden side drawer: when
  46. the user touches the side, both the (transparent) drawer handle and the
  47. interface below should receive pointer events. This effect is achieved by
  48. marking the drawer handle pass-through.
  49. # Disambiguation
  50. When more than one handler matches a pointer event, the event queue
  51. follows a set of rules for distributing the event.
  52. As long as the pointer has not received a Press event, all
  53. matching handlers receive all events.
  54. When a pointer is pressed, the set of matching handlers is
  55. recorded. The set is not updated according to the pointer position
  56. and hit areas. Rather, handlers stay in the matching set until they
  57. no longer appear in a InputOp or when another handler in the set
  58. grabs the pointer.
  59. A handler can exclude all other handler from its matching sets
  60. by setting the Grab flag in its InputOp. The Grab flag is sticky
  61. and stays in effect until the handler no longer appears in any
  62. matching sets.
  63. The losing handlers are notified by a Cancel event.
  64. For multiple grabbing handlers, the foremost handler wins.
  65. # Priorities
  66. Handlers know their position in a matching set of a pointer through
  67. event priorities. The Shared priority is for matching sets with
  68. multiple handlers; the Grabbed priority indicate exclusive access.
  69. Priorities are useful for deferred gesture matching.
  70. Consider a scrollable list of clickable elements. When the user touches an
  71. element, it is unknown whether the gesture is a click on the element
  72. or a drag (scroll) of the list. While the click handler might light up
  73. the element in anticipation of a click, the scrolling handler does not
  74. scroll on finger movements with lower than Grabbed priority.
  75. Should the user release the finger, the click handler registers a click.
  76. However, if the finger moves beyond a threshold, the scrolling handler
  77. determines that the gesture is a drag and sets its Grab flag. The
  78. click handler receives a Cancel (removing the highlight) and further
  79. movements for the scroll handler has priority Grabbed, scrolling the
  80. list.
  81. */
  82. package pointer