Bind to multiple properties simultaneously

Is there any way in GJS to bind a property to multiple properties simultaneously? For example, I have three buttons and the third button should only be enabled only if the other two buttons are checked? Something like property binding in QML.

There is no direct way to make a property binding conditional on multiple properties, but you can the underlying property notification mechanism to write your own property binding:

this._button1.connect("notify::active", () => {
    if (this._button1.get_active() && this._button2.get_active()) {
        this._button3.set_sensitive(true);
    } else {
        this._button3.set_sensitive(false);
    }
});
this._button2.connect("notify::active", () => {
    if (this._button1.get_active() && this._button2.get_active()) {
        this._button3.set_sensitive(true);
    } else {
        this._button3.set_sensitive(false);
    }
});

You can, of course, abstract the signal handler into its own function:

_enableThirdButton() {
    this._button3.set_sensitive(this._button1.get_active() && this._button2.get_active());
}

// ...

this._button1.connect("notify::active", this._enableThirdButton.bind(this));
this._button2.connect("notify::active", this._enableThirdButton.bind(this));

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.