Gjs
Have a question? Ask Fedora.
Edit this page

Gjs: Javascript Bindings for GNOME

Gjs is a Javascript binding for GNOME and can be used to interact with Gtk+. It is mainly based on the Spidermonkey Javascript engine and the GObject introspection framework.

To install Gjs on Fedora, run:

sudo dnf install gjs gjs-devel

Getting started

Writing your first Gtk+ application

Writing Gjs applications for the GNOME Desktop is very similiar to coding with traditional languages in the plataform such as Python.

Firstly, we need to import the desired libraries for our program:

const Gtk = imports.gi.Gtk;

After that, we will initialize Gtk+.

Gtk.init(null, 0);

To create our window, we need to create a GtkWindow object. Gjs allows the initialization of object attributes to be made when the object is constructed.

let window = new Gtk.Window({
    title: "Hello World"
});

In the lines below we are connecting the destroy event of the window to ensure that the application is terminated if we click on the x button to close the window.

window.connect("destroy", function () {
  Gtk.main_quit();
});

After that, we need to display the window, by calling its show method:

window.show();

Finally, we start the GTK+ processing loop which we quit when the window is closed.

Gtk.main();

To run the program, open a terminal, change to the directory of the file, and enter:

gjs hello.gjs

The complete program can be seen below:

const Gtk = imports.gi.Gtk;

Gtk.init(null, 0);

let window = new Gtk.Window({
    title: "Hello World"
});

window.connect("destroy", function () {
  Gtk.main_quit();
});
window.show();

Gtk.main();

More information

Authors: Felipe Borges