index.html 2.9 KB

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