Skip to content

Instantly share code, notes, and snippets.

@RossComputerGuy
Created November 2, 2021 08:10
Show Gist options
  • Save RossComputerGuy/79741ffca44b6cbd48f368fed62ce672 to your computer and use it in GitHub Desktop.
Save RossComputerGuy/79741ffca44b6cbd48f368fed62ce672 to your computer and use it in GitHub Desktop.
GTK 4.0 PopoverMenuBar w/ DBus
/**
* GTK 4 Popover Menubar with DBus
*
* Compile: valac popovermenu-dbus.vala --pkg gtk4 --pkg gio-2.0 --pkg gio-unix-2.0
*/
namespace Example {
private static string? dbus_id;
private static string? dbus_obj;
private const GLib.OptionEntry[] options = {
{ "dbus-id", '\0', GLib.OptionFlags.NONE, GLib.OptionArg.STRING, ref dbus_id, "The ID of the DBus application to use", "ID" },
{ "dbus-obj", '\0', GLib.OptionFlags.NONE, GLib.OptionArg.STRING, ref dbus_obj, "The object path of the DBus application to use", "OBJ_PATH" },
{ null }
};
public class ExampleApp : Gtk.Application {
public ExampleApp() {
Object(application_id: "com.example.application");
this.add_main_option_entries(options);
}
public override void startup() {
base.startup();
var action = new GLib.SimpleAction("test", null);
action.activate.connect(() => {
stdout.printf("Received event\n");
});
this.add_action(action);
var menu = new GLib.Menu();
var submenu = new GLib.Menu();
submenu.append("Test", "app.test");
menu.append_submenu("Application", submenu);
this.set_menubar(menu);
}
public override void activate() {
base.activate();
if (this.get_windows().is_empty()) {
new ExampleWin(this).show();
} else {
this.get_windows().first().data.present();
}
}
}
public class ExampleWin : Gtk.ApplicationWindow {
public ExampleWin(Gtk.Application application) {
Object(application: application);
this.title = "GTK 4.0 Popover Menubar DBus";
var conn = this.application.get_dbus_connection();
var id = dbus_id == null ? this.application.get_application_id() : dbus_id;
var obj_path = dbus_obj == null ? this.application.get_dbus_object_path() : dbus_obj;
var menubar = new Gtk.PopoverMenuBar.from_model(new GLib.Menu());
menubar.set_menu_model(GLib.DBusMenuModel.@get(conn, id, obj_path + "/menus/menubar"));
menubar.insert_action_group("app", GLib.DBusActionGroup.@get(conn, id, obj_path));
var box = new Gtk.CenterBox();
box.set_center_widget(menubar);
this.set_child(box);
}
}
public static int main(string[] args) {
Gtk.init();
return new ExampleApp().run(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment