tauri/examples/multiwindow/index.html

88 lines
2.9 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html>
2022-11-03 15:27:43 +03:00
<head>
<style>
#response {
white-space: pre-wrap;
}
</style>
</head>
2022-11-03 15:27:43 +03:00
<body>
<div id="window-label"></div>
<div id="container"></div>
<div id="response"></div>
2022-11-03 15:27:43 +03:00
<script>
const WebviewWindow = window.__TAURI__.window.WebviewWindow
const appWindow = window.__TAURI__.window.appWindow
const windowLabel = appWindow.label
const windowLabelContainer = document.getElementById('window-label')
2022-11-03 15:27:43 +03:00
windowLabelContainer.innerText = 'This is the ' + windowLabel + ' window.'
const container = document.getElementById('container')
2022-11-03 15:27:43 +03:00
function createWindowMessageBtn(label) {
const tauriWindow = WebviewWindow.getByLabel(label)
const button = document.createElement('button')
2022-11-03 15:27:43 +03:00
button.innerText = 'Send message to ' + label
button.addEventListener('click', function () {
tauriWindow.emit('clicked', 'message from ' + windowLabel)
})
container.appendChild(button)
}
2022-11-03 15:27:43 +03:00
// global listener
const responseContainer = document.getElementById('response')
2022-11-03 15:27:43 +03:00
window.__TAURI__.event.listen('clicked', function (event) {
responseContainer.innerHTML +=
'Got ' + JSON.stringify(event) + ' on global listener\n\n'
})
window.__TAURI__.event.listen('tauri://window-created', function (event) {
createWindowMessageBtn(event.payload.label)
})
2022-11-03 15:27:43 +03:00
// listener tied to this window
appWindow.listen('clicked', function (event) {
responseContainer.innerText +=
'Got ' + JSON.stringify(event) + ' on window listener\n\n'
})
2022-11-03 15:27:43 +03:00
const createWindowButton = document.createElement('button')
2022-11-03 15:27:43 +03:00
createWindowButton.innerHTML = 'Create window'
createWindowButton.addEventListener('click', function () {
const webviewWindow = new WebviewWindow(
2022-11-03 15:27:43 +03:00
Math.random().toString().replace('.', ''),
{
tabbingIdentifier: windowLabel
}
)
webviewWindow.once('tauri://created', function () {
responseContainer.innerHTML += 'Created new webview'
})
webviewWindow.once('tauri://error', function (e) {
responseContainer.innerHTML += 'Error creating new webview'
})
})
2022-11-03 15:27:43 +03:00
container.appendChild(createWindowButton)
const globalMessageButton = document.createElement('button')
2022-11-03 15:27:43 +03:00
globalMessageButton.innerHTML = 'Send global message'
globalMessageButton.addEventListener('click', function () {
// emit to all windows
window.__TAURI__.event.emit('clicked', 'message from ' + windowLabel)
})
container.appendChild(globalMessageButton)
const allWindows = window.__TAURI__.window.getAll()
for (const index in allWindows) {
const label = allWindows[index].label
2022-11-03 15:27:43 +03:00
if (label === windowLabel) {
continue
}
createWindowMessageBtn(label)
}
2022-11-03 15:27:43 +03:00
</script>
</body>
</html>