Creating tool items, GLib.Actions, and keyboard shortcuts
GTK and GLib have a powerful API called GLib.Action which can be used to define the primary actions of your app, assign them keyboard shortcuts, use them as entry points for your app and tie them to Actionable widgets like Buttons and Menu Items. In this section, we're going to create a Quit action for your app with an assigned keyboard shortcut and a Button that shows that shortcut in a tooltip.
Begin by creating a Gtk.Application with a Gtk.ApplicationWindow as you've done in . Once you have that set up, let's create a new . Typically your app will have a HeaderBar, at the top of the window, which will contain tool items that users will interact with to trigger your app's actions.
Since we're using this HeaderBar as our app's main titlebar, we need to set show_title_buttons to true so that GTK knows to include window controls. We can then override our Window's built-in titlebar with the titlebar property.
Now, create a new with a big colorful icon and add it to the HeaderBar:
Build and run your app. You can see that it now has a custom HeaderBar with a big red icon in it. But when you click on it, nothing happens.
Define a new Quit action and register it with Application from inside the startup method:
You'll notice that we do a few things here:
Instantiate a new with the name "quit"
Add the action to our Gtk.Application's
Set the "accelerators" (keyboard shortcuts) for "app.quit" to <Control>q and <Control>w. Notice that the action name is prefixed with app
Now we can tie the action to the HeaderBar Button by assigning the action_name property of our Button:
Build and run your app again and see that you can now quit the app either through the defined keyboard shortcuts or by clicking the Button in the HeaderBar.
You may have noticed that in elementary apps you can hover your pointer over tool items to see a description of the button and any available keyboard shortcuts associated with it. We can add the same thing to our Button with .
First, make sure you've included Granite in the build dependencies declared in your meson.build file, then set the tooltip_markup property of your HeaderBar Button:
Build and run your app and then hover over the HeaderBar Button to see its description and associated keyboard shortcuts.
ActionMapGtk.ApplicationConnect the activate signal of our SimpleAction to Application's quit () function.


protected override void activate () {
var headerbar = new Gtk.HeaderBar () {
show_title_buttons = true
};
var main_window = new Gtk.ApplicationWindow (this) {
default_height = 300,
default_width = 300,
title = "Actions",
titlebar = headerbar
};
main_window.present ();
}protected override void activate () {
var button = new Gtk.Button.from_icon_name ("process-stop");
button.add_css_class (Granite.STYLE_CLASS_LARGE_ICONS);
var headerbar = new Gtk.HeaderBar () {
show_title_buttons = true
};
headerbar.pack_start (button);
var main_window = new Gtk.ApplicationWindow (this) {
default_height = 300,
default_width = 300,
title = "Actions",
titlebar = headerbar
};
main_window.present ();
}protected override void startup () {
base.startup ();
var quit_action = new SimpleAction ("quit", null);
add_action (quit_action);
set_accels_for_action ("app.quit", {"<Control>q", "<Control>w"});
quit_action.activate.connect (quit);
} var button = new Gtk.Button.from_icon_name ("process-stop") {
action_name = "app.quit"
};executable(
meson.project_name(),
'src' / 'Application.vala',
dependencies: [
dependency('granite-7'),
dependency('gtk4')
],
install: true
)var button = new Gtk.Button.from_icon_name ("process-stop") {
action_name = "app.quit",
tooltip_markup = Granite.markup_accel_tooltip (
get_accels_for_action ("app.quit"),
"Quit"
)
};