|
|
## Objects
|
|
|
## GObjects
|
|
|
|
|
|
### Constructors
|
|
|
|
|
|
Object constructors look like JavaScript constructors, and can take a map of properties. Example:
|
|
|
Object constructors look like JavaScript constructors, and can take a map of properties.
|
|
|
|
|
|
```js
|
|
|
let label = new Gtk.Label({
|
... | ... | @@ -14,12 +14,35 @@ let label = new Gtk.Label({ |
|
|
});
|
|
|
```
|
|
|
|
|
|
Subclassing GObjects is done like so:
|
|
|
|
|
|
```js
|
|
|
var MyLabel = GObject.registerClass({
|
|
|
// GObject
|
|
|
Implements: [ Gtk.Orientable ], // Interfaces the subclass implements
|
|
|
Properties: {}, // Custom properties
|
|
|
Signals: {}, // Custom signals
|
|
|
// Gtk
|
|
|
CssName: '', // CSS name
|
|
|
Template: 'resource:///path/example.ui', // Builder template
|
|
|
Children: [ 'label-child' ], // Template children
|
|
|
InternalChildren: [ 'internal-box' ] // Template internal (private) children
|
|
|
}, class MyLabel extends Gtk.Label {
|
|
|
// Currently GObjects use _init, not construct
|
|
|
_init(params) {
|
|
|
// Chaining up
|
|
|
super._init(params);
|
|
|
}
|
|
|
});
|
|
|
```
|
|
|
|
|
|
### Properties
|
|
|
|
|
|
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 (_).
|
|
|
GObject properties may be retrieved and set using native property style access or standard get/set methods. Note that 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.connect('notify::use-markup', () => { ... });
|
|
|
label.use_markup = true;
|
|
|
label.set_halign(Gtk.Align.FILL);
|
|
|
}
|
... | ... | @@ -30,24 +53,36 @@ GObject subclasses can define their own properties, which is necessary if you wa |
|
|
```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
|
|
|
'example-prop': GObject.ParamSpec.string(
|
|
|
'example-prop', // property name
|
|
|
'ExampleProperty', // nickname
|
|
|
'An example read write property', // description
|
|
|
GObject.ParamFlags.READWRITE, // READABLE/READWRITE/CONTRUCT/etc
|
|
|
'' // implement defaults manually
|
|
|
)
|
|
|
}
|
|
|
}, class ExampleApplication extends GObject.Object {
|
|
|
_init() {
|
|
|
super._init();
|
|
|
}, class MyLabel extends Gtk.Label {
|
|
|
_init(params) {
|
|
|
super._init(params);
|
|
|
}
|
|
|
|
|
|
get example_prop() {
|
|
|
if (this._example_prop === undefined) {
|
|
|
return 'A default';
|
|
|
}
|
|
|
|
|
|
return this._example_prop;
|
|
|
}
|
|
|
|
|
|
set example_prop(value) {
|
|
|
this._example_prop = value;
|
|
|
}
|
|
|
});
|
|
|
```
|
|
|
|
|
|
### Signals
|
|
|
|
|
|
Every object inherited from GObject has `connect()`, `disconnect()` and `emit()` methods.
|
|
|
Every object inherited from GObject has `connect()`, `connect_after()`, `disconnect()` and `emit()` methods.
|
|
|
|
|
|
```js
|
|
|
let handlerId = label.connect('activate-link', (label, uri) => {
|
... | ... | @@ -69,7 +104,7 @@ GObject subclasses can define their own signals. |
|
|
```js
|
|
|
var MyLabel = GObject.registerClass({
|
|
|
Signals: {
|
|
|
"my-signal": {
|
|
|
'my-signal': {
|
|
|
flags: GObject.SignalFlags.RUN_FIRST,
|
|
|
param_types: [ GObject.TYPE_STRING ]
|
|
|
}
|
... | ... | @@ -77,6 +112,7 @@ var MyLabel = GObject.registerClass({ |
|
|
}, class ExampleApplication extends GObject.Object {
|
|
|
_init() {
|
|
|
super._init();
|
|
|
this.emit('my-signal', 'a string parameter');
|
|
|
}
|
|
|
});
|
|
|
```
|
... | ... | |