diff --git a/main.js b/main.js index 8881280d2..62576b1b2 100644 --- a/main.js +++ b/main.js @@ -4,6 +4,7 @@ const ipc = electron.ipcMain; var path = require('path'); var ghReleases = require('electron-gh-releases'); +var Positioner = require('electron-positioner'); require('crash-reporter').start(); @@ -22,21 +23,18 @@ var autoStart = new AutoLaunch({ }); app.on('ready', function() { + var cachedBounds; var appIcon = new Tray(iconIdle); - - var options = { - x: null, - y: null - }; + var windowPosition = (process.platform === 'win32') ? 'trayBottomCenter' : 'trayCenter'; initWindow(); appIcon.on('click', function (e, bounds) { - if (appIcon.window && appIcon.window.isVisible()) { - return hideWindow(); - } else { - showWindow(bounds); - } + if (e.altKey || e.shiftKey || e.ctrlKey || e.metaKey) return hideWindow(); + if (appIcon.window && appIcon.window.isVisible()) return hideWindow(); + bounds = bounds || cachedBounds; + cachedBounds = bounds; + showWindow(cachedBounds); }); function initWindow () { @@ -46,10 +44,13 @@ app.on('ready', function() { show: false, frame: false, resizable: false, - 'standard-window': false + webPreferences: { + overlayScrollbars: true + } }; appIcon.window = new BrowserWindow(defaults); + appIcon.positioner = new Positioner(appIcon.window); appIcon.window.loadURL('file://' + __dirname + '/index.html'); appIcon.window.on('blur', hideWindow); appIcon.window.setVisibleOnAllWorkspaces(true); @@ -58,20 +59,16 @@ app.on('ready', function() { checkAutoUpdate(false); } - function showWindow (bounds) { - if (options.x) { - appIcon.window.show(); - } else { - if (bounds) { - options.x = bounds.x - 200 + (bounds.width / 2); - options.y = bounds.y; - appIcon.window.setPosition(options.x, options.y); - } else { - var electronScreen = require('screen'); - var defaultSize = electronScreen.getPrimaryDisplay().workAreaSize; - appIcon.window.setPosition(defaultSize.width - 525, 15); - } + function showWindow (trayPos) { + // Thanks to https://github.com/maxogden/menubar/ + // Default the window to the right if `trayPos` bounds are undefined or null. + var noBoundsPosition; + if (trayPos === undefined || trayPos.x === 0) { + noBoundsPosition = (process.platform === 'win32') ? 'bottomRight' : 'topRight'; } + + var position = appIcon.positioner.calculate(noBoundsPosition || windowPosition, trayPos); + appIcon.window.setPosition(position.x, position.y); appIcon.window.show(); } @@ -102,7 +99,7 @@ app.on('ready', function() { } function hideWindow () { - if (!appIcon.window) { return; } + if (!appIcon.window) return; appIcon.window.hide(); } @@ -163,7 +160,7 @@ app.on('ready', function() { } ipc.on('reopen-window', function() { - showWindow(); + showWindow(cachedBounds); }); ipc.on('update-icon', function(event, arg) { diff --git a/package.json b/package.json index 4f9baa8b2..94234dbfd 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "auto-launch": "=1.0.1", "bootstrap": "=3.3.6", "electron-gh-releases": "=2.0.2", + "electron-positioner": "=2.0.2", "font-awesome": "=4.5.0", "history": "=1.14.0", "octicons": "=3.1.0",