gioui.org@v0.6.1-0.20240506124620-7a9ce51988ce/io/pointer/doc.go (about)

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