... | ... | @@ -14,18 +14,37 @@ let label = new Gtk.Label({ |
|
|
});
|
|
|
```
|
|
|
|
|
|
> **PC** I'd suggest moving towards examples that aren't Clutter, or taken from GNOME Shell code (e.g. `Big.BoxAlignment`.) The focus should be on GTK and friends, so `Gtk.Label` with some properties might be appropriate here.
|
|
|
|
|
|
### Properties
|
|
|
|
|
|
GObject properties may be retrieved and set using JavaScript property style access. Example:
|
|
|
GObject properties may be retrieved and set using native property style access or standard get/set methods. However variables in JavaScript can't contain hyphens (-) so when a property name is unquoted use an underscore (_).
|
|
|
|
|
|
```js
|
|
|
if (label.halign === Gtk.Align.CENTER) {
|
|
|
label.halign = Gtk.Align.FILL;
|
|
|
label.use_markup = true;
|
|
|
label.set_halign(Gtk.Align.FILL);
|
|
|
}
|
|
|
```
|
|
|
|
|
|
GObject subclasses can define their own properties, which is necessary if you want to use the `GObject.notify()` or `GObject.bind_property()`.
|
|
|
|
|
|
```js
|
|
|
var MyLabel = GObject.registerClass({
|
|
|
Properties: {
|
|
|
"exampleprop": GObject.ParamSpec.string(
|
|
|
"exampleprop", // property name
|
|
|
"ExampleProperty", // nickname
|
|
|
"An example read write property", // description
|
|
|
GObject.ParamFlags.READWRITE, // read/write/construct...
|
|
|
"" // implement defaults manually
|
|
|
)
|
|
|
}
|
|
|
}, class ExampleApplication extends GObject.Object {
|
|
|
_init() {
|
|
|
super._init();
|
|
|
}
|
|
|
});
|
|
|
```
|
|
|
|
|
|
### Signals
|
|
|
|
|
|
Every object inherited from GObject has `connect()`, `disconnect()` and `emit()` methods.
|
... | ... | @@ -45,6 +64,23 @@ label.emit('activate-link', 'https://www.gnome.org'); |
|
|
label.disconnect(handlerId);
|
|
|
```
|
|
|
|
|
|
GObject subclasses can define their own signals.
|
|
|
|
|
|
```js
|
|
|
var MyLabel = GObject.registerClass({
|
|
|
Signals: {
|
|
|
"my-signal": {
|
|
|
flags: GObject.SignalFlags.RUN_FIRST,
|
|
|
param_types: [ GObject.TYPE_STRING ]
|
|
|
}
|
|
|
}
|
|
|
}, class ExampleApplication extends GObject.Object {
|
|
|
_init() {
|
|
|
super._init();
|
|
|
}
|
|
|
});
|
|
|
```
|
|
|
|
|
|
**NOTE:** GJS also includes a built-in [`signals`](Modules#signals) module for applying signals to native JavaScript classes.
|
|
|
|
|
|
## Enumerations and Flags
|
... | ... | @@ -76,9 +112,7 @@ Unions are currently opaque. |
|
|
|
|
|
## Multiple return values (caller-allocates)
|
|
|
|
|
|
In GJS caller-allocates (variables passed into a function) and functions with multiple out parameters are returned as an array of return values.
|
|
|
|
|
|
For functions which have multiple out parameters, the return values are gathered into an array. Example:
|
|
|
In GJS caller-allocates (variables passed into a function) and functions with multiple out parameters are returned as an array of return values. If the function has a return value, it will be the first element of that array.
|
|
|
|
|
|
```js
|
|
|
let [minimumSize, naturalSize] = label.get_preferred_size();
|
... | ... | @@ -89,11 +123,4 @@ let [ret, contents] = file.get_contents(null); |
|
|
if (ret) {
|
|
|
log(contents);
|
|
|
}
|
|
|
```
|
|
|
|
|
|
If the function has a return value, in addition to the out parameters, the return value will be the first element of such array.
|
|
|
|
|
|
```js
|
|
|
let file = new Gio.File.new_for_path('/proc/cpuinfo');
|
|
|
let [ret, contents] = file.get_contents(null);
|
|
|
``` |
|
|
\ No newline at end of file |