@@ -5,18 +5,27 @@ var dialog = require('dialog');
55var Menu = require ( 'menu' ) ;
66var shell = require ( 'shell' ) ;
77var BrowserWindow = require ( 'browser-window' ) ;
8+ var os = require ( 'os' ) ;
9+ var autoUpdater = require ( 'auto-updater' ) ;
810
9- app . on ( 'window-all-closed' , function ( ) {
10- if ( process . platform != 'darwin' ) {
11- app . quit ( ) ;
12- }
13- } ) ;
11+ var applySettings , settings , settingsFilePath = path . join ( app . getPath ( 'userData' ) , 'settings.json' ) ;
12+ try {
13+ settings = JSON . parse ( fs . readFileSync ( settingsFilePath ) ) ;
14+ } catch ( e ) {
15+ settings = {
16+ appUrl : 'https://app.classeur.io'
17+ } ;
18+ }
1419
15- var appUri = 'http://localhost:11583' ;
20+ if ( process . platform === 'darwin' ) {
21+ var platform = os . platform ( ) + '_' + os . arch ( ) ;
22+ var version = app . getVersion ( ) ;
23+ autoUpdater . setFeedUrl ( 'http://download.classeur.io/update/' + platform + '/' + version ) ;
24+ }
1625
1726function checkUrl ( url ) {
1827 url = typeof url === 'string' ? url : url . getUrl ( ) ;
19- return url . slice ( 0 , appUri . length ) === appUri ;
28+ return url . slice ( 0 , settings . appUrl . length ) === settings . appUrl ;
2029}
2130
2231function checkOrigin ( cb ) {
@@ -84,6 +93,7 @@ ClasseurCtx.prototype.clean = function() {
8493} ;
8594
8695var lastWindowOffset = 0 ;
96+
8797function createWindow ( cb ) {
8898 var browserWindow = new BrowserWindow ( {
8999 width : 1050 ,
@@ -99,8 +109,8 @@ function createWindow(cb) {
99109 browserWindow . webContents . classeurCtx = classeurCtx ;
100110
101111 windows [ browserWindow . id ] = browserWindow ;
102- browserWindow . loadUrl ( appUri ) ;
103- browserWindow . openDevTools ( ) ;
112+ browserWindow . loadUrl ( settings . appUrl ) ;
113+ // browserWindow.openDevTools();
104114 browserWindow . on ( 'closed' , function ( ) {
105115 classeurCtx . clean ( ) ;
106116 delete windows [ browserWindow . id ] ;
@@ -120,6 +130,25 @@ function createWindow(cb) {
120130 return browserWindow ;
121131}
122132
133+ var settingsWindow ;
134+
135+ function openSettingsWindow ( ) {
136+ if ( settingsWindow ) {
137+ settingsWindow . show ( ) ;
138+ return ;
139+ }
140+ settingsWindow = new BrowserWindow ( {
141+ width : 320 ,
142+ height : 160 ,
143+ resizable : false ,
144+ title : 'Settings' ,
145+ } ) ;
146+ settingsWindow . on ( 'closed' , function ( ) {
147+ settingsWindow = undefined ;
148+ } ) ;
149+ settingsWindow . loadUrl ( 'file://' + __dirname + '/settings.html' ) ;
150+ }
151+
123152function openFile ( browserWindow , path ) {
124153 browserWindow . webContents . classeurCtx . watchFile ( path ) ;
125154 browserWindow . focus ( ) ;
@@ -159,10 +188,10 @@ function newFileDialog() {
159188var isReady , openWhenReady ;
160189app . on ( 'open-file' , function ( evt , path ) {
161190 evt . preventDefault ( ) ;
162- if ( isReady ) {
191+ if ( isReady ) {
163192 return createWindow ( function ( browserWindow ) {
164- openFile ( browserWindow , path ) ;
165- } ) ;
193+ openFile ( browserWindow , path ) ;
194+ } ) ;
166195 }
167196 openWhenReady = path ;
168197} ) ;
@@ -171,6 +200,15 @@ app.on('activate-with-no-open-windows', function() {
171200 createWindow ( ) ;
172201} ) ;
173202
203+ app . on ( 'window-all-closed' , function ( ) {
204+ if ( applySettings ) {
205+ applySettings = false ;
206+ createWindow ( ) ;
207+ } else if ( process . platform != 'darwin' ) {
208+ app . quit ( ) ;
209+ }
210+ } ) ;
211+
174212var ipc = require ( 'ipc' ) ;
175213ipc . on ( 'getVersion' , checkOrigin ( function ( evt ) {
176214 var classeurCtx = evt . sender . classeurCtx ;
@@ -196,17 +234,43 @@ ipc.on('saveFile', checkOrigin(function(evt, file) {
196234 }
197235} ) ) ;
198236
237+ ipc . on ( 'getSettings' , function ( evt ) {
238+ evt . sender . send ( 'settings' , settings ) ;
239+ } ) ;
240+
241+ ipc . on ( 'setSettings' , function ( evt , data ) {
242+ settings = data ;
243+ fs . writeFileSync ( settingsFilePath , JSON . stringify ( data ) ) ;
244+ settingsWindow && settingsWindow . destroy ( ) ;
245+ applySettings = true ;
246+ Object . keys ( windows ) . forEach ( function ( id ) {
247+ windows [ id ] . destroy ( ) ;
248+ } ) ;
249+ } ) ;
250+
199251function onReady ( ) {
200252 var template = [ {
201253 label : 'Classeur' ,
202254 submenu : [ {
255+ label : 'New window' ,
256+ click : function ( ) {
257+ createWindow ( ) ;
258+ }
259+ } , {
260+ type : 'separator'
261+ } , {
203262 label : 'New local file' ,
204263 click : newFileDialog
205264 } , {
206265 label : 'Open local file' ,
207266 click : openFileDialog
208267 } , {
209268 type : 'separator'
269+ } , {
270+ label : 'Settings' ,
271+ click : openSettingsWindow
272+ } , {
273+ type : 'separator'
210274 } , {
211275 label : 'Hide Classeur' ,
212276 accelerator : 'Command+H' ,
0 commit comments