main_menu.js 8.3 KB

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