Skip to content

Commit a02eeab

Browse files
imlucaskangas
authored andcommitted
INT-357 fix(electron) window management improvements
Squashed: fix(electron): You can close the connect window and not quit the app fix(electron): open connect dialog via app event fix(electron): File menu for win32 + show connect dialog fix(electron): track open windows and quit when the last one closed
1 parent 3d98040 commit a02eeab

File tree

2 files changed

+68
-25
lines changed

2 files changed

+68
-25
lines changed

src/electron/menu.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ var app = require('app');
22
var Menu = require('menu');
33
var debug = require('debug')('scout-electron:menu');
44

5-
65
function getTemplate(_window) {
76
if (process.platform === 'darwin') {
87
return [
@@ -83,6 +82,13 @@ function getTemplate(_window) {
8382
{
8483
label: 'View',
8584
submenu: [
85+
{
86+
label: 'Connect to...',
87+
accelerator: 'Command+N',
88+
click: function() {
89+
app.emit('show connect dialog');
90+
}
91+
},
8692
{
8793
label: 'Reload',
8894
accelerator: 'Command+R',
@@ -125,6 +131,28 @@ function getTemplate(_window) {
125131
}
126132

127133
return [
134+
{
135+
label: 'File',
136+
submenu: [
137+
{
138+
label: 'Connect to...',
139+
accelerator: 'Ctrl+N',
140+
click: function() {
141+
app.emit('show connect dialog');
142+
}
143+
},
144+
{
145+
type: 'separator'
146+
},
147+
{
148+
label: 'Quit',
149+
accelerator: 'Ctrl+Q',
150+
click: function() {
151+
app.quit();
152+
}
153+
}
154+
]
155+
},
128156
{
129157
label: 'View',
130158
submenu: [

src/electron/window-manager.js

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@ var DEFAULT_URL = 'file://' + path.join(RESOURCES, 'index.html#connect');
1212
var DEFAULT_WIDTH = 1024;
1313
var DEFAULT_HEIGHT = 700;
1414

15+
var DEFAULT_HEIGHT_DIALOG;
16+
17+
if (process.platform === 'win32') {
18+
DEFAULT_HEIGHT_DIALOG = 460;
19+
} else if (process.platform === 'linux') {
20+
DEFAULT_HEIGHT_DIALOG = 430;
21+
} else {
22+
DEFAULT_HEIGHT_DIALOG = 400;
23+
}
1524
var DEFAULT_WIDTH_DIALOG = 600;
16-
var DEFAULT_HEIGHT_DIALOG = 400;
1725

18-
var main = module.exports.main = null;
19-
var childWindows = [];
26+
var connectWindow;
27+
var windowsOpenCount = 0;
2028

2129
module.exports.create = function(opts) {
2230
opts = _.defaults(opts || {}, {
@@ -36,41 +44,48 @@ module.exports.create = function(opts) {
3644
});
3745
attachMenu(_window);
3846
_window.loadUrl(opts.url);
39-
if (main) {
40-
childWindows.push(_window);
41-
}
47+
4248
_window.webContents.on('new-window', function(event, url, frameName, disposition) {
4349
debug('got new-window event!', event, url, frameName, disposition);
4450
event.preventDefault();
4551
module.exports.create({
4652
url: 'file://' + RESOURCES + '/index.html' + url.replace('file://', '')
4753
});
4854
});
55+
56+
if (opts.url === DEFAULT_URL) {
57+
connectWindow = _window;
58+
connectWindow.on('closed', function() {
59+
debug('connect window closed.');
60+
connectWindow = null;
61+
});
62+
}
63+
windowsOpenCount++;
64+
_window.on('closed', function() {
65+
windowsOpenCount--;
66+
if (windowsOpenCount === 0) {
67+
debug('all windows closed. quitting.');
68+
app.quit();
69+
}
70+
});
4971
return _window;
5072
};
5173

52-
app.on('ready', function() {
53-
// var height = DEFAULT_HEIGHT;
54-
var height = DEFAULT_HEIGHT_DIALOG;
55-
if (process.platform === 'win32') {
56-
height += 60;
57-
} else if (process.platform === 'linux') {
58-
height += 30;
74+
app.on('show connect dialog', function(opts) {
75+
if (connectWindow) {
76+
connectWindow.focus();
77+
return connectWindow;
5978
}
60-
debug('loading main window', DEFAULT_URL);
6179

62-
main = module.exports.main = module.exports.create({
63-
height: height,
80+
opts = opts || {};
81+
opts = _.extend(opts || {}, {
82+
height: DEFAULT_HEIGHT_DIALOG,
6483
width: DEFAULT_WIDTH_DIALOG,
6584
url: DEFAULT_URL
6685
});
86+
module.exports.create(opts);
87+
});
6788

68-
main.on('closed', function() {
69-
debug('main window closed. killing children.');
70-
main = null;
71-
/*eslint no-unused-vars:0*/
72-
childWindows.map(function(_window) {
73-
_window = null;
74-
});
75-
});
89+
app.on('ready', function() {
90+
app.emit('show connect dialog');
7691
});

0 commit comments

Comments
 (0)