How to implement double click in GTK4

I implemented a column view with a tree structure and I would like to open/close the clicked node when double clicking on its header row. However, documentation on how to implement double clicks is hard to come by. As far as I understand, it should be possible with the Gtk.GestureClick, so I tried to implement it with a button first:

def on_button_pressed(click: Gtk.GestureClick, n_clicks: int, x: float, y: float):
    print('button clicked', click, n_clicks, x, y)

my_btn = Gtk.Button.new(label="click me")
gesture_click = Gtk.GestureClick.new()
gesture_click.connect("pressed", on_button_pressed)
my_btn.add_controller(gesture_click)

However, this implementation doesn’t work as expected. It registers only a single click, no matter whether it’s a double click or single click. Then nothing else is recognized anymore:

button clicked <Gtk.GestureClick object at 0x7cb4ec3c5d40 (GtkGestureClick at 0x6407e470a9c0)> 1 43.078128814697266 88.82811737060547

Thanks for any help in this regard.

You’ll want to “claim” the event in your callback like so:

def on_button_pressed(click: Gtk.GestureClick, n_clicks: int, x: float, y: float):
    click.set_state(Gtk.EventSequenceState.CLAIMED)
    print('button clicked', click, n_clicks, x, y)
1 Like

Hi,

It’s probably because another existing GestureClick claims the sequence, so will prevent the event from reaching yours.

A workaround could be set your gesture propagation-phase to CAPTURE, so it gets processed before the others.

1 Like

Great! Thank you, this successfully lets the button recognize the later clicks. However, how do I now recognize double clicks?

The n_clicks parameter tells me how many clicks this sequence contains. So, my naive approach would be to add

if n_clicks != 2:
    return

However, when I double-click multiple times, n_clicks increases further, for example:

button clicked <Gtk.GestureClick object at 0x7beda4166940 (GtkGestureClick at 0x59bc817d1720)> 1 65.1875 55.47265625
button clicked <Gtk.GestureClick object at 0x7beda4166940 (GtkGestureClick at 0x59bc817d1720)> 2 65.1875 55.47265625
button clicked <Gtk.GestureClick object at 0x7beda4166940 (GtkGestureClick at 0x59bc817d1720)> 1 65.1875 55.47265625
button clicked <Gtk.GestureClick object at 0x7beda4166940 (GtkGestureClick at 0x59bc817d1720)> 2 65.1875 55.47265625
button clicked <Gtk.GestureClick object at 0x7beda4166940 (GtkGestureClick at 0x59bc817d1720)> 1 65.1875 55.47265625
button clicked <Gtk.GestureClick object at 0x7beda4166940 (GtkGestureClick at 0x59bc817d1720)> 2 65.1875 55.47265625
button clicked <Gtk.GestureClick object at 0x7beda4166940 (GtkGestureClick at 0x59bc817d1720)> 3 65.1875 55.47265625
button clicked <Gtk.GestureClick object at 0x7beda4166940 (GtkGestureClick at 0x59bc817d1720)> 4 65.1875 55.47265625
button clicked <Gtk.GestureClick object at 0x7beda4166940 (GtkGestureClick at 0x59bc817d1720)> 5 65.1875 55.47265625
button clicked <Gtk.GestureClick object at 0x7beda4166940 (GtkGestureClick at 0x59bc817d1720)> 6 65.1875 55.47265625
button clicked <Gtk.GestureClick object at 0x7beda4166940 (GtkGestureClick at 0x59bc817d1720)> 7 65.1875 55.47265625
button clicked <Gtk.GestureClick object at 0x7beda4166940 (GtkGestureClick at 0x59bc817d1720)> 8 65.1875 55.47265625
button clicked <Gtk.GestureClick object at 0x7beda4166940 (GtkGestureClick at 0x59bc817d1720)> 9 65.1875 55.47265625
button clicked <Gtk.GestureClick object at 0x7beda4166940 (GtkGestureClick at 0x59bc817d1720)> 10 65.1875 55.47265625

I guess, I have to somehow cancel the sequence when n_clicks >= 2?

It depends on what you want to do, but you could use a modulo operation in this case:

if not n_clicks % 2:
    # toggle whether node is opened
2 Likes

Ok, that makes sense. Thank you very much!