GTK4 on macOS: GtkDropTarget doesn't receive drops unless window is focused

I’m using GTK4 on macOS and trying to handle file drops from Finder using GtkDropTarget. However, I noticed that the on_drop callback is not triggered when the GTK window does not have focus.

Steps to reproduce:

  1. Launch a GTK4 application on macOS that has a GtkDropTarget set up.
  2. Without clicking the window, drag a file from Finder and drop it onto the GTK window.
  3. The on_drop callback is not called.
  4. After clicking the window (i.e., giving it focus), the previously dropped file is suddenly received, and the callback fires.

This behavior does not occur on Linux or Windows. On those platforms, the drop is handled immediately regardless of window focus.

Is this a known limitation of GTK4 on macOS, or is there a workaround for this?

Ideally, I want the GTK app to accept drops even when it is not focused, just like native macOS apps do.

Environment:

  • GTK version: 4.18.6
  • macOS version: 12.6.3

c language code:

#include <gtk/gtk.h>

static void on_drop(GtkDropTarget *target, const GValue *value,
                   double x, double y, gpointer data) {
    GtkWidget *widget = GTK_WIDGET(data);

    if (G_VALUE_HOLDS(value, GDK_TYPE_FILE_LIST)) {
        GSList *files = g_value_get_boxed(value);

        for (GSList *iter = files; iter; iter = iter->next) {
            GFile *file = iter->data;
            gchar *path = g_file_get_path(file);
            g_print("Dropped file: %s\n", path);
            g_free(path);
        }
    }
}

static void
activate(GtkApplication *app, gpointer user_data)
{
    GtkWidget *window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(window), "file drop");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);

    GtkDropTarget *drop_target = gtk_drop_target_new(G_TYPE_STRING, GDK_ACTION_COPY);
    gtk_drop_target_set_gtypes(drop_target, (GType[1]){GDK_TYPE_FILE_LIST}, 1);
    g_signal_connect(drop_target, "drop", G_CALLBACK(on_drop), NULL);
    gtk_widget_add_controller(window, GTK_EVENT_CONTROLLER(drop_target));

    gtk_window_present(GTK_WINDOW(window));
}

int main(int argc, char **argv)
{
    GtkApplication *app = gtk_application_new("com.example.drop-test", G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    return g_application_run(G_APPLICATION(app), argc, argv);
}

clang drop-test2.c -o drop-test2 pkg-config --cflags --libs gtk4