diamondburned / gotk4

Autogenerated GTK4 bindings for Go
GNU Affero General Public License v3.0
525 stars 20 forks source link

Support binding Go objects to GObjects with `GObject.set_qdata` #117

Open diamondburned opened 1 year ago

diamondburned commented 1 year ago

GObject.set_qdata_full is an incredibly useful that allows users to bind any arbitrary pointer to a GObject that can be destroyed later on. We can use this to allow users to bind a Go object of any type to a GObject.

Proposed API:

type ObjectDataType[T any] struct{ quark C.GQuark }

func NewObjectDataType[T any]() ObjectDataType[T]
func (d ObjectDataType[T]) SetForObject(obj glib.Objector, T)
func (d ObjectDataType[T]) FromObject(obj glib.Objector) T
func (d ObjectDataType[T]) IsInObject(obj glib.Objector) bool

Usage:

var rowID = glib.NewObjectDataType[discord.MessageID]()

func(id discord.MessageID) {
  row := gtk.NewListBoxRow()
  row.AddCSSClass("message-row")
  row.ConnectSignalX(func() {
    messageID := rowID.FromObject(row)
    // use messageID as normal without needing id!
  })

  rowID.SetForObject(row, id)
}(id)