index.html 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 = 'This is the ' + windowLabel + ' window.'
  13. var container = document.getElementById('container')
  14. function createWindowMessageBtn(label) {
  15. var tauriWindow = WebviewWindow.getByLabel(label)
  16. var button = document.createElement('button')
  17. button.innerHTML = 'Send message to ' + label
  18. button.addEventListener('click', function () {
  19. tauriWindow.emit('clicked', 'message from ' + windowLabel)
  20. })
  21. container.appendChild(button)
  22. }
  23. // global listener
  24. window.__TAURI__.event.listen('clicked', function (event) {
  25. responseContainer.innerHTML +=
  26. '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 +=
  35. 'Got ' + JSON.stringify(event) + ' on window listener<br><br>'
  36. })
  37. var createWindowButton = document.createElement('button')
  38. createWindowButton.innerHTML = 'Create window'
  39. createWindowButton.addEventListener('click', function () {
  40. var webviewWindow = new WebviewWindow(Math.random().toString())
  41. webviewWindow.once('tauri://created', function () {
  42. responseContainer.innerHTML += 'Created new webview'
  43. })
  44. webviewWindow.once('tauri://error', function () {
  45. responseContainer.innerHTML += 'Error creating new webview'
  46. })
  47. })
  48. container.appendChild(createWindowButton)
  49. var globalMessageButton = document.createElement('button')
  50. globalMessageButton.innerHTML = 'Send global message'
  51. globalMessageButton.addEventListener('click', function () {
  52. // emit to all windows
  53. window.__TAURI__.event.emit('clicked', 'message from ' + windowLabel)
  54. })
  55. container.appendChild(globalMessageButton)
  56. var allWindows = window.__TAURI__.window.getAll()
  57. for (var index in allWindows) {
  58. var label = allWindows[index].label
  59. if (label === windowLabel) {
  60. continue
  61. }
  62. createWindowMessageBtn(label)
  63. }
  64. </script>
  65. </body>
  66. </html>