22const { app, BrowserWindow, Tray, Menu, ipcMain} = require ( 'electron' )
33const path = require ( 'path' )
44
5- const Shuffled = require ( './shuffled' )
5+ const Shuffled = require ( './utils/shuffled' )
6+ const AppSettings = require ( './utils/settings' )
7+
68let microbreakIdeas = new Shuffled ( [
79 'Go grab a glass of water.' ,
810 'Slowly look all the way left, then right.' ,
@@ -23,9 +25,11 @@ let appIcon = null
2325let microbreakWin = null
2426let appStartupWin = null
2527let aboutWin = null
28+ let settingsWin = null
2629let finishMicrobreakTimer
2730let startMicrobreakTimer
2831let planMicrobreakTimer
32+ let settings
2933
3034function createTrayIcon ( ) {
3135 if ( process . platform === 'darwin' ) {
@@ -43,7 +47,7 @@ function showStartUpWindow () {
4347 frame : false ,
4448 alwaysOnTop : true ,
4549 title : 'stretchly' ,
46- backgroundColor : '#478484' ,
50+ backgroundColor : settings . get ( 'mainColor' ) ,
4751 width : 600 ,
4852 height : 170
4953 } )
@@ -58,7 +62,7 @@ function startMicrobreak () {
5862 microbreakWin = new BrowserWindow ( {
5963 frame : false ,
6064 alwaysOnTop : true ,
61- backgroundColor : '#478484' ,
65+ backgroundColor : settings . get ( 'mainColor' ) ,
6266 title : 'stretchly'
6367 } )
6468 microbreakWin . on ( 'close' , function ( ) { microbreakWin = null } )
@@ -67,7 +71,7 @@ function startMicrobreak () {
6771 microbreakWin . webContents . on ( 'did-finish-load' , ( ) => {
6872 microbreakWin . webContents . send ( 'breakIdea' , microbreakIdeas . randomElement )
6973 } )
70- finishMicrobreakTimer = setTimeout ( finishMicrobreak , 20000 )
74+ finishMicrobreakTimer = setTimeout ( finishMicrobreak , settings . get ( 'microbreakDuration' ) )
7175}
7276
7377function finishMicrobreak ( ) {
@@ -77,14 +81,19 @@ function finishMicrobreak () {
7781}
7882
7983function planMicrobreak ( ) {
80- startMicrobreakTimer = setTimeout ( startMicrobreak , 600000 )
84+ startMicrobreakTimer = setTimeout ( startMicrobreak , settings . get ( 'microbreakInterval' ) )
8185}
8286
8387ipcMain . on ( 'finish-microbreak' , function ( ) {
8488 clearTimeout ( finishMicrobreakTimer )
8589 finishMicrobreak ( )
8690} )
8791
92+ ipcMain . on ( 'save-setting' , function ( event , key , value ) {
93+ settings . set ( key , value )
94+ settingsWin . webContents . send ( 'renderSettings' , settings . data )
95+ } )
96+
8897let shouldQuit = app . makeSingleInstance ( function ( commandLine , workingDirectory ) {
8998 if ( appIcon ) {
9099 // Someone tried to run a second instance
@@ -96,6 +105,7 @@ if (shouldQuit) {
96105 app . quit ( )
97106}
98107
108+ app . on ( 'ready' , loadSettings )
99109app . on ( 'ready' , createTrayIcon )
100110app . on ( 'ready' , planMicrobreak )
101111app . on ( 'ready' , showStartUpWindow )
@@ -104,6 +114,12 @@ app.on('window-all-closed', () => {
104114 // do nothing, so app wont get closed
105115} )
106116
117+ function loadSettings ( ) {
118+ const dir = app . getPath ( 'userData' )
119+ const settingsFile = `${ dir } /config.json`
120+ settings = new AppSettings ( settingsFile )
121+ }
122+
107123function pauseMicrobreaks ( ) {
108124 if ( microbreakWin ) {
109125 clearTimeout ( finishMicrobreakTimer )
@@ -123,12 +139,25 @@ function showAboutWindow () {
123139 const modalPath = path . join ( 'file://' , __dirname , 'about.html' )
124140 aboutWin = new BrowserWindow ( {
125141 alwaysOnTop : true ,
126- backgroundColor : '#478484' ,
142+ backgroundColor : settings . get ( 'mainColor' ) ,
127143 title : 'About stretchly'
128144 } )
129145 aboutWin . loadURL ( modalPath )
130146}
131147
148+ function showSettingsWindow ( ) {
149+ const modalPath = path . join ( 'file://' , __dirname , 'settings.html' )
150+ settingsWin = new BrowserWindow ( {
151+ alwaysOnTop : true ,
152+ backgroundColor : settings . get ( 'mainColor' ) ,
153+ title : 'Settings'
154+ } )
155+ settingsWin . loadURL ( modalPath )
156+ settingsWin . webContents . on ( 'did-finish-load' , ( ) => {
157+ settingsWin . webContents . send ( 'renderSettings' , settings . data )
158+ } )
159+ }
160+
132161function getTrayMenu ( MicrobreaksPaused ) {
133162 let trayMenu = [ ]
134163
@@ -138,6 +167,12 @@ function getTrayMenu (MicrobreaksPaused) {
138167 showAboutWindow ( )
139168 }
140169 } , {
170+ label : 'Settings' ,
171+ click : function ( ) {
172+ showSettingsWindow ( )
173+ }
174+ }
175+ , {
141176 type : 'separator'
142177 } )
143178
0 commit comments