Skip to content

Commit 3c93460

Browse files
committed
chore: window management apis to electron
1 parent 4a4b9a5 commit 3c93460

1 file changed

Lines changed: 62 additions & 21 deletions

File tree

src/phoenix/shell.js

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,22 @@ async function openURLInPhoenixWindow(url, {
8585
acceptFirstMouse: acceptFirstMouse === undefined ? true : acceptFirstMouse,
8686
fileDropEnabled: false
8787
});
88-
tauriWindow.isTauriWindow = true;
88+
tauriWindow.isNativeWindow = true;
8989
return tauriWindow;
9090
}
91+
if(window.__ELECTRON__){
92+
const label = await window.electronAPI.createPhoenixWindow(url, {
93+
windowTitle,
94+
fullscreen,
95+
resizable: resizable === undefined ? true : resizable,
96+
height: height || defaultHeight,
97+
minHeight: minHeight || 600,
98+
width: width || defaultWidth,
99+
minWidth: minWidth || 800,
100+
isExtension: _prefixPvt === PHOENIX_EXTENSION_WINDOW_PREFIX
101+
});
102+
return { label, isNativeWindow: true };
103+
}
91104
let features = 'toolbar=no,location=no, status=no, menubar=no, scrollbars=yes';
92105
features = `${features}, width=${width||defaultWidth}, height=${height||defaultHeight}`;
93106
if(resizable === undefined || resizable){
@@ -97,7 +110,7 @@ async function openURLInPhoenixWindow(url, {
97110
features = "";
98111
}
99112
const nativeWindow = window.open(url, '_blank', features);
100-
nativeWindow.isTauriWindow = false;
113+
nativeWindow.isNativeWindow = false;
101114
return nativeWindow;
102115
}
103116

@@ -160,9 +173,14 @@ Phoenix.app = {
160173
let extensionWindowCount = 0;
161174
try{
162175
instanceCount = await Phoenix.app.getPhoenixInstanceCount();
163-
const allTauriWindowsLabels = await window.__TAURI__.invoke('_get_window_labels');
164-
for(let tauriWindowLabel of allTauriWindowsLabels){
165-
if(tauriWindowLabel && tauriWindowLabel.startsWith(PHOENIX_EXTENSION_WINDOW_PREFIX)) {
176+
let allWindowLabels;
177+
if(window.__TAURI__){
178+
allWindowLabels = await window.__TAURI__.invoke('_get_window_labels');
179+
} else if(window.__ELECTRON__){
180+
allWindowLabels = await window.electronAPI.getWindowLabels();
181+
}
182+
for(let windowLabel of allWindowLabels){
183+
if(windowLabel && windowLabel.startsWith(PHOENIX_EXTENSION_WINDOW_PREFIX)) {
166184
extensionWindowCount ++;
167185
}
168186
}
@@ -179,18 +197,30 @@ Phoenix.app = {
179197
console.error(e);
180198
}
181199
}
182-
window.__TAURI__.process.exit(0);
200+
if(window.__TAURI__){
201+
window.__TAURI__.process.exit(0);
202+
} else if(window.__ELECTRON__){
203+
window.electronAPI.quitApp(0);
204+
}
183205
return;
184206
}
185-
window.__TAURI__.window.getCurrent().close();
207+
if(window.__TAURI__){
208+
window.__TAURI__.window.getCurrent().close();
209+
} else if(window.__ELECTRON__){
210+
window.electronAPI.closeWindow();
211+
}
186212
},
187213
focusWindow: function () {
188214
if(!Phoenix.isNativeApp){
189215
return Promise.reject(new Error("focusWindow is not supported in browsers"));
190216
}
191-
window.__TAURI__.window.getCurrent().setAlwaysOnTop(true);
192-
window.__TAURI__.window.getCurrent().setFocus();
193-
window.__TAURI__.window.getCurrent().setAlwaysOnTop(false);
217+
if(window.__TAURI__){
218+
window.__TAURI__.window.getCurrent().setAlwaysOnTop(true);
219+
window.__TAURI__.window.getCurrent().setFocus();
220+
window.__TAURI__.window.getCurrent().setAlwaysOnTop(false);
221+
} else if(window.__ELECTRON__){
222+
return window.electronAPI.focusWindow();
223+
}
194224
},
195225
/**
196226
* Gets the commandline argument in desktop builds and null in browser builds.
@@ -456,21 +486,27 @@ Phoenix.app = {
456486
console.error("isPrimaryDesktopPhoenixWindow is not supported in browsers!");
457487
return true;
458488
}
459-
const currentWindowLabel = window.__TAURI__.window.getCurrent().label;
489+
let currentWindowLabel, allWindowLabels;
490+
if(window.__TAURI__){
491+
currentWindowLabel = window.__TAURI__.window.getCurrent().label;
492+
allWindowLabels = await window.__TAURI__.invoke('_get_window_labels');
493+
} else if(window.__ELECTRON__){
494+
currentWindowLabel = await window.electronAPI.getCurrentWindowLabel();
495+
allWindowLabels = await window.electronAPI.getWindowLabels();
496+
}
460497
if(currentWindowLabel === 'main'){
461498
// main window if there will be the primary
462499
return true;
463500
}
464-
const allTauriWindowsLabels = await window.__TAURI__.invoke('_get_window_labels');
465-
if(allTauriWindowsLabels.includes('main')){
466-
// we are not main and there is a main window in tauri windows
501+
if(allWindowLabels.includes('main')){
502+
// we are not main and there is a main window
467503
return false;
468504
}
469505
// the main window has been closed and some other window is the primary now.
470506
// the one with the lowest label is primary
471-
for(let tauriWindowLabel of allTauriWindowsLabels){
472-
if(tauriWindowLabel && tauriWindowLabel.startsWith(PHOENIX_WINDOW_PREFIX) &&
473-
currentWindowLabel !== tauriWindowLabel && currentWindowLabel > tauriWindowLabel) {
507+
for(let windowLabel of allWindowLabels){
508+
if(windowLabel && windowLabel.startsWith(PHOENIX_WINDOW_PREFIX) &&
509+
currentWindowLabel !== windowLabel && currentWindowLabel > windowLabel) {
474510
return false;
475511
}
476512
}
@@ -484,12 +520,17 @@ Phoenix.app = {
484520
if(!Phoenix.isNativeApp) {
485521
// there is no primary window concept in browsers. all are primary for now.
486522
console.error("getPhoenixInstanceCount is not supported in browsers!");
487-
return true;
523+
return 1;
488524
}
489525
let windowCount = 0;
490-
const allTauriWindowsLabels = await window.__TAURI__.invoke('_get_window_labels');
491-
for(let tauriWindowLabel of allTauriWindowsLabels){
492-
if(tauriWindowLabel && (tauriWindowLabel.startsWith(PHOENIX_WINDOW_PREFIX) || tauriWindowLabel === 'main')) {
526+
let allWindowLabels;
527+
if(window.__TAURI__){
528+
allWindowLabels = await window.__TAURI__.invoke('_get_window_labels');
529+
} else if(window.__ELECTRON__){
530+
allWindowLabels = await window.electronAPI.getWindowLabels();
531+
}
532+
for(let windowLabel of allWindowLabels){
533+
if(windowLabel && (windowLabel.startsWith(PHOENIX_WINDOW_PREFIX) || windowLabel === 'main')) {
493534
windowCount ++;
494535
}
495536
}

0 commit comments

Comments
 (0)