gir.core icon indicating copy to clipboard operation
gir.core copied to clipboard

No way to emit signals

Open MegaEgorik opened this issue 5 months ago • 3 comments

I have to manually add DllImport method g_signal_emit_by_name to my code to emit signals. Instead of:

[DllImport("libgobject-2.0.so.0")]
static extern void g_signal_emit_by_name(nint handle, string signal);
...
g_signal_emit_by_name(widget.Handle.DangerousGetHandle(), "some-signal");

It should be like this:

widget.OnSomeSignal();
//or
widget.OnSomeSignal.Invoke();

But currently any signal is wrapped by event and events cant be called publicly. So my suggestion is to get rid of events and use delegates instead.

MegaEgorik avatar Jul 20 '25 22:07 MegaEgorik

Yeah I'm not totally happy with the C# events either as they don't support GObjects signal Details parameter.

For this reason there are static event descriptors for each event of a class. You should be able to add some generic extension methods to the SignalDefinition interface.

This is some in between solution as it does create only one instance of an object per signal and class.

For your second solution would you create a class or a struct?

badcel avatar Jul 21 '25 12:07 badcel

Well i tried to implement a type which acts like event but also have Invoke method but then i realized that maybe its not the best idea. I noticed that in Gio.ListModel there is both signal and instance method with same name. So maybe just add method which emits event? Like this:

Gtk.Button button = ...;
button.OnClicked += (_,_) => ...;
button.Clicked(); //g_signal_emit_by_name(button, Gtk.Button.ClickedSignal.UnmanagedName) 

MegaEgorik avatar Jul 22 '25 04:07 MegaEgorik

I think I won't do this. The events are already prefixed with "On" because of naming conflicts if I remember correctly. Adding more GirCore specific methods to gobjects raises the chance for new naming conflicts.

So in the first step I will add a method to the signal descriptor which I plan to do for 0.7.0. Until this lands you can help yourself with the extension method.

Just as an example here is the Notify-Signal-Eventdescriptor which you should be able to extend with an "Invoke" method.

But this issue needs to be thought through before a 1.0 release.

badcel avatar Jul 22 '25 04:07 badcel