... | ... | @@ -18,7 +18,7 @@ let drawingArea = new Gtk.DrawingArea({ |
|
|
|
|
|
drawingArea.connect("draw", (widget, cr) => {
|
|
|
// Cairo in GJS uses camelCase function names
|
|
|
cr.setSourceRGB(255, 0, 0);
|
|
|
cr.setSourceRGB(1.0, 0.0, 0.0);
|
|
|
cr.setOperator(Cairo.Operator.DEST_OVER);
|
|
|
cr.arc(16, 16, 16, 0, 2*Math.PI);
|
|
|
cr.fill();
|
... | ... | @@ -34,21 +34,33 @@ drawingArea.connect("draw", (widget, cr) => { |
|
|
|
|
|
### [Format](https://gitlab.gnome.org/GNOME/gjs/blob/master/modules/format.js)
|
|
|
|
|
|
Provides `vprintf()`, `printf()` and the `format()` method meant to be applied to the String prototype.
|
|
|
The format import provides `vprintf()`, `printf()` and the `format()` method for applying to `String.prototype`.
|
|
|
|
|
|
**NOTE:** Native [template literals][template-literals] should be preferred except in few situations like Gettext (See [Bug #50920][bug-50920]).
|
|
|
|
|
|
```js
|
|
|
const Format = imports.format;
|
|
|
let foo = "Pi";
|
|
|
let bar = 1;
|
|
|
let baz = Math.PI;
|
|
|
|
|
|
// Using native template literals (Output: Pi to 2 decimal points: 3.14)
|
|
|
`${foo} to ${bar*2} decimal points: ${baz.toFixed(bar*2)}`
|
|
|
|
|
|
// Applying format() to the string prototype
|
|
|
const Format = imports.format;
|
|
|
String.prototype.format = Format.format;
|
|
|
|
|
|
// Pi to 2 decimal points: 3.14
|
|
|
"%s to %d decimal points: %.2f".format("Pi", 2, 3.14159);
|
|
|
// Using format() (Output: Pi to 2 decimal points: 3.14)
|
|
|
"%s to %d decimal points: %.2f".format(foo, bar*2, baz);
|
|
|
|
|
|
// Using format() with Gettext
|
|
|
_("%d:%d").format(11, 59);
|
|
|
Gettext.ngettext("I have %d apple", "I have %d apples", num).format(num);
|
|
|
|
|
|
// With Gettext since _() returns a string
|
|
|
_("%d:%d").format(hours, minutes);
|
|
|
```
|
|
|
|
|
|
> **PC** You could mention that string templates (backtick strings) are an easier and more Javascript-y way to do this. I wouldn't recommend using Format in new code except in a few specialized situations.
|
|
|
[template-literals]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
|
|
|
[bug-50920]: https://savannah.gnu.org/bugs/?50920
|
|
|
|
|
|
### [Gettext](https://gitlab.gnome.org/GNOME/gjs/blob/master/modules/gettext.js)
|
|
|
|
... | ... | @@ -58,17 +70,14 @@ Helper functions for gettext. See also [examples/gettext.js][example-gettext] fo |
|
|
|
|
|
### [jsUnit](https://gitlab.gnome.org/GNOME/gjs/blob/master/modules/jsUnit.js)
|
|
|
|
|
|
Unit test functions for GJS, demonstrated extensively in the [test-suite][installed-tests].
|
|
|
Unit test functions. [Jasmine][jasmine-gjs] should be preferred, as demonstrated in the GJS [test suite][installed-tests].
|
|
|
|
|
|
[jasmine-gjs]: https://github.com/ptomato/jasmine-gjs
|
|
|
[installed-tests]: https://gitlab.gnome.org/GNOME/gjs/blob/master/installed-tests/js
|
|
|
|
|
|
> **PC** JSUnit isn't actually used anywhere in GJS anymore. It's a module so other code can still use it, but it's not actually a very good unit test library. I wouldn't recommend using it.
|
|
|
|
|
|
### [Lang](https://gitlab.gnome.org/GNOME/gjs/blob/master/modules/lang.js)
|
|
|
|
|
|
Utilities that are "meta-language" things like manipulating object props.
|
|
|
|
|
|
Prior to arrow functions, Lang.bind() was necessary to bind `this` to the function context:
|
|
|
Lang is a mostly obsolete library supporting the legacy classes in GJS. For example, `Lang.bind()` was necessary to bind `this` to the function context before the availability of arrow functions:
|
|
|
|
|
|
```js
|
|
|
const Lang = imports.lang;
|
... | ... | @@ -162,17 +171,58 @@ obj.disconnectAll(); |
|
|
|
|
|
### [System](https://gitlab.gnome.org/GNOME/gjs/blob/master/modules/system.cpp)
|
|
|
|
|
|
Offers `System.version` (eg. `gjs --version`) and `System.programInvocationName`, used especially with `Gio.Application.run()`:
|
|
|
The System module offers a number of useful functions and properties for debugging and shell interaction (eg. ARGV):
|
|
|
|
|
|
```js
|
|
|
const System = imports.system;
|
|
|
* `addressOf(object)`
|
|
|
|
|
|
...
|
|
|
Return the memory address of any object as a string in hexadecimal, e.g. `0xb4f170f0`.
|
|
|
|
|
|
(new MyApplication()).run([System.programInvocationName].concat(ARGV));
|
|
|
```
|
|
|
* `refcount(gobject)`
|
|
|
|
|
|
Return the reference count of any GObject-derived type (almost any class from GTK, Clutter, GLib, Gio, etc.). When an object's reference count is zero, it is cleaned up and erased from memory.
|
|
|
|
|
|
* `breakpoint()`
|
|
|
|
|
|
This is the real gem of the System module! It allows just the tiniest amount of decent debugging practice in GJS. Put `System.breakpoint()` in your code and run it under GDB like so:
|
|
|
|
|
|
```
|
|
|
gdb --args gjs my_program.js
|
|
|
```
|
|
|
|
|
|
When GJS reaches the breakpoint, it will stop executing and return you to the GDB prompt, where you can examine the stack or other things, or type `cont` to continue running. Note that if you run the program outside of GDB, it will abort at the breakpoint, so make sure to remove the breakpoint when you're done debugging.
|
|
|
|
|
|
* `gc()`
|
|
|
|
|
|
Run the garbage collector.
|
|
|
|
|
|
* `exit(error_code)`
|
|
|
|
|
|
This works the same as C's `exit()` function; exits the program, passing a certain error code to the shell. The shell expects the error code to be zero if there was no error, or non-zero (any value you please) to indicate an error. This value is used by other tools such as `make`; if `make` calls a program that returns a non-zero error code, then `make` aborts the build.
|
|
|
|
|
|
* `version`
|
|
|
|
|
|
This property presumably contains version information about GJS.
|
|
|
|
|
|
* `programInvocationName`
|
|
|
|
|
|
This property contains the name of the script as it was invoked from the command line. In C and other languages, this information is contained in the first element of the platform's equivalent of `argv`, but GJS's `ARGV` only contains the subsequent command-line arguments, so `ARGV[0]` in GJS is the same as `argv[1]` in C.
|
|
|
|
|
|
Example of passing ARGV to a `Gio.Application`/`Gtk.Application` (See also:
|
|
|
[examples/gtk-application.js][example-application]):
|
|
|
|
|
|
```js
|
|
|
imports.gi.versions.Gtk = "3.0";
|
|
|
const Gtk = imports.gi.Gtk;
|
|
|
const System = imports.system;
|
|
|
|
|
|
let myApp = new Gtk.Application();
|
|
|
myApp.connect("activate", () => log("activated"));
|
|
|
myApp.run([System.programInvocationName].concat(ARGV));
|
|
|
|
|
|
(new MyApplication()).run([System.programInvocationName].concat(ARGV));
|
|
|
```
|
|
|
|
|
|
see [examples/gtk-application.js](https://gitlab.gnome.org/GNOME/gjs/blob/master/examples/gtk-application.js)
|
|
|
[example-application]: https://gitlab.gnome.org/GNOME/gjs/blob/master/examples/gtk-application.js
|
|
|
|
|
|
### [Tweener](https://gitlab.gnome.org/GNOME/gjs/blob/master/modules/tweener/)
|
|
|
|
... | ... | |