Skip to content

Commit 91e0111

Browse files
imlucaskangas
authored andcommitted
📝 How window-manager.js works
1 parent a908bf4 commit 91e0111

File tree

1 file changed

+65
-13
lines changed

1 file changed

+65
-13
lines changed

src/electron/window-manager.js

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,74 @@
1+
/**
2+
* A high-level wrapper around electron's builtin
3+
* [BrowserWindow](https://github.com/atom/electron/blob/master/docs/api/browser-window.md)
4+
* class
5+
*/
6+
var path = require('path');
17
var _ = require('lodash');
2-
3-
var BrowserWindow = require('browser-window');
48
var app = require('app');
5-
var debug = require('debug')('scout-electron:window-manager');
69
var attachMenu = require('./menu');
7-
var path = require('path');
10+
var BrowserWindow = require('browser-window');
11+
var debug = require('debug')('scout-electron:window-manager');
812

13+
/**
14+
* When running in electron, we're in `RESOURCES/src/electron`.
15+
*/
916
var RESOURCES = path.resolve(__dirname, '../../');
17+
18+
/**
19+
* The app's HTML shell which is the output of `./src/index.jade`
20+
* created by the `build:pages` gulp task.
21+
*/
1022
var DEFAULT_URL = 'file://' + path.join(RESOURCES, 'index.html#connect');
1123

24+
/**
25+
* The outer dimensions to use for new windows.
26+
*/
1227
var DEFAULT_WIDTH = 1024;
1328
var DEFAULT_HEIGHT = 700;
1429

15-
var DEFAULT_HEIGHT_DIALOG;
16-
17-
if (process.platform === 'win32') {
18-
DEFAULT_HEIGHT_DIALOG = 500;
19-
} else if (process.platform === 'linux') {
20-
DEFAULT_HEIGHT_DIALOG = 470;
21-
} else {
22-
DEFAULT_HEIGHT_DIALOG = 440;
23-
}
30+
/**
31+
* The outer window dimensions to use for new dialog
32+
* windows like the connection and setup dialogs.
33+
*/
2434
var DEFAULT_WIDTH_DIALOG = 640;
35+
var DEFAULT_HEIGHT_DIALOG = 600;
36+
/**
37+
* Adjust the heights to account for platforms
38+
* that use a single menu bar at the top of the screen.
39+
*/
40+
if (process.platform === 'linux') {
41+
DEFAULT_HEIGHT_DIALOG -= 30;
42+
DEFAULT_HEIGHT -= 30;
43+
} else if (process.platform === 'darwin') {
44+
DEFAULT_HEIGHT_DIALOG -= 60;
45+
DEFAULT_HEIGHT -= 60;
46+
}
2547

48+
/**
49+
* We want want the Connect dialog window to be special
50+
* and for there to ever only be one instance of it
51+
* so we'll use scope to essentially make it a Singleton.
52+
*/
2653
var connectWindow;
54+
55+
// @todo (imlucas): Removed in setup branch as we dont need to do this anymore
56+
// as a `all-windows-closed` event has been added to the `app` event api
57+
// since this code was laid down.
2758
var windowsOpenCount = 0;
2859

60+
/**
61+
* Call me instead of using `new BrowserWindow()` directly because i'll:
62+
*
63+
* 1. Make sure the window is the right size
64+
* 2. Doesn't load a blank screen
65+
* 3. Overrides `window.open` so we have control over message passing via URL's
66+
67+
*
68+
* @param {Object} opts - Smaller subset of [`BrowserWindow#options`][0].
69+
* @return {BrowserWindow}
70+
* [0]: http://git.io/vnwTY
71+
*/
2972
module.exports.create = function(opts) {
3073
opts = _.defaults(opts || {}, {
3174
width: DEFAULT_WIDTH,
@@ -49,6 +92,7 @@ module.exports.create = function(opts) {
4992
debug('intercepting new-window (disregard the "error" message '
5093
+ 'preventDefault is about to cause)');
5194
event.preventDefault();
95+
5296
module.exports.create({
5397
url: 'file://' + RESOURCES + '/index.html' + decodeURIComponent(url.replace('file://', ''))
5498
});
@@ -61,6 +105,8 @@ module.exports.create = function(opts) {
61105
connectWindow = null;
62106
});
63107
}
108+
109+
// @see `all-windows-closed` above
64110
windowsOpenCount++;
65111
_window.on('closed', function() {
66112
windowsOpenCount--;
@@ -87,6 +133,12 @@ app.on('show connect dialog', function(opts) {
87133
module.exports.create(opts);
88134
});
89135

136+
/**
137+
* When electron's main renderer has completed setup,
138+
* we'll always show the [connect][./src/connect] dialog
139+
* on start which is responsible for retaining it's own
140+
* state between application launches.
141+
*/
90142
app.on('ready', function() {
91143
app.emit('show connect dialog');
92144
});

0 commit comments

Comments
 (0)