Gnome 3 extension development hints #6 Extension Preferences
Posted March 16, 2013 by atejeda at Development, Gnome, Gnome3, Hints.In order to create your preferences mechanism for your extension, there’s a layout which must be followed:
- myExtension@someExtension/ : the root path of the extension.
- myExtension@someExtension/schemas/ : the root schema path of the extension.
- myExtension@someExtension/schemas/org.gnome.shell.extensions.myExtension.gschema.xml : The schema file which define the preference data structure.
- myExtension@someExtension/prefs.js : Display the GUI coded with GTK.
- myExtension@someExtension/convenience.js : Utils for our extensions (thirdparty).
First, define the schema:
In myExtension@someExtension/schemas/org.gnome.shell.extensions.myExtension.gschema.xml the data structure of the preferences must be defined, this structure is just a schema for your settings:
The line 2 define the text domain which is gnome3-myExtension, line 3 fined the id of the schema, 4 to 11 define the json structure in which the settings will be saved, in this case it will be a string saved as json data, line 12 and 13 are self descriptive.
Be aware that this file must be compiled using: glib-compile-schemas schemas
Second, setup a file will be used as a “proxy” to save, and retrieve the settings saved. This file must be used since the prefs.js file do not permit saved and get configuration data directly form the schema, also in this way we keep a compatibility/scalability structure for the next gnome-shell versions. An example of this file:
Third, the prefs.js file:
- The ‘gettext-domain’ is defined in the metadata.json.
- Methods init, widget_initliaze, buildPrefsWidget must be defined.
In this example:
- widget_initliaze define the GTk objects, the documentation of GUI elements and how to create them can be found as PyGTK.
- widget_packaging, like the method name define how the elements is/are packaged, see PyGTK documentation.
- widget_connect, connect the ‘click’ button to save the settings.
- save_settings_button_callback, the method which is triggered by the save-button, line 103 and 103 get the settings saved, replace with the values defined in the GUI and line 100 save this settings as json data to the schema.
- widget_init_values, this method get the saved (settings) value, line 117 and 118 get the json data.
- buildPrefsWidget, initialize the preference GUI.
You can create lot of settings for your extension, the GUI is strongly based on GTk (see PyGTK documentation).