gircore / gir.core

A C# binding generator for GObject based libraries providing a C# friendly API surface
https://gircore.github.io/
MIT License
307 stars 28 forks source link

Button pressed/released events #1073

Closed Daniel15 closed 3 months ago

Daniel15 commented 3 months ago

I've never written an app using Gtk, so let me know if I'm missing something obvious.

I want to have a button that does different things when pressed vs released. I found this Python code on StackOverflow: https://stackoverflow.com/a/19441817

self.button = Gtk.Button("Hi Printer")
self.button.connect("pressed", self.on_button_clicked)
self.button.connect("released", self.on_button_released)

With Gir.Core, I could add an OnClicked event handler for a Gtk.Button:

_hello.OnClicked += (sender, args) => Console.WriteLine("Clicked");

but I don't see pressed and released events. It looks like the file containing the OnClicked event is a generated file (Button.Signals.Generated.cs) so I'm not quite sure what to do.

Thanks!

Daniel15 commented 3 months ago

This is my fault - This was only available in Gtk3, and removed in Gtk4. Apparently I can use a custom widget and GtkGestureClick to handle this. I'll try figure it out, or think of a new UI design that doesn't need this.

Daniel15 commented 3 months ago

This works!

var gesture = GestureClick.New();
gesture.PropagationPhase = PropagationPhase.Capture;
gesture.OnPressed += (_, _) => Console.WriteLine("Pressed");
gesture.OnReleased += (_, _) => Console.WriteLine("Released");
_hello.AddController(gesture);
badcel commented 3 months ago

Hey, great you solved your problem. I could not have helped you very much as I'm not very familar with GTK either: I'm just the guy working on bindings for GObject based libraries😅.

Having some C# docs or sample projects to refer to for common problems would be very nice. So any help is welcome.