rhx / SwiftGtk

A Swift wrapper around gtk-3.x and gtk-4.x that is largely auto-generated from gobject-introspection
https://rhx.github.io/SwiftGtk/
BSD 2-Clause "Simplified" License
317 stars 26 forks source link

Set event mask without using `rawValue` and casting #25

Closed lschmierer closed 4 years ago

lschmierer commented 4 years ago

Is there a way to set the event mask without using rawValue and casting?

add(events) expects CInt (Int32) while GDK_KEY_PRESS_MASK is UInt32

add(events: CInt(EventMask.key_press_mask.rawValue))
rhx commented 4 years ago

Yes, it's a bit unfortunate that GIR doesn't seem to have a way to identify typed bit masks here. It doesn't help that gdk_add_events has a <type name="gint" c:type="gint"/> (signed int) signature for its parameter, while GdkEventMask is unsigned.

rhx commented 4 years ago

This is fixed in gir2swift version 9: bitfields are no longer a typealias of the underlying C enum. Instead, the Swift type is now an OptionSet whose rawValue can be initialised from the C type. This way, you can now do something like

widget.add(events: .buttonPressMask)
// or:
widget.add(events: [.keyPressMask, .buttonPressMask])

This is a source breaking change for any code that assumes that the C and Swift types are the same (e.g. GdkEventMask == EventMask is no longer a valid assumption). The Swift type can still be constructed from the underlying C type. To get the underlying C value from the Swift type, use the value property.