Skip to content

Commit ea1e5fb

Browse files
committed
Make ObjectByName work with Go types too.
Fixes go-qml#91.
1 parent 2fcb6e2 commit ea1e5fb

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

datatype.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,15 @@ func unpackDataValue(dvalue *C.DataValue, engine *Engine) interface{} {
153153
var c uint32 = *(*uint32)(datap)
154154
return color.RGBA{byte(c >> 16), byte(c >> 8), byte(c), byte(c >> 24)}
155155
case C.DTGoAddr:
156-
return (*(**valueFold)(datap)).gvalue
156+
// ObjectByName also does this fold conversion, to have access
157+
// to the cvalue. Perhaps the fold should be returned.
158+
fold := (*(**valueFold)(datap))
159+
ensureEngine(engine.addr, unsafe.Pointer(fold))
160+
return fold.gvalue
157161
case C.DTInvalid:
158162
return nil
159163
case C.DTObject:
160-
// TODO Would be good to preserve identity on the Go side. See ensureEngine as well.
164+
// TODO Would be good to preserve identity on the Go side. See initGoType as well.
161165
obj := &Common{
162166
engine: engine,
163167
addr: *(*unsafe.Pointer)(datap),

qml.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -639,13 +639,21 @@ func (obj *Common) Map(property string) *Map {
639639
func (obj *Common) ObjectByName(objectName string) Object {
640640
cname, cnamelen := unsafeStringData(objectName)
641641
var dvalue C.DataValue
642+
var object Object
642643
RunMain(func() {
643644
qname := C.newString(cname, cnamelen)
644645
defer C.delString(qname)
645646
C.objectFindChild(obj.addr, qname, &dvalue)
647+
value := unpackDataValue(&dvalue, obj.engine)
648+
if dvalue.dataType == C.DTGoAddr {
649+
datap := unsafe.Pointer(&dvalue.data)
650+
fold := (*(**valueFold)(datap))
651+
object = &Common{fold.cvalue, obj.engine}
652+
} else {
653+
object, _ = value.(Object)
654+
}
646655
})
647-
object, ok := unpackDataValue(&dvalue, obj.engine).(Object)
648-
if !ok {
656+
if object == nil {
649657
panic(fmt.Sprintf("cannot find descendant with objectName == %q", objectName))
650658
}
651659
return object

qml_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -595,14 +595,23 @@ var tests = []struct {
595595
QMLLog: "Identical: true",
596596
},
597597
{
598-
Summary: "Object finding via objectName",
598+
Summary: "Object finding via ObjectByName",
599599
QML: `Item { Item { objectName: "subitem"; property string s: "<found>" } }`,
600600
Done: func(c *TestData) {
601601
obj := c.root.ObjectByName("subitem")
602602
c.Check(obj.String("s"), Equals, "<found>")
603603
c.Check(func() { c.root.ObjectByName("foo") }, Panics, `cannot find descendant with objectName == "foo"`)
604604
},
605605
},
606+
{
607+
Summary: "Object finding via ObjectByName on GoType",
608+
QML: `Item { GoType { objectName: "subitem"; property string s: "<found>" } }`,
609+
Done: func(c *TestData) {
610+
obj := c.root.ObjectByName("subitem")
611+
c.Check(obj.String("s"), Equals, "<found>")
612+
c.Check(func() { c.root.ObjectByName("foo") }, Panics, `cannot find descendant with objectName == "foo"`)
613+
},
614+
},
606615
{
607616
Summary: "Register Go type",
608617
QML: `GoType { objectName: "test"; Component.onCompleted: console.log("String is", stringValue) }`,

0 commit comments

Comments
 (0)