|
| 1 | +// To run this script: |
| 2 | +// deno run --allow-read --allow-write --allow-run --allow-net --allow-ffi custom_web_server.ts |
| 3 | + |
| 4 | +// To import from local (Debugging and Development) |
| 5 | +import { WebUI } from "../../mod.ts"; |
| 6 | + |
| 7 | +// To import from online `https://deno.land` (Production) |
| 8 | +// import { WebUI } from "https://deno.land/x/[email protected]/mod.ts"; |
| 9 | + |
| 10 | +async function allEvents(e: WebUI.Event) { |
| 11 | + /* |
| 12 | + e.window: WebUI; |
| 13 | + e.eventType: WebUI.EventType; |
| 14 | + e.element: string; |
| 15 | + */ |
| 16 | + console.log(`\nallEvents: window = '${e.window}'`); |
| 17 | + console.log(`allEvents: eventType = '${e.eventType}'`); |
| 18 | + console.log(`allEvents: element = '${e.element}'`); |
| 19 | + switch (e.eventType) { |
| 20 | + case WebUI.EventType.Disconnected: |
| 21 | + // Window disconnection event |
| 22 | + console.log(`Window closed.`); |
| 23 | + break; |
| 24 | + case WebUI.EventType.Connected: |
| 25 | + // Window connection event |
| 26 | + console.log(`Window connected.`); |
| 27 | + break; |
| 28 | + case WebUI.EventType.MouseClick: |
| 29 | + // Mouse click event |
| 30 | + console.log(`Mouse click.`); |
| 31 | + break; |
| 32 | + case WebUI.EventType.Navigation: |
| 33 | + // Window navigation event |
| 34 | + const url = e.arg.string(0); |
| 35 | + console.log(`Navigation to '${url}'`); |
| 36 | + // Because we used `webui_bind(MyWindow, "", events);` |
| 37 | + // WebUI will block all `href` link clicks and sent here instead. |
| 38 | + // We can then control the behaviour of links as needed. |
| 39 | + e.window.navigate(url); |
| 40 | + break; |
| 41 | + case WebUI.EventType.Callback: |
| 42 | + // Function call event |
| 43 | + console.log(`Function call.`); |
| 44 | + break; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +async function myBackendFunc(e: WebUI.Event) { |
| 49 | + const a = e.arg.number(0); // First argument |
| 50 | + const b = e.arg.number(1); // Second argument |
| 51 | + const c = e.arg.number(2); // Third argument |
| 52 | + console.log(`\nFirst argument: ${a}`); |
| 53 | + console.log(`Second argument: ${b}`); |
| 54 | + console.log(`Third argument: ${c}`); |
| 55 | +} |
| 56 | + |
| 57 | +// Create new window |
| 58 | +const myWindow = new WebUI(); |
| 59 | + |
| 60 | +// Bind All Events |
| 61 | +myWindow.bind("", allEvents); |
| 62 | + |
| 63 | +// Bind Backend Function |
| 64 | +myWindow.bind("myBackendFunc", myBackendFunc); |
| 65 | + |
| 66 | +// Bind Exit Function |
| 67 | +myWindow.bind("exit", () => { |
| 68 | + // Close all windows and exit |
| 69 | + WebUI.exit(); |
| 70 | +}); |
| 71 | + |
| 72 | +// Set the web-server/WebSocket port that WebUI should |
| 73 | +// use. This means `webui.js` will be available at: |
| 74 | +// http://localhost:MY_PORT_NUMBER/webui.js |
| 75 | +myWindow.setPort(8081); |
| 76 | + |
| 77 | +// Show a new window and point to our custom web server |
| 78 | +// Assuming the custom web server is running on port |
| 79 | +// 8080... |
| 80 | +myWindow.show("http://localhost:8080/"); |
| 81 | + |
| 82 | +// Wait until all windows get closed |
| 83 | +await WebUI.wait(); |
| 84 | + |
| 85 | +console.log("Thank you."); |
0 commit comments