|
| 1 | +using System; |
| 2 | +using System.Collections.Concurrent; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Runtime.CompilerServices; |
| 8 | +using System.Text; |
| 9 | +using System.Threading; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using Newtonsoft.Json; |
| 12 | + |
| 13 | +namespace WebUI |
| 14 | +{ |
| 15 | + |
| 16 | + public class DomEvent |
| 17 | + { |
| 18 | + |
| 19 | + public string currentTargetId { get; set; } |
| 20 | + public string originalTargetId { get; set; } |
| 21 | + [JsonConverter(typeof(MilisecondEpochConverter))] |
| 22 | + public DateTime timestamp { get; set; } |
| 23 | + public string type { get; set; } |
| 24 | + public Dictionary<string, string> additionalProps; //map of additional property values requested |
| 25 | + |
| 26 | + } |
| 27 | + public enum CommonEventTypes { load, unload, error, resize, scroll, focus, blur, click, dblclick, mousedown, mouseup, mouseover, mouseout, mousemove, input, keydown, keypress, keyup, submit, change, DOMNodeInserted, DOMNodeRemoved, DOMSubtreeModified, DOMNodeInsertedIntoDocument, DOMNodeRemovedFromDocument, DOMContentLoaded, hashchange, beforeunload } |
| 28 | + public static class WebUINetUI |
| 29 | + { |
| 30 | + |
| 31 | + public class EventOpts |
| 32 | + { |
| 33 | + public bool capture { get; set; } |
| 34 | + public bool once { get; set; } |
| 35 | + public string abortKey { get; set; } |
| 36 | + |
| 37 | + } |
| 38 | + private const int startId = 5432; |
| 39 | + private static volatile int curFuncId = startId; |
| 40 | + // CommonEventTypes |
| 41 | + public static void AddEventListener(this Window window, Action<DomEvent> OnFired, CommonEventTypes eventType, String domId, EventOpts additionalOpts = null, params KeyValuePair<string, string>[] additionalEventCaptureProps) => window.AddEventListener(OnFired, eventType.ToString(), domId, additionalOpts, additionalEventCaptureProps); |
| 42 | + |
| 43 | + public static void AddEventListener(this Window window, Action<DomEvent> OnFired, String eventType, String domId, EventOpts additionalOpts = null, params KeyValuePair<string, string>[] additionalEventCaptureProps) |
| 44 | + { |
| 45 | + |
| 46 | + //addCSEventListener(type, elementID, csFuncID, additionalProps, options, abortKey) |
| 47 | + var addlDict = additionalEventCaptureProps?.Length > 0 ? |
| 48 | + additionalEventCaptureProps.ToDictionary(x => x.Key, x => x.Value) : null; |
| 49 | + var abortKey = additionalOpts?.abortKey; |
| 50 | + if (abortKey != null) |
| 51 | + additionalOpts.abortKey = null; |
| 52 | + var curId = window._RegisterBoundFunction(OnCalled: (str) => OnFired(Newtonsoft.Json.JsonConvert.DeserializeObject<DomEvent[]>(str)[0]), registerFunc: false); |
| 53 | + window.InvokeJavascriptMethod($"{ourJSClass}.addCSEventListener", eventType, domId, curId, addlDict, additionalOpts, abortKey); |
| 54 | + } |
| 55 | + private static async void JavascriptFuncCallback(this Window window, int csFuncId, int jsCallId, string jsonOfArgs) |
| 56 | + { |
| 57 | + if (!csFuncIdToFired.TryGetValue(csFuncId, out var info)) |
| 58 | + throw new Exception($"The JS func callback handler was passed an invalid function id {csFuncId}"); ; |
| 59 | + if (info.normFunc != null) |
| 60 | + { |
| 61 | + Window.UseSpecificDispatcher.InvokeWithVoid(() => info.normFunc(jsonOfArgs)); |
| 62 | + return; |
| 63 | + } |
| 64 | + var res = await Window.UseSpecificDispatcher.InvokeWithReturnType(() => info.taskFunc(jsonOfArgs)); |
| 65 | + |
| 66 | + await window.ScriptEvaluateMethod<string>($"{ourJSClass}.setCSFunctionResult", jsCallId, res);//we don't use invoke so we can get the error |
| 67 | + //window.InvokeJavascriptMethod("setCSFunctionResult", jsCallId, res); |
| 68 | + |
| 69 | + } |
| 70 | + private const string ourJSClass = $"window.WebUINet"; |
| 71 | + public static void SetDebug(this Window window, bool debugging = true) |
| 72 | + { |
| 73 | + var jsDebug = debugging ? "true" : "false"; |
| 74 | + window.InvokeJavaScript($@"{ourJSClass}.DEBUG_MODE={jsDebug}; |
| 75 | +window.WebuiBridge.setLogging({jsDebug}); |
| 76 | +"); |
| 77 | + } |
| 78 | + public static void RegisterBoundFunction(this Window window, String RegisteredName, Func<string, Task<string>> OnCalled) => window._RegisterBoundFunction(RegisteredName, OnCalledTask: OnCalled); |
| 79 | + public static void RegisterBoundFunction(this Window window, String RegisteredName, Action<string> OnCalled) => window._RegisterBoundFunction(RegisteredName, OnCalled); |
| 80 | + |
| 81 | + private static int _RegisterBoundFunction(this Window window, String RegisteredName = null, Action<string> OnCalled = null, Func<string, Task<string>> OnCalledTask = null, bool registerFunc = true) |
| 82 | + { |
| 83 | + var curId = Interlocked.Increment(ref curFuncId); |
| 84 | + csFuncIdToFired[curId] = new RegisteredFunc { taskFunc = OnCalledTask, normFunc = OnCalled }; |
| 85 | + if (registerFunc) |
| 86 | + { |
| 87 | + //we don't use invoke so we can get the error |
| 88 | + window.ScriptEvaluateMethod<string>($"{ourJSClass}.addCSFunction", curId, RegisteredName, OnCalledTask != null); //not awaiting but exception will at least show in logs just no ST |
| 89 | + //window.InvokeJavascriptMethod("WebUINet.addCSFunction", curId, RegisteredName, OnCalledTask != null); |
| 90 | + } |
| 91 | + if (curId == startId + 1) |
| 92 | + { |
| 93 | + window.RegisterEventHandler(RawFuncCallback, "webuiNet_Callback"); |
| 94 | + } |
| 95 | + return curId; |
| 96 | + } |
| 97 | + public static void InitOurBridge(this Window window) |
| 98 | + { |
| 99 | + OurJSBytes = Encoding.UTF8.GetBytes(File.ReadAllText("webui_net.js")); |
| 100 | + window.SetFileHandler((path) => path == "/webui_net.js" ? OurJSBytes : null); |
| 101 | + } |
| 102 | + private static byte[] OurJSBytes; |
| 103 | + private static object RawFuncCallback(WebUI.Events.Event evt, string element, ulong handlerId) |
| 104 | + { |
| 105 | + evt.Window.JavascriptFuncCallback((int)evt.GetNumber(0), (int)evt.GetNumber(1), evt.GetString(2)); |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + private class RegisteredFunc |
| 110 | + { |
| 111 | + public Func<string, Task<string>> taskFunc; |
| 112 | + public Action<string> normFunc; |
| 113 | + } |
| 114 | + private static ConcurrentDictionary<int, RegisteredFunc> csFuncIdToFired = new(); |
| 115 | + } |
| 116 | +} |
0 commit comments