Custom Modules
The framework provides modules that app creators can combine to create their apps. Now, they can also write and use their own modules using Gjs.
Writing a Module
A custom module can look as simple as the example below. Note that the custom Fixed
class is created from the Module
class, which is the basic requirement. Other requirements could apply depending on what type of module it is or, more importantly, the type of slot it should go into.
const Gtk = imports.gi.Gtk;
const Module = imports.framework.interfaces.module;
var Fixed = new Module.Class({
Name: 'Banner.Fixed',
Extends: Gtk.Label,
_init(props={}) {
props.label = 'Fixed';
this.parent(props);
},
});
Including a Module
To include a module, the file must be added under the /app/js/
prefix in the app's gresource
file.
<?xml version="1.0" ?>
<gresources>
<gresource prefix="/app/js">
<file>banner/fixed.js</file>
</gresource>
</gresources>
Using a Module
Finally, to use a module, it must be referenced from the app's YAML file.
root:
type: Banner.Fixed
A Custom Importer
To access a custom module class, from another custom module, an importer named custom_modules
is provided.
const Fixed = custom_modules.banner.fixed;
The results of the search are