1
1
const { app, BrowserWindow, ipcMain, Tray, Menu} = require ( 'electron' )
2
2
const path = require ( 'path' )
3
3
const Starling = require ( 'starling-developer-sdk' )
4
+ const storage = require ( 'electron-json-storage' )
4
5
5
6
const assetsDirectory = path . join ( __dirname , 'assets' )
6
7
7
8
let starling = undefined
8
9
let tray = undefined
9
10
let trayMenu = undefined
10
11
let authWindow = undefined
11
- let displayInStatusbar = false
12
12
13
- // Don't show the app in the doc
13
+ let config = {
14
+ starling : { }
15
+ }
16
+
17
+ // don't show the app in the dock
14
18
app . dock . hide ( )
15
19
16
20
app . on ( 'ready' , ( ) => {
21
+ createSystemMenu ( )
17
22
createTray ( )
23
+ loadConfig ( )
18
24
} )
19
25
20
- // Quit the app when the window is closed
21
- app . on ( 'window-all-closed' , ( ) => {
22
- app . quit ( )
23
- } )
26
+ const loadConfig = ( ) => {
27
+ storage . get ( 'starling' , function ( error , data ) {
28
+ config . starling = {
29
+ accessToken : data . accessToken ,
30
+ displayInStatusbar : data . displayInStatusbar || true
31
+ }
32
+
33
+ if ( config . starling . accessToken ) {
34
+ starling = new Starling ( { accessToken : config . starling . accessToken } )
35
+ starling . getBalance ( ) . catch ( ( ) => {
36
+ starling = null
37
+ loadPreAuthTray ( )
38
+ } ) . then ( ( ) => {
39
+ loadTray ( )
40
+ } )
41
+ } else {
42
+ loadPreAuthTray ( )
43
+ }
44
+ } )
45
+ }
24
46
25
47
const createTray = ( ) => {
26
48
tray = new Tray ( path . join ( assetsDirectory , 'sbTemplate.png' ) )
49
+ }
50
+
51
+ const loadPreAuthTray = ( ) => {
52
+ tray . setTitle ( '' )
27
53
trayMenu = Menu . buildFromTemplate ( [
28
- { label : 'Enter Personal Auth Token ...' , click : doPersonalAuth } ,
54
+ { label : 'Enter personal auth token ...' , click : doPersonalAuth } ,
29
55
{ type : 'separator' } ,
30
56
{ label : 'Quit' , role : 'quit' }
31
57
] )
@@ -39,7 +65,6 @@ const loadTray = () => {
39
65
40
66
updateTray ( )
41
67
setInterval ( updateTray , 30000 )
42
-
43
68
}
44
69
45
70
const updateTray = ( ) => {
@@ -48,44 +73,93 @@ const updateTray = () => {
48
73
}
49
74
50
75
starling . getBalance ( ) . then ( ( { data} ) => {
51
- if ( displayInStatusbar ) {
76
+ if ( config . starling . displayInStatusbar ) {
52
77
tray . setTitle ( `£${ data . effectiveBalance } ` )
53
78
} else {
54
79
tray . setTitle ( '' )
55
80
}
56
81
57
82
trayMenu = Menu . buildFromTemplate ( [
58
- { type : 'checkbox' , label : 'Display in statusbar' , click : setDisplayInStatusbar , checked : displayInStatusbar } ,
83
+ { type : 'checkbox' , label : 'Display in statusbar' , click : setDisplayInStatusbar , checked : config . starling . displayInStatusbar } ,
59
84
{ type : 'separator' } ,
60
85
{ label : `Balance` , enabled : false } ,
61
86
{ label : ` - £${ data . effectiveBalance } ` , enabled : false } ,
62
87
{ type : 'separator' } ,
88
+ { label : 'Log out' , click : logOut } ,
63
89
{ label : 'Quit' , role : 'quit' }
64
90
] )
65
91
tray . setContextMenu ( trayMenu )
66
- } ) ;
92
+ } )
67
93
}
68
94
69
95
const setDisplayInStatusbar = ( item ) => {
70
- displayInStatusbar = item . checked
96
+ config . starling . displayInStatusbar = item . checked
71
97
updateTray ( )
98
+ storage . set ( 'starling' , config . starling , ( error ) => {
99
+ //
100
+ } )
72
101
}
73
102
74
103
const createAuthWindow = ( ) => {
75
- if ( authWindow ) {
104
+ if ( authWindow && ! authWindow . isDestroyed ( ) ) {
76
105
authWindow . close ( )
77
106
}
78
107
79
- return new BrowserWindow ( { } )
108
+ return new BrowserWindow ( {
109
+ width : 320 ,
110
+ height : 420 ,
111
+ titleBarStyle : 'hidden-inset' ,
112
+ resizable : false ,
113
+ alwaysOnTop : true
114
+ } )
80
115
}
81
116
82
117
const doPersonalAuth = ( ) => {
83
118
authWindow = createAuthWindow ( )
84
119
authWindow . loadURL ( `file://${ path . join ( __dirname , 'index.html' ) } ` )
85
120
}
86
121
87
- ipcMain . on ( 'personal-auth' , ( event , token ) => {
122
+ ipcMain . on ( 'personal-auth-token ' , ( event , token ) => {
88
123
starling = new Starling ( { accessToken : token } )
89
- authWindow . hide ( )
90
- loadTray ( )
124
+ starling . getBalance ( ) . then ( ( { data} ) => {
125
+ config . starling . accessToken = token
126
+ storage . set ( 'starling' , config . starling , function ( error ) {
127
+ authWindow . hide ( )
128
+ loadTray ( )
129
+ } )
130
+ } ) . catch ( ( error ) => {
131
+ event . sender . send ( 'personal-auth-error' , 'Invalid auth token' )
132
+ } )
91
133
} )
134
+
135
+ const logOut = ( ) => {
136
+ config . starling . accessToken = null
137
+ storage . set ( 'starling' , config . starling , function ( error ) {
138
+ loadPreAuthTray ( )
139
+ } )
140
+ }
141
+
142
+ // required to enable certain system functions such as clipboard
143
+ const createSystemMenu = ( ) => {
144
+ var template = [ {
145
+ label : "StarlingBar" ,
146
+ submenu : [
147
+ { label : "About Application" , selector : "orderFrontStandardAboutPanel:" } ,
148
+ { type : "separator" } ,
149
+ { label : "Quit" , accelerator : "Command+Q" , click : function ( ) { app . quit ( ) } }
150
+ ]
151
+ } , {
152
+ label : "Edit" ,
153
+ submenu : [
154
+ { label : "Undo" , accelerator : "CmdOrCtrl+Z" , selector : "undo:" } ,
155
+ { label : "Redo" , accelerator : "Shift+CmdOrCtrl+Z" , selector : "redo:" } ,
156
+ { type : "separator" } ,
157
+ { label : "Cut" , accelerator : "CmdOrCtrl+X" , selector : "cut:" } ,
158
+ { label : "Copy" , accelerator : "CmdOrCtrl+C" , selector : "copy:" } ,
159
+ { label : "Paste" , accelerator : "CmdOrCtrl+V" , selector : "paste:" } ,
160
+ { label : "Select All" , accelerator : "CmdOrCtrl+A" , selector : "selectAll:" }
161
+ ]
162
+ } ]
163
+
164
+ Menu . setApplicationMenu ( Menu . buildFromTemplate ( template ) )
165
+ }
0 commit comments