Gtk.Widget.prototype.get_toplevel

function get_toplevel():Gtk.Widget {
    // Gjs wrapper for gtk_widget_get_toplevel()
}

This function returns the topmost widget in the container hierarchy widget is a part of. If widget has no parent widgets, it will be returned as the topmost widget. No reference will be added to the returned widget; it should not be unreferenced.

Note the difference in behavior vs. Gtk.Widget.prototype.get_ancestor; <literal>gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW)</literal> would return null if widget wasn't inside a toplevel window, and if the window was inside a Gtk.Window<!-- -->-derived widget which was in turn inside the toplevel Gtk.Window. While the second case may seem unlikely, it actually happens when a Gtk.Plug is embedded inside a Gtk.Socket within the same application.

To reliably find the toplevel Gtk.Window, use Gtk.Widget.prototype.get_toplevel and check if the %TOPLEVEL flags is set on the result. |[ GtkWidget *toplevel = gtk_widget_get_toplevel (widget); if (gtk_widget_is_toplevel (toplevel)) { /&ast; Perform action on toplevel. &ast;/ } ]|

Returns

the topmost ancestor of widget, or widget itself if there's no ancestor.