diamondburned/gotk4

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

diamondburned opened this issue · 0 comments

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)