Skip to content

Commit 7f72b4e

Browse files
committed
refactor: unified electron and tauri platfrom test working
1 parent b711f36 commit 7f72b4e

File tree

5 files changed

+284
-532
lines changed

5 files changed

+284
-532
lines changed

src/phoenix/shell.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,25 @@ async function _getTauriWindowLabel(prefix) {
6666
throw new Error("Could not get a free window label to create tauri window");
6767
}
6868

69+
/**
70+
* Opens a URL in a new Phoenix window. Works across all platforms (Tauri, Electron, browser).
71+
*
72+
* @param {string} url - The URL to open in the new window
73+
* @param {Object} [options] - Window configuration options
74+
* @param {string} [options.windowTitle] - Title for the window (defaults to label or URL)
75+
* @param {boolean} [options.fullscreen] - Whether to open in fullscreen mode
76+
* @param {boolean} [options.resizable=true] - Whether the window is resizable
77+
* @param {number} [options.height=900] - Window height in pixels
78+
* @param {number} [options.minHeight=600] - Minimum window height in pixels
79+
* @param {number} [options.width=1366] - Window width in pixels
80+
* @param {number} [options.minWidth=800] - Minimum window width in pixels
81+
* @param {boolean} [options.acceptFirstMouse=true] - (Tauri only) Accept first mouse click
82+
* @param {boolean} [options.preferTabs] - (Browser only) Prefer opening in a new tab
83+
* @param {string} [options._prefixPvt] - Internal: window label prefix
84+
* @returns {Promise<{label: string, isNativeWindow: boolean}>} Window object with `label` and `isNativeWindow` properties.
85+
* - In Tauri/Electron: `{ label: string, isNativeWindow: true }` (Tauri returns WebviewWindow instance with these props)
86+
* - In browser: Returns window.open() result with `isNativeWindow: false`
87+
*/
6988
async function openURLInPhoenixWindow(url, {
7089
windowTitle, fullscreen, resizable,
7190
height, minHeight, width, minWidth, acceptFirstMouse, preferTabs, _prefixPvt = PHOENIX_EXTENSION_WINDOW_PREFIX
@@ -242,6 +261,27 @@ Phoenix.app = {
242261
return window.electronAPI.focusWindow();
243262
}
244263
},
264+
/**
265+
* Closes a window by its label. Returns true if window was found and closed, false otherwise.
266+
* @param {string} label - The window label to close
267+
* @return {Promise<boolean>}
268+
*/
269+
closeWindowByLabel: async function (label) {
270+
if(!Phoenix.isNativeApp){
271+
throw new Error("closeWindowByLabel is not supported in browsers");
272+
}
273+
if(window.__TAURI__){
274+
const win = window.__TAURI__.window.WebviewWindow.getByLabel(label);
275+
if(win){
276+
await win.close();
277+
return true;
278+
}
279+
return false;
280+
} else if(window.__ELECTRON__){
281+
return window.electronAPI.closeWindowByLabel(label);
282+
}
283+
return false;
284+
},
245285
/**
246286
* Gets the commandline argument in desktop builds and null in browser builds.
247287
* Will always return CLI of the current process only.

test/UnitTestSuite.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ define(function (require, exports, module) {
2323

2424
require("spec/Phoenix-platform-test");
2525
require("spec/Tauri-platform-test");
26-
require("spec/Electron-platform-test");
2726
require("spec/trust-ring-test");
2827
require("spec/utframework-suite-test");
2928
require("spec/Async-test");

test/spec/Electron-platform-test.html

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,10 @@
44
<meta charset="UTF-8">
55
<title>Test electron apis accessible</title>
66
<script>
7-
const ELECTRON_TEST_SIGNAL_KEY = 'ELECTRON_PLATFORM_TEST_SIGNAL';
8-
97
async function init() {
108
if (window.electronAPI) {
11-
// Get our window label
12-
const myLabel = await window.electronAPI.getCurrentWindowLabel();
13-
14-
// Signal to parent that we're ready
15-
await window.electronAPI.putItem(ELECTRON_TEST_SIGNAL_KEY, myLabel);
16-
17-
// Poll for close signal from parent
18-
const pollInterval = setInterval(async () => {
19-
try {
20-
const items = await window.electronAPI.getAllItems();
21-
if (items && items[`${ELECTRON_TEST_SIGNAL_KEY}_CLOSE_${myLabel}`]) {
22-
clearInterval(pollInterval);
23-
// Clear the close signal
24-
await window.electronAPI.putItem(`${ELECTRON_TEST_SIGNAL_KEY}_CLOSE_${myLabel}`, null);
25-
// Close ourselves
26-
await window.electronAPI.closeWindow();
27-
}
28-
} catch (e) {
29-
// Ignore errors
30-
}
31-
}, 100);
9+
// Signal to all windows that we're ready (mirrors Tauri's event.emit)
10+
await window.electronAPI.emitToAllWindows('PLATFORM_API_WORKING', 'ready');
3211
}
3312
}
3413

0 commit comments

Comments
 (0)