Working with GIO: GActions
Last updated
Last updated
Actions can be defined as a signals which can have attached callbacks. They are created to be a part of a group called action groups. GNOME by default creates two action groups for us, one for application and one for the window. The action group for application is called app
and the action group for window is called win
.
For example, we want to have an about dialog. An About dialog is something which is specific to the application itself and not to any specific window. There are multiple ways we can go about this. One way is that we can have a callback and on some buton click, we use the callback function to show up the about window. Or we can go the much cleaner way and setup an action called show_about_dialog on the application. And then call this GAction anywhere else in the application.
One more very useful property of GAction is that we can assign a keyboard acclerator (shortcuts) very easily.
The first step is to make the UI file for the about dialog. Let us create a file called about_dialog.ui in the src directory. Open in UI designer and double click to add a about dialog.
You can fill in all the details you care about, I filled in the ID (about_dialog), Name (Splash), Logo (applications-graphics), License (GPL v3) and Authors (My name!).
But this won't be included in the build unless we add this UI file in the gresource.xml file. So, quickly open the gresource.xml file and add it there. Here are the updated contents of splash.gresource.xml
Now let us jump back to src/main.py
and write a function called set_up_actions
. Before this, let us understand how to define a simple action. You can find the API documentation here. We can write a helper function which will be creating a simple action, Connecting the action to a callback function, adding it to the application window (remember application window already has a action group predefined) and finally set up a keyboard accelerator if needed.
Let's walk through the code which got added. from line 17-22. We are defining an array of actions we want to define. It will have a name, a function to execute and a optional accel. and later from line 24-36, we just create the action using Gio.SimpleAction.new
and connect it with the function and setup the accel if needed.
From Line 39-45, we are getting a new Gtk.Builder
instance from resource , getting the dialog and showing it.
The next step is to make the menu button on the header bar functional. The menu button on press should come up with a popover menu. Which currently will have one option. About
. On click should show the about dialog. GIO also gives us the convenience of creating a menu model based on actions. You can find the documentation for that here.
There are some very small additions here. First up at Line 16, we also get the open_menu_button
which is of type Gtk.MenuButton
. And we have a helper function called set_menu_items
which creates an instance of Gio.Menu
, adds one menu item labeled About which inturn calls the app.about
action. Also in Line 50, we call the set_menu items function in the __init__
method.
Run the application to test it now.
GIO actions themselves are very powerful when used right. This is just one instance used to demonstrate how they can be helpful.