Advanced Subclassing
These topics extend Subclassing GObject for classes that depend on construction order or disposal, redeclare inherited properties, or implement interfaces with special contracts.
Construction
vfuncConstructed runs after native construct properties are set, while super() is still executing. JavaScript field initializers and the constructor body have not run yet. Other properties passed to the constructor, including element props, are set after it returns: a binding or notify handler connected there receives them, but reading them directly there returns their defaults. Use declared fields or native properties for state this override needs; private fields are unavailable at that point.
Objects created by native code do not run the JavaScript constructor or its field initializers. Initialize state needed by those instances in vfuncConstructed, chaining to the parent. Keep ordinary component setup in React.
Disposal
As in GJS, vfuncDispose runs only while the instance's JavaScript object is alive, such as after runDispose() or when a window is destroyed. An object released by GTK is usually disposed after its JavaScript object has been garbage-collected; GTKX then skips the override, which could no longer reach the instance's JavaScript state, and runs the parent implementation. Stop timers and other JavaScript-driven work where it is started, such as in vfuncUnmap or an effect cleanup, rather than relying on vfuncDispose.
Overriding an inherited property
Use GObject.ParamSpec.override("property-name", Source) when explicitly redeclaring a property from a parent class or interface. Source can be its generated class, interface, or GType. Implemented interface properties are overridden automatically. Native reads and writes go through a getter and setter of the same camelCase name when the class defines them, as they do for declared properties.
Some interface properties are read through a virtual function, such as Gtk.Editable's text, which reads vfuncGetText. A write to such a property throws unless the class defines a getter and setter for it, because the virtual function would never see the written value.
Class-level setup
Use classInit for native operations that must run during type registration, and cssName to set a widget subclass's CSS name. classInit receives the generated class-struct wrapper, such as Gtk.WidgetClass, after GTKX installs the declared properties, signals and virtual functions. Its parameter is typed as object; annotate it with the class struct of the class you extend:
import * as Gtk from "@gtkx/gi/gtk";
import { registerClass } from "@gtkx/runtime";
class WaveformBase extends Gtk.Widget {}
const Waveform = registerClass(WaveformBase, {
typeName: "ExampleWaveform",
cssName: "waveform",
classInit(klass: Gtk.WidgetClass) {
klass.setAccessibleRole(Gtk.AccessibleRole.IMG);
},
});This is type setup, not a component mount effect. If it throws, registration fails to return, but the native type remains registered and cannot be retried under the same name.
Slots with no generated member
Use callParent when the virtual function has no generated member, including vfuncDispose. Pass the class whose override is running, the vfunc name, the instance, and its arguments. The same helper can chain interface slots that TypeScript cannot resolve through super.
Delegating Gtk.Editable
GTK's recipe for implementing Gtk.Editable wraps an inner Gtk.Text: return it from vfuncGetDelegate, call initDelegate() once it exists and finishDelegate() in vfuncDispose. GTKX forwards the interface's properties (text, cursorPosition, editable, widthChars, xalign and the rest) to the delegate automatically, as gtk_editable_delegate_set_property and gtk_editable_delegate_get_property do in C, so skip that step of the recipe. Props such as text and editable on the class's element then reach the inner field:
import * as Gtk from "@gtkx/gi/gtk";
import { callParent, registerClass } from "@gtkx/runtime";
class SearchFieldBase extends Gtk.Widget implements Gtk.EditableImpl {
declare private delegate: Gtk.Text | null;
declare initDelegate: Gtk.Editable["initDelegate"];
declare finishDelegate: Gtk.Editable["finishDelegate"];
override vfuncConstructed(): void {
super.vfuncConstructed();
const text = new Gtk.Text({ hexpand: true });
text.setParent(this);
this.delegate = text;
this.initDelegate();
}
vfuncGetDelegate(): Gtk.Editable | null {
return this.delegate;
}
vfuncDispose(): void {
if (this.delegate !== null) {
this.finishDelegate();
this.delegate.unparent();
this.delegate = null;
}
callParent(SearchField, "vfuncDispose", this);
}
}
const SearchField = registerClass(SearchFieldBase, {
typeName: "ExampleSearchField",
implements: [Gtk.Editable],
classInit(klass: Gtk.WidgetClass) {
klass.setLayoutManagerType(Gtk.BoxLayout);
},
});Create the delegate in vfuncConstructed: reading or writing an editable property while vfuncGetDelegate returns null throws.
Reporting a vfunc error
Throw from a vfunc that reports errors through GError. GTKX preserves a thrown GLib.Error, such as new GLib.Error(Gio.IOErrorEnum, Gio.IOErrorEnum.PERMISSION_DENIED, "Read-only"), and converts other exceptions into a native error. A virtual function without an error channel cannot report failure that way; see Error Handling.
GtkBuilder and .ui files
GTKX applications define their interface in JSX, where signal handlers are props; port a .ui file or a Gtk.Template to JSX rather than loading it. Gtk.Builder can still create instances of registered classes from XML, initialized in vfuncConstructed as in Construction, but <signal> handlers in a .ui file are not supported. GTKX has no built-in JavaScript Gtk.BuilderScope like GJS's, and a scope implemented with registerClass cannot return a closure from vfuncCreateClosure, so such a file fails to load.