Popover showin every other click in gtk4

So I have GtkMenuButton with an attached popover. Inside the popover all I have is a box with a button in it. I connect to the show and closed signals to create/destroy the button inside the popover.

The weird behaviour is that on every second click the popover quickly shows and hides thus making it look like it doesn’t show every other click.

This only happens as long as I remove a widget in the closed signal. If I don’t remove a widget everything works.

The signal handlers look something like this:

    g_signal_connect(popover->popover, "show", gfn(
        Auto popover = static_cast<UiPopover*>(data);
        Auto button = ui_button_new(context.fast_mem, 0, "I am a popup", false, false, false);
        gtk_box_append(GTK_BOX(popover->box), button->widget);
        printf("open\n");
    ), popover);

    g_signal_connect(popover->popover, "closed", gfn(
        Auto popover = static_cast<UiPopover*>(data);
        popover->box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
        gtk_popover_set_child(GTK_POPOVER(popover->popover), popover->box);
        printf("closed\n");
    ), popover);

I notice that when I use a GtkGesture it works correctly but only for mouse clicks…

    GtkGesture *gesture = gtk_gesture_click_new();
    gtk_widget_add_controller(popover->widget, GTK_EVENT_CONTROLLER(gesture));
    g_signal_connect(gesture, "pressed", G_CALLBACK(+[](GtkGestureClick *gesture, gint n_press, gdouble x, gdouble y, gpointer data){
        Auto popover = static_cast<UiPopover*>(data);
        Auto button = ui_button_new(context.fast_mem, 0, "I am a popup", false, false, false);
        gtk_box_append(GTK_BOX(popover->box), button->widget);
        printf("open\n");
    }), popover);

So it appears that when I idle_add the widget in the show handler, then everything works:

    g_signal_connect(popover->popover, "show", G_CALLBACK(+[](GObject *obj, gpointer data){
        Auto popover = static_cast<UiPopover*>(data);
        g_idle_add(+[](gpointer data){
            Auto popover = static_cast<UiPopover*>(data);
            Auto button = ui_button_new(context.fast_mem, 0, "I am a popup", false, false, false);
            gtk_box_append(GTK_BOX(popover->box), button->widget);
            gtk_popover_set_default_widget(GTK_POPOVER(popover->popover), button->widget);
            printf("open\n");
            return 0;
        }, popover);
    }), popover);

Why is that?

Hi,

I suppose it’s because popovers track the focused widget to show/hide themselves, so adding/removing widgets at the same time may add confusion.

Consider trying g_signal_connect_after().
You can also connect to the windows notify::focus-widget to watch which widget gets the focus, that may give you some hints.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.