GestureProtocol

public protocol GestureProtocol : EventControllerProtocol

GtkGesture is the base class for gesture recognition.

Although GtkGesture is quite generalized to serve as a base for multi-touch gestures, it is suitable to implement single-touch and pointer-based gestures (using the special nil GdkEventSequence value for these).

The number of touches that a GtkGesture need to be recognized is controlled by the [propertyGtk.Gesture:n-points] property, if a gesture is keeping track of less or more than that number of sequences, it won’t check whether the gesture is recognized.

As soon as the gesture has the expected number of touches, it will check regularly if it is recognized, the criteria to consider a gesture as “recognized” is left to GtkGesture subclasses.

A recognized gesture will then emit the following signals:

  • [signalGtk.Gesture::begin] when the gesture is recognized.
  • [signalGtk.Gesture::update], whenever an input event is processed.
  • [signalGtk.Gesture::end] when the gesture is no longer recognized.

Event propagation

In order to receive events, a gesture needs to set a propagation phase through [methodGtk.EventController.set_propagation_phase].

In the capture phase, events are propagated from the toplevel down to the target widget, and gestures that are attached to containers above the widget get a chance to interact with the event before it reaches the target.

In the bubble phase, events are propagated up from the target widget to the toplevel, and gestures that are attached to containers above the widget get a chance to interact with events that have not been handled yet.

States of a sequence

Whenever input interaction happens, a single event may trigger a cascade of GtkGestures, both across the parents of the widget receiving the event and in parallel within an individual widget. It is a responsibility of the widgets using those gestures to set the state of touch sequences accordingly in order to enable cooperation of gestures around the GdkEventSequences triggering those.

Within a widget, gestures can be grouped through [methodGtk.Gesture.group]. Grouped gestures synchronize the state of sequences, so calling [methodGtk.Gesture.set_sequence_state] on one will effectively propagate the state throughout the group.

By default, all sequences start out in the GTK_EVENT_SEQUENCE_NONE state, sequences in this state trigger the gesture event handler, but event propagation will continue unstopped by gestures.

If a sequence enters into the GTK_EVENT_SEQUENCE_DENIED state, the gesture group will effectively ignore the sequence, letting events go unstopped through the gesture, but the “slot” will still remain occupied while the touch is active.

If a sequence enters in the GTK_EVENT_SEQUENCE_CLAIMED state, the gesture group will grab all interaction on the sequence, by:

  • Setting the same sequence to GTK_EVENT_SEQUENCE_DENIED on every other gesture group within the widget, and every gesture on parent widgets in the propagation chain.
  • Emitting [signalGtk.Gesture::cancel] on every gesture in widgets underneath in the propagation chain.
  • Stopping event propagation after the gesture group handles the event.

Note: if a sequence is set early to GTK_EVENT_SEQUENCE_CLAIMED on GDK_TOUCH_BEGIN/GDK_BUTTON_PRESS (so those events are captured before reaching the event widget, this implies GTK_PHASE_CAPTURE), one similar event will emulated if the sequence changes to GTK_EVENT_SEQUENCE_DENIED. This way event coherence is preserved before event propagation is unstopped again.

Sequence states can’t be changed freely. See [methodGtk.Gesture.set_sequence_state] to know about the possible lifetimes of a GdkEventSequence.

Touchpad gestures

On the platforms that support it, GtkGesture will handle transparently touchpad gesture events. The only precautions users of GtkGesture should do to enable this support are:

  • If the gesture has GTK_PHASE_NONE, ensuring events of type GDK_TOUCHPAD_SWIPE and GDK_TOUCHPAD_PINCH are handled by the GtkGesture

The GestureProtocol protocol exposes the methods and properties of an underlying GtkGesture instance. The default implementation of these can be found in the protocol extension below. For a concrete class that implements these methods and properties, see Gesture. Alternatively, use GestureRef as a lighweight, unowned reference if you already have an instance you just want to use.

  • ptr

    Untyped pointer to the underlying GtkGesture instance.

    Declaration

    Swift

    var ptr: UnsafeMutableRawPointer! { get }
  • gesture_ptr Default implementation

    Typed pointer to the underlying GtkGesture instance.

    Default Implementation

    Return the stored, untyped pointer as a typed pointer to the GtkGesture instance.

    Declaration

    Swift

    var gesture_ptr: UnsafeMutablePointer<GtkGesture>! { get }
  • Required Initialiser for types conforming to GestureProtocol

    Declaration

    Swift

    init(raw: UnsafeMutableRawPointer)

