Skip to content

Commit f1f29c4

Browse files
authored
[ONCALL-184] fix: bg-process opens for windows (#240)
1 parent 00d00da commit f1f29c4

File tree

2 files changed

+100
-68
lines changed

2 files changed

+100
-68
lines changed

src/main/main.ts

Lines changed: 94 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@
1212
import "core-js/stable";
1313
import "regenerator-runtime/runtime";
1414
import path from "path";
15-
import { app, BrowserWindow, shell, dialog, Tray, Menu, clipboard, ipcMain } from "electron";
15+
import {
16+
app,
17+
BrowserWindow,
18+
shell,
19+
dialog,
20+
Tray,
21+
Menu,
22+
clipboard,
23+
ipcMain,
24+
} from "electron";
1625
import log from "electron-log";
1726
import MenuBuilder from "./menu";
1827
import {
@@ -74,18 +83,19 @@ const installExtensions = async () => {
7483
};
7584

7685
export default function createTrayMenu(ip?: string, port?: number) {
77-
if(tray) { // tray is recreated when proxy parameters are ready
86+
if (tray) {
87+
// tray is recreated when proxy parameters are ready
7888
tray.destroy();
79-
tray = null
89+
tray = null;
8090
}
8191
const proxyAddress = `${ip}:${port}`;
8292
const menuOptions: Electron.MenuItemConstructorOptions[] = [
8393
{
8494
label: "Show Requestly",
8595
click: () => {
86-
if(webAppWindow && !webAppWindow.isDestroyed()) {
87-
if(webAppWindow.isMinimized()) {
88-
webAppWindow.restore()
96+
if (webAppWindow && !webAppWindow.isDestroyed()) {
97+
if (webAppWindow.isMinimized()) {
98+
webAppWindow.restore();
8999
}
90100
webAppWindow.show();
91101
webAppWindow.focus();
@@ -132,7 +142,8 @@ export default function createTrayMenu(ip?: string, port?: number) {
132142
{
133143
label: "🐞 Report an Issue",
134144
click: () => {
135-
const issueURL = "https://github.com/requestly/requestly/issues/new/choose";
145+
const issueURL =
146+
"https://github.com/requestly/requestly/issues/new/choose";
136147
shell.openExternal(issueURL);
137148
},
138149
},
@@ -152,14 +163,15 @@ export default function createTrayMenu(ip?: string, port?: number) {
152163
webAppWindow?.close();
153164
},
154165
},
155-
]
166+
];
156167

157-
if(!ip || !port) { // for case when proxy is not ready
158-
menuOptions.splice(1,2)
168+
if (!ip || !port) {
169+
// for case when proxy is not ready
170+
menuOptions.splice(1, 2);
159171
}
160172
const trayMenu = Menu.buildFromTemplate(menuOptions);
161173

162-
if(process.platform === "win32") {
174+
if (process.platform === "win32") {
163175
tray = new Tray(getAssetPath("[email protected]"));
164176
} else {
165177
tray = new Tray(getAssetPath("iconTemplate.png"));
@@ -168,7 +180,7 @@ export default function createTrayMenu(ip?: string, port?: number) {
168180
tray.setContextMenu(trayMenu);
169181
}
170182

171-
let closingAccepted = false
183+
let closingAccepted = false;
172184
const createWindow = async () => {
173185
if (isDevelopment) {
174186
await installExtensions();
@@ -182,15 +194,15 @@ const createWindow = async () => {
182194
return path.join(RESOURCES_PATH, ...paths);
183195
};
184196

185-
let framelessOptions = {}
197+
let framelessOptions = {};
186198

187199
// Only for OSX right now. Needs to be tested for linux and windows
188-
if(process.platform === "darwin") {
200+
if (process.platform === "darwin") {
189201
framelessOptions = {
190202
frame: false,
191203
titleBarStyle: "hidden" as "hidden",
192204
trafficLightPosition: { x: 16, y: 16 },
193-
}
205+
};
194206
}
195207

196208
webAppWindow = new BrowserWindow({
@@ -205,7 +217,7 @@ const createWindow = async () => {
205217
sandbox: false,
206218
preload: path.join(__dirname, "preload.js"),
207219
},
208-
...framelessOptions
220+
...framelessOptions,
209221
});
210222
webAppWindow.webContents.setVisualZoomLevelLimits(1, 3);
211223

@@ -221,17 +233,30 @@ const createWindow = async () => {
221233
});
222234

223235
// @ts-ignore
224-
webAppWindow.webContents.once("did-fail-load", (event, errorCode, errorDescription, validatedUrl, isMainFrame, frameProcessId, frameRoutingId) => {
225-
if(isMainFrame) {
226-
console.error(`did-fail-load errorCode=${errorCode} url=${validatedUrl}`);
227-
if (webAppWindow) webAppWindow.hide();
228-
dialog.showErrorBox(
229-
"No internet",
230-
"Unable to connect to Requestly servers. Make sure you're connected to the internet or try removing any active proxy."
231-
);
232-
app.quit();
236+
webAppWindow.webContents.once(
237+
"did-fail-load",
238+
(
239+
_event,
240+
errorCode,
241+
_errorDescription,
242+
validatedUrl,
243+
isMainFrame,
244+
_frameProcessId,
245+
_frameRoutingId
246+
) => {
247+
if (isMainFrame) {
248+
console.error(
249+
`did-fail-load errorCode=${errorCode} url=${validatedUrl}`
250+
);
251+
if (webAppWindow) webAppWindow.hide();
252+
dialog.showErrorBox(
253+
"No internet",
254+
"Unable to connect to Requestly servers. Make sure you're connected to the internet or try removing any active proxy."
255+
);
256+
app.quit();
257+
}
233258
}
234-
});
259+
);
235260

236261
webAppWindow.once("ready-to-show", () => {
237262
if (!webAppWindow) {
@@ -246,7 +271,7 @@ const createWindow = async () => {
246271
webAppWindow.show();
247272
webAppWindow.focus();
248273

249-
executeOnWebAppReadyHandlers();
274+
executeOnWebAppReadyHandlers();
250275
}
251276

252277
// Close loading splash screen
@@ -256,19 +281,19 @@ const createWindow = async () => {
256281
}
257282
});
258283

259-
webAppWindow.on('close', async (event) => {
260-
if(!closingAccepted) {
284+
webAppWindow.on("close", async (event) => {
285+
if (!closingAccepted) {
261286
event.preventDefault();
262-
webAppWindow?.webContents.send("initiate-app-close")
287+
webAppWindow?.webContents.send("initiate-app-close");
263288
}
264-
})
289+
});
265290

266-
webAppWindow.on('closed', async () => {
291+
webAppWindow.on("closed", async () => {
267292
saveCookies();
268293
await getReadyToQuitApp();
269294
webAppWindow = null;
270295
return;
271-
})
296+
});
272297
const enableBGWindowDebug = () => {
273298
// Show bg window and toggle the devtools
274299
try {
@@ -280,6 +305,7 @@ const createWindow = async () => {
280305
// Show Window
281306
// eslint-disable-next-line
282307
globalAny.backgroundWindow.show();
308+
283309
// eslint-disable-next-line
284310
globalAny.backgroundWindow.webContents.toggleDevTools();
285311
}
@@ -295,17 +321,17 @@ const createWindow = async () => {
295321
// Open urls in the user's browser
296322
webAppWindow.webContents.setWindowOpenHandler((details) => {
297323
shell.openExternal(details.url);
298-
return { action: 'deny' }
324+
return { action: "deny" };
299325
});
300326
};
301327

302328
let onWebAppReadyHandlers: (() => void)[] = [];
303329
function executeOnWebAppReadyHandlers() {
304-
if(onWebAppReadyHandlers.length > 0) {
305-
onWebAppReadyHandlers.forEach(callback => {
330+
if (onWebAppReadyHandlers.length > 0) {
331+
onWebAppReadyHandlers.forEach((callback) => {
306332
callback();
307-
})
308-
onWebAppReadyHandlers = []
333+
});
334+
onWebAppReadyHandlers = [];
309335
}
310336
}
311337

@@ -322,16 +348,16 @@ function handleCustomProtocolURL(urlString: string) {
322348

323349
// custom protocol (requestly) handler
324350
app.on("open-url", (_event, rqUrl) => {
325-
if(webAppWindow && !webAppWindow.isDestroyed()) {
326-
handleCustomProtocolURL(rqUrl)
351+
if (webAppWindow && !webAppWindow.isDestroyed()) {
352+
handleCustomProtocolURL(rqUrl);
327353
} else {
328-
onWebAppReadyHandlers.push(() => handleCustomProtocolURL(rqUrl))
354+
onWebAppReadyHandlers.push(() => handleCustomProtocolURL(rqUrl));
329355
}
330-
})
356+
});
331357

332358
async function handleFileOpen(filePath: string, webAppWindow?: BrowserWindow) {
333359
trackRecentlyAccessedFile(filePath);
334-
log.info("filepath opened", filePath)
360+
log.info("filepath opened", filePath);
335361
webAppWindow?.show();
336362
webAppWindow?.focus();
337363
try {
@@ -346,22 +372,22 @@ async function handleFileOpen(filePath: string, webAppWindow?: BrowserWindow) {
346372
path: filePath,
347373
};
348374

349-
webAppWindow?.webContents.send("open-file", fileObject)
375+
webAppWindow?.webContents.send("open-file", fileObject);
350376
} catch (error) {
351-
logger.error(`Error while reading file ${filePath}`, error)
352-
onWebAppReadyHandlers.push(() => handleFileOpen(filePath))
377+
logger.error(`Error while reading file ${filePath}`, error);
378+
onWebAppReadyHandlers.push(() => handleFileOpen(filePath));
353379
}
354380
}
355381

356-
app.on('open-file', async (event, filePath) => {
382+
app.on("open-file", async (event, filePath) => {
357383
event.preventDefault();
358-
if(webAppWindow && !webAppWindow.isDestroyed()) {
384+
if (webAppWindow && !webAppWindow.isDestroyed()) {
359385
handleFileOpen(filePath, webAppWindow);
360386
} else {
361-
logger.log("webAppWindow not ready")
362-
onWebAppReadyHandlers.push(() => handleFileOpen(filePath))
387+
logger.log("webAppWindow not ready");
388+
onWebAppReadyHandlers.push(() => handleFileOpen(filePath));
363389
}
364-
return
390+
return;
365391
});
366392

367393
// This method will be called when Electron has finished
@@ -391,13 +417,15 @@ app.on("ready", () => {
391417
registerMainProcessEventsForWebAppWindow(webAppWindow);
392418
registerMainProcessCommonEvents();
393419

394-
if (process.platform === 'win32') {
420+
if (process.platform === "win32") {
395421
// Set the path of electron.exe and your app.
396422
// These two additional parameters are only available on windows.
397423
// Setting this is required to get this working in dev mode.
398-
app.setAsDefaultProtocolClient('requestly', process.execPath, [path.resolve(process.argv[1])]);
424+
app.setAsDefaultProtocolClient("requestly", process.execPath, [
425+
path.resolve(process.argv[1]),
426+
]);
399427
} else {
400-
app.setAsDefaultProtocolClient('requestly');
428+
app.setAsDefaultProtocolClient("requestly");
401429
}
402430
});
403431
loadingScreenWindow.loadURL(
@@ -428,14 +456,15 @@ app
428456
.whenReady()
429457
.then(() => {
430458
if (process.env.IS_SETAPP_BUILD === "true") {
431-
log.log("[SETAPP] build identified")
459+
log.log("[SETAPP] build identified");
432460
const setappFramework = require("@setapp/framework-wrapper");
433-
setappFramework.SetappManager.shared.reportUsageEvent(setappFramework.SETAPP_USAGE_EVENT.USER_INTERACTION);
434-
log.log("[SETAPP] integration complete")
461+
setappFramework.SetappManager.shared.reportUsageEvent(
462+
setappFramework.SETAPP_USAGE_EVENT.USER_INTERACTION
463+
);
464+
log.log("[SETAPP] integration complete");
435465
}
436466

437467
app.on("activate", () => {
438-
439468
// On macOS it's common to re-create a window in the app when the
440469
// dock icon is clicked and there are no other windows open.
441470
if (BrowserWindow.getAllWindows().length === 0) {
@@ -448,9 +477,9 @@ app
448477
});
449478

450479
ipcMain.handle("quit-app", (_event) => {
451-
closingAccepted = true
480+
closingAccepted = true;
452481
webAppWindow?.close();
453-
})
482+
});
454483

455484
app.on("before-quit", () => {
456485
// cleanup when quitting has been finalised
@@ -460,10 +489,10 @@ app.on("before-quit", () => {
460489
backgroundWindow?.removeAllListeners();
461490

462491
ipcMain.removeAllListeners();
463-
process.on('uncaughtException', (err) => {
464-
logger.error('Unhandled Exception while quitting:', err);
492+
process.on("uncaughtException", (err) => {
493+
logger.error("Unhandled Exception while quitting:", err);
465494
});
466-
process.on('unhandledRejection', (err) => {
467-
logger.error('Unhandled Rejection while quitting:', err);
495+
process.on("unhandledRejection", (err) => {
496+
logger.error("Unhandled Rejection while quitting:", err);
468497
});
469-
})
498+
});

src/main/menu.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,11 @@ export default class MenuBuilder {
125125
},
126126
},
127127
{
128-
label: "Toggle Developer Tools",
128+
label: "Debug Requestly",
129129
accelerator: "Alt+Command+I",
130130
click: () => {
131131
this.mainWindow.webContents.toggleDevTools();
132+
this.enableBGWindowDebug();
132133
},
133134
},
134135
{
@@ -324,10 +325,11 @@ export default class MenuBuilder {
324325
},
325326
},
326327
{
327-
label: "Toggle &Developer Tools",
328+
label: "Debug Requestly",
328329
accelerator: "Alt+Ctrl+I",
329330
click: () => {
330331
this.mainWindow.webContents.toggleDevTools();
332+
this.enableBGWindowDebug();
331333
},
332334
},
333335
{
@@ -386,10 +388,11 @@ export default class MenuBuilder {
386388
},
387389
},
388390
{
389-
label: "Toggle &Developer Tools",
391+
label: "Debug Requestly",
390392
accelerator: "Alt+Ctrl+I",
391393
click: () => {
392394
this.mainWindow.webContents.toggleDevTools();
395+
this.enableBGWindowDebug();
393396
},
394397
},
395398
{

0 commit comments

Comments
 (0)