diff --git a/main.js b/main.js index 62576b1b2..59cf35378 100644 --- a/main.js +++ b/main.js @@ -17,6 +17,11 @@ var dialog = require('dialog'); var iconIdle = path.join(__dirname, 'images', 'tray-idleTemplate.png'); var iconActive = path.join(__dirname, 'images', 'tray-active.png'); +// Utilities +var isDarwin = (process.platform === 'darwin'); +var isLinux = (process.platform === 'linux'); +var isWindows = (process.platform === 'win32'); + var autoStart = new AutoLaunch({ name: 'Gitify', path: process.execPath.match(/.*?\.app/)[0] @@ -25,7 +30,7 @@ var autoStart = new AutoLaunch({ app.on('ready', function() { var cachedBounds; var appIcon = new Tray(iconIdle); - var windowPosition = (process.platform === 'win32') ? 'trayBottomCenter' : 'trayCenter'; + var windowPosition = (isWindows) ? 'trayBottomCenter' : 'trayCenter'; initWindow(); @@ -60,11 +65,30 @@ app.on('ready', function() { } 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'; + if (!isDarwin && trayPos !== undefined) { + var displaySize = electron.screen.getPrimaryDisplay().workAreaSize; + var trayPosX = trayPos.x; + var trayPosY = trayPos.y; + + if (isLinux) { + var cursorPointer = electron.screen.getCursorScreenPoint(); + trayPosX = cursorPointer.x; + trayPosY = cursorPointer.y; + } + + var x = (trayPosX < (displaySize.width / 2)) ? 'left' : 'right'; + var y = (trayPosY < (displaySize.height / 2)) ? 'top' : 'bottom'; + + if (x === 'right' && y === 'bottom') { + noBoundsPosition = (isWindows) ? 'trayBottomCenter' : 'bottomRight'; + } else if (x === 'left' && y === 'bottom') { + noBoundsPosition = 'bottomLeft'; + } else if (y === 'top') { + noBoundsPosition = (isWindows) ? 'trayCenter' : 'topRight'; + } + } else if (trayPos === undefined) { + noBoundsPosition = (isWindows) ? 'bottomRight' : 'topRight'; } var position = appIcon.positioner.calculate(noBoundsPosition || windowPosition, trayPos);