@@ -6,21 +6,20 @@ const {
66 shell,
77 Tray,
88} = require ( 'electron' ) ;
9- const path = require ( 'path ' ) ;
10- const settings = require ( 'electron-settings ' ) ;
9+ const appPackage = require ( './package.json ' ) ;
10+ const icon = require ( './icon ' ) ;
1111const menus = require ( './menus' ) ;
12+ const notification = require ( './notification' ) ;
13+ const settings = require ( './settings' ) ;
14+ const versionChecker = require ( './versionChecker' ) ;
1215
1316const ELECTRON_VERSION = process . versions . electron ;
1417const APP_NAME = app . name ;
1518const APP_VERSION = app . getVersion ( ) ;
16- const APP_DESCRIPTION = 'Unofficial Basecamp GNU/Linux Desktop Client.' ;
19+ const APP_DESCRIPTION = appPackage . description ;
1720const BASECAMP_URL = 'https://launchpad.37signals.com' ;
18- const ICONS_PATH = path . join ( app . getAppPath ( ) , '..' , 'assets' , 'icons' ) ;
19- const DEFAULTS = {
20- iconScheme : 'white' ,
21- showBadge : true ,
22- } ;
2321
22+ /** @type {BrowserWindow } */
2423let win ;
2524let tray ;
2625let unreadsNotified = false ;
@@ -36,28 +35,30 @@ const basecamp = {
3635 this . addTrayIcon ( ) ;
3736 this . setIcons ( ) ;
3837 this . addWindowEvents ( ) ;
38+ this . bootstrap ( ) ;
3939 } ,
4040
4141 /**
4242 * Creates the app window.
4343 */
4444 createWindow ( url ) {
45- win = new BrowserWindow ( {
46- y : settings . getSync ( 'posY' , 0 ) ,
47- x : settings . getSync ( 'posX' , 0 ) ,
48- width : settings . getSync ( 'width' , 770 ) ,
49- height : settings . getSync ( 'height' , 700 ) ,
45+ const config = {
46+ y : settings . get ( 'posY' ) ,
47+ x : settings . get ( 'posX' ) ,
48+ width : settings . get ( 'width' ) ,
49+ height : settings . get ( 'height' ) ,
5050 title : APP_NAME ,
51- icon : this . getIcon ( 'icon' ) ,
52- autoHideMenuBar : true ,
53- backgroundColor : '#f5efe6' ,
51+ icon : icon ( 'icon' ) ,
52+ autoHideMenuBar : settings . get ( 'autoHideMenu' ) ,
53+ backgroundColor : settings . get ( 'appBackgroundColor' ) ,
5454 webPreferences : {
5555 nodeIntegration : false ,
56- preload : `${ __dirname } /integration.js` ,
5756 } ,
58- } ) ;
57+ } ;
58+
59+ win = new BrowserWindow ( config ) ;
5960
60- if ( settings . getSync ( 'isMaximized' , false ) ) {
61+ if ( settings . get ( 'isMaximized' ) ) {
6162 win . maximize ( ) ;
6263 }
6364
@@ -68,11 +69,11 @@ const basecamp = {
6869 win
6970 . on ( 'close' , ( ) => {
7071 const bounds = win . getBounds ( ) ;
71- settings . setSync ( 'posX' , bounds . x ) ;
72- settings . setSync ( 'posY' , bounds . y ) ;
73- settings . setSync ( 'width' , bounds . width ) ;
74- settings . setSync ( 'height' , bounds . height ) ;
75- settings . setSync ( 'isMaximized' , win . isMaximized ( ) ) ;
72+ settings . set ( 'posX' , bounds . x ) ;
73+ settings . set ( 'posY' , bounds . y ) ;
74+ settings . set ( 'width' , bounds . width ) ;
75+ settings . set ( 'height' , bounds . height ) ;
76+ settings . set ( 'isMaximized' , win . isMaximized ( ) ) ;
7677 } )
7778 . on ( 'closed' , ( ) => {
7879 tray = null ;
@@ -104,11 +105,32 @@ const basecamp = {
104105 return win ;
105106 } ,
106107
108+ bootstrap ( ) {
109+ if ( settings . get ( 'checkNewVersion' ) ) {
110+ this . checkNewVersion ( ) ;
111+ }
112+ } ,
113+
114+ /**
115+ * Checks for new versions.
116+ */
117+ checkNewVersion ( notifyLatest ) {
118+ versionChecker . check ( ) . then ( ( check ) => {
119+ if ( check . comparison === 1 ) {
120+ notification ( `New version available ${ check . repoVersion } ` ) ;
121+ } else if ( notifyLatest === true ) {
122+ notification ( check . comparison === 0
123+ ? `${ check . appVersion } is the latest version`
124+ : `Dev version ${ check . appVersion } (latest is ${ check . repoVersion } )` ) ;
125+ }
126+ } ) ;
127+ } ,
128+
107129 /**
108130 * Adds the app menu.
109131 */
110132 addAppMenu ( ) {
111- Menu . setApplicationMenu ( menus . forApp ( basecamp , settings , DEFAULTS ) ) ;
133+ Menu . setApplicationMenu ( menus . forApp ( this ) ) ;
112134 } ,
113135
114136 /**
@@ -129,7 +151,7 @@ const basecamp = {
129151 * Adds the tray icon.
130152 */
131153 addTrayIcon ( ) {
132- tray = new Tray ( this . getIcon ( 'tray' ) ) ;
154+ tray = new Tray ( icon ( 'tray' ) ) ;
133155 tray . setToolTip ( APP_NAME ) ;
134156 tray . setContextMenu ( menus . forTray ( ) ) ;
135157 tray . on ( 'click' , ( ) => {
@@ -141,6 +163,16 @@ const basecamp = {
141163 } ) ;
142164 } ,
143165
166+ /**
167+ * Enables or disables menu auto hiding
168+ */
169+ switchAutoHideMenu ( ) {
170+ const isAutoHide = ! settings . get ( 'autoHideMenu' ) ;
171+ win . setAutoHideMenuBar ( isAutoHide ) ;
172+ win . setMenuBarVisibility ( ! isAutoHide ) ;
173+ settings . set ( 'autoHideMenu' , isAutoHide ) ;
174+ } ,
175+
144176 /**
145177 * Go to previous page on history.
146178 */
@@ -182,7 +214,7 @@ const basecamp = {
182214 showAboutDialog ( ) {
183215 dialog . showMessageBox ( win , {
184216 type : 'info' ,
185- icon : this . getIcon ( 'logo' ) ,
217+ icon : icon ( 'logo' ) ,
186218 buttons : [ 'Ok' ] ,
187219 defaultId : 0 ,
188220 title : 'About' ,
@@ -208,7 +240,7 @@ const basecamp = {
208240 }
209241 }
210242
211- win . webContents . executeJavaScript ( 'typeof BC === \'undefined\' ? 0 : BC.unreads.all' , false , result => ( result ) ) . then ( ( result ) => {
243+ win . webContents . executeJavaScript ( 'typeof BC === \'undefined\' ? 0 : BC.unreads.all' , false , ( result ) => ( result ) ) . then ( ( result ) => {
212244 const unreads = result . length ;
213245
214246 win . setTitle ( unreads > 0 ? `${ fixedTitle } • ${ unreads } ` : fixedTitle ) ;
@@ -217,18 +249,11 @@ const basecamp = {
217249
218250 if ( unreads > 0 ) {
219251 if ( ! unreadsNotified ) {
220- const notification = `
221- new Notification(
222- '${ APP_NAME } ',
223- { body: 'You have ${ unreads } unread notifications' }
224- );` ;
225-
226- win . webContents . executeJavaScript ( notification ) ;
227-
252+ notification ( `You have ${ unreads } unread notifications` ) ;
228253 unreadsNotified = true ;
229254 }
230255
231- if ( settings . getSync ( 'showBadge' , DEFAULTS . showBadge ) ) {
256+ if ( settings . get ( 'showBadge' ) ) {
232257 this . setIcons ( `-unreads-${ ( unreads > 10 ? '10p' : unreads ) } ` ) ;
233258 } else {
234259 this . setIcons ( '-unreads' ) ;
@@ -243,20 +268,8 @@ const basecamp = {
243268 * Sets the app & tray icons.
244269 */
245270 setIcons ( suffix ) {
246- win . setIcon ( this . getIcon ( `icon${ suffix ? '-unreads' : '' } ` ) ) ;
247- tray . setImage ( this . getIcon ( `tray${ suffix || '' } ` ) ) ;
248- } ,
249-
250- /**
251- * Gets the corresponding icon path.
252- *
253- * @param {string } icon
254- *
255- * @return {string }
256- */
257- getIcon ( icon ) {
258- const iconScheme = settings . getSync ( 'iconScheme' ) || DEFAULTS . iconScheme ;
259- return `${ ICONS_PATH } /${ iconScheme } /${ icon } .png` ;
271+ win . setIcon ( icon ( `icon${ suffix ? '-unreads' : '' } ` ) ) ;
272+ tray . setImage ( icon ( `tray${ suffix || '' } ` ) ) ;
260273 } ,
261274
262275 /**
@@ -277,7 +290,7 @@ const basecamp = {
277290 * @param {string } color
278291 */
279292 configureIconScheme ( color ) {
280- settings . setSync ( 'iconScheme' , color ) ;
293+ settings . set ( 'iconScheme' , color ) ;
281294 win . reload ( ) ;
282295 } ,
283296
@@ -287,7 +300,7 @@ const basecamp = {
287300 * @param {string } color
288301 */
289302 configureShowBadge ( config ) {
290- settings . setSync ( 'showBadge' , config ) ;
303+ settings . set ( 'showBadge' , config ) ;
291304 win . reload ( ) ;
292305 } ,
293306} ;
0 commit comments