Hi,
I am trying to create a popovermenu that is nested instead of the sliding menus created by default in Gtk4. Using the method Gtk.PopoverMenu.new_from_model_full(giomenumodel, Gtk.PopoverMenuFlags.NESTED) lead to errors and fails-
(gjs:153067): Gjs-CRITICAL **: 19:28:11.268: Object Gtk.PopoverMenu (0x5566cb88c470), has been already finalized — impossible to access it. This might be caused by the object having been destroyed from C code using something such as destroy(), dispose(), or remove() vfuncs.
(gjs:153067): Gjs-CRITICAL **: 19:28:14.653: Object 0x5566cb88c8f0 of type GtkPopoverMenu has been finalized while it was still owned by gjs, this is due to invalid memory management.
Is this a problem with gjs, gtk4 or the way I am using the method? Any suggestions?
A small program demonstrating the errors, at least on my system is attached. It can be run from the command line.
#!/usr/bin/env gjs
imports.gi.versions.Gtk = "4.0";
const { Gio, Gtk } = imports.gi;
class ImageViewerWindow {
constructor(app) {
this._app = app;
this._window = null;
this._box = null;
this._fileChooserButton = null;
}
_buildUI() {
this._window = new Gtk.ApplicationWindow({
application: this._app,
defaultHeight: 600,
defaultWidth: 800
});
this._box = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL
});
this._fileChooserButton = Gtk.Button.new_with_label('Menu');
this._fileChooserButton.connect('clicked', (button) => {
log('activated')
//this.popupmenu = Gtk.PopoverMenu.new_from_model(this.desktopBackgroundGioMenu);
this.popupmenu = Gtk.PopoverMenu.new_from_model_full(this.desktopBackgroundGioMenu, Gtk.PopoverMenuFlags.NESTED);
this.popupmenu.set_parent(this._fileChooserButton);
this.popupmenu.popup();
});
this._box.append(this._fileChooserButton);
this._box.show();
this._window.set_child(this._box);
this.desktopBackgroundGioMenu = Gio.Menu.new();
this.desktopBackgroundGioMenu.append("New Folder", "app.doNewFolder");
this.pasteUndoRedoMenu = Gio.Menu.new();
this.pasteUndoRedoMenu.append("Paste", "app.doPaste");
this.pasteUndoRedoMenu.append("Undo", "app.doUndo");
this.pasteUndoRedoMenu.append("Redo", "app.doRedo");
this.desktopBackgroundGioMenu.append_submenu("Paste Menu", this.pasteUndoRedoMenu);
}
getWidget() {
this._buildUI();
return this._window;
}
}
const application = new Gtk.Application({
application_id: 'org.gnome.Sandbox.MenuExample',
flags: Gio.ApplicationFlags.FLAGS_NONE
});
application.connect('activate', app => {
let activeWindow = app.activeWindow;
if (!activeWindow) {
let imageViewerWindow = new ImageViewerWindow(app);
activeWindow = imageViewerWindow.getWidget();
}
activeWindow.present();
});
application.run(null);