index.html 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 windowLabel = window.__TAURI__.window.getCurrentWindow().label;
  9. var windowLabelContainer = document.getElementById("window-label");
  10. windowLabelContainer.innerHTML =
  11. "This is the " + windowLabel + " window.";
  12. var container = document.getElementById("container");
  13. function createWindowMessageBtn(label) {
  14. var tauriWindow = window.__TAURI__.window.getTauriWindow(label)
  15. var button = document.createElement("button");
  16. button.innerHTML = "Send message to " + label;
  17. button.addEventListener("click", function () {
  18. tauriWindow.emit("clicked", "message from " + windowLabel);
  19. });
  20. container.appendChild(button);
  21. }
  22. // global listener
  23. window.__TAURI__.event.listen("clicked", function (event) {
  24. responseContainer.innerHTML += "Got " + JSON.stringify(event) + " on global listener<br><br>";
  25. })
  26. window.__TAURI__.event.listen('tauri://window-created', function (event) {
  27. createWindowMessageBtn(event.payload.label)
  28. })
  29. var responseContainer = document.getElementById("response");
  30. var thisTauriWindow = window.__TAURI__.window.getTauriWindow()
  31. // listener tied to this window
  32. thisTauriWindow.listen("clicked", function (event) {
  33. responseContainer.innerHTML += "Got " + JSON.stringify(event) + " on window listener<br><br>";
  34. });
  35. var createWindowButton = document.createElement("button");
  36. createWindowButton.innerHTML = "Create window";
  37. createWindowButton.addEventListener("click", function () {
  38. window.__TAURI__.window.createWindow(Math.random().toString());
  39. });
  40. container.appendChild(createWindowButton);
  41. var globalMessageButton = document.createElement("button");
  42. globalMessageButton.innerHTML = "Send global message";
  43. globalMessageButton.addEventListener("click", function () {
  44. // emit to all windows
  45. window.__TAURI__.event.emit("clicked", "message from " + windowLabel);
  46. });
  47. container.appendChild(globalMessageButton);
  48. for (var index in window.__TAURI__.window.getWindows()) {
  49. var label = window.__TAURI__.window.getWindows()[index].label;
  50. if (label === windowLabel) {
  51. continue;
  52. }
  53. createWindowMessageBtn(label)
  54. }
  55. </script>
  56. </body>
  57. </html>