Gesture Class

  • Bind a GesturePropertyName source property to a given target object.

    Declaration

    Swift

    @discardableResult
    @inlinable
    func bind<Q, T>(property source_property: GesturePropertyName, to target: T, _ target_property: Q, flags f: BindingFlags = .default, transformFrom transform_from: @escaping GLibObject.ValueTransformer = { $0.transform(destValue: $1) }, transformTo transform_to: @escaping GLibObject.ValueTransformer = { $0.transform(destValue: $1) }) -> BindingRef! where Q : PropertyNameProtocol, T : ObjectProtocol

    Parameters

    source_property

    the source property to bind

    target

    the target object to bind to

    target_property

    the target property to bind to

    flags

    the flags to pass to the Binding

    transform_from

    ValueTransformer to use for forward transformation

    transform_to

    ValueTransformer to use for backwards transformation

    Return Value

    binding reference or nil in case of an error

  • get(property:) Extension method

    Get the value of a Gesture property

    Declaration

    Swift

    @inlinable
    func get(property: GesturePropertyName) -> GLibObject.Value

    Parameters

    property

    the property to get the value for

    Return Value

    the value of the named property

  • set(property:value:) Extension method

    Set the value of a Gesture property. Note that this will only have an effect on properties that are writable and not construct-only!

    Declaration

    Swift

    @inlinable
    func set(property: GesturePropertyName, value v: GLibObject.Value)

    Parameters

    property

    the property to get the value for

    Return Value

    the value of the named property

