Skip to content

Commit d4328a6

Browse files
committed
chore: adding metrics, storage assetsURL, zoom window with electron support
1 parent 2a81d8e commit d4328a6

5 files changed

Lines changed: 50 additions & 16 deletions

File tree

src/extensionsIntegrated/appUpdater/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,11 @@ define(function (require, exports, module) {
517517

518518
let updateInstalledDialogShown = false, updateFailedDialogShown = false;
519519
AppInit.appReady(function () {
520-
if(!Phoenix.isNativeApp || Phoenix.isTestWindow) {
520+
if(!window.__TAURI__ || Phoenix.isTestWindow) {
521521
// app updates are only for desktop builds
522522
return;
523523
}
524+
// todo electron edge for app updater
524525
if (brackets.platform === "mac") {
525526
// in mac, the `update.app.tar.gz` is downloaded, and only extracted on app quit.
526527
// we do this only in mac as the `.app` file is extracted only at app quit and deleted

src/phoenix/init_vfs.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ function _getNativeAssetUrl(fullFilePath) {
4848
}
4949
return null;
5050
}
51-
// todo electron
51+
if(window.__ELECTRON__) {
52+
if(fullFilePath.startsWith(tauriAssetServeDir)){
53+
const platformPath = fs.getTauriPlatformPath(fullFilePath);
54+
return window.electronAPI.convertToAssetURL(platformPath);
55+
}
56+
return null;
57+
}
5258
return null;
5359
}
5460

src/phoenix/shell.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,12 @@ Phoenix.app = {
537537
if(scaleFactor < .1 || scaleFactor > 2) {
538538
throw new Error("zoomWebView scale factor should be between .1 and 2");
539539
}
540-
return window.__TAURI__.tauri.invoke("zoom_window", {scaleFactor: scaleFactor});
540+
if(window.__TAURI__) {
541+
return window.__TAURI__.tauri.invoke("zoom_window", {scaleFactor: scaleFactor});
542+
}
543+
if(window.__ELECTRON__) {
544+
return window.electronAPI.zoomWindow(scaleFactor);
545+
}
541546
},
542547
_openUrlInBrowserWin: function (url, browser) {
543548
// private API for internal use only. May be removed at any time.

src/storage.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,14 @@ import {set, entries, createStore} from './thirdparty/idb-keyval.js';
206206
storageNodeConnector
207207
.execPeer("putItem", {key, value: valueToStore})
208208
.catch(_reportPutItemError);
209-
// this is an in-memory tauri store that takes care of multi window case, since we have a single
210-
// instance, all windows share this and can reconstruct the full view from the dumb file + this map
211-
// when the editor boots up instead of having to write the dump file frequently.
212-
window.__TAURI__.invoke('put_item', { key, value: JSON.stringify(valueToStore) });
209+
// this is an in-memory tauri/electron store that takes care of multi window case, since we have a
210+
// single instance, all windows share this and can reconstruct the full view from the dump file + this
211+
// map when the editor boots up instead of having to write the dump file frequently.
212+
if(window.__TAURI__) {
213+
window.__TAURI__.invoke('put_item', { key, value: JSON.stringify(valueToStore) });
214+
} else if(window.__ELECTRON__) {
215+
window.electronAPI.putItem(key, JSON.stringify(valueToStore));
216+
}
213217
}
214218
if(window.debugMode || isBrowser) {
215219
// in debug mode, we write to browser storage in tauri and browser builds for eazy debug of storage.
@@ -291,13 +295,18 @@ import {set, entries, createStore} from './thirdparty/idb-keyval.js';
291295
return;
292296
}
293297
if(isDesktop){
294-
async function mergeTauriInMemoryStorage() {
295-
// The tauri storeage is mainly used in multi window case, where if there are 2+ windows, each window
296-
// is till live and has not commited the dump file to disc(they only do that on exit or 30 every secs).
298+
async function mergeNativeInMemoryStorage() {
299+
// The native storage is mainly used in multi window case, where if there are 2+ windows, each window
300+
// is still live and has not committed the dump file to disc (they only do that on exit or every 30 secs).
297301
// so the dump file may be stale after window._tauriStorageRestorePromise in the case.
298-
// we merge the local memory cache maintained at tauri rust side to address this.
302+
// we merge the local memory cache maintained at tauri/electron side to address this.
299303
try {
300-
const map = await window.__TAURI__.invoke('get_all_items') || {};
304+
let map = {};
305+
if(window.__TAURI__) {
306+
map = await window.__TAURI__.invoke('get_all_items') || {};
307+
} else if(window.__ELECTRON__) {
308+
map = await window.electronAPI.getAllItems() || {};
309+
}
301310
for(const key of Object.keys(map)){
302311
cache[key] = JSON.parse(map[key]);
303312
}
@@ -317,7 +326,7 @@ import {set, entries, createStore} from './thirdparty/idb-keyval.js';
317326
}
318327
})
319328
.catch(console.error)
320-
.finally(mergeTauriInMemoryStorage);
329+
.finally(mergeNativeInMemoryStorage);
321330
return;
322331
}
323332
// Use browser default storage- IndexedDB
@@ -342,8 +351,10 @@ import {set, entries, createStore} from './thirdparty/idb-keyval.js';
342351
// do things to do that are critical to user experience here
343352
// We try to set window zoom as early as possible to prevent zoom flicker
344353
const zoomFactor = PhStore.getItem(_PHSTORE_BOOT_DESKTOP_ZOOM_SCALE_KEY) || 1;
345-
if(Phoenix.isNativeApp){
354+
if(window.__TAURI__){
346355
window.__TAURI__.tauri.invoke("zoom_window", {scaleFactor: zoomFactor});
356+
} else if(window.__ELECTRON__) {
357+
window.electronAPI.zoomWindow(zoomFactor);
347358
}
348359
});
349360
/**

src/utils/Metrics.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,17 @@ define(function (require, exports, module) {
194194
});
195195
}
196196

197+
function _sendNativeGAEvent(analyticsID, customUserID, events=[]) {
198+
if(window.__TAURI__){
199+
return _sendTauriGAEvent(analyticsID, customUserID, events=[]);
200+
}
201+
if(window.__ELECTRON__){
202+
// todo electron send event to metrics window with electron
203+
return;
204+
}
205+
console.error("Metrics send event failed. Unknown native platform");
206+
}
207+
197208
let tauriGAEvents = new Map();
198209

199210
function _sendGaEvent(eventAct, category, label, count) {
@@ -216,7 +227,7 @@ define(function (require, exports, module) {
216227

217228
const TAURI_GA_EVENT_QUEUE_INTERVAL = 3000;
218229
function _sendQueuedTauriGAEvents() {
219-
_sendTauriGAEvent(brackets.config.googleAnalyticsIDDesktop, userID, Array.from(tauriGAEvents.values()));
230+
_sendNativeGAEvent(brackets.config.googleAnalyticsIDDesktop, userID, Array.from(tauriGAEvents.values()));
220231
tauriGAEvents.clear();
221232
}
222233

@@ -227,7 +238,7 @@ define(function (require, exports, module) {
227238
// https urls and not the tauri custom protocol urls. So we have a hidden window that loads ga from a
228239
// http(s) page which is usually `https://phcode.dev/desktop-metrics.html` or
229240
// "http://localhost:8000/src/metrics.html" for live dev builds in tauri.
230-
_sendTauriGAEvent(brackets.config.googleAnalyticsIDDesktop, userID);
241+
_sendNativeGAEvent(brackets.config.googleAnalyticsIDDesktop, userID);
231242
setInterval(_sendQueuedTauriGAEvents, TAURI_GA_EVENT_QUEUE_INTERVAL);
232243
return;
233244
}

0 commit comments

Comments
 (0)