index.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <div id="window-label"></div>
  5. <div id="container"></div>
  6. <div id="response"></div>
  7. <script>
  8. var WebviewWindow = window.__TAURI__.window.WebviewWindow
  9. var thisTauriWindow = window.__TAURI__.window.getCurrent();
  10. var windowLabel = thisTauriWindow.label;
  11. var windowLabelContainer = document.getElementById("window-label");
  12. windowLabelContainer.innerHTML =
  13. "This is the " + windowLabel + " window.";
  14. var container = document.getElementById("container");
  15. function createWindowMessageBtn(label) {
  16. var tauriWindow = WebviewWindow.getByLabel(label)
  17. var button = document.createElement("button");
  18. button.innerHTML = "Send message to " + label;
  19. button.addEventListener("click", function () {
  20. tauriWindow.emit("clicked", "message from " + windowLabel);
  21. });
  22. container.appendChild(button);
  23. }
  24. // global listener
  25. window.__TAURI__.event.listen("clicked", function (event) {
  26. responseContainer.innerHTML += "Got " + JSON.stringify(event) + " on global listener<br><br>";
  27. })
  28. window.__TAURI__.event.listen('tauri://window-created', function (event) {
  29. createWindowMessageBtn(event.payload.label)
  30. })
  31. var responseContainer = document.getElementById("response");
  32. // listener tied to this window
  33. thisTauriWindow.listen("clicked", function (event) {
  34. responseContainer.innerHTML += "Got " + JSON.stringify(event) + " on window listener<br><br>";
  35. });
  36. var createWindowButton = document.createElement("button");
  37. createWindowButton.innerHTML = "Create window";
  38. createWindowButton.addEventListener("click", function () {
  39. var webviewWindow = new WebviewWindow(Math.random().toString());
  40. webviewWindow.once('tauri://created', function () {
  41. responseContainer.innerHTML += "Created new webview"
  42. })
  43. webviewWindow.once('tauri://error', function () {
  44. responseContainer.innerHTML += "Error creating new webview"
  45. })
  46. });
  47. container.appendChild(createWindowButton);
  48. var globalMessageButton = document.createElement("button");
  49. globalMessageButton.innerHTML = "Send global message";
  50. globalMessageButton.addEventListener("click", function () {
  51. // emit to all windows
  52. window.__TAURI__.event.emit("clicked", "message from " + windowLabel);
  53. });
  54. container.appendChild(globalMessageButton);
  55. var allWindows = window.__TAURI__.window.getAll()
  56. for (var index in allWindows) {
  57. var label = allWindows[index].label;
  58. if (label === windowLabel) {
  59. continue;
  60. }
  61. createWindowMessageBtn(label)
  62. }
  63. </script>
  64. </body>
  65. </html>