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. const WebviewWindow = window.__TAURI__.window.WebviewWindow
  16. const appWindow = window.__TAURI__.window.appWindow
  17. const windowLabel = appWindow.label
  18. const windowLabelContainer = document.getElementById('window-label')
  19. windowLabelContainer.innerText = 'This is the ' + windowLabel + ' window.'
  20. const container = document.getElementById('container')
  21. function createWindowMessageBtn(label) {
  22. const tauriWindow = WebviewWindow.getByLabel(label)
  23. const 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. const responseContainer = document.getElementById('response')
  32. window.__TAURI__.event.listen('clicked', function (event) {
  33. responseContainer.innerHTML +=
  34. 'Got ' + JSON.stringify(event) + ' on global listener\n\n'
  35. })
  36. window.__TAURI__.event.listen('tauri://window-created', function (event) {
  37. createWindowMessageBtn(event.payload.label)
  38. })
  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. const createWindowButton = document.createElement('button')
  45. createWindowButton.innerHTML = 'Create window'
  46. createWindowButton.addEventListener('click', function () {
  47. const 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. const 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. const allWindows = window.__TAURI__.window.getAll()
  69. for (const index in allWindows) {
  70. const label = allWindows[index].label
  71. if (label === windowLabel) {
  72. continue
  73. }
  74. createWindowMessageBtn(label)
  75. }
  76. </script>
  77. </body>
  78. </html>