class Gtk::Gesture

Overview

Gtk::Gesture is the base class for gesture recognition.

Although Gtk::Gesture 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 Gdk::EventSequence value for these).

The number of touches that a Gtk::Gesture need to be recognized is controlled by the [property@Gtk.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 Gtk::Gesture 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 Gtk::EventController#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 Gtk::Gestures, 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 Gdk::EventSequences triggering those.

Within a widget, gestures can be grouped through Gtk::Gesture#group. Grouped gestures synchronize the state of sequences, so calling Gtk::Gesture#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:

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 be 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 Gtk::Gesture#sequence_state= to know about the possible lifetimes of a Gdk::EventSequence.

Touchpad gestures

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

Direct Known Subclasses

Defined in:

lib/gi-crystal/src/auto/gtk-4.0/gesture.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class Gtk::EventController

==(other : self) ==, current_event : Gdk::Event | Nil current_event, current_event_device : Gdk::Device | Nil current_event_device, current_event_state : Gdk::ModifierType current_event_state, current_event_time : UInt32 current_event_time, hash(hasher) hash, name : String | Nil name, name=(value : String) : String
name=(name : String | Nil) : Nil
name=
, propagation_limit : Gtk::PropagationLimit propagation_limit, propagation_limit=(limit : Gtk::PropagationLimit) : Nil propagation_limit=, propagation_phase : Gtk::PropagationPhase propagation_phase, propagation_phase=(phase : Gtk::PropagationPhase) : Nil propagation_phase=, reset : Nil reset, widget : Gtk::Widget widget

Constructor methods inherited from class Gtk::EventController

new
new(*, name : String | Nil = nil, propagation_limit : Gtk::PropagationLimit | Nil = nil, propagation_phase : Gtk::PropagationPhase | Nil = nil, widget : Gtk::Widget | Nil = nil)
new

Class methods inherited from class Gtk::EventController

g_type : UInt64 g_type

Instance methods inherited from class GObject::Object

==(other : self) ==, bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding bind_property, bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding bind_property_full, data(key : String) : Pointer(Void) | Nil data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, hash(hasher) hash, notify(property_name : String) : Nil notify, notify_by_pspec(pspec : GObject::ParamSpec) : Nil notify_by_pspec, notify_signal notify_signal, property(property_name : String, value : _) : Nil property, qdata(quark : UInt32) : Pointer(Void) | Nil qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void) | Nil) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void) | Nil steal_data, steal_qdata(quark : UInt32) : Pointer(Void) | Nil steal_qdata, thaw_notify : Nil thaw_notify, to_unsafe : Pointer(Void) to_unsafe, watch_closure(closure : GObject::Closure) : Nil watch_closure

Constructor methods inherited from class GObject::Object

cast(obj : GObject::Object) : self cast, new(pointer : Pointer(Void), transfer : GICrystal::Transfer)
new
new
, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv

Class methods inherited from class GObject::Object

cast?(obj : GObject::Object) : self | Nil cast?, compat_control(what : UInt64, data : Pointer(Void) | Nil) : UInt64 compat_control, g_type : UInt64 g_type, interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec interface_find_property, interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec) interface_list_properties

Macros inherited from class GObject::Object

previous_vfunc(*args) previous_vfunc, previous_vfunc!(*args) previous_vfunc!, signal(signature) signal

Constructor Detail

def self.new #

Initialize a new Gesture.


[View source]
def self.new(*, n_points : UInt32 | Nil = nil, name : String | Nil = nil, propagation_limit : Gtk::PropagationLimit | Nil = nil, propagation_phase : Gtk::PropagationPhase | Nil = nil, widget : Gtk::Widget | Nil = nil) #

[View source]

Class Method Detail

def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.


[View source]

Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


def begin_signal #

[View source]
def bounding_box : 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.


[View source]
def bounding_box_center(x : Float64, y : Float64) : Bool #

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.


[View source]
def cancel_signal #

[View source]
def device : Gdk::Device | Nil #

Returns the logical Gdk::Device that is currently operating on gesture.

This returns nil if the gesture is not being interacted.


[View source]
def end_signal #

[View source]
def group(gesture : Gtk::Gesture) : Nil #

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 Gdk::EventSequences is kept in sync for all of those, so calling Gtk::Gesture#sequence_state=, on one will transfer the same value to the others.

Groups also perform an "implicit grabbing" of sequences, if a Gdk::EventSequence state is set to %GTK_EVENT_SEQUENCE_CLAIMED on one group, every other gesture group attached to the same Gtk::Widget will switch the state for that sequence to %GTK_EVENT_SEQUENCE_DENIED.


[View source]
def group : GLib::List #

Returns all gestures in the group of gesture


[View source]
def handles_sequence(sequence : Gdk::EventSequence | Nil) : Bool #

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


[View source]
def hash(hasher) #
Description copied from class Reference

See Object#hash(hasher)


def is_active : Bool #

Returns true if the gesture is currently active.

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


[View source]
def is_grouped_with(other : Gtk::Gesture) : Bool #

Returns true if both gestures pertain to the same group.


[View source]
def is_recognized : 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.


[View source]
def last_event(sequence : Gdk::EventSequence | Nil) : Gdk::Event | Nil #

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.


[View source]
def last_updated_sequence : Gdk::EventSequence | Nil #

Returns the Gdk::EventSequence that was last updated on gesture.


[View source]
def n_points : UInt32 #

[View source]
def n_points=(value : UInt32) : UInt32 #

[View source]
def point(sequence : Gdk::EventSequence | Nil) : Bool #

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.


[View source]
def sequence_state(sequence : Gdk::EventSequence) : Gtk::EventSequenceState #

Returns the sequence state, as seen by gesture.


[View source]
def sequence_state_changed_signal #

[View source]
def sequences : GLib::List #

Returns the list of Gdk::EventSequences currently being interpreted by gesture.


[View source]
def set_sequence_state(sequence : Gdk::EventSequence, state : Gtk::EventSequenceState) : Bool #

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 Gtk::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:

WARNING ⚠️ The following code is in c ⚠️

static void
first_gesture_begin_cb (Gtk::Gesture       *first_gesture,
                        Gdk::EventSequence *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 (Gtk::Gesture       *second_gesture,
                         Gdk::EventSequence *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.


[View source]
def state=(state : Gtk::EventSequenceState) : Bool #

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

See Gtk::Gesture#sequence_state= for more details on sequence states.


[View source]
def ungroup : Nil #

Separates gesture into an isolated group.


[View source]
def update_signal #

[View source]