Making GNOME-Shell plugins save their config
I’m working on a GNOME-Shell plugin that can show alternate timezones. As part of the plugin, I want it to remember what the user’s selected timezone is.
A good extension to pull apart seems to be the “System Monitor”.
The “short version” of the minimum you need to do is as follows…
Add a schema name to your metadata.json
, eg:
"settings-schema": "org.gnome.shell.extensions.system-monitor",
(Replace “system-monitor” with a unique name for your own extension)
Create a schemas
directory inside your extension’s directory, and create a file inside that called YOURSCHEMANAME.gschema.xml
, eg. org.gnome.shell.extensions.system-monitor.gschema.xml
.
Populate this with appropriate gschema magic; the system-monitor schema is pretty handy again here.
Inside your extension’s schemas
directory, run:
glib-compile-schemas .
(Don’t miss the “.” :)
Download convenience.js
into your extension’s directory.
Inside your extension.js
, set up some new imports:
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Convenience = Me.imports.convenience;
Somewhere during init() or enable(), you’ll want to grab a reference to your loaded schema object:
this._schema = Convenience.getSettings();
You can now get and set keys, eg. I’m using:
this._schema.get_string('tz'));
and
this._schema.get_string('tz'));
You probably should set up an actual preferences window… you can learn more about that at http://blog.mecheye.net/2012/02/more-extension-api-breaks/.