muon/muon.go

322 lines
7.0 KiB
Go
Raw Normal View History

2019-10-01 21:49:22 -05:00
package muon
2019-10-01 21:38:14 -05:00
import (
2019-10-03 00:01:39 -05:00
"encoding/json"
"fmt"
2019-10-01 21:38:14 -05:00
"net"
"net/http"
"reflect"
"unsafe"
2019-10-01 21:59:10 -05:00
. "github.com/ImVexed/muon/ultralight"
2019-10-01 21:38:14 -05:00
)
2019-10-04 18:54:37 -05:00
// Window represents a single Ultralight instance
2019-10-01 21:38:14 -05:00
type Window struct {
wnd ULWindow
2019-10-07 19:29:22 -05:00
ov ULOverlay
2019-10-01 21:38:14 -05:00
view ULView
app ULApp
handler http.Handler
cfg *Config
}
type ipf struct {
Function reflect.Value
ParamTypes []reflect.Type
ReturnTypes []reflect.Type
}
2019-10-04 18:54:37 -05:00
// Config contains configurable controls for the Ultralight engine
2019-10-01 21:38:14 -05:00
type Config struct {
Title string
Height uint32
Width uint32
X int32
Y int32
2019-10-07 19:29:22 -05:00
Resizeable bool
Borderless bool
Tilted bool
Maximizable bool
2019-10-01 21:38:14 -05:00
}
2019-10-04 18:54:37 -05:00
// New creates a Ultralight Window
2019-10-01 21:38:14 -05:00
func New(cfg *Config, handler http.Handler) *Window {
w := &Window{
cfg: cfg,
handler: handler,
}
ufg := UlCreateConfig()
2019-10-07 14:04:16 -05:00
std := UlCreateSettings()
w.app = UlCreateApp(std, ufg)
2019-10-01 21:38:14 -05:00
mm := UlAppGetMainMonitor(w.app)
2019-10-07 19:29:22 -05:00
var hint uint32
if cfg.Resizeable {
hint |= 4
}
if cfg.Borderless {
hint |= 1
}
if cfg.Tilted {
hint |= 2
}
if cfg.Maximizable {
hint |= 8
}
w.wnd = UlCreateWindow(mm, w.cfg.Height, w.cfg.Width, false, hint)
2019-10-01 21:38:14 -05:00
UlWindowSetTitle(w.wnd, w.cfg.Title)
UlAppSetWindow(w.app, w.wnd)
2019-10-07 19:29:22 -05:00
w.ov = UlCreateOverlay(w.wnd, w.cfg.Height, w.cfg.Width, w.cfg.X, w.cfg.Y)
2019-10-01 21:38:14 -05:00
2019-10-07 19:29:22 -05:00
UlWindowSetResizeCallback(w.wnd, resizeCallback(w.ov), unsafe.Pointer(&w.ov))
2019-10-01 21:38:14 -05:00
2019-10-07 19:29:22 -05:00
w.view = UlOverlayGetView(w.ov)
2019-10-01 21:38:14 -05:00
return w
}
2019-10-04 18:54:37 -05:00
// Start sets up the Ultralight runtime and begins showing the Window
2019-10-01 21:38:14 -05:00
func (w *Window) Start() error {
addr, err := serveHandler(w.handler)
if err != nil {
return err
}
url := UlCreateString(addr)
defer UlDestroyString(url)
UlViewLoadURL(w.view, url)
UlAppRun(w.app)
return nil
}
// Bind registers the given function to the given name in the Window's JS global object
func (w *Window) Bind(name string, function interface{}) {
f := &ipf{
Function: reflect.ValueOf(function),
}
t := f.Function.Type()
f.ParamTypes = make([]reflect.Type, t.NumIn())
for i := 0; i < t.NumIn(); i++ {
f.ParamTypes[i] = t.In(i)
}
f.ReturnTypes = make([]reflect.Type, t.NumOut())
for i := 0; i < t.NumOut(); i++ {
2019-10-03 00:01:39 -05:00
f.ReturnTypes[i] = t.Out(i)
2019-10-01 21:38:14 -05:00
}
addFunctionToView(w.view, name, w.makeIPCCallback(f))
}
2019-10-03 00:01:39 -05:00
// Eval evaluates a given JavaScript string in the given Window view. `ret` is necessary for JSON serialization if an object is returned.
func (w *Window) Eval(js string, ret reflect.Type) (interface{}, error) {
us := UlCreateString(js)
defer UlDestroyString(us)
ref := UlViewEvaluateScript(w.view, us)
ctx := UlViewGetJSContext(w.view)
val, err := fromJSValue(ctx, ref, ret)
if err != nil {
return nil, err
}
return val.Interface(), nil
}
2019-10-07 19:29:22 -05:00
// Resize changes the given Window's size
func (w *Window) Resize(width int, height int) {
UlOverlayResize(w.ov, uint32(width), uint32(height))
}
// Move sets the Window's position to the given coordinates
func (w *Window) Move(x int, y int) {
UlOverlayMoveTo(w.ov, int32(x), int32(y))
}
2019-10-01 21:38:14 -05:00
func (w *Window) makeIPCCallback(f *ipf) func(JSContextRef, JSObjectRef, JSObjectRef, uint, []JSValueRef, []JSValueRef) JSValueRef {
return func(
ctx JSContextRef,
function JSObjectRef,
thisObject JSObjectRef,
argumentCount uint,
arguments []JSValueRef,
exception []JSValueRef) JSValueRef {
params := make([]reflect.Value, argumentCount)
for i := uint(0); i < argumentCount; i++ {
2019-10-03 00:01:39 -05:00
val, err := fromJSValue(ctx, arguments[i], f.ParamTypes[i])
if err != nil {
panic(err)
2019-10-01 21:38:14 -05:00
}
2019-10-03 00:01:39 -05:00
params[i] = val
2019-10-01 21:38:14 -05:00
}
val := f.Function.Call(params)
if len(val) > 1 {
2019-10-03 00:01:39 -05:00
panic("Javascript does not support more than 1 return value!")
2019-10-01 21:38:14 -05:00
}
2019-10-03 00:01:39 -05:00
return toJSValue(ctx, val[0])
}
}
func fromJSValue(ctx JSContextRef, value JSValueRef, rtype reflect.Type) (reflect.Value, error) {
var rv reflect.Value
var err error
if JSValueIsArray(ctx, value) {
l := JSStringCreateWithUTF8CString("length")
defer JSStringRelease(l)
obj := *(*JSObjectRef)(unsafe.Pointer(&value))
prop := JSObjectGetProperty(ctx, obj, l, nil)
length := int(JSValueToNumber(ctx, prop, nil))
values := reflect.MakeSlice(rtype, length, length)
for i := 0; i < length; i++ {
ref := JSObjectGetPropertyAtIndex(ctx, obj, uint32(i), nil)
val, err := fromJSValue(ctx, ref, rtype.Elem())
if err != nil {
return reflect.Value{}, err
}
values.Index(i).Set(val)
}
return values, nil
}
switch JSValueGetType(ctx, value) {
case KJSTypeBoolean:
rv = reflect.ValueOf(JSValueToBoolean(ctx, value))
case KJSTypeNumber:
rv = reflect.ValueOf(JSValueToNumber(ctx, value, nil))
case KJSTypeString:
ref := JSValueToStringCopy(ctx, value, nil)
rv = reflect.ValueOf(fromJSString(ref))
JSStringRelease(ref)
case KJSTypeObject:
ref := JSValueCreateJSONString(ctx, value, 0, nil)
obj := reflect.New(rtype).Interface()
if err = json.Unmarshal([]byte(fromJSString(ref)), &obj); err == nil {
rv = reflect.Indirect(reflect.ValueOf(obj))
2019-10-01 21:38:14 -05:00
}
2019-10-03 00:01:39 -05:00
JSStringRelease(ref)
default:
panic("Not implemented")
}
return rv, err
}
func fromJSString(str JSStringRef) string {
len := JSStringGetMaximumUTF8CStringSize(str)
data := make([]byte, len)
written := JSStringGetUTF8CString(str, data, len)
return string(data[:written-1])
}
func toJSValue(ctx JSContextRef, value reflect.Value) JSValueRef {
var jsv JSValueRef
var err error
switch value.Kind() {
case reflect.Float64:
jsv = JSValueMakeNumber(ctx, value.Float())
case reflect.Bool:
jsv = JSValueMakeBoolean(ctx, value.Bool())
case reflect.String:
str := JSStringCreateWithUTF8CString(value.String())
jsv = JSValueMakeString(ctx, str)
JSStringRelease(str)
case reflect.Ptr:
return toJSValue(ctx, reflect.Indirect(value))
case reflect.Struct:
if json, err := json.Marshal(value.Interface()); err == nil {
str := JSStringCreateWithUTF8CString(string(json))
jsv = JSValueMakeFromJSONString(ctx, str)
JSStringRelease(str)
}
case reflect.Slice, reflect.Array:
rets := make([]JSValueRef, value.Len())
for i := 0; i < value.Len(); i++ {
rets[i] = toJSValue(ctx, value.Index(i))
}
arr := JSObjectMakeArray(ctx, uint(len(rets)), rets, nil)
jsv = *(*JSValueRef)(unsafe.Pointer(&arr))
default:
panic("Not implemented!")
}
if err != nil {
fmt.Println(err.Error())
return JSValueMakeNull(ctx)
2019-10-01 21:38:14 -05:00
}
2019-10-03 00:01:39 -05:00
return jsv
2019-10-01 21:38:14 -05:00
}
func addFunctionToView(view ULView, name string, callback JSObjectCallAsFunctionCallback) {
ctx := UlViewGetJSContext(view)
gobj := JSContextGetGlobalObject(ctx)
fn := JSStringCreateWithUTF8CString(name)
defer JSStringRelease(fn)
fob := JSObjectMakeFunctionWithCallback(ctx, fn, callback)
val := *(*JSValueRef)(unsafe.Pointer(&fob))
JSObjectSetProperty(ctx, gobj, fn, val, KJSPropertyAttributeNone, []JSValueRef{})
}
2019-10-07 19:29:22 -05:00
func resizeCallback(ov ULOverlay) func(userData unsafe.Pointer, width uint32, height uint32) {
return func(userData unsafe.Pointer, width uint32, height uint32) {
UlOverlayResize(ov, width, height)
}
2019-10-01 21:38:14 -05:00
}
func serveHandler(handler http.Handler) (string, error) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return "", err
}
go func() {
panic(http.Serve(ln, handler))
}()
return "http://" + ln.Addr().String(), nil
}