Skip to content

Commit aa24cf1

Browse files
committed
chore: ahell apis to electron continued for linux
1 parent 3c93460 commit aa24cf1

1 file changed

Lines changed: 128 additions & 51 deletions

File tree

src/phoenix/shell.js

Lines changed: 128 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,12 @@ Phoenix.app = {
136136
if(!Phoenix.isNativeApp){
137137
throw new Error("getProcessID is not supported in browsers");
138138
}
139-
return window.__TAURI__.invoke("get_process_id");
139+
if(window.__TAURI__){
140+
return window.__TAURI__.invoke("get_process_id");
141+
}
142+
if(window.__ELECTRON__){
143+
return window.electronAPI.getProcessId();
144+
}
140145
},
141146
registerQuitTimeAppUpdateHandler: function (handler) {
142147
if(!Phoenix.isNativeApp){
@@ -148,7 +153,12 @@ Phoenix.app = {
148153
if(!Phoenix.isNativeApp){
149154
throw new Error("toggle_devtools is not supported in browsers");
150155
}
151-
return window.__TAURI__.invoke("toggle_devtools", {});
156+
if(window.__TAURI__){
157+
return window.__TAURI__.invoke("toggle_devtools", {});
158+
}
159+
if(window.__ELECTRON__){
160+
return window.electronAPI.toggleDevTools();
161+
}
152162
},
153163
onCloseWindowRequested: function (_closeHandlerCb) {
154164
if(typeof _closeHandlerCb !== 'function'){
@@ -158,12 +168,22 @@ Phoenix.app = {
158168
throw new Error("onCloseWindowRequested can only be registered once!");
159169
}
160170
closeHandlerCb = _closeHandlerCb;
161-
window.__TAURI__.window.appWindow.onCloseRequested((event)=>{
162-
const shouldClose = closeHandlerCb();
163-
if(!shouldClose){
164-
event.preventDefault();
165-
}
166-
});
171+
if(window.__TAURI__){
172+
window.__TAURI__.window.appWindow.onCloseRequested((event)=>{
173+
const shouldClose = closeHandlerCb();
174+
if(!shouldClose){
175+
event.preventDefault();
176+
}
177+
});
178+
} else if(window.__ELECTRON__){
179+
window.electronAPI.registerCloseHandler();
180+
window.electronAPI.onCloseRequested(()=>{
181+
const shouldClose = closeHandlerCb();
182+
if(shouldClose){
183+
window.electronAPI.allowClose();
184+
}
185+
});
186+
}
167187
},
168188
closeWindow: async function (forceClose) {
169189
if(!Phoenix.isNativeApp){
@@ -252,8 +272,13 @@ Phoenix.app = {
252272
};
253273
}
254274
cliArgs = null;
255-
cliCWD = await window.__TAURI__.invoke("get_current_working_dir");
256-
cliArgs = await window.__TAURI__.invoke('_get_commandline_args');
275+
if(window.__TAURI__){
276+
cliCWD = await window.__TAURI__.invoke("get_current_working_dir");
277+
cliArgs = await window.__TAURI__.invoke('_get_commandline_args');
278+
} else if(window.__ELECTRON__){
279+
cliCWD = await window.electronAPI.getCwd();
280+
cliArgs = await window.electronAppAPI.getCliArgs();
281+
}
257282
return {
258283
cwd: cliCWD,
259284
args: cliArgs
@@ -316,37 +341,49 @@ Phoenix.app = {
316341
}
317342
},
318343
clipboardReadText: function () {
319-
if(Phoenix.isNativeApp){
344+
if(window.__TAURI__){
320345
return window.__TAURI__.clipboard.readText();
321-
} else if(window.navigator && window.navigator.clipboard){
346+
}
347+
if(window.__ELECTRON__){
348+
return window.electronAPI.clipboardReadText();
349+
}
350+
if(window.navigator && window.navigator.clipboard){
322351
return window.navigator.clipboard.readText();
323352
}
324353
return Promise.reject(new Error("clipboardReadText: Not supported."));
325354
},
326355
clipboardReadFiles: function () {
327356
return new Promise((resolve, reject)=>{
328-
if(Phoenix.isNativeApp){
329-
window.__TAURI__.tauri.invoke('_get_clipboard_files')
330-
.then(files =>{
331-
if(!files){
332-
resolve(files);
333-
return;
334-
}
335-
const vfsPaths = [];
336-
for(let platformPath of files) {
337-
vfsPaths.push(Phoenix.VFS.getTauriVirtualPath(platformPath));
338-
}
339-
resolve(vfsPaths);
340-
}).catch(reject);
357+
let filesPromise;
358+
if(window.__TAURI__){
359+
filesPromise = window.__TAURI__.tauri.invoke('_get_clipboard_files');
360+
} else if(window.__ELECTRON__){
361+
filesPromise = window.electronAPI.clipboardReadFiles();
341362
} else {
342363
resolve();
364+
return;
343365
}
366+
filesPromise.then(files =>{
367+
if(!files){
368+
resolve(files);
369+
return;
370+
}
371+
const vfsPaths = [];
372+
for(let platformPath of files) {
373+
vfsPaths.push(Phoenix.VFS.getTauriVirtualPath(platformPath));
374+
}
375+
resolve(vfsPaths);
376+
}).catch(reject);
344377
});
345378
},
346379
copyToClipboard: function (textToCopy) {
347-
if(Phoenix.isNativeApp){
380+
if(window.__TAURI__){
348381
return window.__TAURI__.clipboard.writeText(textToCopy);
349-
} else if(window.navigator && window.navigator.clipboard){
382+
}
383+
if(window.__ELECTRON__){
384+
return window.electronAPI.clipboardWriteText(textToCopy);
385+
}
386+
if(window.navigator && window.navigator.clipboard){
350387
return window.navigator.clipboard.writeText(textToCopy);
351388
}
352389
const textArea = document.createElement("textarea");
@@ -362,20 +399,30 @@ Phoenix.app = {
362399
// use browser full screen api in browsers.
363400
return Promise.resolve(!!document.fullscreenElement);
364401
}
365-
return window.__TAURI__.window.appWindow.isFullscreen();
402+
if(window.__TAURI__){
403+
return window.__TAURI__.window.appWindow.isFullscreen();
404+
}
405+
if(window.__ELECTRON__){
406+
return window.electronAPI.isFullscreen();
407+
}
366408
},
367409
setFullscreen: function (enable) {
368410
if(!Phoenix.isNativeApp) {
369411
// use browser full screen api in browsers.
370412
if (enable) {
371413
return document.documentElement.requestFullscreen();
372414
} else if (document.exitFullscreen) {
373-
return document.exitFullscreen();
415+
return document.exitFullscreen();
374416
} else {
375417
return Promise.resolve();
376418
}
377419
}
378-
return window.__TAURI__.window.appWindow.setFullscreen(enable);
420+
if(window.__TAURI__){
421+
return window.__TAURI__.window.appWindow.setFullscreen(enable);
422+
}
423+
if(window.__ELECTRON__){
424+
return window.electronAPI.setFullscreen(enable);
425+
}
379426
},
380427
getDisplayLocation: function (fullVFSPath) {
381428
// reruns a user-friendly location that can be shown to the user to make some sense of the virtual file path.
@@ -420,56 +467,77 @@ Phoenix.app = {
420467
throw new Error("Please specify a path to move to trash");
421468
}
422469
if(!fullVFSPath.startsWith(Phoenix.VFS.getTauriDir())) {
423-
throw new Error("moveToTrash only works with tauri paths, but got: "+ fullVFSPath);
470+
throw new Error("moveToTrash only works with native paths, but got: "+ fullVFSPath);
424471
}
425472
const platformPath = Phoenix.fs.getTauriPlatformPath(fullVFSPath);
426-
return window.__TAURI__.invoke('move_to_trash', { deletePath: platformPath });
473+
if(window.__TAURI__){
474+
return window.__TAURI__.invoke('move_to_trash', { deletePath: platformPath });
475+
}
476+
if(window.__ELECTRON__){
477+
return window.electronAPI.moveToTrash(platformPath);
478+
}
427479
},
428480
setWindowTitle: async function (title) {
429481
window.document.title = title;
430-
if(Phoenix.isNativeApp) {
482+
if(window.__TAURI__) {
431483
await window.__TAURI__.window.appWindow.setTitle(title);
484+
} else if(window.__ELECTRON__) {
485+
await window.electronAPI.setWindowTitle(title);
432486
}
433487
},
434488
getWindowTitle: async function () {
435-
if(Phoenix.isNativeApp) {
489+
if(window.__TAURI__) {
436490
return window.__TAURI__.window.appWindow.title();
437491
}
492+
if(window.__ELECTRON__) {
493+
return window.electronAPI.getWindowTitle();
494+
}
438495
return window.document.title;
439496
},
440497
openPathInFileBrowser: function (fullVFSPath){
441498
return new Promise((resolve, reject)=>{
442-
if(!window.__TAURI__ ||
499+
if(!Phoenix.isNativeApp ||
443500
!fullVFSPath.startsWith(Phoenix.VFS.getTauriDir())) {
444-
reject("openPathInFileBrowser is only currently supported in Native builds for tauri paths!");
501+
reject("openPathInFileBrowser is only currently supported in Native builds for native paths!");
445502
return;
446503
}
447504
if(fullVFSPath.toLowerCase().startsWith("http://")
448505
|| fullVFSPath.toLowerCase().startsWith("https://")
449506
|| fullVFSPath.toLowerCase().startsWith("file://")) {
450-
reject("Please use openPathInFileBrowser API to open URLs");
507+
reject("Please use openURLInDefaultBrowser API to open URLs");
451508
return;
452509
}
453510
const platformPath = Phoenix.fs.getTauriPlatformPath(fullVFSPath);
454-
window.__TAURI__.tauri
455-
.invoke('show_in_folder', {path: platformPath})
456-
.then(resolve)
457-
.catch(reject);
511+
if(window.__TAURI__){
512+
window.__TAURI__.tauri
513+
.invoke('show_in_folder', {path: platformPath})
514+
.then(resolve)
515+
.catch(reject);
516+
} else if(window.__ELECTRON__){
517+
window.electronAPI.showInFolder(platformPath);
518+
resolve();
519+
}
458520
});
459521
},
460522
openURLInDefaultBrowser: function (url, tabIdentifier='_blank'){
461523
return new Promise((resolve, reject)=>{
462-
if(!window.__TAURI__) {
524+
if(!Phoenix.isNativeApp) {
463525
resolve(window.open(url, tabIdentifier, 'noopener,noreferrer'));
464526
return;
465527
}
466528
if( !(url.toLowerCase().startsWith("http://") || url.toLowerCase().startsWith("https://")) ) {
467529
reject("openURLInDefaultBrowser: URL should be http or https, but was " + url);
468530
return;
469531
}
470-
window.__TAURI__.shell.open(url)
471-
.then(resolve)
472-
.catch(reject);
532+
if(window.__TAURI__){
533+
window.__TAURI__.shell.open(url)
534+
.then(resolve)
535+
.catch(reject);
536+
} else if(window.__ELECTRON__){
537+
window.electronAPI.openExternal(url)
538+
.then(resolve)
539+
.catch(reject);
540+
}
473541
});
474542
},
475543
/**
@@ -537,17 +605,21 @@ Phoenix.app = {
537605
return windowCount;
538606
},
539607
/**
540-
* Returns the operating system CPU architecture for which the tauri app was compiled. Possible values are
541-
* 'x86', 'x86_64', 'arm', 'aarch64', 'mips', 'mips64', 'powerpc', 'powerpc64', 'riscv64', 's390x', 'sparc64'.
608+
* Returns the operating system CPU architecture for which the app was compiled. Possible values are
609+
* 'x86', 'x86_64', 'arm', 'aarch64', 'arm64', 'ia32', 'x64', etc.
542610
* @return {Promise<string>}
543611
*/
544612
getPlatformArch: async function () {
545613
if(!Phoenix.isNativeApp) {
546-
// there is no primary window concept in browsers. all are primary for now.
547614
console.error("getPlatformArch is not supported in browsers!");
548-
return true;
615+
return null;
616+
}
617+
if(window.__TAURI__){
618+
return window.__TAURI__.os.arch();
619+
}
620+
if(window.__ELECTRON__){
621+
return window.electronAPI.getPlatformArch();
549622
}
550-
return window.__TAURI__.os.arch();
551623
},
552624
openNewPhoenixEditorWindow: async function (preferredWidth, preferredHeight, _cliArgsArray, _cwd) {
553625
const phoenixURL = new URL(location.href);
@@ -594,7 +666,12 @@ Phoenix.app = {
594666
if(Phoenix.platform !== "win") {
595667
throw new Error("_openUrlInBrowserWin is only supported in windows");
596668
}
597-
return window.__TAURI__.invoke('_open_url_in_browser_win', { url, browser });
669+
if(window.__TAURI__){
670+
return window.__TAURI__.invoke('_open_url_in_browser_win', { url, browser });
671+
}
672+
if(window.__ELECTRON__){
673+
return window.electronAPI.openUrlInBrowserWin(url, browser);
674+
}
598675
},
599676
getApplicationSupportDirectory: Phoenix.VFS.getAppSupportDir,
600677
getExtensionsDirectory: Phoenix.VFS.getExtensionDir,

0 commit comments

Comments
 (0)