hugopl / gi-crystal

Tool to generate Crystal bindings for gobject-based libraries (i.e. GTK)
BSD 3-Clause "New" or "Revised" License
45 stars 3 forks source link

[Request] non-void signals #134

Open GeopJr opened 10 months ago

GeopJr commented 10 months ago

Could the signal macro support non-void signals?

Use case: I'm emitting a singal from a child widget to call another function from the parent and want it to return a bool if it succeeded or not

(exact use case: A widget has a button and a clicked signal. The parent connects to the signal that provides the hash function to copy in string (e.g. "MD5"), the parent then copies it to clipboard and should return a bool to the widget if it succeeded or not so the button displays the correct feedback icon (:heavy_check_mark: or :x:))

hugopl commented 10 months ago

You mean: Support signal return values?

Meanwhile it doesn't support, but yes, it's possible to have this.

First step is to let the signal macro register a signal with a return value, so we can inform to GObject the right GType used for the return type at:

https://github.com/hugopl/gi-crystal/blob/main/src/bindings/g_object/object.cr#L596

Then we also need to register a GObject::SignalAcumulator callback.

I never used (or remember to had used) signals with return values, so I would need to do some experiments in libtest to first see it working in C, to later change the macro to let it work in Crystal.

GeopJr commented 10 months ago

You mean: Support signal return values?

Yes!

I never used (or remember to had used) signals with return values

FWIW, there's quite a few in GTK, here's some: Gtk::ComboBox::PopdownSignal, Gdk::Surface::EventSignal, Gtk::SpinButton::InputSignal

hugopl commented 4 months ago

Hmmm, this issue is easier than I expected to implement, it's just a matter of using

void
g_signal_emitv (
  const GValue* instance_and_params,
  guint signal_id,
  GQuark detail,
  GValue* return_value
)

to emit signals when there's a return value, then get the data from this GValue.

I'll kept this issue in mind when reworking the signals stuff on GICrystal to avoid object leaks.