main_menu.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /**
  2. * @author oldj
  3. * @blog https://oldj.net
  4. */
  5. 'use strict'
  6. const path = require('path')
  7. const paths = require('../server/paths')
  8. const {Menu, shell, dialog} = require('electron')
  9. const m_lang = require('../server/lang')
  10. const getPref = require('../server/actions/getPref')
  11. const svr = require('../server/svr')
  12. const version = require('../version').version
  13. function doInit (app, lang) {
  14. let last_path = null
  15. const template = [
  16. {
  17. label: lang.file,
  18. submenu: [
  19. {
  20. label: lang.new,
  21. accelerator: 'CommandOrControl+N',
  22. click: () => {
  23. svr.broadcast('add_hosts')
  24. }
  25. }, {
  26. type: 'separator'
  27. }, {
  28. label: lang.import,
  29. accelerator: 'Alt+CommandOrControl+I',
  30. click: () => {
  31. dialog.showOpenDialog({
  32. title: lang.import,
  33. defaultPath: path.join(last_path || paths.home_path, 'sh.json'),
  34. filters: [
  35. {name: 'JSON', extensions: ['json']},
  36. {name: 'All Files', extensions: ['*']}
  37. ]
  38. }, (fns) => {
  39. if (fns && fns.length > 0) {
  40. svr.emit('to_import', fns[0])
  41. last_path = path.dirname(fns[0])
  42. }
  43. })
  44. }
  45. }, {
  46. label: lang.export,
  47. accelerator: 'Alt+CommandOrControl+E',
  48. click: () => {
  49. dialog.showSaveDialog({
  50. title: lang.export,
  51. defaultPath: path.join(last_path || paths.home_path, 'sh.json'),
  52. filters: [
  53. {name: 'JSON', extensions: ['json']},
  54. {name: 'All Files', extensions: ['*']}
  55. ]
  56. }, (fn) => {
  57. if (fn) {
  58. svr.emit('to_export', fn)
  59. last_path = path.dirname(fn)
  60. }
  61. })
  62. }
  63. }, {
  64. type: 'separator'
  65. }, {
  66. label: lang.preferences,
  67. accelerator: 'CommandOrControl+,',
  68. click: () => {
  69. //app.mainWindow.webContents.send('show_preferences')
  70. svr.broadcast('show_preferences')
  71. }
  72. }
  73. ]
  74. },
  75. {
  76. label: lang.edit,
  77. submenu: [
  78. {
  79. role: 'undo'
  80. }, {
  81. role: 'redo'
  82. }, {
  83. type: 'separator'
  84. }, {
  85. role: 'cut'
  86. }, {
  87. role: 'copy'
  88. }, {
  89. role: 'paste'
  90. }, {
  91. role: 'pasteandmatchstyle'
  92. }, {
  93. role: 'delete'
  94. }, {
  95. role: 'selectall'
  96. }, {
  97. type: 'separator'
  98. }, {
  99. label: lang.search,
  100. accelerator: 'CommandOrControl+F',
  101. click () {
  102. // ipcMain.emit('to_search');
  103. //app.mainWindow.webContents.send('to_search')
  104. svr.broadcast('to_search')
  105. }
  106. }, {
  107. label: lang.comment,
  108. accelerator: 'CommandOrControl+/',
  109. click () {
  110. // ipcMain.emit('to_search');
  111. //app.mainWindow.webContents.send('to_comment')
  112. svr.broadcast('to_comment')
  113. }
  114. }]
  115. }, {
  116. label: lang.view,
  117. submenu: [
  118. // {
  119. // label: 'Reload',
  120. // accelerator: 'CmdOrCtrl+R',
  121. // click (item, focusedWindow) {
  122. // if (focusedWindow) focusedWindow.reload()
  123. // }
  124. // },
  125. // {
  126. // label: 'Toggle Developer Tools',
  127. // accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
  128. // click (item, focusedWindow) {
  129. // if (focusedWindow) focusedWindow.webContents.toggleDevTools()
  130. // }
  131. // },
  132. // {
  133. // type: 'separator'
  134. // },
  135. {
  136. role: 'resetzoom'
  137. },
  138. {
  139. role: 'zoomin'
  140. },
  141. {
  142. role: 'zoomout'
  143. },
  144. {
  145. type: 'separator'
  146. },
  147. {
  148. role: 'togglefullscreen'
  149. }
  150. ]
  151. }, {
  152. label: lang.window,
  153. role: 'window',
  154. submenu: [
  155. {
  156. role: 'minimize'
  157. }, {
  158. role: 'close'
  159. }]
  160. }, {
  161. label: lang.help,
  162. role: 'help',
  163. submenu: [
  164. {
  165. label: lang.check_update,
  166. click () {
  167. //m_chk_update.check()
  168. // todo
  169. }
  170. }, {
  171. type: 'separator'
  172. }, {
  173. label: lang.feedback,
  174. click () {
  175. shell.openExternal('https://github.com/oldj/SwitchHosts/issues')
  176. }
  177. }, {
  178. label: lang.homepage,
  179. click () {
  180. shell.openExternal('https://oldj.github.io/SwitchHosts/')
  181. }
  182. }]
  183. }
  184. ]
  185. const name = require('electron').app.getName()
  186. const os = process.platform
  187. if (os === 'darwin') {
  188. template.unshift({
  189. label: name,
  190. submenu: [
  191. {
  192. role: 'about'
  193. }, {
  194. type: 'separator'
  195. },
  196. // {
  197. // role: 'services',
  198. // submenu: []
  199. // },
  200. // {
  201. // type: 'separator'
  202. // },
  203. {
  204. role: 'hide'
  205. }, {
  206. role: 'hideothers'
  207. }, {
  208. role: 'unhide'
  209. }, {
  210. type: 'separator'
  211. }, {
  212. role: 'quit'
  213. }]
  214. })
  215. // Edit menu.
  216. /*template[2].submenu.push(
  217. {
  218. type: 'separator'
  219. },
  220. {
  221. label: 'Speech',
  222. submenu: [
  223. {
  224. role: 'startspeaking'
  225. },
  226. {
  227. role: 'stopspeaking'
  228. }
  229. ]
  230. }
  231. );*/
  232. // Window menu.
  233. template[4].submenu = [
  234. {
  235. label: 'Close',
  236. accelerator: 'CmdOrCtrl+W',
  237. role: 'close'
  238. },
  239. {
  240. label: 'Minimize',
  241. accelerator: 'CmdOrCtrl+M',
  242. role: 'minimize'
  243. },
  244. {
  245. label: 'Zoom',
  246. role: 'zoom'
  247. },
  248. {
  249. type: 'separator'
  250. },
  251. {
  252. label: 'Bring All to Front',
  253. role: 'front'
  254. }
  255. ]
  256. } else if (os === 'win32') {
  257. template[0].submenu.unshift({
  258. type: 'separator'
  259. })
  260. template[0].submenu.unshift({
  261. role: 'about',
  262. click: () => {
  263. dialog.showMessageBox({
  264. type: 'info',
  265. buttons: [],
  266. title: 'About',
  267. message: `${name} v${version.slice(0, 3).join('.')} (${version[3]})`
  268. })
  269. }
  270. })
  271. template[0].submenu.push({
  272. type: 'separator'
  273. })
  274. template[0].submenu.push({
  275. label: 'Quit',
  276. role: 'quit',
  277. accelerator: 'CmdOrCtrl+Q'
  278. })
  279. // VIEW
  280. template[2].submenu.splice(0, 4)
  281. }
  282. if (process.env.ENV === 'dev') {
  283. // VIEW
  284. template[3].submenu = [
  285. {
  286. label: 'Reload',
  287. accelerator: 'CmdOrCtrl+R',
  288. click (item, focusedWindow) {
  289. if (focusedWindow) focusedWindow.reload()
  290. }
  291. },
  292. {
  293. label: 'Toggle Developer Tools',
  294. accelerator: process.platform === 'darwin'
  295. ? 'Alt+Command+I'
  296. : 'Ctrl+Shift+I',
  297. click (item, focusedWindow) {
  298. if (focusedWindow) focusedWindow.webContents.toggleDevTools()
  299. }
  300. },
  301. {
  302. type: 'separator'
  303. }
  304. ].concat(template[3].submenu)
  305. }
  306. const menu = Menu.buildFromTemplate(template)
  307. Menu.setApplicationMenu(menu)
  308. }
  309. exports.init = function (app, language = 'en') {
  310. let lang = m_lang.getLang(language)
  311. doInit(app, lang)
  312. }