energy/cef/cef-v8-bind-render.go

118 lines
2.9 KiB
Go
Raw Normal View History

2023-03-24 15:20:26 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
// 基于IPC的字段数据绑定 - 渲染(子)进程
package cef
import (
2023-03-24 18:08:28 +08:00
"bytes"
2023-03-24 15:20:26 +08:00
"fmt"
2023-03-24 15:51:44 +08:00
"github.com/energye/energy/pkgs/json"
2023-03-24 18:18:33 +08:00
"strings"
2023-03-24 18:08:28 +08:00
"text/template"
2023-03-24 15:20:26 +08:00
)
var bindRender *bindRenderProcess
type bindRenderProcess struct {
2023-03-24 23:08:44 +08:00
isInitBindIPC bool
handler *ICefV8Handler
2023-03-24 15:20:26 +08:00
}
2023-03-24 15:51:44 +08:00
func (m *bindRenderProcess) initBindIPC() {
2023-03-24 23:08:44 +08:00
if m.isInitBindIPC {
return
}
m.isInitBindIPC = true
2023-03-24 15:51:44 +08:00
renderIPC.addCallback(func(channelId int64, data json.JSON) bool {
if data != nil {
//messageJSON := data.JSONObject()
//messageId := messageJSON.GetIntByKey(ipc_id)// messageId: 同步永远是1
//name := messageJSON.GetStringByKey(ipc_name)
//argumentList := messageJSON.GetArrayByKey(ipc_argumentList)
//if name == internalIPCJSExecuteGoSyncEventReplay {
// return true
//}
}
return false
})
}
2023-03-24 15:20:26 +08:00
func (m *bindRenderProcess) makeBind() {
m.handler = V8HandlerRef.New()
m.handler.Execute(func(name string, object *ICefV8Value, arguments *TCefV8ValueArray, retVal *ResultV8Value, exception *ResultString) bool {
fmt.Println("v8Handler.Execute", name, renderIPC)
2023-03-24 15:51:44 +08:00
if renderIPC != nil {
2023-03-25 12:09:00 +08:00
//renderIPC.ipc.Send()
2023-03-24 15:51:44 +08:00
}
2023-03-24 15:20:26 +08:00
return false
})
//注册js
2023-03-24 22:23:23 +08:00
// var jsCode = `
//let bind;
//if (!bind) {
// bind = {};
//}
//(function () {
// Object.defineProperty(bind, 'myparam', {
// get(){
// native function getMyParam();
// return getMyParam();
// },
// set(v){
// native function setMyParam();
// setMyParam(v);
// }
// });
//})();
//`
// fmt.Println("jsCode", jsCode)
2023-03-24 18:08:28 +08:00
var buf = &bytes.Buffer{}
buf.WriteString(fmt.Sprintf(`let %s=null;if(%s===null){%s={};}`, internalBind, internalBind, internalBind))
var tempText = `
2023-03-24 18:18:33 +08:00
{{range $i, $v := $.fields}}
Object.defineProperty({{$.bind}}, "{{$v}}", {
get(){
native function {{getter ($v)}}();
return {{getter ($v) }}();
},
set(v){
native function {{setter ($v)}}();
{{setter ($v) }}(v);
}
});
{{end}}`
2023-03-24 18:08:28 +08:00
var funcs = make(template.FuncMap)
funcs["getter"] = func(v string) string {
2023-03-24 18:18:33 +08:00
return "get" + strings.ToUpper(string(v[0])) + v[1:]
2023-03-24 18:08:28 +08:00
}
funcs["setter"] = func(v string) string {
2023-03-24 18:18:33 +08:00
return "set" + strings.ToUpper(string(v[0])) + v[1:]
2023-03-24 18:08:28 +08:00
}
temp, err := template.New("v8bind").Funcs(funcs).Parse(tempText)
if err != nil {
panic(err)
}
var data = map[string]any{}
data["bind"] = internalBind
var field = []string{"myparam", "myparam1"}
data["fields"] = field
var tempResult = &bytes.Buffer{}
err = temp.Execute(tempResult, data)
if err != nil {
panic(err)
}
2023-03-24 18:18:33 +08:00
buf.WriteString("(function(){")
buf.Write(tempResult.Bytes())
buf.WriteString("})();")
fmt.Println("tempResult:", buf.String())
registerExtension(fmt.Sprintf("%s/%s", internalV8Bind, internalBind), buf.String(), m.handler)
2023-03-24 15:20:26 +08:00
}