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 Clipboard = require ( 'clipboard' ) ;
10+ var Connection = require ( 'mongodb-connection-model' ) ;
11+ var Notifier = require ( 'node-notifier' ) ;
12+
13+ var _ = require ( 'lodash' ) ;
14+ var app = require ( 'app' ) ;
1115var config = require ( './config' ) ;
1216var debug = require ( 'debug' ) ( 'scout-electron:window-manager' ) ;
1317var dialog = require ( 'dialog' ) ;
14- var Notifier = require ( 'node-notifier ' ) ;
18+ var path = require ( 'path ' ) ;
1519
1620/**
1721 * When running in electron, we're in `RESOURCES/src/electron`.
1822 */
1923var RESOURCES = path . resolve ( __dirname , '../../' ) ;
24+ var SCOUT_ICON_PATH = RESOURCES + '/images/scout.png' ;
2025
2126/**
2227 * The app's HTML shell which is the output of `./src/index.jade`
@@ -30,12 +35,56 @@ var DEFAULT_URL = 'file://' + path.join(RESOURCES, 'index.html#connect');
3035 * so we'll use scope to essentially make it a Singleton.
3136 */
3237var connectWindow ;
38+ var curNotifierOnClickFn ;
39+ var lastClipboardTxt ;
3340
3441// @todo (imlucas): Removed in setup branch as we dont need to do this anymore
3542// as a `all-windows-closed` event has been added to the `app` event api
3643// since this code was laid down.
3744var windowsOpenCount = 0 ;
3845
46+ var autofillConnect = function ( ) {
47+ debug ( 'autofillConnect()' ) ;
48+ var connection = Connection . from ( Clipboard . readText ( ) ) . toJSON ( ) ;
49+ connectWindow . webContents . send ( 'message' , 'update-connection' , connection ) ;
50+ } ;
51+
52+ function isConnectDialog ( url ) {
53+ return url === DEFAULT_URL ;
54+ }
55+
56+ function isMongoURI ( str ) {
57+ return str . indexOf ( 'mongodb://' ) > - 1 ;
58+ }
59+
60+ // returns true if the application is a single instance application otherwise
61+ // focus the second window (which we'll quit from) and return false
62+ // see "app.makeSingleInstance" in https://github.com/atom/electron/blob/master/docs/api/app.md
63+ function isSingleInstance ( _window ) {
64+ var isNotSingle = app . makeSingleInstance ( function ( commandLine , workingDirectory ) {
65+ debug ( 'Someone tried to run a second instance! We should focus our window' , {
66+ commandLine : commandLine ,
67+ workingDirectory : workingDirectory
68+ } ) ;
69+ if ( _window ) {
70+ if ( _window . isMinimized ( ) ) {
71+ _window . restore ( ) ;
72+ }
73+ _window . focus ( ) ;
74+ }
75+ return true ;
76+ } ) ;
77+
78+ return ! isNotSingle ;
79+ }
80+
81+ function openDevTools ( ) {
82+ debug ( 'openDevTools()' ) ;
83+ AppMenu . lastFocusedWindow . openDevTools ( {
84+ detach : true
85+ } ) ;
86+ }
87+
3988/**
4089 * Call me instead of using `new BrowserWindow()` directly because i'll:
4190 *
@@ -66,23 +115,7 @@ module.exports.create = function(opts) {
66115 } ) ;
67116 AppMenu . load ( _window ) ;
68117
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 ) {
118+ if ( ! isSingleInstance ( _window ) ) {
86119 app . quit ( ) ;
87120 return null ;
88121 }
@@ -99,9 +132,28 @@ module.exports.create = function(opts) {
99132 } ) ;
100133 } ) ;
101134
102- if ( opts . url === DEFAULT_URL ) { // if it's the connect dialog
135+ if ( isConnectDialog ( opts . url ) ) {
103136 AppMenu . hideConnect ( _window ) ;
104137 connectWindow = _window ;
138+ connectWindow . on ( 'focus' , function ( ) {
139+ debug ( 'connect window focused.' ) ;
140+ var cbTxt = Clipboard . readText ( ) ;
141+ if ( cbTxt === lastClipboardTxt ) {
142+ return ;
143+ }
144+ lastClipboardTxt = cbTxt ;
145+
146+ if ( isMongoURI ( cbTxt ) ) {
147+ debug ( 'mongoURI detected.' ) ;
148+ curNotifierOnClickFn = autofillConnect ;
149+ Notifier . notify ( {
150+ 'icon' : SCOUT_ICON_PATH ,
151+ 'message' : 'Click this notification to autofill the connection fields from your clipboard.' ,
152+ 'title' : 'Autofill Connection' ,
153+ 'wait' : true
154+ } ) ;
155+ }
156+ } ) ;
105157 connectWindow . on ( 'closed' , function ( ) {
106158 debug ( 'connect window closed.' ) ;
107159 connectWindow = null ;
@@ -166,8 +218,9 @@ app.on('show share submenu', function() {
166218
167219app . on ( 'show bugsnag OS notification' , function ( errorMsg ) {
168220 if ( _ . contains ( [ 'development' , 'testing' ] , process . env . NODE_ENV ) ) {
221+ curNotifierOnClickFn = openDevTools ;
169222 Notifier . notify ( {
170- 'icon' : RESOURCES + '/images/scout.png' ,
223+ 'icon' : SCOUT_ICON_PATH ,
171224 'message' : errorMsg ,
172225 'title' : 'MongoDB Compass Exception' ,
173226 'wait' : true
@@ -185,9 +238,7 @@ app.on('ready', function() {
185238 app . emit ( 'show connect dialog' ) ;
186239
187240 Notifier . on ( 'click' , function ( ) {
188- AppMenu . lastFocusedWindow . openDevTools ( {
189- detach : true
190- } ) ;
241+ curNotifierOnClickFn ( ) ;
191242 } ) ;
192243} ) ;
193244
0 commit comments