1
1
'use strict'
2
2
3
3
void ( function ( ) {
4
+ const FINDER_UPDATE = 1000
5
+
4
6
const remote = require ( 'remote' )
5
7
const Tray = remote . require ( 'tray' )
6
8
const Menu = remote . require ( 'menu' )
@@ -9,6 +11,9 @@ void (function () {
9
11
const ipc = require ( 'ipc' )
10
12
const grunt = require ( './lib/Grunt' )
11
13
const tildify = require ( 'tildify' )
14
+ const currentPath = require ( 'current-path' )
15
+
16
+ var finderInterval
12
17
13
18
// Set up tray menu.
14
19
let tray = new Tray ( __dirname + '/gruntTemplate.png' )
@@ -17,25 +22,48 @@ void (function () {
17
22
build ( )
18
23
19
24
function build ( ) {
20
- let current = window . localStorage . getItem ( 'current' ) || 'Choose folder...'
21
- trayMenu . append ( new MenuItem ( {
22
- label : tildify ( current ) ,
25
+ // Current menu
26
+ let currentMenu = new Menu ( )
27
+ currentMenu . append ( new MenuItem ( {
28
+ label : 'Follow Finder' ,
29
+ type : 'checkbox' ,
30
+ checked : window . localStorage . getItem ( 'followFinder' ) === 'true' ,
31
+ click : function ( item ) {
32
+ window . localStorage . setItem ( 'followFinder' , item . checked )
33
+ rebuild ( )
34
+ }
35
+ } ) )
36
+
37
+ currentMenu . append ( new MenuItem ( { type : 'separator' } ) )
38
+
39
+ currentMenu . append ( new MenuItem ( {
40
+ label : 'Choose folder...' ,
23
41
click : function ( ) {
24
42
dialog . showOpenDialog ( { properties : [ 'openDirectory' ] } , function ( dir ) {
25
43
if ( dir !== undefined ) {
26
44
window . localStorage . setItem ( 'current' , dir )
45
+
46
+ // Do not follow finder
47
+ window . localStorage . setItem ( 'followFinder' , 'false' )
48
+ clearInterval ( finderInterval )
49
+
27
50
if ( Object . keys ( global . processes ) . length > 0 ) {
28
51
// Stop all tasks
29
52
grunt . stopAll ( )
30
53
} else {
31
- trayMenu = new Menu ( )
32
- build ( )
54
+ rebuild ( )
33
55
}
34
56
}
35
57
} )
36
58
}
37
59
} ) )
38
60
61
+ let current = window . localStorage . getItem ( 'current' ) || 'Choose folder...'
62
+ trayMenu . append ( new MenuItem ( {
63
+ label : tildify ( current ) ,
64
+ submenu : currentMenu
65
+ } ) )
66
+
39
67
trayMenu . append ( new MenuItem ( { type : 'separator' } ) )
40
68
41
69
grunt . getTasks ( )
@@ -47,14 +75,15 @@ void (function () {
47
75
click : function ( ) {
48
76
grunt . runTask ( task , function ( ) {
49
77
// Rebuild menu
50
- trayMenu = new Menu ( )
51
- build ( )
78
+ rebuild ( )
52
79
} )
53
80
}
54
81
}
55
82
56
- if ( global . processes [ task ] ) {
57
- item . checked = true
83
+ if ( global . processes [ current ] ) {
84
+ if ( global . processes [ current ] [ task ] ) {
85
+ item . checked = true
86
+ }
58
87
}
59
88
60
89
trayMenu . append ( new MenuItem ( item ) )
@@ -71,5 +100,25 @@ void (function () {
71
100
72
101
tray . setContextMenu ( trayMenu )
73
102
} )
103
+
104
+ // Follow Finder
105
+ if ( window . localStorage . getItem ( 'followFinder' ) === 'true' ) {
106
+ finderInterval = setInterval ( function ( ) {
107
+ currentPath ( function ( err , path ) {
108
+ if ( ! err && window . localStorage . getItem ( 'current' ) !== path ) {
109
+ window . localStorage . setItem ( 'current' , path )
110
+
111
+ rebuild ( )
112
+ }
113
+ } )
114
+ } , FINDER_UPDATE )
115
+ }
116
+ }
117
+
118
+ // Rebuild the tray
119
+ function rebuild ( ) {
120
+ clearInterval ( finderInterval )
121
+ trayMenu = new Menu ( )
122
+ build ( )
74
123
}
75
124
} ) ( )
0 commit comments