Skip to content

Commit

Permalink
fix: file>new window working in tauri
Browse files Browse the repository at this point in the history
  • Loading branch information
abose committed Oct 31, 2023
1 parent 36858d6 commit a61db4d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 18 deletions.
43 changes: 26 additions & 17 deletions src/phoenix/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,27 @@ import initTauriShell from "./tauriShell.js";

initVFS();

let windowLabelCount = 0;
// create a unique prefix for each window so that it doesnt collide with
// new window from other phoenix windows in same app session.
const labelPrefix = `phcode-${crypto.randomUUID().split("-")[0]}`;
// We can only have a maximum of 30 windows that have access to tauri apis
// This limit is set in file `tauri.conf.json` in phoenix-desktop repo at json paths
// this limit is there due to our use of phcode:// custom protocol.
// /tauri/security/dangerousRemoteDomainIpcAccess/0/windows and
// /tauri/security/dangerousRemoteDomainIpcAccess/1/windows
let MAX_ALLOWED_TAURI_WINDOWS = 30;

async function getTauriWindowLabel() {
const tauriWindows = await window.__TAURI__.window.getAll();
const windowLabels = {};
for(let {label} of tauriWindows) {
windowLabels[label]=true;
}
for(let i=1; i<=MAX_ALLOWED_TAURI_WINDOWS; i++){
const windowLabel = `phcode-${i}`;
if(!windowLabels[windowLabel]){
return windowLabel;
}
}
throw new Error("Could not get a free window label to create tauri window");
}
Phoenix.app = {
getNodeState: function (cbfn){
cbfn(new Error('Node cannot be run in phoenix browser mode'));
Expand Down Expand Up @@ -99,22 +116,14 @@ Phoenix.app = {
.catch(reject);
});
},
openURLInPhoenixWindow: function (url, {
windowTitle, windowLabel, fullscreen, resizable,
openURLInPhoenixWindow: async function (url, {
windowTitle, fullscreen, resizable,
height, minHeight, width, minWidth, acceptFirstMouse, preferTabs
} = {}){
const defaultHeight = 900, defaultWidth = 1366;
if(window.__TAURI__){
let tauriWindow;
if(windowLabel) {
tauriWindow = window.__TAURI__.window.WebviewWindow.getByLabel(windowLabel);
if(tauriWindow) {
console.error(`An existing window already exists for windowLabel:${windowLabel}, please close window and call openURLInPhoenixWindow!`);
return tauriWindow;
}
}

tauriWindow = new window.__TAURI__.window.WebviewWindow(windowLabel || `${labelPrefix}-${windowLabelCount++}`, {
const windowLabel = await getTauriWindowLabel();
const tauriWindow = new window.__TAURI__.window.WebviewWindow(windowLabel, {
url,
title: windowTitle || windowLabel || url,
fullscreen,
Expand All @@ -136,7 +145,7 @@ Phoenix.app = {
if(preferTabs) {
features = "";
}
const nativeWindow = window.open(url, windowLabel||'_blank', features);
const nativeWindow = window.open(url, '_blank', features);
nativeWindow.isTauriWindow = false;
return nativeWindow;
},
Expand Down
13 changes: 13 additions & 0 deletions test/spec/Tauri-platform-test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test tauri apis accessible</title>
<script>
window.__TAURI__.event.emit('TAURI_API_WORKING', { data: 'Hello from Window!' });
</script>
</head>
<body>
sending event with tauri api...
</body>
</html>
37 changes: 36 additions & 1 deletion test/spec/Tauri-platform-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
*/

/*global describe, it, expect, beforeEach, afterEach, fs, path*/
/*global describe, it, expect, beforeEach, afterEach, fs, path, Phoenix*/

define(function (require, exports, module) {
if(!window.__TAURI__) {
Expand Down Expand Up @@ -94,6 +94,41 @@ define(function (require, exports, module) {
it("Should not be able to fetch files in appLocalData folder", async function () {
await testAssetNotAccessibleFolder(await window.__TAURI__.path.appLocalDataDir());
});

function createWebView() {
return new Promise((resolve, reject)=>{
let currentURL = new URL(location.href);
let pathParts = currentURL.pathname.split('/');
pathParts[pathParts.length - 1] = 'spec/Tauri-platform-test.html';
currentURL.pathname = pathParts.join('/');

let newURL = currentURL.href;
Phoenix.app.openURLInPhoenixWindow(newURL)
.then(tauriWindow =>{
expect(tauriWindow.label.startsWith("phcode-")).toBeTrue();
tauriWindow.listen('TAURI_API_WORKING', function () {
resolve(tauriWindow);
});
}).catch(reject);
});

}

it("Should be able to spawn tauri windows", async function () {
const tauriWindow = await createWebView();
await tauriWindow.close();
});

const maxWindows = 25;
it(`Should be able to spawn ${maxWindows} tauri windows`, async function () {
const tauriWindows = [];
for(let i=0; i<maxWindows; i++){
tauriWindows.push(await createWebView());
}
for(let i=0; i<maxWindows; i++){
await tauriWindows[i].close();
}
}, 120000);
});
});
});

0 comments on commit a61db4d

Please sign in to comment.