33 * [BrowserWindow](https://github.com/atom/electron/blob/master/docs/api/browser-window.md)
44 * class
55 */
6- var path = require ( 'path' ) ;
7- var _ = require ( 'lodash' ) ;
8- var app = require ( 'app' ) ;
6+
97var AppMenu = require ( './menu' ) ;
108var BrowserWindow = require ( 'browser-window' ) ;
9+ var Notifier = require ( 'node-notifier' ) ;
10+ var Path = require ( 'path' ) ;
11+
12+ var _ = require ( 'lodash' ) ;
13+ var app = require ( 'app' ) ;
1114var config = require ( './config' ) ;
1215var debug = require ( 'debug' ) ( 'scout-electron:window-manager' ) ;
1316var dialog = require ( 'dialog' ) ;
14- var Notifier = require ( 'node-notifier' ) ;
1517
1618/**
1719 * When running in electron, we're in `RESOURCES/src/electron`.
1820 */
19- var RESOURCES = path . resolve ( __dirname , '../../' ) ;
21+ var RESOURCES = Path . resolve ( __dirname , '../../' ) ;
22+ var SCOUT_ICON_PATH = RESOURCES + '/images/scout.png' ;
2023
2124/**
2225 * The app's HTML shell which is the output of `./src/index.jade`
2326 * created by the `build:pages` gulp task.
2427 */
25- var DEFAULT_URL = 'file://' + path . join ( RESOURCES , 'index.html#connect' ) ;
28+ var DEFAULT_URL = 'file://' + Path . join ( RESOURCES , 'index.html#connect' ) ;
2629
2730/**
2831 * We want the Connect dialog window to be special
@@ -36,6 +39,38 @@ var connectWindow;
3639// since this code was laid down.
3740var windowsOpenCount = 0 ;
3841
42+ function isConnectDialog ( url ) {
43+ return url === DEFAULT_URL ;
44+ }
45+
46+ // returns true if the application is a single instance application otherwise
47+ // focus the second window (which we'll quit from) and return false
48+ // see "app.makeSingleInstance" in https://github.com/atom/electron/blob/master/docs/api/app.md
49+ function isSingleInstance ( _window ) {
50+ var isNotSingle = app . makeSingleInstance ( function ( commandLine , workingDirectory ) {
51+ debug ( 'Someone tried to run a second instance! We should focus our window' , {
52+ commandLine : commandLine ,
53+ workingDirectory : workingDirectory
54+ } ) ;
55+ if ( _window ) {
56+ if ( _window . isMinimized ( ) ) {
57+ _window . restore ( ) ;
58+ }
59+ _window . focus ( ) ;
60+ }
61+ return true ;
62+ } ) ;
63+
64+ return ! isNotSingle ;
65+ }
66+
67+ function openDevTools ( ) {
68+ debug ( 'openDevTools()' ) ;
69+ AppMenu . lastFocusedWindow . openDevTools ( {
70+ detach : true
71+ } ) ;
72+ }
73+
3974/**
4075 * Call me instead of using `new BrowserWindow()` directly because i'll:
4176 *
@@ -66,23 +101,7 @@ module.exports.create = function(opts) {
66101 } ) ;
67102 AppMenu . load ( _window ) ;
68103
69- // makes the application a single instance application
70- // see "app.makeSingleInstance" in https://github.com/atom/electron/blob/master/docs/api/app.md
71- var shouldQuit = app . makeSingleInstance ( function ( commandLine , workingDirectory ) {
72- debug ( 'Someone tried to run a second instance! We should focus our window' , {
73- commandLine : commandLine ,
74- workingDirectory : workingDirectory
75- } ) ;
76- if ( _window ) {
77- if ( _window . isMinimized ( ) ) {
78- _window . restore ( ) ;
79- }
80- _window . focus ( ) ;
81- }
82- return true ;
83- } ) ;
84-
85- if ( shouldQuit ) {
104+ if ( ! isSingleInstance ( _window ) ) {
86105 app . quit ( ) ;
87106 return null ;
88107 }
@@ -99,9 +118,13 @@ module.exports.create = function(opts) {
99118 } ) ;
100119 } ) ;
101120
102- if ( opts . url === DEFAULT_URL ) { // if it's the connect dialog
121+ if ( isConnectDialog ( opts . url ) ) {
103122 AppMenu . hideConnect ( _window ) ;
104123 connectWindow = _window ;
124+ connectWindow . on ( 'focus' , function ( ) {
125+ debug ( 'connect window focused.' ) ;
126+ connectWindow . webContents . send ( 'message' , 'connect-window-focused' ) ;
127+ } ) ;
105128 connectWindow . on ( 'closed' , function ( ) {
106129 debug ( 'connect window closed.' ) ;
107130 connectWindow = null ;
@@ -167,10 +190,17 @@ app.on('show share submenu', function() {
167190app . on ( 'show bugsnag OS notification' , function ( errorMsg ) {
168191 if ( _ . contains ( [ 'development' , 'testing' ] , process . env . NODE_ENV ) ) {
169192 Notifier . notify ( {
170- 'icon' : RESOURCES + '/images/scout.png' ,
193+ 'icon' : SCOUT_ICON_PATH ,
171194 'message' : errorMsg ,
172195 'title' : 'MongoDB Compass Exception' ,
173196 'wait' : true
197+ } , function ( err , resp ) {
198+ if ( err ) {
199+ debug ( err ) ;
200+ }
201+ if ( resp === 'Activate\n' ) {
202+ openDevTools ( ) ;
203+ }
174204 } ) ;
175205 }
176206} ) ;
@@ -183,12 +213,6 @@ app.on('show bugsnag OS notification', function(errorMsg) {
183213 */
184214app . on ( 'ready' , function ( ) {
185215 app . emit ( 'show connect dialog' ) ;
186-
187- Notifier . on ( 'click' , function ( ) {
188- AppMenu . lastFocusedWindow . openDevTools ( {
189- detach : true
190- } ) ;
191- } ) ;
192216} ) ;
193217
194218var ipc = require ( 'ipc' ) ;
0 commit comments