index.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. tabbingIdentifier: windowLabel
  51. }
  52. )
  53. webviewWindow.once('tauri://created', function () {
  54. responseContainer.innerHTML += 'Created new webview'
  55. })
  56. webviewWindow.once('tauri://error', function (e) {
  57. responseContainer.innerHTML += 'Error creating new webview'
  58. })
  59. })
  60. container.appendChild(createWindowButton)
  61. var globalMessageButton = document.createElement('button')
  62. globalMessageButton.innerHTML = 'Send global message'
  63. globalMessageButton.addEventListener('click', function () {
  64. // emit to all windows
  65. window.__TAURI__.event.emit('clicked', 'message from ' + windowLabel)
  66. })
  67. container.appendChild(globalMessageButton)
  68. var allWindows = window.__TAURI__.window.getAll()
  69. for (var index in allWindows) {
  70. var label = allWindows[index].label
  71. if (label === windowLabel) {
  72. continue
  73. }
  74. createWindowMessageBtn(label)
  75. }
  76. </script>
  77. </body>
  78. </html>