Skip to content

Commit 5acc45c

Browse files
committed
Use process-specific electron/{main,renderer,common} imports.
Signed-off-by: Anders Kaseorg <[email protected]>
1 parent 343e0ed commit 5acc45c

22 files changed

+113
-105
lines changed

app/common/remote.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import electron from "electron";
2-
31
export const {app, dialog} =
42
process.type === "renderer"
53
? // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
64
(require("@electron/remote") as typeof import("@electron/remote"))
7-
: electron;
5+
: // eslint-disable-next-line @typescript-eslint/no-require-imports
6+
require("electron/main");

app/main/autoupdater.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {app, dialog, session, shell} from "electron";
1+
import {shell} from "electron/common";
2+
import {app, dialog, session} from "electron/main";
23
import util from "util";
34

45
import log from "electron-log";

app/main/badge-settings.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
import electron, {app} from "electron";
1+
import {nativeImage} from "electron/common";
2+
import type {BrowserWindow} from "electron/main";
3+
import {app} from "electron/main";
24

35
import * as ConfigUtil from "../common/config-util";
46

57
import {send} from "./typed-ipc-main";
68

7-
function showBadgeCount(
8-
messageCount: number,
9-
mainWindow: electron.BrowserWindow,
10-
): void {
9+
function showBadgeCount(messageCount: number, mainWindow: BrowserWindow): void {
1110
if (process.platform === "win32") {
1211
updateOverlayIcon(messageCount, mainWindow);
1312
} else {
1413
app.badgeCount = messageCount;
1514
}
1615
}
1716

18-
function hideBadgeCount(mainWindow: electron.BrowserWindow): void {
17+
function hideBadgeCount(mainWindow: BrowserWindow): void {
1918
if (process.platform === "win32") {
2019
mainWindow.setOverlayIcon(null, "");
2120
} else {
@@ -25,7 +24,7 @@ function hideBadgeCount(mainWindow: electron.BrowserWindow): void {
2524

2625
export function updateBadge(
2726
badgeCount: number,
28-
mainWindow: electron.BrowserWindow,
27+
mainWindow: BrowserWindow,
2928
): void {
3029
if (ConfigUtil.getConfigItem("badgeOption", true)) {
3130
showBadgeCount(badgeCount, mainWindow);
@@ -36,7 +35,7 @@ export function updateBadge(
3635

3736
function updateOverlayIcon(
3837
messageCount: number,
39-
mainWindow: electron.BrowserWindow,
38+
mainWindow: BrowserWindow,
4039
): void {
4140
if (!mainWindow.isFocused()) {
4241
mainWindow.flashFrame(
@@ -54,8 +53,8 @@ function updateOverlayIcon(
5453
export function updateTaskbarIcon(
5554
data: string,
5655
text: string,
57-
mainWindow: electron.BrowserWindow,
56+
mainWindow: BrowserWindow,
5857
): void {
59-
const img = electron.nativeImage.createFromDataURL(data);
58+
const img = nativeImage.createFromDataURL(data);
6059
mainWindow.setOverlayIcon(img, text);
6160
}

app/main/index.ts

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import electron, {app, dialog, session} from "electron";
1+
import type {IpcMainEvent, SaveDialogOptions, WebContents} from "electron/main";
2+
import {BrowserWindow, app, dialog, powerMonitor, session} from "electron/main";
23
import fs from "fs";
34
import path from "path";
45

@@ -25,7 +26,7 @@ sentryInit();
2526
let mainWindowState: windowStateKeeper.State;
2627

2728
// Prevent window being garbage collected
28-
let mainWindow: Electron.BrowserWindow;
29+
let mainWindow: BrowserWindow;
2930
let badgeCount: number;
3031

3132
let isQuitting = false;
@@ -50,15 +51,15 @@ const toggleApp = (): void => {
5051
}
5152
};
5253

53-
function createMainWindow(): Electron.BrowserWindow {
54+
function createMainWindow(): BrowserWindow {
5455
// Load the previous state with fallback to defaults
5556
mainWindowState = windowStateKeeper({
5657
defaultWidth: 1100,
5758
defaultHeight: 720,
5859
path: `${app.getPath("userData")}/config`,
5960
});
6061

61-
const win = new electron.BrowserWindow({
62+
const win = new BrowserWindow({
6263
// This settings needs to be saved in config
6364
title: "Zulip",
6465
icon: iconPath(),
@@ -239,7 +240,7 @@ function createMainWindow(): Electron.BrowserWindow {
239240
"certificate-error",
240241
(
241242
event: Event,
242-
webContents: Electron.WebContents,
243+
webContents: WebContents,
243244
urlString: string,
244245
error: string,
245246
) => {
@@ -275,7 +276,7 @@ ${error}`,
275276
);
276277

277278
// Temporarily remove this event
278-
// electron.powerMonitor.on('resume', () => {
279+
// powerMonitor.on('resume', () => {
279280
// mainWindow.reload();
280281
// send(page, 'destroytray');
281282
// });
@@ -308,67 +309,58 @@ ${error}`,
308309
BadgeSettings.updateBadge(badgeCount, mainWindow);
309310
});
310311

311-
ipcMain.on(
312-
"toggle-menubar",
313-
(_event: Electron.IpcMainEvent, showMenubar: boolean) => {
314-
mainWindow.autoHideMenuBar = showMenubar;
315-
mainWindow.setMenuBarVisibility(!showMenubar);
316-
send(page, "toggle-autohide-menubar", showMenubar, true);
317-
},
318-
);
312+
ipcMain.on("toggle-menubar", (_event: IpcMainEvent, showMenubar: boolean) => {
313+
mainWindow.autoHideMenuBar = showMenubar;
314+
mainWindow.setMenuBarVisibility(!showMenubar);
315+
send(page, "toggle-autohide-menubar", showMenubar, true);
316+
});
319317

320-
ipcMain.on(
321-
"update-badge",
322-
(_event: Electron.IpcMainEvent, messageCount: number) => {
323-
badgeCount = messageCount;
324-
BadgeSettings.updateBadge(badgeCount, mainWindow);
325-
send(page, "tray", messageCount);
326-
},
327-
);
318+
ipcMain.on("update-badge", (_event: IpcMainEvent, messageCount: number) => {
319+
badgeCount = messageCount;
320+
BadgeSettings.updateBadge(badgeCount, mainWindow);
321+
send(page, "tray", messageCount);
322+
});
328323

329324
ipcMain.on(
330325
"update-taskbar-icon",
331-
(_event: Electron.IpcMainEvent, data: string, text: string) => {
326+
(_event: IpcMainEvent, data: string, text: string) => {
332327
BadgeSettings.updateTaskbarIcon(data, text, mainWindow);
333328
},
334329
);
335330

336331
ipcMain.on(
337332
"forward-message",
338333
<Channel extends keyof RendererMessage>(
339-
_event: Electron.IpcMainEvent,
334+
_event: IpcMainEvent,
340335
listener: Channel,
341336
...parameters: Parameters<RendererMessage[Channel]>
342337
) => {
343338
send(page, listener, ...parameters);
344339
},
345340
);
346341

347-
ipcMain.on(
348-
"update-menu",
349-
(_event: Electron.IpcMainEvent, props: MenuProps) => {
350-
AppMenu.setMenu(props);
351-
if (props.activeTabIndex !== undefined) {
352-
const activeTab = props.tabs[props.activeTabIndex];
353-
mainWindow.setTitle(`Zulip - ${activeTab.name}`);
354-
}
355-
},
356-
);
342+
ipcMain.on("update-menu", (_event: IpcMainEvent, props: MenuProps) => {
343+
AppMenu.setMenu(props);
344+
if (props.activeTabIndex !== undefined) {
345+
const activeTab = props.tabs[props.activeTabIndex];
346+
mainWindow.setTitle(`Zulip - ${activeTab.name}`);
347+
}
348+
});
357349

358350
ipcMain.on(
359351
"toggleAutoLauncher",
360-
async (_event: Electron.IpcMainEvent, AutoLaunchValue: boolean) => {
352+
async (_event: IpcMainEvent, AutoLaunchValue: boolean) => {
361353
await setAutoLaunch(AutoLaunchValue);
362354
},
363355
);
364356

365357
ipcMain.on(
366358
"downloadFile",
367-
(_event: Electron.IpcMainEvent, url: string, downloadPath: string) => {
359+
(_event: IpcMainEvent, url: string, downloadPath: string) => {
368360
page.downloadURL(url);
369361
page.session.once("will-download", async (_event: Event, item) => {
370362
if (ConfigUtil.getConfigItem("promptDownload", false)) {
371-
const showDialogOptions: electron.SaveDialogOptions = {
363+
const showDialogOptions: SaveDialogOptions = {
372364
defaultPath: path.join(downloadPath, item.getFilename()),
373365
};
374366
item.setSaveDialogOptions(showDialogOptions);
@@ -443,26 +435,23 @@ ${error}`,
443435

444436
ipcMain.on(
445437
"realm-name-changed",
446-
(_event: Electron.IpcMainEvent, serverURL: string, realmName: string) => {
438+
(_event: IpcMainEvent, serverURL: string, realmName: string) => {
447439
send(page, "update-realm-name", serverURL, realmName);
448440
},
449441
);
450442

451443
ipcMain.on(
452444
"realm-icon-changed",
453-
(_event: Electron.IpcMainEvent, serverURL: string, iconURL: string) => {
445+
(_event: IpcMainEvent, serverURL: string, iconURL: string) => {
454446
send(page, "update-realm-icon", serverURL, iconURL);
455447
},
456448
);
457449

458-
ipcMain.on(
459-
"save-last-tab",
460-
(_event: Electron.IpcMainEvent, index: number) => {
461-
ConfigUtil.setConfigItem("lastActiveTab", index);
462-
},
463-
);
450+
ipcMain.on("save-last-tab", (_event: IpcMainEvent, index: number) => {
451+
ConfigUtil.setConfigItem("lastActiveTab", index);
452+
});
464453

465-
ipcMain.on("focus-this-webview", (event: Electron.IpcMainEvent) => {
454+
ipcMain.on("focus-this-webview", (event: IpcMainEvent) => {
466455
send(page, "focus-webview-with-id", event.sender.id);
467456
mainWindow.show();
468457
});
@@ -472,8 +461,7 @@ ${error}`,
472461
setInterval(() => {
473462
// Set user idle if no activity in 1 second (idleThresholdSeconds)
474463
const idleThresholdSeconds = 1; // 1 second
475-
const idleState =
476-
electron.powerMonitor.getSystemIdleState(idleThresholdSeconds);
464+
const idleState = powerMonitor.getSystemIdleState(idleThresholdSeconds);
477465
if (idleState === "active") {
478466
send(page, "set-active");
479467
} else {

app/main/linux-update-util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {app, dialog} from "electron";
1+
import {app, dialog} from "electron/main";
22
import fs from "fs";
33
import path from "path";
44

app/main/linuxupdater.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {Notification, app, net} from "electron";
1+
import type {Session} from "electron/main";
2+
import {Notification, app, net} from "electron/main";
23

34
import getStream from "get-stream";
45
import * as semver from "semver";
@@ -14,9 +15,7 @@ const logger = new Logger({
1415
file: "linux-update-util.log",
1516
});
1617

17-
export async function linuxUpdateNotification(
18-
session: Electron.Session,
19-
): Promise<void> {
18+
export async function linuxUpdateNotification(session: Session): Promise<void> {
2019
let url = "https://api.github.com/repos/zulip/zulip-desktop/releases";
2120
url = ConfigUtil.getConfigItem("betaUpdate", false) ? url : url + "/latest";
2221

app/main/menu.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import {BrowserWindow, Menu, app, shell} from "electron";
1+
import {shell} from "electron/common";
2+
import type {MenuItemConstructorOptions} from "electron/main";
3+
import {BrowserWindow, Menu, app} from "electron/main";
24

35
import AdmZip from "adm-zip";
46

@@ -13,9 +15,7 @@ import {send} from "./typed-ipc-main";
1315

1416
const appName = app.name;
1517

16-
function getHistorySubmenu(
17-
enableMenu: boolean,
18-
): Electron.MenuItemConstructorOptions[] {
18+
function getHistorySubmenu(enableMenu: boolean): MenuItemConstructorOptions[] {
1919
return [
2020
{
2121
label: t.__("Back"),
@@ -41,7 +41,7 @@ function getHistorySubmenu(
4141
];
4242
}
4343

44-
function getToolsSubmenu(): Electron.MenuItemConstructorOptions[] {
44+
function getToolsSubmenu(): MenuItemConstructorOptions[] {
4545
return [
4646
{
4747
label: t.__("Check for Updates"),
@@ -107,7 +107,7 @@ function getToolsSubmenu(): Electron.MenuItemConstructorOptions[] {
107107
];
108108
}
109109

110-
function getViewSubmenu(): Electron.MenuItemConstructorOptions[] {
110+
function getViewSubmenu(): MenuItemConstructorOptions[] {
111111
return [
112112
{
113113
label: t.__("Reload"),
@@ -256,7 +256,7 @@ function getViewSubmenu(): Electron.MenuItemConstructorOptions[] {
256256
];
257257
}
258258

259-
function getHelpSubmenu(): Electron.MenuItemConstructorOptions[] {
259+
function getHelpSubmenu(): MenuItemConstructorOptions[] {
260260
return [
261261
{
262262
label: `${appName + " Desktop"} v${app.getVersion()}`,
@@ -290,8 +290,8 @@ function getHelpSubmenu(): Electron.MenuItemConstructorOptions[] {
290290
function getWindowSubmenu(
291291
tabs: TabData[],
292292
activeTabIndex?: number,
293-
): Electron.MenuItemConstructorOptions[] {
294-
const initialSubmenu: Electron.MenuItemConstructorOptions[] = [
293+
): MenuItemConstructorOptions[] {
294+
const initialSubmenu: MenuItemConstructorOptions[] = [
295295
{
296296
label: t.__("Minimize"),
297297
role: "minimize",
@@ -367,7 +367,7 @@ function getWindowSubmenu(
367367
return initialSubmenu;
368368
}
369369

370-
function getDarwinTpl(props: MenuProps): Electron.MenuItemConstructorOptions[] {
370+
function getDarwinTpl(props: MenuProps): MenuItemConstructorOptions[] {
371371
const {tabs, activeTabIndex, enableMenu = false} = props;
372372

373373
return [
@@ -532,7 +532,7 @@ function getDarwinTpl(props: MenuProps): Electron.MenuItemConstructorOptions[] {
532532
];
533533
}
534534

535-
function getOtherTpl(props: MenuProps): Electron.MenuItemConstructorOptions[] {
535+
function getOtherTpl(props: MenuProps): MenuItemConstructorOptions[] {
536536
const {tabs, activeTabIndex, enableMenu = false} = props;
537537
return [
538538
{

app/main/request.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type {ClientRequest, IncomingMessage} from "electron";
2-
import {app, net} from "electron";
1+
import type {ClientRequest, IncomingMessage, Session} from "electron/main";
2+
import {app, net} from "electron/main";
33
import fs from "fs";
44
import path from "path";
55
import stream from "stream";
@@ -57,7 +57,7 @@ const generateFilePath = (url: string): string => {
5757

5858
export const _getServerSettings = async (
5959
domain: string,
60-
session: Electron.Session,
60+
session: Session,
6161
): Promise<ServerConf> => {
6262
const response = await fetchResponse(
6363
net.request({
@@ -89,7 +89,7 @@ export const _getServerSettings = async (
8989

9090
export const _saveServerIcon = async (
9191
url: string,
92-
session: Electron.Session,
92+
session: Session,
9393
): Promise<string> => {
9494
try {
9595
const response = await fetchResponse(net.request({url, session}));
@@ -113,7 +113,7 @@ export const _saveServerIcon = async (
113113

114114
export const _isOnline = async (
115115
url: string,
116-
session: Electron.Session,
116+
session: Session,
117117
): Promise<boolean> => {
118118
try {
119119
const response = await fetchResponse(

app/main/sentry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {app} from "electron";
1+
import {app} from "electron/main";
22

33
import * as Sentry from "@sentry/electron";
44

0 commit comments

Comments
 (0)