Skip to content

Commit 4432c1f

Browse files
Merge pull request #131 from ekonstantinidis/fix-positioning
Fix Window Positioning
2 parents 8846ed2 + 82aa76a commit 4432c1f

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

main.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const ipc = electron.ipcMain;
44

55
var path = require('path');
66
var ghReleases = require('electron-gh-releases');
7+
var Positioner = require('electron-positioner');
78

89
require('crash-reporter').start();
910

@@ -22,21 +23,18 @@ var autoStart = new AutoLaunch({
2223
});
2324

2425
app.on('ready', function() {
26+
var cachedBounds;
2527
var appIcon = new Tray(iconIdle);
26-
27-
var options = {
28-
x: null,
29-
y: null
30-
};
28+
var windowPosition = (process.platform === 'win32') ? 'trayBottomCenter' : 'trayCenter';
3129

3230
initWindow();
3331

3432
appIcon.on('click', function (e, bounds) {
35-
if (appIcon.window && appIcon.window.isVisible()) {
36-
return hideWindow();
37-
} else {
38-
showWindow(bounds);
39-
}
33+
if (e.altKey || e.shiftKey || e.ctrlKey || e.metaKey) return hideWindow();
34+
if (appIcon.window && appIcon.window.isVisible()) return hideWindow();
35+
bounds = bounds || cachedBounds;
36+
cachedBounds = bounds;
37+
showWindow(cachedBounds);
4038
});
4139

4240
function initWindow () {
@@ -46,10 +44,13 @@ app.on('ready', function() {
4644
show: false,
4745
frame: false,
4846
resizable: false,
49-
'standard-window': false
47+
webPreferences: {
48+
overlayScrollbars: true
49+
}
5050
};
5151

5252
appIcon.window = new BrowserWindow(defaults);
53+
appIcon.positioner = new Positioner(appIcon.window);
5354
appIcon.window.loadURL('file://' + __dirname + '/index.html');
5455
appIcon.window.on('blur', hideWindow);
5556
appIcon.window.setVisibleOnAllWorkspaces(true);
@@ -58,20 +59,16 @@ app.on('ready', function() {
5859
checkAutoUpdate(false);
5960
}
6061

61-
function showWindow (bounds) {
62-
if (options.x) {
63-
appIcon.window.show();
64-
} else {
65-
if (bounds) {
66-
options.x = bounds.x - 200 + (bounds.width / 2);
67-
options.y = bounds.y;
68-
appIcon.window.setPosition(options.x, options.y);
69-
} else {
70-
var electronScreen = require('screen');
71-
var defaultSize = electronScreen.getPrimaryDisplay().workAreaSize;
72-
appIcon.window.setPosition(defaultSize.width - 525, 15);
73-
}
62+
function showWindow (trayPos) {
63+
// Thanks to https://github.com/maxogden/menubar/
64+
// Default the window to the right if `trayPos` bounds are undefined or null.
65+
var noBoundsPosition;
66+
if (trayPos === undefined || trayPos.x === 0) {
67+
noBoundsPosition = (process.platform === 'win32') ? 'bottomRight' : 'topRight';
7468
}
69+
70+
var position = appIcon.positioner.calculate(noBoundsPosition || windowPosition, trayPos);
71+
appIcon.window.setPosition(position.x, position.y);
7572
appIcon.window.show();
7673
}
7774

@@ -102,7 +99,7 @@ app.on('ready', function() {
10299
}
103100

104101
function hideWindow () {
105-
if (!appIcon.window) { return; }
102+
if (!appIcon.window) return;
106103
appIcon.window.hide();
107104
}
108105

@@ -163,7 +160,7 @@ app.on('ready', function() {
163160
}
164161

165162
ipc.on('reopen-window', function() {
166-
showWindow();
163+
showWindow(cachedBounds);
167164
});
168165

169166
ipc.on('update-icon', function(event, arg) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"auto-launch": "=1.0.1",
4747
"bootstrap": "=3.3.6",
4848
"electron-gh-releases": "=2.0.2",
49+
"electron-positioner": "=2.0.2",
4950
"font-awesome": "=4.5.0",
5051
"history": "=1.14.0",
5152
"octicons": "=3.1.0",

0 commit comments

Comments
 (0)