-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
94 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
go 1.22.0 | ||
|
||
use ( | ||
. | ||
./pkg | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package glib | ||
|
||
import ( | ||
"reflect" | ||
"sync" | ||
"unsafe" | ||
|
||
"github.com/diamondburned/gotk4/pkg/core/gbox" | ||
) | ||
|
||
// #include <glib.h> | ||
// #include <glib-object.h> | ||
// #include "glib.go.h" | ||
import "C" | ||
|
||
var bindingNames sync.Map // map[reflect.Type]C.GQuark | ||
|
||
// Associate value with object | ||
func Bind[T any](obj Objector, value T) { | ||
object := BaseObject(obj) | ||
name := bindingName[T]() | ||
|
||
ptr := C.gpointer(gbox.Assign(value)) | ||
|
||
C.g_object_set_data_full(object.native(), (*C.gchar)(name), ptr, (*[0]byte)(C._gotk4_data_destroy)) | ||
} | ||
|
||
// Disassociate value from object | ||
func Unbind[T any](obj Objector) { | ||
name := bindingName[T]() | ||
|
||
ptr := C.g_object_steal_data(BaseObject(obj).native(), (*C.gchar)(name)) | ||
defer gbox.Delete(uintptr(ptr)) | ||
} | ||
|
||
// Obtain value associated with object | ||
func Bounded[T any](obj Objector) *T { | ||
name := bindingName[T]() | ||
|
||
ptr := C.g_object_get_data(BaseObject(obj).native(), name) | ||
|
||
value, ok := gbox.Get(uintptr(ptr)).(T) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
return &value | ||
} | ||
|
||
func bindingName[T any]() *C.gchar { | ||
t := reflect.TypeFor[T]() | ||
|
||
if v, ok := bindingNames.Load(t); ok { | ||
quark := v.(C.GQuark) | ||
return C.g_quark_to_string(quark) | ||
} | ||
|
||
name := "_gotk4_" + t.String() | ||
|
||
nameC := C.CString(name) | ||
defer C.free(unsafe.Pointer(nameC)) | ||
|
||
quark := C.g_quark_from_string(nameC) | ||
if v, lost := bindingNames.LoadOrStore(t, quark); lost { | ||
quark = v.(C.GQuark) | ||
} | ||
|
||
return C.g_quark_to_string(quark) | ||
} | ||
|
||
//export _gotk4_data_destroy | ||
func _gotk4_data_destroy(ptr C.gpointer) { | ||
gbox.Delete(uintptr(ptr)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,27 @@ | ||
package test | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/diamondburned/gotk4/pkg/gtk/v4" | ||
"github.com/diamondburned/gotk4/pkg/core/glib" | ||
"github.com/diamondburned/gotk4/pkg/gio/v2" | ||
) | ||
|
||
func TestObjectData(t *testing.T) { | ||
gtk.Init() | ||
testObjectData(t) | ||
|
||
label := gtk.NewLabel("label") | ||
runtime.GC() | ||
} | ||
|
||
func testObjectData(t *testing.T) { | ||
app := gio.NewApplication("foo.bar", gio.ApplicationFlagsNone) | ||
|
||
label.SetObjectData("foo", "bar") | ||
glib.Bind(app, "foo") | ||
|
||
if label.ObjectData("foo") != "bar" { | ||
if value := glib.Bounded[string](app); value == nil || *value != "foo" { | ||
t.Fatal("returned data did not match expected data") | ||
} | ||
|
||
label.StealObjectData("foo") | ||
glib.Unbind[string](app) | ||
} |