From 79af84864d16060f4811d1db3a9fbf85f811b747 Mon Sep 17 00:00:00 2001 From: Oskar Date: Wed, 13 Nov 2024 15:13:47 +0100 Subject: [PATCH] Replace `nseventmonitor` with `nseventforwarder` --- desktop/package-lock.json | 32 +++-------------- desktop/packages/mullvad-vpn/gulpfile.js | 5 ++- desktop/packages/mullvad-vpn/package.json | 7 ++-- .../mullvad-vpn/src/main/user-interface.ts | 14 ++++---- .../mullvad-vpn/tasks/distribution.js | 35 +++++++++++-------- desktop/packages/mullvad-vpn/tasks/scripts.js | 10 ++++++ 6 files changed, 50 insertions(+), 53 deletions(-) diff --git a/desktop/package-lock.json b/desktop/package-lock.json index f454d28091c6..5f146d52b1df 100644 --- a/desktop/package-lock.json +++ b/desktop/package-lock.json @@ -10011,6 +10011,7 @@ "version": "2.19.0", "resolved": "https://registry.npmjs.org/nan/-/nan-2.19.0.tgz", "integrity": "sha512-nO1xXxfh/RWNxfd/XPfbIfFk5vgLsAxUR9y5O0cHMJu/AW9U95JLXqthYHjEp+8gQ5p96K9jUp8nbVOxCdRbtw==", + "dev": true, "optional": true }, "node_modules/nanoid": { @@ -10183,20 +10184,6 @@ "resolved": "packages/nseventforwarder", "link": true }, - "node_modules/nseventmonitor": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/nseventmonitor/-/nseventmonitor-1.0.5.tgz", - "integrity": "sha512-CP1Qvr3yAY08o+2KpNUMjVTM6KM37sQEgRp+3KvzWCcutzwQwsUy4IkixModqgmxqB3BP281SRlizqOnf7sk0w==", - "hasInstallScript": true, - "optional": true, - "os": [ - "darwin" - ], - "dependencies": { - "@mapbox/node-pre-gyp": "^1.0.11", - "nan": "^2.19.0" - } - }, "node_modules/number-is-nan": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz", @@ -14624,6 +14611,7 @@ "gl-matrix": "^3.4.3", "google-protobuf": "^3.21.0", "node-gettext": "^3.0.0", + "nseventforwarder": "0.0.0", "react": "^18.3.1", "react-dom": "^18.3.1", "react-redux": "^7.2.9", @@ -14677,8 +14665,7 @@ "xvfb-maybe": "^0.2.1" }, "optionalDependencies": { - "grpc-tools": "^1.12.4", - "nseventmonitor": "^1.0.5" + "grpc-tools": "^1.12.4" } }, "packages/nseventforwarder": { @@ -22538,7 +22525,7 @@ "gulp-typescript": "^6.0.0-alpha.1", "mocha": "^10.2.0", "node-gettext": "^3.0.0", - "nseventmonitor": "^1.0.5", + "nseventforwarder": "0.0.0", "playwright": "^1.41.1", "postject": "^1.0.0-alpha.6", "react": "^18.3.1", @@ -22566,6 +22553,7 @@ "version": "2.19.0", "resolved": "https://registry.npmjs.org/nan/-/nan-2.19.0.tgz", "integrity": "sha512-nO1xXxfh/RWNxfd/XPfbIfFk5vgLsAxUR9y5O0cHMJu/AW9U95JLXqthYHjEp+8gQ5p96K9jUp8nbVOxCdRbtw==", + "dev": true, "optional": true }, "nanoid": { @@ -22707,16 +22695,6 @@ "@neon-rs/load": "^0.1.73" } }, - "nseventmonitor": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/nseventmonitor/-/nseventmonitor-1.0.5.tgz", - "integrity": "sha512-CP1Qvr3yAY08o+2KpNUMjVTM6KM37sQEgRp+3KvzWCcutzwQwsUy4IkixModqgmxqB3BP281SRlizqOnf7sk0w==", - "optional": true, - "requires": { - "@mapbox/node-pre-gyp": "^1.0.11", - "nan": "^2.19.0" - } - }, "number-is-nan": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz", diff --git a/desktop/packages/mullvad-vpn/gulpfile.js b/desktop/packages/mullvad-vpn/gulpfile.js index 072e3652e062..ff5a82df17f0 100644 --- a/desktop/packages/mullvad-vpn/gulpfile.js +++ b/desktop/packages/mullvad-vpn/gulpfile.js @@ -23,7 +23,10 @@ task( 'build', series('clean', 'set-prod-env', parallel(assets.copyAll, scripts.buildProto), scripts.build), ); -task('develop', series('clean', 'set-dev-env', scripts.buildProto, watch.start)); +task( + 'develop', + series('clean', 'set-dev-env', scripts.buildProto, scripts.buildNseventforwarder, watch.start), +); task('pack-win', series('build', dist.packWin)); task('pack-linux', series('build', dist.packLinux)); task('pack-mac', series('build', dist.packMac)); diff --git a/desktop/packages/mullvad-vpn/package.json b/desktop/packages/mullvad-vpn/package.json index 104291e82446..b67b7e7421f2 100644 --- a/desktop/packages/mullvad-vpn/package.json +++ b/desktop/packages/mullvad-vpn/package.json @@ -25,11 +25,11 @@ "redux": "^4.2.0", "simple-plist": "^1.3.1", "sprintf-js": "^1.1.2", - "styled-components": "^6.1.0" + "styled-components": "^6.1.0", + "nseventforwarder": "0.0.0" }, "optionalDependencies": { - "grpc-tools": "^1.12.4", - "nseventmonitor": "^1.0.5" + "grpc-tools": "^1.12.4" }, "devDependencies": { "@playwright/test": "^1.41.1", @@ -76,7 +76,6 @@ }, "scripts": { "preinstall": "test -d node_modules || mkdir node_modules", - "postinstall": "cross-env ELECTRON_BUILDER_ALLOW_UNRESOLVED_DEPENDENCIES=true electron-builder install-app-deps", "build": "gulp build", "build-proto": "gulp build-proto", "pack-test-executable": "./scripts/build-test-executable.sh", diff --git a/desktop/packages/mullvad-vpn/src/main/user-interface.ts b/desktop/packages/mullvad-vpn/src/main/user-interface.ts index f7b81247ff26..a0792463732a 100644 --- a/desktop/packages/mullvad-vpn/src/main/user-interface.ts +++ b/desktop/packages/mullvad-vpn/src/main/user-interface.ts @@ -487,7 +487,7 @@ export default class UserInterface implements WindowControllerDelegate { }); } - // setup NSEvent monitor to fix inconsistent window.blur on macOS + // setup NSEvent forwarder to fix inconsistent window.blur on macOS // see https://github.com/electron/electron/issues/8689 private installMacOsMenubarAppWindowHandlers() { if (this.delegate.isUnpinnedWindow()) { @@ -495,14 +495,14 @@ export default class UserInterface implements WindowControllerDelegate { } // eslint-disable-next-line @typescript-eslint/no-require-imports - const { NSEventMonitor, NSEventMask } = require('nseventmonitor'); - const macEventMonitor = new NSEventMonitor(); - const eventMask = NSEventMask.leftMouseDown | NSEventMask.rightMouseDown; + const nseventforwarder = require('nseventforwarder'); + let nseventforwarderStop: ReturnType; - this.windowController.window?.on('show', () => - macEventMonitor.start(eventMask, () => this.windowController.hide()), + this.windowController.window?.on( + 'show', + () => (nseventforwarderStop = nseventforwarder.start(() => this.windowController.hide())), ); - this.windowController.window?.on('hide', () => macEventMonitor.stop()); + this.windowController.window?.on('hide', () => nseventforwarderStop?.()); this.windowController.window?.on('blur', () => { // Make sure to hide the menubar window when other program captures the focus. // But avoid doing that when dev tools capture the focus to make it possible to inspect the UI diff --git a/desktop/packages/mullvad-vpn/tasks/distribution.js b/desktop/packages/mullvad-vpn/tasks/distribution.js index 81ba0da267b0..8037bcbd66b3 100644 --- a/desktop/packages/mullvad-vpn/tasks/distribution.js +++ b/desktop/packages/mullvad-vpn/tasks/distribution.js @@ -60,7 +60,7 @@ const config = { 'node_modules/', '!node_modules/grpc-tools', '!node_modules/@types', - '!node_modules/nseventmonitor/build/Release', + '!node_modules/nseventforwarder/target', ], // Make sure that all files declared in "extraResources" exists and abort if they don't. @@ -84,7 +84,7 @@ const config = { target: 'pkg', arch: getMacArch(), }, - singleArchFiles: 'node_modules/nseventmonitor/lib/binding/Release/**', + singleArchFiles: 'node_modules/nseventforwarder/dist/**', artifactName: 'MullvadVPN-${version}.${ext}', category: 'public.app-category.tools', icon: distAssets('icon-macos.icns'), @@ -328,13 +328,15 @@ function packMac() { config: { ...config, asarUnpack: ['**/*.node'], - beforeBuild: (options) => { + beforeBuild: async (options) => { switch (options.arch) { case 'x64': process.env.TARGET_TRIPLE = 'x86_64-apple-darwin'; + execFileSync('npm', ['-w', 'nseventforwarder', 'run', 'build-x86']); break; case 'arm64': process.env.TARGET_TRIPLE = 'aarch64-apple-darwin'; + execFileSync('npm', ['-w', 'nseventforwarder', 'run', 'build-arm']); break; default: delete process.env.TARGET_TRIPLE; @@ -347,17 +349,7 @@ function packMac() { return true; }, beforePack: async (context) => { - try { - // `@electron/universal` tries to lipo together libraries built for the same architecture - // if they're present for both targets. So make sure we remove libraries for other archs. - // Remove the workaround once the issue has been fixed: - // https://github.com/electron/universal/issues/41#issuecomment-1496288834 - await fs.promises.rm('node_modules/nseventmonitor/lib/binding/Release', { - recursive: true, - }); - } catch { - // noop - } + await removeNseventforwarderNativeModules(); config.beforePack?.(context); }, afterPack: (context) => { @@ -537,6 +529,21 @@ function productVersion(extraArgs) { return execFileSync('cargo', args, { encoding: 'utf-8' }).trim(); } +// `@electron/universal` tries to lipo together libraries built for the same architecture +// if they're present for both targets. So make sure we remove libraries for other archs. +// Remove the workaround once the issue has been fixed: +// https://github.com/electron/universal/issues/41#issuecomment-1496288834 +// +// dist/darwin-x64/index.node +// dist/darwin-arm64/index.node +async function removeNseventforwarderNativeModules() { + try { + await fs.promises.rm('../../node_modules/nseventforwarder/dist/', { recursive: true }); + } catch { + // noop + } +} + packWin.displayName = 'builder-win'; packMac.displayName = 'builder-mac'; packLinux.displayName = 'builder-linux'; diff --git a/desktop/packages/mullvad-vpn/tasks/scripts.js b/desktop/packages/mullvad-vpn/tasks/scripts.js index 20acc69c35c8..9074cd1f1dec 100644 --- a/desktop/packages/mullvad-vpn/tasks/scripts.js +++ b/desktop/packages/mullvad-vpn/tasks/scripts.js @@ -113,12 +113,22 @@ function buildProto(callback) { exec('bash ./scripts/build-proto.sh', (err) => callback(err)); } +function buildNseventforwarder(callback) { + if (process.platform === 'darwin') { + exec('npm -w nseventforwarder run build-debug', (err) => callback(err)); + } else { + callback(); + } +} + compileScripts.displayName = 'compile-scripts'; buildProto.displayName = 'build-proto'; +buildNseventforwarder.displayName = 'build-nseventforwarder'; exports.build = series( compileScripts, parallel(makeBrowserifyPreload(false), makeBrowserifyRenderer(false)), ); exports.buildProto = buildProto; +exports.buildNseventforwarder = buildNseventforwarder; exports.makeWatchCompiler = makeWatchCompiler;