Skip to content

mullvad/NSEventMonitor

Repository files navigation

NSEventMonitor

Build Status dependencies Status devDependencies Status

Currently when building menubar apps with Electron, window.on('blur', ...) does not fire if user clicks on other menubar items leaving displayed window on screen.

Native macOS popovers usually hide if user clicks anywhere on screen. This extension attempts to fix that for Electron apps, so the following will never be the case anymore:

Example

import { NSEventMonitor, NSEventMask } from 'nseventmonitor';

let macEventMonitor = new NSEventMonitor();

window.on('blur', () => {
  window.hide();
});

window.on('show', () => {
  // start capturing global mouse events
  macEventMonitor.start((NSEventMask.leftMouseDown | NSEventMask.rightMouseDown), () => {
    window.hide();
  });
});

window.on('hide', () => {
  // stop capturing global mouse events
  macEventMonitor.stop();
});

To run example app:

$ cd example
$ npm i
$ npm start

Electron support

Electron comes with its own Node.js runtime, which may differ from the one installed on your desktop. Therefore all native modules have to be built specifically for Electron.

We strongly advise to use electron-builder and add a postinstall script to your package.json in order to automatically compile native modules for Electron:

electron-builder install-app-deps

Building from source

To compile the extension run the following command:

$ npm i --build-from-source

You can confirm everything built correctly by running the test suite.

To run tests:

$ npm test

Note: tests are currently disabled due to issues with automation on macOS.