refactor: refactor parent APIs on WindowBuilder (#8622)

* refactor: refactor parent APIs on `WindowBuilder`

closes #8587 #1643

* fix build

* clippy

* support parent in JS and config

* change files

* fix build

* clippy

* fix doctests

* fix linux build

* fix doctests

* update docs

* fix api, update example to use JS API

* fix merge

* lint

* fix tests on windows

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
Amr Bashir
2024-01-31 18:59:14 +02:00
committed by GitHub
parent a2fc3a6357
commit 9eaeb5a8cd
28 changed files with 465 additions and 151 deletions

View File

@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
const WebviewWindow = window.__TAURI__.window.WebviewWindow
const WebviewWindow = window.__TAURI__.webview.WebviewWindow
const routeSelect = document.querySelector('#route')
const link = document.querySelector('#link')

View File

@@ -1,54 +1,59 @@
<!DOCTYPE html>
<html>
<head>
<style>
#response {
white-space: pre-wrap;
}
</style>
</head>
<body>
<div id="window-label"></div>
<div id="container"></div>
<div id="response"></div>
<head>
<style>
#response {
white-space: pre-wrap;
}
</style>
</head>
<script>
const WebviewWindow = window.__TAURI__.window.WebviewWindow
const thisTauriWindow = window.__TAURI__.window.getCurrent()
const windowLabel = thisTauriWindow.label
const windowLabelContainer = document.getElementById('window-label')
windowLabelContainer.innerText = 'This is the ' + windowLabel + ' window.'
<body>
<div id="window-label"></div>
<div id="container"></div>
<div id="response"></div>
const container = document.getElementById('container')
<script>
const WebviewWindow = window.__TAURI__.webview.WebviewWindow
const thisTauriWindow = window.__TAURI__.window.getCurrent()
const windowLabel = thisTauriWindow.label
const windowLabelContainer = document.getElementById('window-label')
windowLabelContainer.innerText = 'This is the ' + windowLabel + ' window.'
const responseContainer = document.getElementById('response')
function runCommand(commandName, args, optional) {
window.__TAURI__.core
.invoke(commandName, args)
.then((response) => {
responseContainer.innerText += `Ok(${response})\n\n`
})
.catch((error) => {
responseContainer.innerText += `Err(${error})\n\n`
})
}
window.__TAURI__.event.listen('tauri://window-created', function (event) {
responseContainer.innerText += 'Got window-created event\n\n'
})
const container = document.getElementById('container')
const createWindowButton = document.createElement('button')
const windowId = Math.random().toString().replace('.', '')
const windowNumber = 1
createWindowButton.innerHTML = 'Create child window ' + windowNumber
createWindowButton.addEventListener('click', function () {
runCommand('create_child_window', {
id: `child-${windowId}-${windowNumber}`
const responseContainer = document.getElementById('response')
function runCommand(commandName, args, optional) {
window.__TAURI__.core
.invoke(commandName, args)
.then((response) => {
responseContainer.innerText += `Ok(${response})\n\n`
})
windowNumber += 1
createWindowButton.innerHTML = 'Create child window ' + windowNumber
.catch((error) => {
responseContainer.innerText += `Err(${error})\n\n`
})
}
window.__TAURI__.event.listen('tauri://window-created', function (event) {
responseContainer.innerText += 'Got window-created event\n\n'
})
const createWindowButton = document.createElement('button')
const windowId = Math.random().toString().replace('.', '')
let windowNumber = 1
createWindowButton.innerHTML = 'Create child window ' + windowNumber
createWindowButton.addEventListener('click', function () {
new WebviewWindow(`child-${windowId}-${windowNumber}`, {
title: 'Child',
width: 400,
height: 300,
parent: thisTauriWindow
})
container.appendChild(createWindowButton)
</script>
</body>
</html>
windowNumber += 1
createWindowButton.innerHTML = 'Create child window ' + windowNumber
})
container.appendChild(createWindowButton)
</script>
</body>
</html>

View File

@@ -4,23 +4,32 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use tauri::{command, webview::PageLoadEvent, WebviewUrl, WebviewWindowBuilder, Window};
#[command]
async fn create_child_window(id: String, window: Window) {
let builder = WebviewWindowBuilder::new(&window, &id, WebviewUrl::default())
.title("Child")
.inner_size(400.0, 300.0);
#[cfg(target_os = "macos")]
let builder = builder.parent_window(window.ns_window().unwrap());
#[cfg(windows)]
let builder = builder.parent_window(window.hwnd().unwrap());
let _webview = builder.build().unwrap();
}
use tauri::{webview::PageLoadEvent, WebviewUrl, WebviewWindowBuilder};
use tauri_utils::acl::{
resolved::{CommandKey, ResolvedCommand},
ExecutionContext,
};
fn main() {
let mut context = tauri::generate_context!("../../examples/parent-window/tauri.conf.json");
for cmd in [
"plugin:event|listen",
"plugin:webview|create_webview_window",
"plugin:window|internal_on_mousemove",
"plugin:window|internal_on_mousedown",
] {
context.resolved_acl().allowed_commands.insert(
CommandKey {
name: cmd.into(),
context: ExecutionContext::Local,
},
ResolvedCommand {
windows: vec!["*".parse().unwrap()],
..Default::default()
},
);
}
tauri::Builder::default()
.on_page_load(|webview, payload| {
if payload.event() == PageLoadEvent::Finished {
@@ -30,7 +39,6 @@ fn main() {
});
}
})
.invoke_handler(tauri::generate_handler![create_child_window])
.setup(|app| {
let _webview = WebviewWindowBuilder::new(app, "main", WebviewUrl::default())
.title("Main")
@@ -39,8 +47,6 @@ fn main() {
Ok(())
})
.run(tauri::generate_context!(
"../../examples/parent-window/tauri.conf.json"
))
.run(context)
.expect("failed to run tauri application");
}