diff options
Diffstat (limited to 'sdk')
-rw-r--r-- | sdk/host.js | 42 | ||||
-rw-r--r-- | sdk/keyboard.js | 15 | ||||
-rw-r--r-- | sdk/main.js | 32 |
3 files changed, 89 insertions, 0 deletions
diff --git a/sdk/host.js b/sdk/host.js new file mode 100644 index 0000000..4d76832 --- /dev/null +++ b/sdk/host.js @@ -0,0 +1,42 @@ +AlicornSDK = {}; +AlicornSDKInternal = {}; + +class AlicornSDKError extends Error { + constructor(props) { + super(props); + } +} + +AlicornSDKInternal["checkWindow"] = (w) => { + if (w === null) { + throw new AlicornSDKError("Cannot make SDK call before window is initialized"); + } +} + +AlicornSDKInternal["resolveWindow"] = (w) => { + let wr = null + wr = WindowManager.stack.filter(i => i.id.substring(7).split("-app-")[0] === w)[0] + + return wr; +} + +AlicornSDK["close"] = (_window) => { + AlicornSDKInternal.checkWindow(_window); + let w = AlicornSDKInternal.resolveWindow(_window); + + WindowManager.queueClose(w); +} + +AlicornSDK["ready"] = (_window) => { + AlicornSDKInternal.checkWindow(_window); + let w = AlicornSDKInternal.resolveWindow(_window); + + w.classList.remove("loading"); +} + +AlicornSDK["keyboard"] = (_window, data) => { + AlicornSDKInternal.checkWindow(_window); + let w = AlicornSDKInternal.resolveWindow(_window); + + KeyboardShortcutHandler(data); +}
\ No newline at end of file diff --git a/sdk/keyboard.js b/sdk/keyboard.js new file mode 100644 index 0000000..06a0623 --- /dev/null +++ b/sdk/keyboard.js @@ -0,0 +1,15 @@ +document.addEventListener('keyup', (e) => { + processKey = true; + if (e.key === "Ctrl" || e.key === "Alt" || e.key === "Shift" || e.key === "Meta") { + if (keyPressShortcut) { + processKey = false; + keyPressShortcut = (e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) || (!e.ctrlKey && e.altKey && !e.shiftKey && !e.metaKey) || (!e.ctrlKey && !e.altKey && e.shiftKey && !e.metaKey) || (!e.ctrlKey && !e.altKey && !e.shiftKey && e.metaKey); + } + } else { + keyPressShortcut = e.ctrlKey || e.altKey || e.shiftKey || e.metaKey; + } + if (processKey) { + shortcut = (e.ctrlKey ? "Control+" : "") + (e.altKey ? "Alt+" : "") + (e.shiftKey ? "Shift+" : "") + (e.metaKey ? "Meta+" : "") + e.key; + alicorn.keyboard(shortcut); + } +})
\ No newline at end of file diff --git a/sdk/main.js b/sdk/main.js new file mode 100644 index 0000000..e0f571c --- /dev/null +++ b/sdk/main.js @@ -0,0 +1,32 @@ +console.log("Alicorn SDK") +const { contextBridge, ipcRenderer } = require('electron'); +global.windowId = null; + +let host = require('@electron/remote').getCurrentWindow().webContents.executeJavaScript; + +ipcRenderer.on('window-id', (event, data) => { + console.log("[SDK] Alicorn Window ID: " + data); + contextBridge.exposeInMainWorld('AlicornWindowID', data); + global.windowId = data; +}) + +contextBridge.exposeInMainWorld('AlicornRoot', require('path').dirname(__dirname)); +contextBridge.exposeInMainWorld('AlicornKeyboardHandler', require('fs').readFileSync("sdk/keyboard.js").toString()); + +contextBridge.exposeInMainWorld('AlicornSDK', { + init: async () => { + let api = {}; + + console.log("[SDK] Establishing features set... This may take a while."); + let features = await host("Object.keys(AlicornSDK)"); + console.log("[SDK] Established features set"); + + for (let feature of features) { + api[feature] = (options) => { + return host("AlicornSDK[" + JSON.stringify(feature) + "](\"" + windowId + "\", " + JSON.stringify(options) + ");"); + }; + } + + return api; + } +})
\ No newline at end of file |