You can write detailed signals with the [Signal (detailed = true)]
attribute.
public class Emitter : Object {
[Signal (detailed = true)]
public signal void detailed_signal ();
public void emit_with_detail (string detail) {
this.detailed_signal[detail] ();
}
}
void main () {
var emitter = new Emitter ();
// Connect only when the detail is "foo".
emitter.detailed_signal["foo"].connect (() => {
print ("Received the signal with 'foo'.\n");
});
// Connect to the signal, whatever is the detail.
emitter.detailed_signal.connect (() => {
print ("Received the signal.\n");
});
emitter.emit_with_detail ("foo"); // Both handlers will be triggered.
emitter.emit_with_detail ("bar"); // Only the general handler will be triggered.
}
This feature is often used with the notify
signal, that any Object
based class has, and which is sent when a property changes. The detail here is the name of the property, so you can choose to connect to this signal only for some of them.
public class Person : Object {
public string name { get; set; }
public int age { get; set; }
}
void main () {
var john = new Person () { name = "John", age = 42 });
john.notify["age"].connect (() => {
print ("Happy birthday!");
});
john.age++;
}