index.html 2.7 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. const WebviewWindow = window.__TAURI__.webview.WebviewWindow
  16. const appWindow = window.__TAURI__.window.getCurrent()
  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 button = document.createElement('button')
  23. button.innerText = 'Send message to ' + label
  24. button.addEventListener('click', function () {
  25. appWindow.emitTo(label, 'clicked', 'message from ' + windowLabel)
  26. })
  27. container.appendChild(button)
  28. }
  29. // global listener
  30. const responseContainer = document.getElementById('response')
  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://webview-created', function (event) {
  36. createWindowMessageBtn(event.payload.label)
  37. })
  38. // listener tied to this window
  39. appWindow.listen('clicked', function (event) {
  40. responseContainer.innerText +=
  41. 'Got ' + JSON.stringify(event) + ' on window listener\n\n'
  42. })
  43. const createWindowButton = document.createElement('button')
  44. createWindowButton.innerHTML = 'Create window'
  45. createWindowButton.addEventListener('click', function () {
  46. const id = Math.random().toString().replace('.', '')
  47. const webview = new WebviewWindow(id, { tabbingIdentifier: windowLabel })
  48. webview.once('tauri://created', function () {
  49. responseContainer.innerHTML += 'Created new window'
  50. })
  51. webview.once('tauri://error', function (e) {
  52. responseContainer.innerHTML += 'Error creating new window ' + e.payload
  53. })
  54. })
  55. container.appendChild(createWindowButton)
  56. const globalMessageButton = document.createElement('button')
  57. globalMessageButton.innerHTML = 'Send global message'
  58. globalMessageButton.addEventListener('click', function () {
  59. // emit to all windows
  60. appWindow.emit('clicked', 'message from ' + windowLabel)
  61. })
  62. container.appendChild(globalMessageButton)
  63. const allWindows = window.__TAURI__.window.getAll()
  64. for (const index in allWindows) {
  65. const label = allWindows[index].label
  66. if (label === windowLabel) {
  67. continue
  68. }
  69. createWindowMessageBtn(label)
  70. }
  71. </script>
  72. </body>
  73. </html>