Skip to content

Commit

Permalink
Replace nseventmonitor with nseventforwarder
Browse files Browse the repository at this point in the history
  • Loading branch information
raksooo committed Nov 14, 2024
1 parent d36acf1 commit 2c9d60d
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 53 deletions.
32 changes: 5 additions & 27 deletions desktop/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion desktop/packages/mullvad-vpn/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
7 changes: 3 additions & 4 deletions desktop/packages/mullvad-vpn/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
14 changes: 7 additions & 7 deletions desktop/packages/mullvad-vpn/src/main/user-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,22 +487,22 @@ 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()) {
return;
}

// 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<typeof nseventforwarder.start>;

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
Expand Down
35 changes: 21 additions & 14 deletions desktop/packages/mullvad-vpn/tasks/distribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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'),
Expand Down Expand Up @@ -329,13 +329,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;
Expand All @@ -348,17 +350,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) => {
Expand Down Expand Up @@ -538,6 +530,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';
Expand Down
10 changes: 10 additions & 0 deletions desktop/packages/mullvad-vpn/tasks/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 2 additions & 0 deletions desktop/packages/nseventforwarder/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ case "$TARGET_TRIPLE" in
esac

if [[ "$(uname -s)" == "Darwin" ]]; then
# We rely (heavily) on a pre-defined CARGO_TARGET_DIR, so don't let any user override it
unset CARGO_TARGET_DIR
npm run cargo-build -- --release --target "$TARGET_TRIPLE"
# Copy the neon library to the correct dists folder, which is what node will
# pick up when loading the library at runtime.
Expand Down

0 comments on commit 2c9d60d

Please sign in to comment.