Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

desktop: refactor `global.win` -> `globalThis.window`

Abdullah Atta11514051b654
diff --git a/apps/web/desktop/config/theme.js b/apps/web/desktop/config/theme.js
index 24db3533b..6c22d0d6c 100644
--- a/apps/web/desktop/config/theme.js
+++ b/apps/web/desktop/config/theme.js
@@ -26,7 +26,8 @@ function getTheme() {
function setTheme(theme) {
nativeTheme.themeSource = theme;
- if (global.win) global.win.setBackgroundColor(getBackgroundColor(theme));
+ if (globalThis.window)
+ globalThis.window.setBackgroundColor(getBackgroundColor(theme));
return JSONStorage.set("theme", theme);
}
diff --git a/apps/web/desktop/electron.js b/apps/web/desktop/electron.js
index b28d9d2b0..a69585d87 100644
--- a/apps/web/desktop/electron.js
+++ b/apps/web/desktop/electron.js
@@ -50,15 +50,10 @@ if (process.platform === "win32") {
app.setAppUserModelId(app.name);
}
-/**
- * @type {BrowserWindow}
- */
-let mainWindow;
-
async function createWindow() {
const mainWindowState = new WindowState({});
setTheme(getTheme());
- mainWindow = new BrowserWindow({
+ const mainWindow = new BrowserWindow({
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
@@ -82,7 +77,7 @@ async function createWindow() {
});
mainWindow.setAutoHideMenuBar(true);
mainWindowState.manage(mainWindow);
- global.win = mainWindow;
+ globalThis.window = mainWindow;
setupMenu(mainWindow);
if (isDevelopment())
@@ -101,8 +96,7 @@ async function createWindow() {
}
mainWindow.on("closed", () => {
- mainWindow = null;
- global.win = null;
+ globalThis.window = null;
});
mainWindow.webContents.setWindowOpenHandler((details) => {
@@ -135,7 +129,7 @@ app.on("window-all-closed", () => {
});
app.on("activate", () => {
- if (mainWindow === null) {
+ if (globalThis.window === null) {
createWindow();
}
});
diff --git a/apps/web/desktop/global.d.ts b/apps/web/desktop/global.d.ts
new file mode 100644
index 000000000..2f1ef14e4
--- /dev/null
+++ b/apps/web/desktop/global.d.ts
@@ -0,0 +1,25 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import { BrowserWindow } from "electron";
+
+declare global {
+ // eslint-disable-next-line no-var
+ var window: BrowserWindow;
+}
diff --git a/apps/web/desktop/ipc/actions/changeAppTheme.js b/apps/web/desktop/ipc/actions/changeAppTheme.js
index 498197acf..f178b2555 100644
--- a/apps/web/desktop/ipc/actions/changeAppTheme.js
+++ b/apps/web/desktop/ipc/actions/changeAppTheme.js
@@ -20,7 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { setTheme } from "../../config/theme";
export default (args) => {
- if (!global.win) return;
+ if (!globalThis.window) return;
const { theme } = args;
setTheme(theme);
};
diff --git a/apps/web/desktop/ipc/actions/setPrivacyMode.js b/apps/web/desktop/ipc/actions/setPrivacyMode.js
index facdde2b3..bd9995046 100644
--- a/apps/web/desktop/ipc/actions/setPrivacyMode.js
+++ b/apps/web/desktop/ipc/actions/setPrivacyMode.js
@@ -20,10 +20,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { setPrivacyMode } from "../../config/privacyMode";
export default (args) => {
- if (!global.win) return;
+ if (!globalThis.window) return;
const { privacyMode } = args;
- global.win.setContentProtection(privacyMode);
- global.win.setThumbnailClip(
+ globalThis.window.setContentProtection(privacyMode);
+ globalThis.window.setThumbnailClip(
privacyMode
? { x: 0, y: 0, width: 1, height: 1 }
: { x: 0, y: 0, width: 0, height: 0 }
diff --git a/apps/web/desktop/ipc/actions/setZoomFactor.js b/apps/web/desktop/ipc/actions/setZoomFactor.js
index 06a193a0e..7dbd3dbd2 100644
--- a/apps/web/desktop/ipc/actions/setZoomFactor.js
+++ b/apps/web/desktop/ipc/actions/setZoomFactor.js
@@ -20,8 +20,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { setZoomFactor } from "../../config/zoomfactor";
export default (args) => {
- if (!global.win) return;
+ if (!globalThis.window) return;
const { zoomFactor } = args;
- global.win.webContents.setZoomFactor(zoomFactor);
+ globalThis.window.webContents.setZoomFactor(zoomFactor);
setZoomFactor(zoomFactor);
};
diff --git a/apps/web/desktop/ipc/index.js b/apps/web/desktop/ipc/index.js
index 1600ccbc7..05862dbe3 100644
--- a/apps/web/desktop/ipc/index.js
+++ b/apps/web/desktop/ipc/index.js
@@ -37,5 +37,5 @@ ipcMain.handle("fromRenderer", async (event, args) => {
const call = getCall(type);
if (!call) return;
- return await call(args, global.win);
+ return await call(args, globalThis.window);
});
diff --git a/apps/web/desktop/ipc/utils.js b/apps/web/desktop/ipc/utils.js
index 968658204..54d96b915 100644
--- a/apps/web/desktop/ipc/utils.js
+++ b/apps/web/desktop/ipc/utils.js
@@ -24,7 +24,8 @@ import { isAbsolute, join } from "path";
function sendMessageToRenderer(type, payload = {}) {
const message = { type, ...payload };
logger.info("Sending message to renderer", message);
- if (global.win) global.win.webContents.send("fromMain", message);
+ if (globalThis.window)
+ globalThis.window.webContents.send("fromMain", message);
}
function resolvePath(_path) {
diff --git a/apps/web/desktop/protocol.js b/apps/web/desktop/protocol.js
index 3f137e970..a522ef297 100644
--- a/apps/web/desktop/protocol.js
+++ b/apps/web/desktop/protocol.js
@@ -117,7 +117,7 @@ async function getBody(request) {
/**
* @type {Electron.Session}
*/
- const session = global.win.webContents.session;
+ const session = globalThis.window.webContents.session;
const blobParts = [];
if (!request.uploadData || !request.uploadData.length) return null;