Gesture signals

  • Connect a Swift signal handler to the given, typed GestureSignalName signal

    Declaration

    Swift

    @discardableResult
    @inlinable
    func connect(signal s: GestureSignalName, flags f: ConnectFlags = ConnectFlags(0), handler h: @escaping SignalHandler) -> Int

    Parameters

    signal

    The signal to connect

    flags

    The connection flags to use

    data

    A pointer to user data to provide to the callback

    destroyData

    A GClosureNotify C function to destroy the data pointed to by userData

    handler

    The Swift signal handler (function or callback) to invoke on the given signal

    Return Value

    The signal handler ID (always greater than 0 for successful connections)

  • Connect a C signal handler to the given, typed GestureSignalName signal

    Declaration

    Swift

    @discardableResult
    @inlinable
    func connect(signal s: GestureSignalName, flags f: ConnectFlags = ConnectFlags(0), data userData: gpointer!, destroyData destructor: GClosureNotify? = nil, signalHandler h: @escaping GCallback) -> Int

    Parameters

    signal

    The signal to connect

    flags

    The connection flags to use

    data

    A pointer to user data to provide to the callback

    destroyData

    A GClosureNotify C function to destroy the data pointed to by userData

    signalHandler

    The C function to be called on the given signal

    Return Value

    The signal handler ID (always greater than 0 for successful connections)

  • onBegin(flags:handler:) Extension method

    Emitted when the gesture is recognized.

    This means the number of touch sequences matches [propertyGtk.Gesture:n-points].

    Note: These conditions may also happen when an extra touch (eg. a third touch on a 2-touches gesture) is lifted, in that situation sequence won’t pertain to the current set of active touches, so don’t rely on this being true.

    Note

    This represents the underlying begin signal

    Declaration

    Swift

    @discardableResult
    @inlinable
    func onBegin(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GestureRef, _ sequence: Gdk.EventSequenceRef?) -> Void) -> Int

    Parameters

    flags

    Flags

    unownedSelf

    Reference to instance of self

    sequence

    the GdkEventSequence that made the gesture to be recognized

    handler

    The signal handler to call Run the given callback whenever the begin signal is emitted

  • beginSignal Extension method

    Typed begin signal for using the connect(signal:) methods

    Declaration

    Swift

    static var beginSignal: GestureSignalName { get }
  • onCancel(flags:handler:) Extension method

    Emitted whenever a sequence is cancelled.

    This usually happens on active touches when [methodGtk.EventController.reset] is called on gesture (manually, due to grabs…), or the individual sequence was claimed by parent widgets’ controllers (see [methodGtk.Gesture.set_sequence_state]).

    gesture must forget everything about sequence as in response to this signal.

    Note

    This represents the underlying cancel signal

    Declaration

    Swift

    @discardableResult
    @inlinable
    func onCancel(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GestureRef, _ sequence: Gdk.EventSequenceRef?) -> Void) -> Int

    Parameters

    flags

    Flags

    unownedSelf

    Reference to instance of self

    sequence

    the GdkEventSequence that was cancelled

    handler

    The signal handler to call Run the given callback whenever the cancel signal is emitted

  • cancelSignal Extension method

    Typed cancel signal for using the connect(signal:) methods

    Declaration

    Swift

    static var cancelSignal: GestureSignalName { get }
  • onEnd(flags:handler:) Extension method

    Emitted when gesture either stopped recognizing the event sequences as something to be handled, or the number of touch sequences became higher or lower than [propertyGtk.Gesture:n-points].

    Note: sequence might not pertain to the group of sequences that were previously triggering recognition on gesture (ie. a just pressed touch sequence that exceeds [propertyGtk.Gesture:n-points]). This situation may be detected by checking through [methodGtk.Gesture.handles_sequence].

    Note

    This represents the underlying end signal

    Declaration

    Swift

    @discardableResult
    @inlinable
    func onEnd(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GestureRef, _ sequence: Gdk.EventSequenceRef?) -> Void) -> Int

    Parameters

    flags

    Flags

    unownedSelf

    Reference to instance of self

    sequence

    the GdkEventSequence that made gesture recognition to finish

    handler

    The signal handler to call Run the given callback whenever the end signal is emitted

  • endSignal Extension method

    Typed end signal for using the connect(signal:) methods

    Declaration

    Swift

    static var endSignal: GestureSignalName { get }
  • Emitted whenever a sequence state changes.

    See [methodGtk.Gesture.set_sequence_state] to know more about the expectable sequence lifetimes.

    Note

    This represents the underlying sequence-state-changed signal

    Declaration

    Swift

    @discardableResult
    @inlinable
    func onSequenceStateChanged(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GestureRef, _ sequence: Gdk.EventSequenceRef?, _ state: EventSequenceState) -> Void) -> Int

    Parameters

    flags

    Flags

    unownedSelf

    Reference to instance of self

    sequence

    the GdkEventSequence that was cancelled

    state

    the new sequence state

    handler

    The signal handler to call Run the given callback whenever the sequenceStateChanged signal is emitted

  • sequenceStateChangedSignal Extension method

    Typed sequence-state-changed signal for using the connect(signal:) methods

    Declaration

    Swift

    static var sequenceStateChangedSignal: GestureSignalName { get }
  • onUpdate(flags:handler:) Extension method

    Emitted whenever an event is handled while the gesture is recognized.

    sequence is guaranteed to pertain to the set of active touches.

    Note

    This represents the underlying update signal

    Declaration

    Swift

    @discardableResult
    @inlinable
    func onUpdate(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GestureRef, _ sequence: Gdk.EventSequenceRef?) -> Void) -> Int

    Parameters

    flags

    Flags

    unownedSelf

    Reference to instance of self

    sequence

    the GdkEventSequence that was updated

    handler

    The signal handler to call Run the given callback whenever the update signal is emitted

  • updateSignal Extension method

    Typed update signal for using the connect(signal:) methods

    Declaration

    Swift

    static var updateSignal: GestureSignalName { get }
  • The notify signal is emitted on an object when one of its properties has its value set through g_object_set_property(), g_object_set(), et al.

    Note that getting this signal doesn’t itself guarantee that the value of the property has actually changed. When it is emitted is determined by the derived GObject class. If the implementor did not create the property with G_PARAM_EXPLICIT_NOTIFY, then any call to g_object_set_property() results in notify being emitted, even if the new value is the same as the old. If they did pass G_PARAM_EXPLICIT_NOTIFY, then this signal is emitted only when they explicitly call g_object_notify() or g_object_notify_by_pspec(), and common practice is to do that only when the value has actually changed.

    This signal is typically used to obtain change notification for a single property, by specifying the property name as a detail in the g_signal_connect() call, like this:

    (C Language Example):

    g_signal_connect (text_view->buffer, "notify::paste-target-list",
                      G_CALLBACK (gtk_text_view_target_list_notify),
                      text_view)
    

    It is important to note that you must use canonical parameter names as detail strings for the notify signal.

    Note

    This represents the underlying notify::n-points signal

    Declaration

    Swift

    @discardableResult
    @inlinable
    func onNotifyNPoints(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: GestureRef, _ pspec: ParamSpecRef) -> Void) -> Int

    Parameters

    flags

    Flags

    unownedSelf

    Reference to instance of self

    pspec

    the GParamSpec of the property which changed.

    handler

    The signal handler to call Run the given callback whenever the notifyNPoints signal is emitted

  • notifyNPointsSignal Extension method

    Typed notify::n-points signal for using the connect(signal:) methods

    Declaration

    Swift

    static var notifyNPointsSignal: GestureSignalName { get }

