index.html 2.8 KB

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