application-menu.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. const {BrowserWindow, Menu, app, shell, dialog} = require('electron')
  2. let template = [{
  3. label: 'Edit',
  4. submenu: [{
  5. label: 'Lol',
  6. accelerator: 'CmdOrCtrl+Z',
  7. role: 'undo'
  8. }, {
  9. label: 'Redo',
  10. accelerator: 'Shift+CmdOrCtrl+Z',
  11. role: 'redo'
  12. }, {
  13. type: 'separator'
  14. }, {
  15. label: 'Cut',
  16. accelerator: 'CmdOrCtrl+X',
  17. role: 'cut'
  18. }, {
  19. label: 'Copy',
  20. accelerator: 'CmdOrCtrl+C',
  21. role: 'copy'
  22. }, {
  23. label: 'Paste',
  24. accelerator: 'CmdOrCtrl+V',
  25. role: 'paste'
  26. }, {
  27. label: 'Select All',
  28. accelerator: 'CmdOrCtrl+A',
  29. role: 'selectall'
  30. }]
  31. }, {
  32. label: 'View',
  33. submenu: [{
  34. label: 'Reload',
  35. accelerator: 'CmdOrCtrl+R',
  36. click: (item, focusedWindow) => {
  37. if (focusedWindow) {
  38. // on reload, start fresh and close any old
  39. // open secondary windows
  40. if (focusedWindow.id === 1) {
  41. BrowserWindow.getAllWindows().forEach(win => {
  42. if (win.id > 1) win.close()
  43. })
  44. }
  45. focusedWindow.reload()
  46. }
  47. }
  48. }, {
  49. label: 'Toggle Full Screen',
  50. accelerator: (() => {
  51. if (process.platform === 'darwin') {
  52. return 'Ctrl+Command+F'
  53. } else {
  54. return 'F11'
  55. }
  56. })(),
  57. click: (item, focusedWindow) => {
  58. if (focusedWindow) {
  59. focusedWindow.setFullScreen(!focusedWindow.isFullScreen())
  60. }
  61. }
  62. }, {
  63. label: 'Toggle Developer Tools',
  64. accelerator: (() => {
  65. if (process.platform === 'darwin') {
  66. return 'Alt+Command+I'
  67. } else {
  68. return 'Ctrl+Shift+I'
  69. }
  70. })(),
  71. click: (item, focusedWindow) => {
  72. if (focusedWindow) {
  73. focusedWindow.toggleDevTools()
  74. }
  75. }
  76. }, {
  77. type: 'separator'
  78. }, {
  79. label: 'App Menu Demo',
  80. click: function (item, focusedWindow) {
  81. if (focusedWindow) {
  82. const options = {
  83. type: 'info',
  84. title: 'Application Menu Demo',
  85. buttons: ['Ok'],
  86. message: 'This demo is for the Menu section, showing how to create a clickable menu item in the application menu.'
  87. }
  88. dialog.showMessageBox(focusedWindow, options, function () {})
  89. }
  90. }
  91. }]
  92. }, {
  93. label: 'Window',
  94. role: 'window',
  95. submenu: [{
  96. label: 'Minimize',
  97. accelerator: 'CmdOrCtrl+M',
  98. role: 'minimize'
  99. }, {
  100. label: 'Close',
  101. accelerator: 'CmdOrCtrl+W',
  102. role: 'close'
  103. }, {
  104. type: 'separator'
  105. }, {
  106. label: 'Reopen Window',
  107. accelerator: 'CmdOrCtrl+Shift+T',
  108. enabled: false,
  109. key: 'reopenMenuItem',
  110. click: () => {
  111. app.emit('activate')
  112. }
  113. }]
  114. }, {
  115. label: 'Help',
  116. role: 'help',
  117. submenu: [{
  118. label: 'Learn More',
  119. click: () => {
  120. shell.openExternal('http://electron.atom.io')
  121. }
  122. }]
  123. }];
  124. const menu = Menu.buildFromTemplate(template);
  125. Menu.setApplicationMenu(menu);