@@ -14,31 +14,31 @@ router.post("/label", (req, res) => {
1414
1515 const { label } = req . body ;
1616
17- state . activeMenuBar . tray . setTitle ( label ) ;
17+ state . tray . setTitle ( label ) ;
1818} ) ;
1919
2020router . post ( "/tooltip" , ( req , res ) => {
2121 res . sendStatus ( 200 ) ;
2222
2323 const { tooltip } = req . body ;
2424
25- state . activeMenuBar . tray . setToolTip ( tooltip ) ;
25+ state . tray . setToolTip ( tooltip ) ;
2626} ) ;
2727
2828router . post ( "/icon" , ( req , res ) => {
2929 res . sendStatus ( 200 ) ;
3030
3131 const { icon } = req . body ;
3232
33- state . activeMenuBar . tray . setImage ( icon ) ;
33+ state . tray . setImage ( icon ) ;
3434} ) ;
3535
3636router . post ( "/context-menu" , ( req , res ) => {
3737 res . sendStatus ( 200 ) ;
3838
3939 const { contextMenu } = req . body ;
4040
41- state . activeMenuBar . tray . setContextMenu ( buildMenu ( contextMenu ) ) ;
41+ state . tray . setContextMenu ( buildMenu ( contextMenu ) ) ;
4242} ) ;
4343
4444router . post ( "/show" , ( req , res ) => {
@@ -84,14 +84,24 @@ router.post("/create", (req, res) => {
8484
8585
8686 if ( onlyShowContextMenu ) {
87+ // Create a tray icon
8788 const tray = new Tray ( icon || state . icon . replace ( "icon.png" , "IconTemplate.png" ) ) ;
8889
90+ // Set the context menu
8991 tray . setContextMenu ( buildMenu ( contextMenu ) ) ;
9092 tray . setToolTip ( tooltip ) ;
93+ tray . setTitle ( label ) ;
94+
95+ // Set the event listeners + send created event
96+ eventsForTray ( tray , onlyShowContextMenu , contextMenu , shouldSendCreatedEvent ) ;
97+
98+ // Set the tray to the state
99+ state . tray = tray ;
91100
92101 if ( ! showDockIcon ) {
93102 app . dock . hide ( ) ;
94103 }
104+
95105 } else {
96106 state . activeMenuBar = menubar ( {
97107 icon : icon || state . icon . replace ( "icon.png" , "IconTemplate.png" ) ,
@@ -122,76 +132,89 @@ router.post("/create", (req, res) => {
122132 state . activeMenuBar . on ( "after-create-window" , ( ) => {
123133 enable ( state . activeMenuBar . window . webContents ) ;
124134 } ) ;
125- }
126135
127- state . activeMenuBar . on ( "ready" , ( ) => {
136+ state . activeMenuBar . on ( "ready" , ( ) => {
137+ // Set the event listeners
138+ eventsForTray ( state . activeMenuBar . tray , onlyShowContextMenu , contextMenu , shouldSendCreatedEvent ) ;
128139
129- state . activeMenuBar . tray . setTitle ( label ) ;
140+ // Set the tray to the state
141+ state . tray = state . activeMenuBar . tray ;
130142
131- if ( shouldSendCreatedEvent ) {
132- notifyLaravel ( "events" , {
133- event : "\\Native\\Laravel\\Events\\MenuBar\\MenuBarCreated"
134- } ) ;
135- }
143+ // Set the title
144+ state . tray . setTitle ( label ) ;
136145
137- state . activeMenuBar . on ( "hide" , ( ) => {
138- notifyLaravel ( "events" , {
139- event : "\\Native\\Laravel\\Events\\MenuBar\\MenuBarHidden"
146+ state . activeMenuBar . on ( "hide" , ( ) => {
147+ notifyLaravel ( "events" , {
148+ event : "\\Native\\Laravel\\Events\\MenuBar\\MenuBarHidden"
149+ } ) ;
140150 } ) ;
141- } ) ;
142151
143- state . activeMenuBar . on ( "show" , ( ) => {
144- notifyLaravel ( "events" , {
145- event : "\\Native\\Laravel\\Events\\MenuBar\\MenuBarShown"
152+ state . activeMenuBar . on ( "show" , ( ) => {
153+ notifyLaravel ( "events" , {
154+ event : "\\Native\\Laravel\\Events\\MenuBar\\MenuBarShown"
155+ } ) ;
146156 } ) ;
157+
147158 } ) ;
159+ }
148160
149- state . activeMenuBar . tray . on ( "drop-files" , ( event , files ) => {
150- notifyLaravel ( "events" , {
151- event : "\\Native\\Laravel\\Events\\MenuBar\\MenuBarDroppedFiles" ,
152- payload : [
153- files
154- ]
155- } ) ;
161+ } ) ;
162+
163+
164+
165+ function eventsForTray ( tray , onlyShowContextMenu , contextMenu , shouldSendCreatedEvent ) {
166+
167+ if ( shouldSendCreatedEvent ) {
168+ notifyLaravel ( "events" , {
169+ event : "\\Native\\Laravel\\Events\\MenuBar\\MenuBarCreated"
156170 } ) ;
171+ }
157172
158- state . activeMenuBar . tray . on ( 'click' , ( combo , bounds , position ) => {
159- notifyLaravel ( 'events' , {
160- event : "\\Native\\Laravel\\Events\\MenuBar\\MenuBarClicked" ,
161- payload : {
162- combo,
163- bounds,
164- position,
165- } ,
166- } ) ;
173+ tray . on ( "drop-files" , ( event , files ) => {
174+ notifyLaravel ( "events" , {
175+ event : "\\Native\\Laravel\\Events\\MenuBar\\MenuBarDroppedFiles" ,
176+ payload : [
177+ files
178+ ]
167179 } ) ;
180+ } ) ;
168181
169- state . activeMenuBar . tray . on ( "right-click" , ( combo , bounds ) => {
170- notifyLaravel ( "events" , {
171- event : "\\Native\\Laravel\\Events\\MenuBar\\MenuBarRightClicked" ,
172- payload : {
173- combo,
174- bounds,
175- }
176- } ) ;
182+ tray . on ( 'click' , ( combo , bounds , position ) => {
183+ notifyLaravel ( 'events' , {
184+ event : "\\Native\\Laravel\\Events\\MenuBar\\MenuBarClicked" ,
185+ payload : {
186+ combo,
187+ bounds,
188+ position,
189+ } ,
190+ } ) ;
191+ } ) ;
177192
178- if ( ! onlyShowContextMenu ) {
179- state . activeMenuBar . hideWindow ( ) ;
180- state . activeMenuBar . tray . popUpContextMenu ( buildMenu ( contextMenu ) ) ;
193+ tray . on ( "right-click" , ( combo , bounds ) => {
194+ notifyLaravel ( "events" , {
195+ event : "\\Native\\Laravel\\Events\\MenuBar\\MenuBarRightClicked" ,
196+ payload : {
197+ combo,
198+ bounds,
181199 }
182200 } ) ;
183201
184- state . activeMenuBar . tray . on ( 'double-click' , ( combo , bounds ) => {
185- notifyLaravel ( 'events' , {
186- event : "\\Native\\Laravel\\Events\\MenuBar\\MenuBarDoubleClicked" ,
187- payload : {
188- combo,
189- bounds,
190- } ,
191- } ) ;
202+ if ( ! onlyShowContextMenu ) {
203+ state . activeMenuBar . hideWindow ( ) ;
204+ tray . popUpContextMenu ( buildMenu ( contextMenu ) ) ;
205+ }
206+ } ) ;
207+
208+ tray . on ( 'double-click' , ( combo , bounds ) => {
209+ notifyLaravel ( 'events' , {
210+ event : "\\Native\\Laravel\\Events\\MenuBar\\MenuBarDoubleClicked" ,
211+ payload : {
212+ combo,
213+ bounds,
214+ } ,
192215 } ) ;
193216 } ) ;
194- } ) ;
217+ }
195218
196219function buildMenu ( contextMenu ) {
197220 let menu = Menu . buildFromTemplate ( [ { role : "quit" } ] ) ;
0 commit comments