11const { menubar } = require ( "menubar" ) ;
22
33const path = require ( "path" ) ;
4- const {
5- app,
6- nativeImage,
7- shell,
8- Tray,
9- Menu,
10- MenuItem,
11- globalShortcut,
12- } = require ( "electron" ) ;
4+ const { app, nativeImage, Tray, Menu, globalShortcut } = require ( "electron" ) ;
5+ const contextMenu = require ( "electron-context-menu" ) ;
136
147const image = nativeImage . createFromPath (
158 path . join ( __dirname , `images/newiconTemplate.png` )
169) ;
1710
11+ const contextMenuTemplate = [
12+ { role : "about" } ,
13+ {
14+ label : "Quit" ,
15+ accelerator : "Command+Q" ,
16+ click : function ( ) {
17+ app . quit ( ) ;
18+ } ,
19+ } ,
20+ ] ;
21+
1822app . on ( "ready" , ( ) => {
1923 const tray = new Tray ( image ) ;
2024
21- const contextMenu = Menu . buildFromTemplate ( [
22- { role : "about" } ,
23- { type : "separator" } ,
24- { role : "quit" } ,
25- ] ) ;
26-
2725 const mb = menubar ( {
2826 browserWindow : {
2927 icon : image ,
@@ -36,6 +34,7 @@ app.on("ready", () => {
3634 height : 550 ,
3735 } ,
3836 tray,
37+ showOnAllWorkspaces : false ,
3938 preloadWindow : true ,
4039 showDockIcon : false ,
4140 icon : image ,
@@ -45,15 +44,13 @@ app.on("ready", () => {
4544 app . dock . hide ( ) ;
4645
4746 tray . on ( "right-click" , ( ) => {
48- mb . tray . popUpContextMenu ( contextMenu ) ;
47+ mb . tray . popUpContextMenu ( Menu . buildFromTemplate ( contextMenuTemplate ) ) ;
4948 } ) ;
5049
5150 const { window } = mb ;
52-
5351 const menu = new Menu ( ) ;
5452
5553 globalShortcut . register ( "CommandOrControl+Shift+g" , ( ) => {
56- console . log ( window . isFocused ( ) , window . isVisible ( ) ) ;
5754 if ( window . isVisible ( ) ) {
5855 mb . hideWindow ( ) ;
5956 } else {
@@ -71,11 +68,40 @@ app.on("ready", () => {
7168 console . log ( "Menubar app is ready." ) ;
7269 } ) ;
7370
71+ app . on ( "web-contents-created" , ( e , contents ) => {
72+ if ( contents . getType ( ) == "webview" ) {
73+ // open link with external browser in webview
74+ contents . on ( "new-window" , ( e , url ) => {
75+ e . preventDefault ( ) ;
76+ shell . openExternal ( url ) ;
77+ } ) ;
78+ // set context menu in webview
79+ contextMenu ( {
80+ window : contents ,
81+ } ) ;
82+
83+ // we can't set the native app menu with "menubar" so need to manually register these events
84+ // register cmd+c/cmd+v events
85+ contents . on ( "before-input-event" , ( event , input ) => {
86+ const { control, meta, key } = input ;
87+ if ( ! control && ! meta ) return ;
88+ if ( key === "c" ) contents . copy ( ) ;
89+ if ( key === "v" ) contents . paste ( ) ;
90+ if ( key === "a" ) contents . selectAll ( ) ;
91+ if ( key === "z" ) contents . undo ( ) ;
92+ if ( key === "y" ) contents . redo ( ) ;
93+ if ( key === "q" ) app . quit ( ) ;
94+ if ( key === "r" ) contents . reload ( ) ;
95+ } ) ;
96+ }
97+ } ) ;
98+
99+ // restore focus to previous app on hiding
74100 mb . on ( "after-hide" , ( ) => {
75101 mb . app . hide ( ) ;
76102 } ) ;
77103
78- // open in new window
104+ // open links in new window
79105 // app.on("web-contents-created", (event, contents) => {
80106 // contents.on("will-navigate", (event, navigationUrl) => {
81107 // event.preventDefault();
@@ -89,7 +115,6 @@ app.on("ready", () => {
89115 "true"
90116 ) ;
91117} ) ;
92- // restore focus to previous app on hiding
93118
94119// Quit when all windows are closed, except on macOS. There, it's common
95120// for applications and their menu bar to stay active until the user quits
0 commit comments