Gesture Class: GestureProtocol extension (methods and fields)

  • getBoundingBox(rect:) Extension method

    If there are touch sequences being currently handled by gesture, returns true and fills in rect with the bounding box containing all active touches.

    Otherwise, false will be returned.

    Note: This function will yield unexpected results on touchpad gestures. Since there is no correlation between physical and pixel distances, these will look as if constrained in an infinitely small area, rect width and height will thus be 0 regardless of the number of touchpoints.

    Declaration

    Swift

    @inlinable
    func getBoundingBox<RectangleT>(rect: RectangleT) -> Bool where RectangleT : RectangleProtocol
  • getBoundingBoxCenter(x:y:) Extension method

    If there are touch sequences being currently handled by gesture, returns true and fills in x and y with the center of the bounding box containing all active touches.

    Otherwise, false will be returned.

    Declaration

    Swift

    @inlinable
    func getBoundingBoxCenter(x: UnsafeMutablePointer<CDouble>!, y: UnsafeMutablePointer<CDouble>!) -> Bool
  • getDevice() Extension method

    Returns the logical GdkDevice that is currently operating on gesture.

    This returns nil if the gesture is not being interacted.

    Declaration

    Swift

    @inlinable
    func getDevice() -> Gdk.DeviceRef!
  • getGroup() Extension method

    Returns all gestures in the group of gesture

    Declaration

    Swift

    @inlinable
    func getGroup() -> GLib.ListRef!
  • getLastEvent(sequence:) Extension method

    Returns the last event that was processed for sequence.

    Note that the returned pointer is only valid as long as the sequence is still interpreted by the gesture. If in doubt, you should make a copy of the event.

    Declaration

    Swift

    @inlinable
    func getLastEvent(sequence: Gdk.EventSequenceRef? = nil) -> Gdk.EventRef!
  • getLastEvent(sequence:) Extension method

    Returns the last event that was processed for sequence.

    Note that the returned pointer is only valid as long as the sequence is still interpreted by the gesture. If in doubt, you should make a copy of the event.

    Declaration

    Swift

    @inlinable
    func getLastEvent<EventSequenceT>(sequence: EventSequenceT?) -> Gdk.EventRef! where EventSequenceT : EventSequenceProtocol
  • getLastUpdatedSequence() Extension method

    Returns the GdkEventSequence that was last updated on gesture.

    Declaration

    Swift

    @inlinable
    func getLastUpdatedSequence() -> Gdk.EventSequenceRef!
  • getPoint(sequence:x:y:) Extension method

    If sequence is currently being interpreted by gesture, returns true and fills in x and y with the last coordinates stored for that event sequence.

    The coordinates are always relative to the widget allocation.

    Declaration

    Swift

    @inlinable
    func getPoint(sequence: Gdk.EventSequenceRef? = nil, x: UnsafeMutablePointer<CDouble>! = nil, y: UnsafeMutablePointer<CDouble>! = nil) -> Bool
  • getPoint(sequence:x:y:) Extension method

    If sequence is currently being interpreted by gesture, returns true and fills in x and y with the last coordinates stored for that event sequence.

    The coordinates are always relative to the widget allocation.

    Declaration

    Swift

    @inlinable
    func getPoint<EventSequenceT>(sequence: EventSequenceT?, x: UnsafeMutablePointer<CDouble>! = nil, y: UnsafeMutablePointer<CDouble>! = nil) -> Bool where EventSequenceT : EventSequenceProtocol
  • getSequenceState(sequence:) Extension method

    Returns the sequence state, as seen by gesture.

    Declaration

    Swift

    @inlinable
    func getSequenceState<EventSequenceT>(sequence: EventSequenceT) -> GtkEventSequenceState where EventSequenceT : EventSequenceProtocol
  • getSequences() Extension method

    Returns the list of GdkEventSequences currently being interpreted by gesture.

    Declaration

    Swift

    @inlinable
    func getSequences() -> GLib.ListRef!
  • getGroup(gesture:) Extension method

    Adds gesture to the same group than group_gesture.

    Gestures are by default isolated in their own groups.

    Both gestures must have been added to the same widget before they can be grouped.

    When gestures are grouped, the state of GdkEventSequences is kept in sync for all of those, so calling [methodGtk.Gesture.set_sequence_state], on one will transfer the same value to the others.

    Groups also perform an “implicit grabbing” of sequences, if a GdkEventSequence state is set to GTK_EVENT_SEQUENCE_CLAIMED on one group, every other gesture group attached to the same GtkWidget will switch the state for that sequence to GTK_EVENT_SEQUENCE_DENIED.

    Declaration

    Swift

    @inlinable
    func getGroup<GestureT>(gesture: GestureT) where GestureT : GestureProtocol
  • handles(sequence:) Extension method

    Returns true if gesture is currently handling events corresponding to sequence.

    Declaration

    Swift

    @inlinable
    func handles(sequence: Gdk.EventSequenceRef? = nil) -> Bool
  • handles(sequence:) Extension method

    Returns true if gesture is currently handling events corresponding to sequence.

    Declaration

    Swift

    @inlinable
    func handles<EventSequenceT>(sequence: EventSequenceT?) -> Bool where EventSequenceT : EventSequenceProtocol
  • isGroupedWith(other:) Extension method

    Returns true if both gestures pertain to the same group.

    Declaration

    Swift

    @inlinable
    func isGroupedWith<GestureT>(other: GestureT) -> Bool where GestureT : GestureProtocol
  • Sets the state of sequence in gesture.

    Sequences start in state GTK_EVENT_SEQUENCE_NONE, and whenever they change state, they can never go back to that state. Likewise, sequences in state GTK_EVENT_SEQUENCE_DENIED cannot turn back to a not denied state. With these rules, the lifetime of an event sequence is constrained to the next four:

    • None
    • None → Denied
    • None → Claimed
    • None → Claimed → Denied

    Note: Due to event handling ordering, it may be unsafe to set the state on another gesture within a [signalGtk.Gesture::begin] signal handler, as the callback might be executed before the other gesture knows about the sequence. A safe way to perform this could be:

    static void
    first_gesture_begin_cb (GtkGesture       *first_gesture,
                            GdkEventSequence *sequence,
                            gpointer          user_data)
    {
      gtk_gesture_set_sequence_state (first_gesture, sequence, GTK_EVENT_SEQUENCE_CLAIMED);
      gtk_gesture_set_sequence_state (second_gesture, sequence, GTK_EVENT_SEQUENCE_DENIED);
    }
    
    static void
    second_gesture_begin_cb (GtkGesture       *second_gesture,
                             GdkEventSequence *sequence,
                             gpointer          user_data)
    {
      if (gtk_gesture_get_sequence_state (first_gesture, sequence) == GTK_EVENT_SEQUENCE_CLAIMED)
        gtk_gesture_set_sequence_state (second_gesture, sequence, GTK_EVENT_SEQUENCE_DENIED);
    }
    

    If both gestures are in the same group, just set the state on the gesture emitting the event, the sequence will be already be initialized to the group’s global state when the second gesture processes the event.

    Declaration

    Swift

    @inlinable
    func setSequenceState<EventSequenceT>(sequence: EventSequenceT, state: GtkEventSequenceState) -> Bool where EventSequenceT : EventSequenceProtocol
  • set(state:) Extension method

    Sets the state of all sequences that gesture is currently interacting with.

    See [methodGtk.Gesture.set_sequence_state] for more details on sequence states.

    Declaration

    Swift

    @inlinable
    func set(state: GtkEventSequenceState) -> Bool
  • ungroup() Extension method

    Separates gesture into an isolated group.

    Declaration

    Swift

    @inlinable
    func ungroup()
  • device Extension method

    Returns the logical GdkDevice that is currently operating on gesture.

    This returns nil if the gesture is not being interacted.

    Declaration

    Swift

    @inlinable
    var device: Gdk.DeviceRef! { get }
  • group Extension method

    Returns all gestures in the group of gesture

    Declaration

    Swift

    @inlinable
    var group: GLib.ListRef! { get }
  • isActive Extension method

    Returns true if the gesture is currently active.

    A gesture is active while there are touch sequences interacting with it.

    Declaration

    Swift

    @inlinable
    var isActive: Bool { get }
  • isRecognized Extension method

    Returns true if the gesture is currently recognized.

    A gesture is recognized if there are as many interacting touch sequences as required by gesture.

    Declaration

    Swift

    @inlinable
    var isRecognized: Bool { get }
  • lastUpdatedSequence Extension method

    Returns the GdkEventSequence that was last updated on gesture.

    Declaration

    Swift

    @inlinable
    var lastUpdatedSequence: Gdk.EventSequenceRef! { get }
  • sequences Extension method

    Returns the list of GdkEventSequences currently being interpreted by gesture.

    Declaration

    Swift

    @inlinable
    var sequences: GLib.ListRef! { get }