1 module awebview.gui.methodhandler; 2 3 import awebview.wrapper.jsobject, 4 awebview.wrapper.weakref; 5 6 7 struct JSMethodTag { wstring name; } 8 9 10 class MethodHandler : JSMethodHandler 11 { 12 void set(JSObj)(JSObj obj) 13 { 14 foreach(name; __traits(allMembers, JSObj)){ 15 static if(is(typeof(mixin(`&obj.` ~ name)) == delegate)) 16 foreach(attr; __traits(getAttributes, mixin(`JSObj.` ~ name))) 17 static if(is(typeof(attr) : JSMethodTag)) 18 set(obj.jsobject.get, attr.name, mixin(`&obj.` ~ name)); 19 } 20 } 21 22 23 void set(ref JSObject obj, wstring name, Dlg dg) 24 { 25 WebString str = name; 26 27 if(!obj.hasMethod(str)) 28 obj.setCustomMethod(str, false); 29 30 assert(obj.hasMethod(str)); 31 _table[obj.remoteId][name] = dg; 32 } 33 34 35 void set(ref JSObject obj, wstring name, DlgRV dg) 36 { 37 WebString str = name; 38 39 if(!obj.hasMethod(str)) 40 obj.setCustomMethod(str, true); 41 42 assert(obj.hasMethod(str)); 43 _tableRV[obj.remoteId][name] = dg; 44 } 45 46 47 void release(JSObj)(JSObj obj) 48 { 49 release(obj.jsobject.remoteId); 50 } 51 52 53 void release(uint objRemoteId) 54 { 55 _table.remove(objRemoteId); 56 _tableRV.remove(objRemoteId); 57 } 58 59 60 override 61 void onCall(awebview.wrapper.cpp.WebView view, 62 uint objId, WeakRef!(const(WebStringCpp)) methodName, 63 WeakRef!(const(JSArrayCpp)) args) 64 { 65 if(auto p = objId in _table) 66 if(auto q = methodName.data in *p) 67 (*q)(args); 68 } 69 70 71 override 72 JSValue onCallWithRV( 73 awebview.wrapper.cpp.WebView view, 74 uint objId, WeakRef!(const(WebStringCpp)) methodName, 75 WeakRef!(const(JSArrayCpp)) args) 76 { 77 if(auto p = objId in _tableRV) 78 if(auto q = methodName.data in *p) 79 return (*q)(args); 80 81 JSValue v = JSValue(JSValue.null_); 82 return v; 83 } 84 85 86 private: 87 alias Dlg = void delegate(WeakRef!(const(JSArrayCpp))); 88 alias DlgRV = JSValue delegate(WeakRef!(const(JSArrayCpp))); 89 90 Dlg[wstring][uint] _table; 91 DlgRV[wstring][uint] _tableRV; 92 }