cutego/interop/interop_api.go

179 lines
4 KiB
Go
Raw Normal View History

initial commit of the interop engine + new flutter module + fixes for minor issues this commit introduces the new experimental `interop` module, which is used to allow the use of `therecipe/qt` from languages other than go or javascript. as a start, there was support for the dart language added (currently only in combination with a custom flutter embedder engine, but plain dart support should follow soon). the interop module makes great use of the qml/js interop functions and logic that were written to make `https://github.com/therecipe/entry` possible. additional languages that are planned to be supported in the near future are: swift, kotlin and haxe (maybe with support for the c#, python and java targets, but if this isn't possible in some clean way then these languages will receive standalone bindings as well). people using `therecipe/qt` from these new languages should then be able to use flutter (and dart) in combination with qt (and go/js) through this binding and also be able to easily communicate across the language barriers with the help of the interop functions. the interop engine theoretically also makes a cgo-less `therecipe/qt` usage possible, which could be used to workaround the ever growing annoyances that are experienced when using cgo projects in combination with go modules. the code for the custom flutter embedder that was created to make the flutter/dart binding work can be found in the `flutter` module. it currently only supports debug builds for the desktop targets, but if there is enough interest, then it shouldn't be too difficult to make release builds there, and on the mobile targets work as well (or even on "embedded" systems such as an raspberry pi for that matter). an example repo how to use the new flutter/dart binding, will be published in the next few days.
2020-06-01 22:27:56 +03:00
package interop
import (
"encoding/json"
"strings"
"github.com/therecipe/qt/core"
)
var (
syncCallChan = make(chan string, 0)
asyncCallChan = make(chan string, 0)
mainThreadIsBlockedAlready bool
SyncCallIntoRemote func(string) string
AsyncCallIntoRemote func(string)
)
func RunOnMainBlockingWithWorkerQueue(f func()) {
done := make(chan bool, 0)
Helper.RunOnMainThread(func() { f(); done <- true })
for {
select {
case s := <-syncCallChan:
syncCallChan <- SyncCallIntoRemote(s)
case <-done:
goto br
}
}
br:
}
//
//
//
var Helper = NewHelper(nil)
type helper struct {
core.QObject
_ func() `constructor:"init"`
_ func(f func()) `slot:"runOnMainThread"`
}
func (h *helper) init() {
h.ConnectRunOnMainThread(func(f func()) { f() })
}
//
//
//
func _syncCallIntoLocal(s string) string {
//println("input:", s)
var i []interface{}
json.Unmarshal([]byte(s), &i)
pseudoIn := NewPseudoQJSValue1(i)
for j := uint(0); j < pseudoIn.Property("length").ToUInt(nil); j++ {
if v := pseudoIn.Property2(j); strings.HasPrefix(v.ToString(), "___REMOTE_CALLBACK___") {
f := map[string]interface{}{
"___pointer": pseudoIn.Property2(1).ToULongLong(nil),
"callable": true,
"callableLocal": false,
"callableName": pseudoIn.Property2(3).ToString(),
}
if vs := v.ToString(); strings.Contains(vs, ":") {
f["callableName"] = strings.Split(vs, ":")[1]
}
fun := NewPseudoQJSValue1(f)
pseudoIn.SetProperty2(j, fun)
//TODO: allow creation of funcs in arbitrary module depth
if pseudoIn.Property2(3).ToString() == "NewGoType" {
ptr := PseudoQJSEngine_qjsEngine(nil)
fn := f["callableName"].(string)
if strings.Count(fn, ".") == 0 {
ptr.GlobalObject().SetProperty(fn, fun)
} else {
var jsv *PseudoQJSValue
if m := ptr.GlobalObject().Property(strings.Split(fn, ".")[0]); m.IsUndefined() {
jsv = ptr.NewObject()
//ptr.GlobalObject().SetProperty(strings.Split(fn, ".")[0], jsv) //TODO: look below
} else {
jsv = m
}
//TODO: PseudoQJSValue should instead automatically update it's parent(s) instead, so it behaves similar to qml.QJSValue
jsv.SetProperty(strings.Split(fn, ".")[1], fun)
ptr.GlobalObject().SetProperty(strings.Split(fn, ".")[0], jsv)
}
}
}
}
var ret *PseudoQJSValue
RunOnMainBlockingWithWorkerQueue(func() {
mainThreadIsBlockedAlready = true
ret = Z_wrapperFunction(pseudoIn)
mainThreadIsBlockedAlready = false
})
var o []byte
if ret == nil {
var someOutput []interface{}
o, _ = json.Marshal(someOutput)
} else {
eg := PseudoQJSEngine_qjsEngine(nil)
//TODO: use reflection and/or recursion instead
switch ret.Type() {
case core.QVariant__Map:
var typ core.QVariant__Type
for _, v := range ret.ToMap() {
typ = v.Type()
break
}
switch typ {
case core.QVariant__Map:
var someOutput map[string]map[string]interface{}
eg.ToGoType(ret, &someOutput)
o, _ = json.Marshal(someOutput)
case core.QVariant__List:
var someOutput map[string][]interface{}
eg.ToGoType(ret, &someOutput)
o, _ = json.Marshal(someOutput)
default:
var someOutput map[string]interface{}
eg.ToGoType(ret, &someOutput)
o, _ = json.Marshal(someOutput)
}
case core.QVariant__List:
var typ core.QVariant__Type
for _, v := range ret.ToList() {
typ = v.Type()
break
}
switch typ {
case core.QVariant__Map:
var someOutput []map[string]interface{}
eg.ToGoType(ret, &someOutput)
o, _ = json.Marshal(someOutput)
case core.QVariant__List:
var someOutput [][]interface{}
eg.ToGoType(ret, &someOutput)
o, _ = json.Marshal(someOutput)
default:
var someOutput []interface{}
eg.ToGoType(ret, &someOutput)
o, _ = json.Marshal(someOutput)
}
default:
var someOutput interface{}
eg.ToGoType(ret, &someOutput)
o, _ = json.Marshal(someOutput)
}
//<<<
}
//println("output:", string(o))
return string(o)
}
func _asyncCallIntoRemoteResponse(s string) { asyncCallChan <- s }