Skip to content

Gtk.Gesture

class — extends EventController

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 None GdkEventSequence value for these).

The number of touches that a GtkGesture need to be recognized is controlled by the 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:

Event propagation

In order to receive events, a gesture needs to set a propagation phase through 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 Gesture.group. Grouped gestures synchronize the state of sequences, so calling Gesture.set_state on one will effectively propagate the state throughout the group.

By default, all sequences start out in the EventSequenceState.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 EventSequenceState.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 EventSequenceState.CLAIMED state, the gesture group will grab all interaction on the sequence, by:

  • Setting the same sequence to EventSequenceState.DENIED on every other gesture group within the widget, and every gesture on parent widgets in the propagation chain.
  • Emitting 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 EventSequenceState.CLAIMED on Gdk.EventType.TOUCH_BEGIN/Gdk.EventType.BUTTON_PRESS (so those events are captured before reaching the event widget, this implies PropagationPhase.CAPTURE), one similar event will be emulated if the sequence changes to EventSequenceState.DENIED. This way event coherence is preserved before event propagation is unstopped again.

Sequence states can't be changed freely. See Gesture.set_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:

Methods

get_bounding_box

def get_bounding_box(self) -> tuple[bool, Gdk.Rectangle]

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.

get_bounding_box_center

def get_bounding_box_center(self) -> tuple[bool, float, float]

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.

get_device

def get_device(self) -> Gdk.Device | None

Returns the logical GdkDevice that is currently operating on gesture.

This returns None if the gesture is not being interacted.

get_group

def get_group(self) -> list[Gesture]

Returns all gestures in the group of gesture

get_last_event

def get_last_event(self, sequence: Gdk.EventSequence | None = ...) -> Gdk.Event | None

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.

Parameters:

  • sequence — a GdkEventSequence

get_last_updated_sequence

def get_last_updated_sequence(self) -> Gdk.EventSequence | None

Returns the GdkEventSequence that was last updated on gesture.

get_point

def get_point(self, sequence: Gdk.EventSequence | None = ...) -> tuple[bool, float, float]

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.

Parameters:

  • sequence — a GdkEventSequence, or None for pointer events

get_sequence_state

def get_sequence_state(self, sequence: Gdk.EventSequence) -> EventSequenceState

Returns the sequence state, as seen by gesture.

Parameters:

  • sequence — a GdkEventSequence

get_sequences

def get_sequences(self) -> list[Gdk.EventSequence]

Returns the list of GdkEventSequences currently being interpreted by gesture.

group

def group(self, gesture: Gesture) -> None

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 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 EventSequenceState.CLAIMED on one group, every other gesture group attached to the same GtkWidget will switch the state for that sequence to EventSequenceState.DENIED.

Parameters:

  • gesture — a GtkGesture

handles_sequence

def handles_sequence(self, sequence: Gdk.EventSequence | None = ...) -> bool

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

Parameters:

  • sequence — a GdkEventSequence

is_active

def is_active(self) -> bool

Returns True if the gesture is currently active.

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

is_grouped_with

def is_grouped_with(self, other: Gesture) -> bool

Returns True if both gestures pertain to the same group.

Parameters:

  • other — another GtkGesture

is_recognized

def is_recognized(self) -> bool

Returns True if the gesture is currently recognized.

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

set_sequence_state

def set_sequence_state(self, sequence: Gdk.EventSequence, state: EventSequenceState | int) -> bool

:::warning Deprecated since 4.10. This API is deprecated. :::

Sets the state of sequence in gesture.

Sequences start in state EventSequenceState.NONE, and whenever they change state, they can never go back to that state. Likewise, sequences in state EventSequenceState.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 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.

Parameters:

  • sequence — a GdkEventSequence
  • state — the sequence state

set_state

def set_state(self, state: EventSequenceState | int) -> bool

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

Sequences start in state EventSequenceState.NONE, and whenever they change state, they can never go back to that state. Likewise, sequences in state EventSequenceState.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 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_state (first_gesture, GTK_EVENT_SEQUENCE_CLAIMED);
  gtk_gesture_set_state (second_gesture, 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_state (second_gesture, 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.

Parameters:

  • state — the sequence state

ungroup

def ungroup(self) -> None

Separates gesture into an isolated group.

Properties

n_points

n_points: int  # read/write

The number of touch points that trigger recognition on this gesture.

Signals

begin

def on_begin(self, sequence: Gdk.EventSequence | None) -> None: ...

Emitted when the gesture is recognized.

This means the number of touch sequences matches 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.

cancel

def on_cancel(self, sequence: Gdk.EventSequence | None) -> None: ...

Emitted whenever a sequence is cancelled.

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

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

end

def on_end(self, sequence: Gdk.EventSequence | None) -> None: ...

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 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 Gesture.n-points). This situation may be detected by checking through Gesture.handles_sequence.

sequence-state-changed

def on_sequence_state_changed(self, sequence: Gdk.EventSequence | None, state: EventSequenceState) -> None: ...

Emitted whenever a sequence state changes.

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

update

def on_update(self, sequence: Gdk.EventSequence | None) -> None: ...

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

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